vjcomp.c revision 37210
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.20 1998/06/16 19:40:42 brian Exp $
21 *
22 *  TODO:
23 */
24#include <sys/types.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
33#include "mbuf.h"
34#include "log.h"
35#include "timer.h"
36#include "fsm.h"
37#include "lcpproto.h"
38#include "slcompress.h"
39#include "lqr.h"
40#include "hdlc.h"
41#include "defs.h"
42#include "iplist.h"
43#include "throughput.h"
44#include "ipcp.h"
45#include "lcp.h"
46#include "ccp.h"
47#include "link.h"
48#include "filter.h"
49#include "descriptor.h"
50#include "mp.h"
51#include "bundle.h"
52#include "vjcomp.h"
53
54#define MAX_VJHEADER 16		/* Maximum size of compressed header */
55
56void
57vj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
58{
59  int type;
60  u_short proto;
61  u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
62
63  log_Printf(LogDEBUG, "vj_SendFrame: COMPPROTO = %x\n",
64            bundle->ncp.ipcp.peer_compproto);
65  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
66      && cproto == PROTO_VJCOMP) {
67    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp),
68                           &bundle->ncp.ipcp.vj.cslc,
69                           &bundle->ncp.ipcp.vj.slstat,
70                           bundle->ncp.ipcp.peer_compproto & 0xff);
71    log_Printf(LogDEBUG, "vj_SendFrame: type = %x\n", type);
72    switch (type) {
73    case TYPE_IP:
74      proto = PROTO_IP;
75      break;
76    case TYPE_UNCOMPRESSED_TCP:
77      proto = PROTO_VJUNCOMP;
78      break;
79    case TYPE_COMPRESSED_TCP:
80      proto = PROTO_VJCOMP;
81      break;
82    default:
83      log_Printf(LogALERT, "Unknown frame type %x\n", type);
84      mbuf_Free(bp);
85      return;
86    }
87  } else
88    proto = PROTO_IP;
89
90  if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, proto, bp))
91    hdlc_Output(l, PRI_NORMAL, proto, bp);
92}
93
94static struct mbuf *
95VjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
96{
97  u_char *bufp;
98  int len, olen, rlen;
99  struct mbuf *nbp;
100  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
101
102  olen = len = mbuf_Length(bp);
103  if (type == TYPE_UNCOMPRESSED_TCP) {
104
105    /*
106     * Uncompressed packet does NOT change its size, so that we can use mbuf
107     * space for uncompression job.
108     */
109    bufp = MBUF_CTOP(bp);
110    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
111                            (ipcp->my_compproto >> 8) & 255);
112    if (len <= 0) {
113      mbuf_Free(bp);
114      bp = NULL;
115    }
116    return (bp);
117  }
118
119  /*
120   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
121   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
122   * Copy unread data info there.
123   */
124  if (len > MAX_VJHEADER)
125    len = MAX_VJHEADER;
126  rlen = len;
127  bufp = work + MAX_HDR;
128  bp = mbuf_Read(bp, bufp, rlen);
129  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
130                          (ipcp->my_compproto >> 8) & 255);
131  if (len <= 0) {
132    mbuf_Free(bp);
133    return NULL;
134  }
135  len -= olen;
136  len += rlen;
137  nbp = mbuf_Alloc(len, MB_VJCOMP);
138  memcpy(MBUF_CTOP(nbp), bufp, len);
139  nbp->next = bp;
140  return (nbp);
141}
142
143struct mbuf *
144vj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
145{
146  u_char type;
147
148  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
149  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
150
151  switch (proto) {
152  case PROTO_VJCOMP:
153    type = TYPE_COMPRESSED_TCP;
154    break;
155  case PROTO_VJUNCOMP:
156    type = TYPE_UNCOMPRESSED_TCP;
157    break;
158  default:
159    log_Printf(LogWARN, "vj_Input...???\n");
160    return (bp);
161  }
162  bp = VjUncompressTcp(ipcp, bp, type);
163  return (bp);
164}
165
166const char *
167vj2asc(u_int32_t val)
168{
169  static char asc[50];		/* The return value is used immediately */
170
171  if (val)
172    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
173            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
174  else
175    strcpy(asc, "VJ disabled");
176  return asc;
177}
178