vjcomp.c revision 45103
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 *
2045103Sbrian * $Id: vjcomp.c,v 1.25 1999/02/06 02:54:47 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>
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"
5043313Sbrian#ifndef NORADIUS
5143313Sbrian#include "radius.h"
5243313Sbrian#endif
5336285Sbrian#include "bundle.h"
5430715Sbrian#include "vjcomp.h"
556059Samurai
5628679Sbrian#define MAX_VJHEADER 16		/* Maximum size of compressed header */
576059Samurai
586059Samuraivoid
5936285Sbrianvj_SendFrame(struct link *l, struct mbuf * bp, struct bundle *bundle)
606059Samurai{
616059Samurai  int type;
6231514Sbrian  u_short proto;
6345103Sbrian  struct ip *pip;
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);
6845103Sbrian  bp = mbuf_Contiguous(bp);
6945103Sbrian  pip = (struct ip *)MBUF_CTOP(bp);
7045103Sbrian  if (pip->ip_p == IPPROTO_TCP && cproto == PROTO_VJCOMP) {
7145103Sbrian    type = sl_compress_tcp(bp, pip, &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
10545103Sbrian  bp = mbuf_Contiguous(bp);
10636285Sbrian  olen = len = mbuf_Length(bp);
1076059Samurai  if (type == TYPE_UNCOMPRESSED_TCP) {
10828679Sbrian
1096059Samurai    /*
11028679Sbrian     * Uncompressed packet does NOT change its size, so that we can use mbuf
11128679Sbrian     * space for uncompression job.
1126059Samurai     */
1136059Samurai    bufp = MBUF_CTOP(bp);
11436960Sbrian    len = sl_uncompress_tcp(&bufp, len, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
11536960Sbrian                            (ipcp->my_compproto >> 8) & 255);
1166735Samurai    if (len <= 0) {
11736285Sbrian      mbuf_Free(bp);
11832663Sbrian      bp = NULL;
1196735Samurai    }
12028679Sbrian    return (bp);
1216059Samurai  }
12228679Sbrian
1236059Samurai  /*
12428679Sbrian   * Handle compressed packet. 1) Read upto MAX_VJHEADER bytes into work
12528679Sbrian   * space. 2) Try to uncompress it. 3) Compute amount of necesary space. 4)
12628679Sbrian   * Copy unread data info there.
1276059Samurai   */
12828679Sbrian  if (len > MAX_VJHEADER)
12928679Sbrian    len = MAX_VJHEADER;
1306059Samurai  rlen = len;
1316059Samurai  bufp = work + MAX_HDR;
13236285Sbrian  bp = mbuf_Read(bp, bufp, rlen);
13336960Sbrian  len = sl_uncompress_tcp(&bufp, olen, type, &ipcp->vj.cslc, &ipcp->vj.slstat,
13436960Sbrian                          (ipcp->my_compproto >> 8) & 255);
1356735Samurai  if (len <= 0) {
13636285Sbrian    mbuf_Free(bp);
13732663Sbrian    return NULL;
1386735Samurai  }
1396059Samurai  len -= olen;
1406059Samurai  len += rlen;
14136285Sbrian  nbp = mbuf_Alloc(len, MB_VJCOMP);
14230715Sbrian  memcpy(MBUF_CTOP(nbp), bufp, len);
1436059Samurai  nbp->next = bp;
14428679Sbrian  return (nbp);
1456059Samurai}
1466059Samurai
1476059Samuraistruct mbuf *
14836285Sbrianvj_Input(struct ipcp *ipcp, struct mbuf *bp, int proto)
1496059Samurai{
1506059Samurai  u_char type;
1516059Samurai
15236285Sbrian  log_Printf(LogDEBUG, "vj_Input: proto %02x\n", proto);
15336285Sbrian  log_DumpBp(LogDEBUG, "Raw packet info:", bp);
1546059Samurai
1556059Samurai  switch (proto) {
1566059Samurai  case PROTO_VJCOMP:
1576059Samurai    type = TYPE_COMPRESSED_TCP;
1586059Samurai    break;
1596059Samurai  case PROTO_VJUNCOMP:
1606059Samurai    type = TYPE_UNCOMPRESSED_TCP;
1616059Samurai    break;
1626059Samurai  default:
16337019Sbrian    log_Printf(LogWARN, "vj_Input...???\n");
16428679Sbrian    return (bp);
1656059Samurai  }
16636285Sbrian  bp = VjUncompressTcp(ipcp, bp, type);
16728679Sbrian  return (bp);
1686059Samurai}
16931514Sbrian
17031514Sbrianconst char *
17132439Sbrianvj2asc(u_int32_t val)
17231514Sbrian{
17337010Sbrian  static char asc[50];		/* The return value is used immediately */
17431514Sbrian
17536285Sbrian  if (val)
17636285Sbrian    snprintf(asc, sizeof asc, "%d VJ slots %s slot compression",
17736285Sbrian            (int)((val>>8)&15)+1, val & 1 ?  "with" : "without");
17836285Sbrian  else
17936285Sbrian    strcpy(asc, "VJ disabled");
18031514Sbrian  return asc;
18131514Sbrian}
182