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