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