vjcomp.c revision 43313
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 *
2043313Sbrian * $Id: vjcomp.c,v 1.23 1998/08/26 18:07:57 brian Exp $
218857Srgrimes *
226059Samurai *  TODO:
236059Samurai */
2443313Sbrian#include <sys/param.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"
5143313Sbrian#ifndef NORADIUS
5243313Sbrian#include "radius.h"
5343313Sbrian#endif
5436285Sbrian#include "bundle.h"
5530715Sbrian#include "vjcomp.h"
566059Samurai
5728679Sbrian#define MAX_VJHEADER 16		/* Maximum size of compressed header */
586059Samurai
596059Samuraivoid
6036285Sbrianvj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
616059Samurai{
626059Samurai  int type;
6331514Sbrian  u_short proto;
6436285Sbrian  u_short cproto = bundle->ncp.ipcp.peer_compproto >> 16;
656059Samurai
6636285Sbrian  log_Printf(LogDEBUG, "vj_SendFrame: COMPPROTO = %x\n",
6736285Sbrian            bundle->ncp.ipcp.peer_compproto);
6828679Sbrian  if (((struct ip *) MBUF_CTOP(bp))->ip_p == IPPROTO_TCP
6928679Sbrian      && cproto == PROTO_VJCOMP) {
7036285Sbrian    type = sl_compress_tcp(bp, (struct ip *)MBUF_CTOP(bp),
7136285Sbrian                           &bundle->ncp.ipcp.vj.cslc,
7236285Sbrian                           &bundle->ncp.ipcp.vj.slstat,
7336285Sbrian                           bundle->ncp.ipcp.peer_compproto & 0xff);
7436285Sbrian    log_Printf(LogDEBUG, "vj_SendFrame: type = %x\n", type);
756059Samurai    switch (type) {
766059Samurai    case TYPE_IP:
776059Samurai      proto = PROTO_IP;
786059Samurai      break;
796059Samurai    case TYPE_UNCOMPRESSED_TCP:
806059Samurai      proto = PROTO_VJUNCOMP;
816059Samurai      break;
826059Samurai    case TYPE_COMPRESSED_TCP:
836059Samurai      proto = PROTO_VJCOMP;
846059Samurai      break;
856059Samurai    default:
8637019Sbrian      log_Printf(LogALERT, "Unknown frame type %x\n", type);
8736285Sbrian      mbuf_Free(bp);
886059Samurai      return;
896059Samurai    }
906059Samurai  } else
916059Samurai    proto = PROTO_IP;
9236285Sbrian
9336285Sbrian  if (!ccp_Compress(&l->ccp, l, PRI_NORMAL, proto, bp))
9436285Sbrian    hdlc_Output(l, PRI_NORMAL, proto, bp);
956059Samurai}
966059Samurai
976059Samuraistatic struct mbuf *
9836285SbrianVjUncompressTcp(struct ipcp *ipcp, struct mbuf * bp, u_char type)
996059Samurai{
1006059Samurai  u_char *bufp;
1016059Samurai  int len, olen, rlen;
1026059Samurai  struct mbuf *nbp;
10328679Sbrian  u_char work[MAX_HDR + MAX_VJHEADER];	/* enough to hold TCP/IP header */
1046059Samurai
10536285Sbrian  olen = len = mbuf_Length(bp);
1066059Samurai  if (type == TYPE_UNCOMPRESSED_TCP) {
10728679Sbrian
1086059Samurai    /*
10928679Sbrian     * Uncompressed packet does NOT change its size, so that we can use mbuf
11028679Sbrian     * space for uncompression job.
1116059Samurai     */
1126059Samurai    bufp = MBUF_CTOP(bp);
11336960Sbrian    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
11436960Sbrian                            (ipcp->my_compproto >> 8) & 255);
1156735Samurai    if (len <= 0) {
11636285Sbrian      mbuf_Free(bp);
11732663Sbrian      bp = NULL;
1186735Samurai    }
11928679Sbrian    return (bp);
1206059Samurai  }
12128679Sbrian
1226059Samurai  /*
12328679Sbrian   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
12428679Sbrian   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
12528679Sbrian   * Copy unread data info there.
1266059Samurai   */
12728679Sbrian  if (len > MAX_VJHEADER)
12828679Sbrian    len = MAX_VJHEADER;
1296059Samurai  rlen = len;
1306059Samurai  bufp = work + MAX_HDR;
13136285Sbrian  bp = mbuf_Read(bp, bufp, rlen);
13236960Sbrian  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
13336960Sbrian                          (ipcp->my_compproto >> 8) & 255);
1346735Samurai  if (len <= 0) {
13536285Sbrian    mbuf_Free(bp);
13632663Sbrian    return NULL;
1376735Samurai  }
1386059Samurai  len -= olen;
1396059Samurai  len += rlen;
14036285Sbrian  nbp = mbuf_Alloc(len, MB_VJCOMP);
14130715Sbrian  memcpy(MBUF_CTOP(nbp), bufp, len);
1426059Samurai  nbp->next = bp;
14328679Sbrian  return (nbp);
1446059Samurai}
1456059Samurai
1466059Samuraistruct mbuf *
14736285Sbrianvj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
1486059Samurai{
1496059Samurai  u_char type;
1506059Samurai
15136285Sbrian  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
15236285Sbrian  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
1536059Samurai
1546059Samurai  switch (proto) {
1556059Samurai  case PROTO_VJCOMP:
1566059Samurai    type = TYPE_COMPRESSED_TCP;
1576059Samurai    break;
1586059Samurai  case PROTO_VJUNCOMP:
1596059Samurai    type = TYPE_UNCOMPRESSED_TCP;
1606059Samurai    break;
1616059Samurai  default:
16237019Sbrian    log_Printf(LogWARN, "vj_Input...???\n");
16328679Sbrian    return (bp);
1646059Samurai  }
16536285Sbrian  bp = VjUncompressTcp(ipcp, bp, type);
16628679Sbrian  return (bp);
1676059Samurai}
16831514Sbrian
16931514Sbrianconst char *
17032439Sbrianvj2asc(u_int32_t val)
17131514Sbrian{
17237010Sbrian  static char asc[50];		/* The return value is used immediately */
17331514Sbrian
17436285Sbrian  if (val)
17536285Sbrian    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
17636285Sbrian            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
17736285Sbrian  else
17836285Sbrian    strcpy(asc, "VJ disabled");
17931514Sbrian  return asc;
18031514Sbrian}
181