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