vjcomp.c revision 46686
1/*
2 *	       Input/Output VJ Compressed packets
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: vjcomp.c,v 1.27 1999/03/31 14:21:46 brian Exp $
21 *
22 *  TODO:
23 */
24#include <sys/param.h>
25#include <netinet/in.h>
26#include <netinet/in_systm.h>
27#include <netinet/ip.h>
28#include <sys/un.h>
29
30#include <stdio.h>
31#include <string.h>
32#include <termios.h>
33
34#include "layer.h"
35#include "mbuf.h"
36#include "log.h"
37#include "timer.h"
38#include "fsm.h"
39#include "proto.h"
40#include "slcompress.h"
41#include "lqr.h"
42#include "hdlc.h"
43#include "defs.h"
44#include "iplist.h"
45#include "throughput.h"
46#include "ipcp.h"
47#include "lcp.h"
48#include "ccp.h"
49#include "link.h"
50#include "filter.h"
51#include "descriptor.h"
52#include "mp.h"
53#ifndef NORADIUS
54#include "radius.h"
55#endif
56#include "bundle.h"
57#include "vjcomp.h"
58
59#define MAX_VJHEADER 16		/* Maximum size of compressed header */
60
61static struct mbuf *
62vj_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp, int pri,
63             u_short *proto)
64{
65  int type;
66  struct ip *pip;
67  u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
68
69  log_Printf(LogDEBUG, "vj_LayerWrite: COMPPROTO = %x\n",
70            bundle->ncp.ipcp.peer_compproto);
71  bp = mbuf_Contiguous(bp);
72  pip = (struct ip *)MBUF_CTOP(bp);
73  if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP &&
74      cproto == PROTO_VJCOMP) {
75    type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc,
76                           &bundle->ncp.ipcp.vj.slstat,
77                           bundle->ncp.ipcp.peer_compproto & 0xff);
78    log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type);
79    switch (type) {
80    case TYPE_IP:
81      break;
82
83    case TYPE_UNCOMPRESSED_TCP:
84      *proto = PROTO_VJUNCOMP;
85      break;
86
87    case TYPE_COMPRESSED_TCP:
88      *proto = PROTO_VJCOMP;
89      break;
90
91    default:
92      log_Printf(LogERROR, "Unknown frame type %x\n", type);
93      mbuf_Free(bp);
94      return NULL;
95    }
96  }
97
98  return bp;
99}
100
101static struct mbuf *
102VjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
103{
104  u_char *bufp;
105  int len, olen, rlen;
106  struct mbuf *nbp;
107  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
108
109  bp = mbuf_Contiguous(bp);
110  olen = len = mbuf_Length(bp);
111  if (type == TYPE_UNCOMPRESSED_TCP) {
112    /*
113     * Uncompressed packet does NOT change its size, so that we can use mbuf
114     * space for uncompression job.
115     */
116    bufp = MBUF_CTOP(bp);
117    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
118                            (ipcp->my_compproto >> 8) & 255);
119    if (len <= 0) {
120      mbuf_Free(bp);
121      bp = NULL;
122    }
123    return bp;
124  }
125
126  /*
127   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
128   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
129   * Copy unread data info there.
130   */
131  if (len > MAX_VJHEADER)
132    len = MAX_VJHEADER;
133  rlen = len;
134  bufp = work + MAX_HDR;
135  bp = mbuf_Read(bp, bufp, rlen);
136  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
137                          (ipcp->my_compproto >> 8) & 255);
138  if (len <= 0) {
139    mbuf_Free(bp);
140    return NULL;
141  }
142  len -= olen;
143  len += rlen;
144  nbp = mbuf_Alloc(len, MB_VJCOMP);
145  memcpy(MBUF_CTOP(nbp), bufp, len);
146  nbp->next = bp;
147  return nbp;
148}
149
150static struct mbuf *
151vj_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
152             u_short *proto)
153{
154  u_char type;
155
156  log_Printf(LogDEBUG, "vj_LayerPull: proto %02x\n", *proto);
157  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
158
159  switch (*proto) {
160  case PROTO_VJCOMP:
161    type = TYPE_COMPRESSED_TCP;
162    break;
163  case PROTO_VJUNCOMP:
164    type = TYPE_UNCOMPRESSED_TCP;
165    break;
166  default:
167    return bp;
168  }
169
170  *proto = PROTO_IP;
171  return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
172}
173
174const char *
175vj2asc(u_int32_t val)
176{
177  static char asc[50];		/* The return value is used immediately */
178
179  if (val)
180    snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
181            (int)((val>>8)&15)+1, val & 1 ?  "" : "out");
182  else
183    strcpy(asc, "VJ disabled");
184  return asc;
185}
186
187struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };
188