vjcomp.c revision 47695
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.30 1999/05/12 09:49:12 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  bp = mbuf_Contiguous(bp);
70  pip = (struct ip *)MBUF_CTOP(bp);
71  if (*proto == PROTO_IP && pip->ip_p == IPPROTO_TCP &&
72      cproto == PROTO_VJCOMP) {
73    type = sl_compress_tcp(bp, pip, &bundle->ncp.ipcp.vj.cslc,
74                           &bundle->ncp.ipcp.vj.slstat,
75                           bundle->ncp.ipcp.peer_compproto & 0xff);
76    log_Printf(LogDEBUG, "vj_LayerWrite: type = %x\n", type);
77    switch (type) {
78    case TYPE_IP:
79      break;
80
81    case TYPE_UNCOMPRESSED_TCP:
82      *proto = PROTO_VJUNCOMP;
83      log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
84      mbuf_SetType(bp, MB_VJOUT);
85      break;
86
87    case TYPE_COMPRESSED_TCP:
88      *proto = PROTO_VJCOMP;
89      log_Printf(LogDEBUG, "vj_LayerPush: PROTO_IP -> PROTO_VJUNCOMP\n");
90      mbuf_SetType(bp, MB_VJOUT);
91      break;
92
93    default:
94      log_Printf(LogERROR, "vj_LayerPush: Unknown frame type %x\n", type);
95      mbuf_Free(bp);
96      return NULL;
97    }
98  }
99
100  return bp;
101}
102
103static struct mbuf *
104VjUncompressTcp(struct ipcp *ipcp, struct mbuf *bp, u_char type)
105{
106  u_char *bufp;
107  int len, olen, rlen;
108  struct mbuf *nbp;
109  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
110
111  bp = mbuf_Contiguous(bp);
112  olen = len = mbuf_Length(bp);
113  if (type == TYPE_UNCOMPRESSED_TCP) {
114    /*
115     * Uncompressed packet does NOT change its size, so that we can use mbuf
116     * space for uncompression job.
117     */
118    bufp = MBUF_CTOP(bp);
119    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
120                            (ipcp->my_compproto >> 8) & 255);
121    if (len <= 0) {
122      mbuf_Free(bp);
123      bp = NULL;
124    } else
125      mbuf_SetType(bp, MB_VJIN);
126    return bp;
127  }
128
129  /*
130   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
131   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
132   * Copy unread data info there.
133   */
134  if (len > MAX_VJHEADER)
135    len = MAX_VJHEADER;
136  rlen = len;
137  bufp = work + MAX_HDR;
138  bp = mbuf_Read(bp, bufp, rlen);
139  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
140                          (ipcp->my_compproto >> 8) & 255);
141  if (len <= 0) {
142    mbuf_Free(bp);
143    return NULL;
144  }
145  len -= olen;
146  len += rlen;
147  nbp = mbuf_Alloc(len, MB_VJIN);
148  memcpy(MBUF_CTOP(nbp), bufp, len);
149  mbuf_SetType(bp, MB_VJIN);
150  nbp->next = bp;
151  return nbp;
152}
153
154static struct mbuf *
155vj_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
156             u_short *proto)
157{
158  u_char type;
159
160  switch (*proto) {
161  case PROTO_VJCOMP:
162    type = TYPE_COMPRESSED_TCP;
163    log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJCOMP -> PROTO_IP\n");
164    break;
165  case PROTO_VJUNCOMP:
166    type = TYPE_UNCOMPRESSED_TCP;
167    log_Printf(LogDEBUG, "vj_LayerPull: PROTO_VJUNCOMP -> PROTO_IP\n");
168    break;
169  default:
170    return bp;
171  }
172
173  *proto = PROTO_IP;
174  return VjUncompressTcp(&bundle->ncp.ipcp, bp, type);
175}
176
177const char *
178vj2asc(u_int32_t val)
179{
180  static char asc[50];		/* The return value is used immediately */
181
182  if (val)
183    snprintf(asc, sizeof asc, "%d VJ slots with%s slot compression",
184            (int)((val>>8)&15)+1, val & 1 ?  "" : "out");
185  else
186    strcpy(asc, "VJ disabled");
187  return asc;
188}
189
190struct layer vjlayer = { LAYER_VJ, "vj", vj_LayerPush, vj_LayerPull };
191