vjcomp.c revision 38559
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 *
2038559Sbrian * $Id: vjcomp.c,v 1.22 1998/08/07 18:42:51 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>
3138559Sbrian#include <string.h>
3230715Sbrian
3330715Sbrian#include "mbuf.h"
3430715Sbrian#include "log.h"
3530715Sbrian#include "timer.h"
366059Samurai#include "fsm.h"
376059Samurai#include "lcpproto.h"
386059Samurai#include "slcompress.h"
3936285Sbrian#include "lqr.h"
406059Samurai#include "hdlc.h"
4136285Sbrian#include "defs.h"
4236285Sbrian#include "iplist.h"
4336285Sbrian#include "throughput.h"
446059Samurai#include "ipcp.h"
4536285Sbrian#include "lcp.h"
4636285Sbrian#include "ccp.h"
4736285Sbrian#include "link.h"
4836285Sbrian#include "filter.h"
4936285Sbrian#include "descriptor.h"
5036285Sbrian#include "mp.h"
5136285Sbrian#include "bundle.h"
5230715Sbrian#include "vjcomp.h"
536059Samurai
5428679Sbrian#define MAX_VJHEADER 16		/* Maximum size of compressed header */
556059Samurai
566059Samuraivoid
5736285Sbrianvj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
586059Samurai{
596059Samurai  int type;
6031514Sbrian  u_short proto;
6136285Sbrian  u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
626059Samurai
6336285Sbrian  log_Printf(LogDEBUG, "vj_SendFrame: COMPPROTO = %x\n",
6436285Sbrian            bundle->ncp.ipcp.peer_compproto);
6528679Sbrian  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
6628679Sbrian      && cproto == PROTO_VJCOMP) {
6736285Sbrian    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp),
6836285Sbrian                           &bundle->ncp.ipcp.vj.cslc,
6936285Sbrian                           &bundle->ncp.ipcp.vj.slstat,
7036285Sbrian                           bundle->ncp.ipcp.peer_compproto & 0xff);
7136285Sbrian    log_Printf(LogDEBUG, "vj_SendFrame: type = %x\n", type);
726059Samurai    switch (type) {
736059Samurai    case TYPE_IP:
746059Samurai      proto = PROTO_IP;
756059Samurai      break;
766059Samurai    case TYPE_UNCOMPRESSED_TCP:
776059Samurai      proto = PROTO_VJUNCOMP;
786059Samurai      break;
796059Samurai    case TYPE_COMPRESSED_TCP:
806059Samurai      proto = PROTO_VJCOMP;
816059Samurai      break;
826059Samurai    default:
8337019Sbrian      log_Printf(LogALERT, "Unknown frame type %x\n", type);
8436285Sbrian      mbuf_Free(bp);
856059Samurai      return;
866059Samurai    }
876059Samurai  } else
886059Samurai    proto = PROTO_IP;
8936285Sbrian
9036285Sbrian  if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, proto, bp))
9136285Sbrian    hdlc_Output(l, PRI_NORMAL, proto, bp);
926059Samurai}
936059Samurai
946059Samuraistatic struct mbuf *
9536285SbrianVjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
966059Samurai{
976059Samurai  u_char *bufp;
986059Samurai  int len, olen, rlen;
996059Samurai  struct mbuf *nbp;
10028679Sbrian  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
1016059Samurai
10236285Sbrian  olen = len = mbuf_Length(bp);
1036059Samurai  if (type == TYPE_UNCOMPRESSED_TCP) {
10428679Sbrian
1056059Samurai    /*
10628679Sbrian     * Uncompressed packet does NOT change its size, so that we can use mbuf
10728679Sbrian     * space for uncompression job.
1086059Samurai     */
1096059Samurai    bufp = MBUF_CTOP(bp);
11036960Sbrian    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
11136960Sbrian                            (ipcp->my_compproto >> 8) & 255);
1126735Samurai    if (len <= 0) {
11336285Sbrian      mbuf_Free(bp);
11432663Sbrian      bp = NULL;
1156735Samurai    }
11628679Sbrian    return (bp);
1176059Samurai  }
11828679Sbrian
1196059Samurai  /*
12028679Sbrian   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
12128679Sbrian   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
12228679Sbrian   * Copy unread data info there.
1236059Samurai   */
12428679Sbrian  if (len > MAX_VJHEADER)
12528679Sbrian    len = MAX_VJHEADER;
1266059Samurai  rlen = len;
1276059Samurai  bufp = work + MAX_HDR;
12836285Sbrian  bp = mbuf_Read(bp, bufp, rlen);
12936960Sbrian  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
13036960Sbrian                          (ipcp->my_compproto >> 8) & 255);
1316735Samurai  if (len <= 0) {
13236285Sbrian    mbuf_Free(bp);
13332663Sbrian    return NULL;
1346735Samurai  }
1356059Samurai  len -= olen;
1366059Samurai  len += rlen;
13736285Sbrian  nbp = mbuf_Alloc(len, MB_VJCOMP);
13830715Sbrian  memcpy(MBUF_CTOP(nbp), bufp, len);
1396059Samurai  nbp->next = bp;
14028679Sbrian  return (nbp);
1416059Samurai}
1426059Samurai
1436059Samuraistruct mbuf *
14436285Sbrianvj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
1456059Samurai{
1466059Samurai  u_char type;
1476059Samurai
14836285Sbrian  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
14936285Sbrian  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
1506059Samurai
1516059Samurai  switch (proto) {
1526059Samurai  case PROTO_VJCOMP:
1536059Samurai    type = TYPE_COMPRESSED_TCP;
1546059Samurai    break;
1556059Samurai  case PROTO_VJUNCOMP:
1566059Samurai    type = TYPE_UNCOMPRESSED_TCP;
1576059Samurai    break;
1586059Samurai  default:
15937019Sbrian    log_Printf(LogWARN, "vj_Input...???\n");
16028679Sbrian    return (bp);
1616059Samurai  }
16236285Sbrian  bp = VjUncompressTcp(ipcp, bp, type);
16328679Sbrian  return (bp);
1646059Samurai}
16531514Sbrian
16631514Sbrianconst char *
16732439Sbrianvj2asc(u_int32_t val)
16831514Sbrian{
16937010Sbrian  static char asc[50];		/* The return value is used immediately */
17031514Sbrian
17136285Sbrian  if (val)
17236285Sbrian    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
17336285Sbrian            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
17436285Sbrian  else
17536285Sbrian    strcpy(asc, "VJ disabled");
17631514Sbrian  return asc;
17731514Sbrian}
178