vjcomp.c revision 37010
16059Samurai/*
26059Samurai *	       Input/Output VJ Compressed packets
36059Samurai *
46059Samurai *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
56059Samurai *
66059Samurai *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
76059Samurai *
86059Samurai * Redistribution and use in source and binary forms are permitted
96059Samurai * provided that the above copyright notice and this paragraph are
106059Samurai * duplicated in all such forms and that any documentation,
116059Samurai * advertising materials, and other materials related to such
126059Samurai * distribution and use acknowledge that the software was developed
136059Samurai * by the Internet Initiative Japan, Inc.  The name of the
146059Samurai * IIJ may not be used to endorse or promote products derived
156059Samurai * from this software without specific prior written permission.
166059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
176059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
186059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
196059Samurai *
2037010Sbrian * $Id: vjcomp.c,v 1.18 1998/06/14 00:56:13 brian Exp $
218857Srgrimes *
226059Samurai *  TODO:
236059Samurai */
2430715Sbrian#include <sys/types.h>
2530715Sbrian#include <netinet/in.h>
2630715Sbrian#include <netinet/in_systm.h>
2730715Sbrian#include <netinet/ip.h>
2836285Sbrian#include <sys/un.h>
2930715Sbrian
3031061Sbrian#include <stdio.h>
3130715Sbrian
3230715Sbrian#include "mbuf.h"
3330715Sbrian#include "log.h"
3430715Sbrian#include "timer.h"
356059Samurai#include "fsm.h"
366059Samurai#include "lcpproto.h"
376059Samurai#include "slcompress.h"
3836285Sbrian#include "lqr.h"
396059Samurai#include "hdlc.h"
4036285Sbrian#include "defs.h"
4136285Sbrian#include "iplist.h"
4236285Sbrian#include "throughput.h"
436059Samurai#include "ipcp.h"
4436285Sbrian#include "lcp.h"
4536285Sbrian#include "ccp.h"
4636285Sbrian#include "link.h"
4736285Sbrian#include "filter.h"
4836285Sbrian#include "descriptor.h"
4936285Sbrian#include "mp.h"
5036285Sbrian#include "bundle.h"
5130715Sbrian#include "vjcomp.h"
526059Samurai
5328679Sbrian#define MAX_VJHEADER 16		/* Maximum size of compressed header */
546059Samurai
556059Samuraivoid
5636285Sbrianvj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
576059Samurai{
586059Samurai  int type;
5931514Sbrian  u_short proto;
6036285Sbrian  u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
616059Samurai
6236285Sbrian  log_Printf(LogDEBUG, "vj_SendFrame: COMPPROTO = %x\n",
6336285Sbrian            bundle->ncp.ipcp.peer_compproto);
6428679Sbrian  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
6528679Sbrian      && cproto == PROTO_VJCOMP) {
6636285Sbrian    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp),
6736285Sbrian                           &bundle->ncp.ipcp.vj.cslc,
6836285Sbrian                           &bundle->ncp.ipcp.vj.slstat,
6936285Sbrian                           bundle->ncp.ipcp.peer_compproto & 0xff);
7036285Sbrian    log_Printf(LogDEBUG, "vj_SendFrame: type = %x\n", type);
716059Samurai    switch (type) {
726059Samurai    case TYPE_IP:
736059Samurai      proto = PROTO_IP;
746059Samurai      break;
756059Samurai    case TYPE_UNCOMPRESSED_TCP:
766059Samurai      proto = PROTO_VJUNCOMP;
776059Samurai      break;
786059Samurai    case TYPE_COMPRESSED_TCP:
796059Samurai      proto = PROTO_VJCOMP;
806059Samurai      break;
816059Samurai    default:
8236285Sbrian      log_Printf(LogERROR, "Unknown frame type %x\n", type);
8336285Sbrian      mbuf_Free(bp);
846059Samurai      return;
856059Samurai    }
866059Samurai  } else
876059Samurai    proto = PROTO_IP;
8836285Sbrian
8936285Sbrian  if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, proto, bp))
9036285Sbrian    hdlc_Output(l, PRI_NORMAL, proto, bp);
916059Samurai}
926059Samurai
936059Samuraistatic struct mbuf *
9436285SbrianVjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
956059Samurai{
966059Samurai  u_char *bufp;
976059Samurai  int len, olen, rlen;
986059Samurai  struct mbuf *nbp;
9928679Sbrian  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
1006059Samurai
10136285Sbrian  olen = len = mbuf_Length(bp);
1026059Samurai  if (type == TYPE_UNCOMPRESSED_TCP) {
10328679Sbrian
1046059Samurai    /*
10528679Sbrian     * Uncompressed packet does NOT change its size, so that we can use mbuf
10628679Sbrian     * space for uncompression job.
1076059Samurai     */
1086059Samurai    bufp = MBUF_CTOP(bp);
10936960Sbrian    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
11036960Sbrian                            (ipcp->my_compproto >> 8) & 255);
1116735Samurai    if (len <= 0) {
11236285Sbrian      mbuf_Free(bp);
11332663Sbrian      bp = NULL;
1146735Samurai    }
11528679Sbrian    return (bp);
1166059Samurai  }
11728679Sbrian
1186059Samurai  /*
11928679Sbrian   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
12028679Sbrian   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
12128679Sbrian   * Copy unread data info there.
1226059Samurai   */
12328679Sbrian  if (len > MAX_VJHEADER)
12428679Sbrian    len = MAX_VJHEADER;
1256059Samurai  rlen = len;
1266059Samurai  bufp = work + MAX_HDR;
12736285Sbrian  bp = mbuf_Read(bp, bufp, rlen);
12836960Sbrian  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
12936960Sbrian                          (ipcp->my_compproto >> 8) & 255);
1306735Samurai  if (len <= 0) {
13136285Sbrian    mbuf_Free(bp);
13232663Sbrian    return NULL;
1336735Samurai  }
1346059Samurai  len -= olen;
1356059Samurai  len += rlen;
13636285Sbrian  nbp = mbuf_Alloc(len, MB_VJCOMP);
13730715Sbrian  memcpy(MBUF_CTOP(nbp), bufp, len);
1386059Samurai  nbp->next = bp;
13928679Sbrian  return (nbp);
1406059Samurai}
1416059Samurai
1426059Samuraistruct mbuf *
14336285Sbrianvj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
1446059Samurai{
1456059Samurai  u_char type;
1466059Samurai
14736285Sbrian  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
14836285Sbrian  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
1496059Samurai
1506059Samurai  switch (proto) {
1516059Samurai  case PROTO_VJCOMP:
1526059Samurai    type = TYPE_COMPRESSED_TCP;
1536059Samurai    break;
1546059Samurai  case PROTO_VJUNCOMP:
1556059Samurai    type = TYPE_UNCOMPRESSED_TCP;
1566059Samurai    break;
1576059Samurai  default:
15836285Sbrian    log_Printf(LogERROR, "vj_Input...???\n");
15928679Sbrian    return (bp);
1606059Samurai  }
16136285Sbrian  bp = VjUncompressTcp(ipcp, bp, type);
16228679Sbrian  return (bp);
1636059Samurai}
16431514Sbrian
16531514Sbrianconst char *
16632439Sbrianvj2asc(u_int32_t val)
16731514Sbrian{
16837010Sbrian  static char asc[50];		/* The return value is used immediately */
16931514Sbrian
17036285Sbrian  if (val)
17136285Sbrian    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
17236285Sbrian            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
17336285Sbrian  else
17436285Sbrian    strcpy(asc, "VJ disabled");
17531514Sbrian  return asc;
17631514Sbrian}
177