hdlc.c revision 26516
16059Samurai/*
26059Samurai *	     PPP High Level Link Control (HDLC) Module
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.
198857Srgrimes *
2026516Sbrian * $Id: hdlc.c,v 1.15 1997/05/26 00:43:59 brian Exp $
218857Srgrimes *
226059Samurai *	TODO:
236059Samurai */
246059Samurai#include "fsm.h"
256059Samurai#include "hdlc.h"
266059Samurai#include "lcpproto.h"
276059Samurai#include "lcp.h"
286059Samurai#include "lqr.h"
2926142Sbrian#include "loadalias.h"
306735Samurai#include "vars.h"
3113389Sphk#include "pred.h"
3213389Sphk#include "modem.h"
3313389Sphk#include "ccp.h"
346059Samurai
356059Samuraistruct hdlcstat {
366059Samurai  int	badfcs;
376059Samurai  int	badaddr;
386059Samurai  int	badcommand;
396059Samurai  int	unknownproto;
406059Samurai} HdlcStat;
416059Samurai
426059Samuraistatic int ifOutPackets, ifOutOctets, ifOutLQRs;
436059Samuraistatic int ifInPackets, ifInOctets;
446059Samurai
456059Samuraistruct protostat {
466059Samurai  u_short number;
476059Samurai  char *name;
486059Samurai  u_long in_count;
496059Samurai  u_long out_count;
506059Samurai} ProtocolStat[] = {
516059Samurai  { PROTO_IP,		"IP" },
526059Samurai  { PROTO_VJUNCOMP,	"VJ_UNCOMP" },
536059Samurai  { PROTO_VJCOMP,	"VJ_COMP" },
546059Samurai  { PROTO_COMPD,	"COMPD" },
556059Samurai  { PROTO_LCP,		"LCP" },
566059Samurai  { PROTO_IPCP,		"IPCP" },
576059Samurai  { PROTO_CCP,		"CCP" },
586059Samurai  { PROTO_PAP,		"PAP" },
596059Samurai  { PROTO_LQR,		"LQR" },
606059Samurai  { PROTO_CHAP,		"CHAP" },
616059Samurai  { 0,			"Others" },
626059Samurai};
636059Samurai
6413760Sphkstatic u_short const fcstab[256] = {
656059Samurai/* 00 */    0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
666059Samurai/* 08 */    0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
676059Samurai/* 10 */    0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
686059Samurai/* 18 */    0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
696059Samurai/* 20 */    0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
706059Samurai/* 28 */    0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
716059Samurai/* 30 */    0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
726059Samurai/* 38 */    0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
736059Samurai/* 40 */    0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
746059Samurai/* 48 */    0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
756059Samurai/* 50 */    0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
766059Samurai/* 58 */    0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
776059Samurai/* 60 */    0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
786059Samurai/* 68 */    0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
796059Samurai/* 70 */    0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
806059Samurai/* 78 */    0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
816059Samurai/* 80 */    0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
826059Samurai/* 88 */    0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
836059Samurai/* 90 */    0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
846059Samurai/* 98 */    0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
856059Samurai/* a0 */    0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
866059Samurai/* a8 */    0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
876059Samurai/* b0 */    0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
886059Samurai/* b8 */    0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
896059Samurai/* c0 */    0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
906059Samurai/* c8 */    0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
916059Samurai/* d0 */    0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
926059Samurai/* d8 */    0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
936059Samurai/* e0 */    0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
946059Samurai/* e8 */    0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
956059Samurai/* f0 */    0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
966059Samurai/* f8 */    0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
976059Samurai};
986059Samurai
996059Samuraivoid
1006059SamuraiHdlcInit()
1016059Samurai{
1026059Samurai  ifInOctets = ifOutOctets = 0;
1036059Samurai  ifInPackets = ifOutPackets = 0;
1046059Samurai  ifOutLQRs = 0;
1056059Samurai}
1066059Samurai
1076059Samurai/*
1086059Samurai *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1096059Samurai *  2.27 for further details.
1106059Samurai */
11113760Sphkinline u_short
11225630SbrianHdlcFcs(u_short fcs, u_char *cp, int len)
1136059Samurai{
1146059Samurai  while (len--)
1156059Samurai    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
1166059Samurai  return(fcs);
1176059Samurai}
1186059Samurai
1196059Samuraivoid
1206059SamuraiHdlcOutput(int pri, u_short proto, struct mbuf *bp)
1216059Samurai{
1226059Samurai  struct mbuf *mhp, *mfcs;
1236059Samurai  struct protostat *statp;
1246059Samurai  struct lqrdata *lqr;
1256059Samurai  u_char *cp;
1266059Samurai  u_short fcs;
1276059Samurai
1286059Samurai  if ((proto & 0xfff1) == 0x21) {	/* Network Layer protocol */
1296059Samurai    if (CcpFsm.state == ST_OPENED) {
1306059Samurai      Pred1Output(pri, proto, bp);
1316059Samurai      return;
1326059Samurai    }
1336059Samurai  }
1346735Samurai  if (DEV_IS_SYNC)
1356735Samurai    mfcs = NULLBUFF;
1366735Samurai  else
1376735Samurai    mfcs = mballoc(2, MB_HDLCOUT);
1386059Samurai  mhp = mballoc(4, MB_HDLCOUT);
1396059Samurai  mhp->cnt = 0;
1406059Samurai  cp = MBUF_CTOP(mhp);
1416059Samurai  if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) {
1426059Samurai    *cp++ = HDLC_ADDR;
1436059Samurai    *cp++ = HDLC_UI;
1446059Samurai    mhp->cnt += 2;
1456059Samurai  }
1466059Samurai  /*
1476059Samurai   *  If possible, compress protocol field.
1486059Samurai   */
1496059Samurai  if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) {
1506059Samurai    *cp++ = proto;
1516059Samurai    mhp->cnt++;
1526059Samurai  } else {
1536059Samurai    *cp++ = proto >> 8;
1546059Samurai    *cp = proto & 0377;
1556059Samurai    mhp->cnt += 2;
1566059Samurai  }
1576059Samurai  mhp->next = bp;
1586059Samurai  bp->next = mfcs;
1596059Samurai
1606059Samurai  lqr = &MyLqrData;
1616059Samurai  lqr->PeerOutPackets = ifOutPackets++;
1626059Samurai  ifOutOctets += plength(mhp) + 1;
1636059Samurai  lqr->PeerOutOctets = ifOutOctets;
1646059Samurai
1656059Samurai  if (proto == PROTO_LQR) {
1666059Samurai    lqr->MagicNumber = LcpInfo.want_magic;
1676059Samurai    lqr->LastOutLQRs = HisLqrData.PeerOutLQRs;
1686059Samurai    lqr->LastOutPackets = HisLqrData.PeerOutPackets;
1696059Samurai    lqr->LastOutOctets = HisLqrData.PeerOutOctets;
1706059Samurai    lqr->PeerInLQRs = HisLqrSave.SaveInLQRs;
1716059Samurai    lqr->PeerInPackets = HisLqrSave.SaveInPackets;
1726059Samurai    lqr->PeerInDiscards = HisLqrSave.SaveInDiscards;
1736059Samurai    lqr->PeerInErrors = HisLqrSave.SaveInErrors;
1746059Samurai    lqr->PeerInOctets = HisLqrSave.SaveInOctets;
1756059Samurai    lqr->PeerOutLQRs = ++ifOutLQRs;
1766059Samurai    LqrDump("LqrOutput", lqr);
1776059Samurai    LqrChangeOrder(lqr, (struct lqrdata *)(MBUF_CTOP(bp)));
1786059Samurai  }
1796059Samurai
1806735Samurai  if (!DEV_IS_SYNC) {
1816735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt);
1826735Samurai    fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt);
1836735Samurai    fcs = ~fcs;
1846735Samurai    cp = MBUF_CTOP(mfcs);
1856735Samurai    *cp++ = fcs & 0377;	/* Low byte first!! */
1866735Samurai    *cp++ = fcs >> 8;
1876735Samurai  }
1886059Samurai
18926516Sbrian  LogDumpBp(LogHDLC, "HdlcOutput", mhp);
1906059Samurai  for (statp = ProtocolStat; statp->number; statp++)
1916059Samurai    if (statp->number == proto)
1926059Samurai      break;
1936059Samurai  statp->out_count++;
1946735Samurai  if (DEV_IS_SYNC)
1956735Samurai    ModemOutput(pri, mhp);
1966735Samurai  else
1976735Samurai    AsyncOutput(pri, mhp, proto);
1986059Samurai}
1996059Samurai
20013379Sphkvoid
20125630SbrianDecodePacket(u_short proto, struct mbuf *bp)
2026059Samurai{
2039448Samurai  u_char *cp;
2049448Samurai
20526516Sbrian  LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto);
20626516Sbrian
2076059Samurai  switch (proto) {
2086059Samurai  case PROTO_LCP:
2096059Samurai    LcpInput(bp);
2106059Samurai    break;
2116059Samurai  case PROTO_PAP:
2126059Samurai    PapInput(bp);
2136059Samurai    break;
2146059Samurai  case PROTO_LQR:
2156059Samurai    HisLqrSave.SaveInLQRs++;
2166059Samurai    LqrInput(bp);
2176059Samurai    break;
2186059Samurai  case PROTO_CHAP:
2196059Samurai    ChapInput(bp);
2206059Samurai    break;
2216059Samurai  case PROTO_VJUNCOMP:
2226059Samurai  case PROTO_VJCOMP:
2236059Samurai    bp = VjCompInput(bp, proto);
2246735Samurai    if (bp == NULLBUFF) {
2256735Samurai      break;
2266735Samurai    }
2276059Samurai    /* fall down */
2286059Samurai  case PROTO_IP:
2296059Samurai    IpInput(bp);
2306059Samurai    break;
2316059Samurai  case PROTO_IPCP:
2326059Samurai    IpcpInput(bp);
2336059Samurai    break;
2346059Samurai  case PROTO_CCP:
2356059Samurai    CcpInput(bp);
2366059Samurai    break;
2376059Samurai  case PROTO_COMPD:
2386059Samurai    Pred1Input(bp);
2396059Samurai    break;
2406059Samurai  default:
24126516Sbrian    LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto);
2429448Samurai    bp->offset -= 2;
2439448Samurai    bp->cnt += 2;
2449448Samurai    cp = MBUF_CTOP(bp);
2459448Samurai    LcpSendProtoRej(cp,  bp->cnt);
2466059Samurai    HisLqrSave.SaveInDiscards++;
2476059Samurai    HdlcStat.unknownproto++;
2486059Samurai    pfree(bp);
2496059Samurai    break;
2506059Samurai  }
2516059Samurai}
2526059Samurai
2536059Samuraiint
2546059SamuraiReportProtStatus()
2556059Samurai{
2566059Samurai  struct protostat *statp;
2576059Samurai  int cnt;
2586059Samurai
2596059Samurai  statp = ProtocolStat;
2606059Samurai  statp--;
2616059Samurai  cnt = 0;
26226516Sbrian  fprintf(VarTerm, "    Protocol     in        out      Protocol      in       out\n");
2636059Samurai  do {
2646059Samurai    statp++;
26526516Sbrian    fprintf(VarTerm, "   %-9s: %8lu, %8lu",
2666059Samurai      statp->name, statp->in_count, statp->out_count);
2676059Samurai    if (++cnt == 2) {
26826516Sbrian      fprintf(VarTerm, "\n");
2696059Samurai      cnt = 0;
2706059Samurai    }
2716059Samurai  } while (statp->number);
2726059Samurai  if (cnt)
27326516Sbrian     fprintf(VarTerm, "\n");
2746059Samurai  return(1);
2756059Samurai}
2766059Samurai
2776059Samuraiint
2786059SamuraiReportHdlcStatus()
2796059Samurai{
2806059Samurai  struct hdlcstat *hp = &HdlcStat;
2816059Samurai
28226516Sbrian  if (VarTerm) {
28326516Sbrian    fprintf(VarTerm, "HDLC level errors\n\n");
28426516Sbrian    fprintf(VarTerm, "FCS: %u  ADDR: %u  COMMAND: %u  PROTO: %u\n",
28526516Sbrian	  hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto);
28626516Sbrian  }
28726516Sbrian  return 0;
2886059Samurai}
2896059Samurai
2906059Samuraistatic struct hdlcstat laststat;
2916059Samurai
2926059Samuraivoid
2936059SamuraiHdlcErrorCheck()
2946059Samurai{
2956059Samurai  struct hdlcstat *hp = &HdlcStat;
2966059Samurai  struct hdlcstat *op = &laststat;
2976059Samurai
2986059Samurai  if (bcmp(hp, op, sizeof(laststat))) {
29926516Sbrian    LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n",
3006059Samurai	hp->badfcs - op->badfcs, hp->badaddr - op->badaddr,
3016059Samurai	hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto);
3026059Samurai  }
3036059Samurai  laststat = HdlcStat;
3046059Samurai}
3056059Samurai
3066059Samuraivoid
3076059SamuraiHdlcInput(struct mbuf *bp)
3086059Samurai{
3096059Samurai  u_short fcs, proto;
3106059Samurai  u_char *cp, addr, ctrl;
3116059Samurai  struct protostat *statp;
3126059Samurai
31326516Sbrian  LogDumpBp(LogHDLC, "HdlcInput:", bp);
3146735Samurai  if (DEV_IS_SYNC)
3156735Samurai    fcs = GOODFCS;
3166735Samurai  else
3176735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
3186059Samurai  HisLqrSave.SaveInOctets += bp->cnt + 1;
3196059Samurai
32026516Sbrian  LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n",
32126516Sbrian	    fcs, (fcs == GOODFCS)? "good" : "bad");
3226059Samurai  if (fcs != GOODFCS) {
3236059Samurai    HisLqrSave.SaveInErrors++;
32426516Sbrian    LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n");
3256059Samurai    HdlcStat.badfcs++;
3266059Samurai    pfree(bp);
3276059Samurai    return;
3286059Samurai  }
3296735Samurai  if (!DEV_IS_SYNC)
3306735Samurai    bp->cnt -= 2;		/* discard FCS part */
33125019Sphk
33225019Sphk  if (bp->cnt < 2) {		/* XXX: raise this bar ? */
33325021Sache	pfree(bp);
33425019Sphk	return;
33525019Sphk  }
33625019Sphk
3376059Samurai  cp = MBUF_CTOP(bp);
3386059Samurai
3396059Samurai  ifInPackets++;
3406059Samurai  ifInOctets += bp->cnt;
3416059Samurai
3426059Samurai  if (!LcpInfo.want_acfcomp) {
3436059Samurai    /*
3446059Samurai     *  We expect that packet is not compressed.
3456059Samurai     */
3466059Samurai    addr = *cp++;
3476059Samurai    if (addr != HDLC_ADDR) {
3486059Samurai      HisLqrSave.SaveInErrors++;
3496059Samurai      HdlcStat.badaddr++;
35026516Sbrian      LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp);
3516059Samurai      pfree(bp);
3526059Samurai      return;
3536059Samurai    }
3546059Samurai
3556059Samurai    ctrl = *cp++;
3566059Samurai    if (ctrl != HDLC_UI) {
3576059Samurai      HisLqrSave.SaveInErrors++;
3586059Samurai      HdlcStat.badcommand++;
35926516Sbrian      LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp);
3606059Samurai      pfree(bp);
3616059Samurai      return;
3626059Samurai    }
3636059Samurai    bp->offset += 2;
3646059Samurai    bp->cnt -= 2;
3656059Samurai  } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
3666059Samurai    /*
3676059Samurai     *  We can receive compressed packet, but peer still send
3686059Samurai     *  uncompressed packet to me.
3696059Samurai     */
3706059Samurai    cp += 2;
3716059Samurai    bp->offset += 2;
3726059Samurai    bp->cnt -= 2;
3736059Samurai  }
3746059Samurai
3756059Samurai  if (LcpInfo.want_protocomp) {
3766059Samurai    proto = 0;
3776059Samurai    cp--;
3786059Samurai    do {
3796059Samurai      cp++; bp->offset++; bp->cnt--;
3806059Samurai      proto = proto << 8;
3816059Samurai      proto += *cp;
3826059Samurai    } while (!(proto & 1));
3836059Samurai  } else {
3846059Samurai    proto = *cp++ << 8;
3856059Samurai    proto |= *cp++;
3866059Samurai    bp->offset += 2;
3876059Samurai    bp->cnt -= 2;
3886059Samurai  }
3896059Samurai
3906059Samurai  for (statp = ProtocolStat; statp->number; statp++)
3916059Samurai    if (statp->number == proto)
3926059Samurai      break;
3936059Samurai  statp->in_count++;
3946059Samurai  HisLqrSave.SaveInPackets++;
3956059Samurai
3966059Samurai  DecodePacket(proto, bp);
3976059Samurai}
398