vjcomp.c revision 36285
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.16.2.17 1998/05/04 03:00:09 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    if (len <= 0) {
111      mbuf_Free(bp);
112      bp = NULL;
113    }
114    return (bp);
115  }
116
117  /*
118   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
119   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
120   * Copy unread data info there.
121   */
122  if (len > MAX_VJHEADER)
123    len = MAX_VJHEADER;
124  rlen = len;
125  bufp = work + MAX_HDR;
126  bp = mbuf_Read(bp, bufp, rlen);
127  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat);
128  if (len <= 0) {
129    mbuf_Free(bp);
130    return NULL;
131  }
132  len -= olen;
133  len += rlen;
134  nbp = mbuf_Alloc(len, MB_VJCOMP);
135  memcpy(MBUF_CTOP(nbp), bufp, len);
136  nbp->next = bp;
137  return (nbp);
138}
139
140struct mbuf *
141vj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
142{
143  u_char type;
144
145  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
146  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
147
148  switch (proto) {
149  case PROTO_VJCOMP:
150    type = TYPE_COMPRESSED_TCP;
151    break;
152  case PROTO_VJUNCOMP:
153    type = TYPE_UNCOMPRESSED_TCP;
154    break;
155  default:
156    log_Printf(LogERROR, "vj_Input...???\n");
157    return (bp);
158  }
159  bp = VjUncompressTcp(ipcp, bp, type);
160  return (bp);
161}
162
163const char *
164vj2asc(u_int32_t val)
165{
166  static char asc[50];
167
168  if (val)
169    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
170            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
171  else
172    strcpy(asc, "VJ disabled");
173  return asc;
174}
175