hdlc.c revision 13760
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 *
2013760Sphk * $Id: hdlc.c,v 1.7 1996/01/11 17:48:46 phk 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"
296735Samurai#include "vars.h"
3013389Sphk#include "pred.h"
3113389Sphk#include "modem.h"
3213389Sphk#include "ccp.h"
336059Samurai
346059Samuraistruct hdlcstat {
356059Samurai  int	badfcs;
366059Samurai  int	badaddr;
376059Samurai  int	badcommand;
386059Samurai  int	unknownproto;
396059Samurai} HdlcStat;
406059Samurai
416059Samuraistatic int ifOutPackets, ifOutOctets, ifOutLQRs;
426059Samuraistatic int ifInPackets, ifInOctets;
436059Samurai
446059Samuraistruct protostat {
456059Samurai  u_short number;
466059Samurai  char *name;
476059Samurai  u_long in_count;
486059Samurai  u_long out_count;
496059Samurai} ProtocolStat[] = {
506059Samurai  { PROTO_IP,		"IP" },
516059Samurai  { PROTO_VJUNCOMP,	"VJ_UNCOMP" },
526059Samurai  { PROTO_VJCOMP,	"VJ_COMP" },
536059Samurai  { PROTO_COMPD,	"COMPD" },
546059Samurai  { PROTO_LCP,		"LCP" },
556059Samurai  { PROTO_IPCP,		"IPCP" },
566059Samurai  { PROTO_CCP,		"CCP" },
576059Samurai  { PROTO_PAP,		"PAP" },
586059Samurai  { PROTO_LQR,		"LQR" },
596059Samurai  { PROTO_CHAP,		"CHAP" },
606059Samurai  { 0,			"Others" },
616059Samurai};
626059Samurai
6313760Sphkstatic u_short const fcstab[256] = {
646059Samurai/* 00 */    0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
656059Samurai/* 08 */    0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
666059Samurai/* 10 */    0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
676059Samurai/* 18 */    0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
686059Samurai/* 20 */    0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
696059Samurai/* 28 */    0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
706059Samurai/* 30 */    0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
716059Samurai/* 38 */    0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
726059Samurai/* 40 */    0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
736059Samurai/* 48 */    0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
746059Samurai/* 50 */    0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
756059Samurai/* 58 */    0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
766059Samurai/* 60 */    0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
776059Samurai/* 68 */    0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
786059Samurai/* 70 */    0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
796059Samurai/* 78 */    0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
806059Samurai/* 80 */    0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
816059Samurai/* 88 */    0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
826059Samurai/* 90 */    0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
836059Samurai/* 98 */    0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
846059Samurai/* a0 */    0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
856059Samurai/* a8 */    0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
866059Samurai/* b0 */    0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
876059Samurai/* b8 */    0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
886059Samurai/* c0 */    0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
896059Samurai/* c8 */    0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
906059Samurai/* d0 */    0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
916059Samurai/* d8 */    0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
926059Samurai/* e0 */    0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
936059Samurai/* e8 */    0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
946059Samurai/* f0 */    0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
956059Samurai/* f8 */    0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
966059Samurai};
976059Samurai
986059Samuraivoid
996059SamuraiHdlcInit()
1006059Samurai{
1016059Samurai  ifInOctets = ifOutOctets = 0;
1026059Samurai  ifInPackets = ifOutPackets = 0;
1036059Samurai  ifOutLQRs = 0;
1046059Samurai}
1056059Samurai
1066059Samurai/*
1076059Samurai *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1086059Samurai *  2.27 for further details.
1096059Samurai */
11013760Sphkinline u_short
1116059SamuraiHdlcFcs(fcs, cp, len)
1126059Samuraiu_short fcs;
1136059Samuraiu_char *cp;
1146059Samuraiint len;
1156059Samurai{
1166059Samurai  while (len--)
1176059Samurai    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
1186059Samurai  return(fcs);
1196059Samurai}
1206059Samurai
1216059Samuraivoid
1226059SamuraiHdlcOutput(int pri, u_short proto, struct mbuf *bp)
1236059Samurai{
1246059Samurai  struct mbuf *mhp, *mfcs;
1256059Samurai  struct protostat *statp;
1266059Samurai  struct lqrdata *lqr;
1276059Samurai  u_char *cp;
1286059Samurai  u_short fcs;
1296059Samurai
1306059Samurai  if ((proto & 0xfff1) == 0x21) {	/* Network Layer protocol */
1316059Samurai    if (CcpFsm.state == ST_OPENED) {
1326059Samurai      Pred1Output(pri, proto, bp);
1336059Samurai      return;
1346059Samurai    }
1356059Samurai  }
1366735Samurai  if (DEV_IS_SYNC)
1376735Samurai    mfcs = NULLBUFF;
1386735Samurai  else
1396735Samurai    mfcs = mballoc(2, MB_HDLCOUT);
1406059Samurai  mhp = mballoc(4, MB_HDLCOUT);
1416059Samurai  mhp->cnt = 0;
1426059Samurai  cp = MBUF_CTOP(mhp);
1436059Samurai  if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) {
1446059Samurai    *cp++ = HDLC_ADDR;
1456059Samurai    *cp++ = HDLC_UI;
1466059Samurai    mhp->cnt += 2;
1476059Samurai  }
1486059Samurai  /*
1496059Samurai   *  If possible, compress protocol field.
1506059Samurai   */
1516059Samurai  if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) {
1526059Samurai    *cp++ = proto;
1536059Samurai    mhp->cnt++;
1546059Samurai  } else {
1556059Samurai    *cp++ = proto >> 8;
1566059Samurai    *cp = proto & 0377;
1576059Samurai    mhp->cnt += 2;
1586059Samurai  }
1596059Samurai  mhp->next = bp;
1606059Samurai  bp->next = mfcs;
1616059Samurai
1626059Samurai  lqr = &MyLqrData;
1636059Samurai  lqr->PeerOutPackets = ifOutPackets++;
1646059Samurai  ifOutOctets += plength(mhp) + 1;
1656059Samurai  lqr->PeerOutOctets = ifOutOctets;
1666059Samurai
1676059Samurai  if (proto == PROTO_LQR) {
1686059Samurai    lqr->MagicNumber = LcpInfo.want_magic;
1696059Samurai    lqr->LastOutLQRs = HisLqrData.PeerOutLQRs;
1706059Samurai    lqr->LastOutPackets = HisLqrData.PeerOutPackets;
1716059Samurai    lqr->LastOutOctets = HisLqrData.PeerOutOctets;
1726059Samurai    lqr->PeerInLQRs = HisLqrSave.SaveInLQRs;
1736059Samurai    lqr->PeerInPackets = HisLqrSave.SaveInPackets;
1746059Samurai    lqr->PeerInDiscards = HisLqrSave.SaveInDiscards;
1756059Samurai    lqr->PeerInErrors = HisLqrSave.SaveInErrors;
1766059Samurai    lqr->PeerInOctets = HisLqrSave.SaveInOctets;
1776059Samurai    lqr->PeerOutLQRs = ++ifOutLQRs;
1786059Samurai    LqrDump("LqrOutput", lqr);
1796059Samurai    LqrChangeOrder(lqr, (struct lqrdata *)(MBUF_CTOP(bp)));
1806059Samurai  }
1816059Samurai
1826735Samurai  if (!DEV_IS_SYNC) {
1836735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt);
1846735Samurai    fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt);
1856735Samurai    fcs = ~fcs;
1866735Samurai    cp = MBUF_CTOP(mfcs);
1876735Samurai    *cp++ = fcs & 0377;	/* Low byte first!! */
1886735Samurai    *cp++ = fcs >> 8;
1896735Samurai  }
1906059Samurai
1916059Samurai  LogDumpBp(LOG_HDLC, "HdlcOutput", mhp);
1926059Samurai  for (statp = ProtocolStat; statp->number; statp++)
1936059Samurai    if (statp->number == proto)
1946059Samurai      break;
1956059Samurai  statp->out_count++;
1966735Samurai  if (DEV_IS_SYNC)
1976735Samurai    ModemOutput(pri, mhp);
1986735Samurai  else
1996735Samurai    AsyncOutput(pri, mhp, proto);
2006059Samurai}
2016059Samurai
20213379Sphkvoid
2036059SamuraiDecodePacket(proto, bp)
2046059Samuraiu_short proto;
2056059Samuraistruct mbuf *bp;
2066059Samurai{
2076059Samurai#ifdef DEBUG
2086059Samurai  logprintf("proto = %04x\n", proto);
2096059Samurai#endif
2109448Samurai  u_char *cp;
2119448Samurai
2126059Samurai  switch (proto) {
2136059Samurai  case PROTO_LCP:
2146059Samurai    LcpInput(bp);
2156059Samurai    break;
2166059Samurai  case PROTO_PAP:
2176059Samurai    PapInput(bp);
2186059Samurai    break;
2196059Samurai  case PROTO_LQR:
2206059Samurai    HisLqrSave.SaveInLQRs++;
2216059Samurai    LqrInput(bp);
2226059Samurai    break;
2236059Samurai  case PROTO_CHAP:
2246059Samurai    ChapInput(bp);
2256059Samurai    break;
2266059Samurai  case PROTO_VJUNCOMP:
2276059Samurai  case PROTO_VJCOMP:
2286059Samurai    bp = VjCompInput(bp, proto);
2296735Samurai    if (bp == NULLBUFF) {
2306735Samurai      break;
2316735Samurai    }
2326059Samurai    /* fall down */
2336059Samurai  case PROTO_IP:
2346059Samurai    IpInput(bp);
2356059Samurai    break;
2366059Samurai  case PROTO_IPCP:
2376059Samurai    IpcpInput(bp);
2386059Samurai    break;
2396059Samurai  case PROTO_CCP:
2406059Samurai    CcpInput(bp);
2416059Samurai    break;
2426059Samurai  case PROTO_COMPD:
2436059Samurai    Pred1Input(bp);
2446059Samurai    break;
2456059Samurai  default:
2469448Samurai    LogPrintf(LOG_PHASE, "Unknown protocol 0x%04x\n", proto);
2479448Samurai    bp->offset -= 2;
2489448Samurai    bp->cnt += 2;
2499448Samurai    cp = MBUF_CTOP(bp);
2509448Samurai    LcpSendProtoRej(cp,  bp->cnt);
2516059Samurai    HisLqrSave.SaveInDiscards++;
2526059Samurai    HdlcStat.unknownproto++;
2536059Samurai    pfree(bp);
2546059Samurai    break;
2556059Samurai  }
2566059Samurai}
2576059Samurai
2586059Samuraiint
2596059SamuraiReportProtStatus()
2606059Samurai{
2616059Samurai  struct protostat *statp;
2626059Samurai  int cnt;
2636059Samurai
2646059Samurai  statp = ProtocolStat;
2656059Samurai  statp--;
2666059Samurai  cnt = 0;
2676059Samurai  printf("    Protocol     in        out      Protocol      in       out\n");
2686059Samurai  do {
2696059Samurai    statp++;
27013389Sphk    printf("   %-9s: %8lu, %8lu",
2716059Samurai      statp->name, statp->in_count, statp->out_count);
2726059Samurai    if (++cnt == 2) {
2736059Samurai      printf("\n");
2746059Samurai      cnt = 0;
2756059Samurai    }
2766059Samurai  } while (statp->number);
2776059Samurai  if (cnt)
2786059Samurai     printf("\n");
2796059Samurai  return(1);
2806059Samurai}
2816059Samurai
2826059Samuraiint
2836059SamuraiReportHdlcStatus()
2846059Samurai{
2856059Samurai  struct hdlcstat *hp = &HdlcStat;
2866059Samurai
2876059Samurai  printf("HDLC level errors\n\n");
2886059Samurai  printf("FCS: %u  ADDR: %u  COMMAND: %u  PROTO: %u\n",
2896059Samurai	hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto);
2906059Samurai  return(1);
2916059Samurai}
2926059Samurai
2936059Samuraistatic struct hdlcstat laststat;
2946059Samurai
2956059Samuraivoid
2966059SamuraiHdlcErrorCheck()
2976059Samurai{
2986059Samurai  struct hdlcstat *hp = &HdlcStat;
2996059Samurai  struct hdlcstat *op = &laststat;
3006059Samurai
3016059Samurai  if (bcmp(hp, op, sizeof(laststat))) {
3029797Sgpalmer    LogPrintf(LOG_PHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n",
3036059Samurai	hp->badfcs - op->badfcs, hp->badaddr - op->badaddr,
3046059Samurai	hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto);
3056059Samurai  }
3066059Samurai  laststat = HdlcStat;
3076059Samurai}
3086059Samurai
3096059Samuraivoid
3106059SamuraiHdlcInput(struct mbuf *bp)
3116059Samurai{
3126059Samurai  u_short fcs, proto;
3136059Samurai  u_char *cp, addr, ctrl;
3146059Samurai  struct protostat *statp;
3156059Samurai
3166059Samurai  LogDumpBp(LOG_HDLC, "HdlcInput:", bp);
3176735Samurai  if (DEV_IS_SYNC)
3186735Samurai    fcs = GOODFCS;
3196735Samurai  else
3206735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
3216059Samurai  HisLqrSave.SaveInOctets += bp->cnt + 1;
3226059Samurai
3236059Samurai#ifdef DEBUG
3246059Samurai  logprintf("fcs = %04x (%s)\n", fcs, (fcs == GOODFCS)? "good" : "bad");
3256059Samurai#endif
3266059Samurai  if (fcs != GOODFCS) {
3276059Samurai    HisLqrSave.SaveInErrors++;
3286059Samurai#ifdef DEBUG
3296059Samurai    logprintf("Bad FCS\n");
3306059Samurai#endif
3316059Samurai    HdlcStat.badfcs++;
3326059Samurai    pfree(bp);
3336059Samurai    return;
3346059Samurai  }
3356735Samurai  if (!DEV_IS_SYNC)
3366735Samurai    bp->cnt -= 2;		/* discard FCS part */
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++;
3506059Samurai#ifdef DEBUG
3516059Samurai      logprintf("addr %02x\n", *cp);
3526059Samurai#endif
3536059Samurai      pfree(bp);
3546059Samurai      return;
3556059Samurai    }
3566059Samurai
3576059Samurai    ctrl = *cp++;
3586059Samurai    if (ctrl != HDLC_UI) {
3596059Samurai      HisLqrSave.SaveInErrors++;
3606059Samurai      HdlcStat.badcommand++;
3616059Samurai#ifdef DEBUG
3626059Samurai      logprintf("command %02x\n", *cp);
3636059Samurai#endif
3646059Samurai      pfree(bp);
3656059Samurai      return;
3666059Samurai    }
3676059Samurai    bp->offset += 2;
3686059Samurai    bp->cnt -= 2;
3696059Samurai  } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
3706059Samurai    /*
3716059Samurai     *  We can receive compressed packet, but peer still send
3726059Samurai     *  uncompressed packet to me.
3736059Samurai     */
3746059Samurai    cp += 2;
3756059Samurai    bp->offset += 2;
3766059Samurai    bp->cnt -= 2;
3776059Samurai  }
3786059Samurai
3796059Samurai  if (LcpInfo.want_protocomp) {
3806059Samurai    proto = 0;
3816059Samurai    cp--;
3826059Samurai    do {
3836059Samurai      cp++; bp->offset++; bp->cnt--;
3846059Samurai      proto = proto << 8;
3856059Samurai      proto += *cp;
3866059Samurai    } while (!(proto & 1));
3876059Samurai  } else {
3886059Samurai    proto = *cp++ << 8;
3896059Samurai    proto |= *cp++;
3906059Samurai    bp->offset += 2;
3916059Samurai    bp->cnt -= 2;
3926059Samurai  }
3936059Samurai
3946059Samurai  for (statp = ProtocolStat; statp->number; statp++)
3956059Samurai    if (statp->number == proto)
3966059Samurai      break;
3976059Samurai  statp->in_count++;
3986059Samurai  HisLqrSave.SaveInPackets++;
3996059Samurai
4006059Samurai  DecodePacket(proto, bp);
4016059Samurai}
402