hdlc.c revision 9448
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 * 209448Samurai * $Id: hdlc.c,v 1.3 1995/05/30 03:50:33 rgrimes 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" 306059Samurai 316059Samuraistruct hdlcstat { 326059Samurai int badfcs; 336059Samurai int badaddr; 346059Samurai int badcommand; 356059Samurai int unknownproto; 366059Samurai} HdlcStat; 376059Samurai 386059Samuraistatic int ifOutPackets, ifOutOctets, ifOutLQRs; 396059Samuraistatic int ifInPackets, ifInOctets; 406059Samurai 416059Samuraistruct protostat { 426059Samurai u_short number; 436059Samurai char *name; 446059Samurai u_long in_count; 456059Samurai u_long out_count; 466059Samurai} ProtocolStat[] = { 476059Samurai { PROTO_IP, "IP" }, 486059Samurai { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 496059Samurai { PROTO_VJCOMP, "VJ_COMP" }, 506059Samurai { PROTO_COMPD, "COMPD" }, 516059Samurai { PROTO_LCP, "LCP" }, 526059Samurai { PROTO_IPCP, "IPCP" }, 536059Samurai { PROTO_CCP, "CCP" }, 546059Samurai { PROTO_PAP, "PAP" }, 556059Samurai { PROTO_LQR, "LQR" }, 566059Samurai { PROTO_CHAP, "CHAP" }, 576059Samurai { 0, "Others" }, 586059Samurai}; 596059Samurai 606059Samuraistatic u_short fcstab[256] = { 616059Samurai/* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 626059Samurai/* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 636059Samurai/* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 646059Samurai/* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 656059Samurai/* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 666059Samurai/* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 676059Samurai/* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 686059Samurai/* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 696059Samurai/* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 706059Samurai/* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 716059Samurai/* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 726059Samurai/* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 736059Samurai/* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 746059Samurai/* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 756059Samurai/* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 766059Samurai/* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 776059Samurai/* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 786059Samurai/* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 796059Samurai/* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 806059Samurai/* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 816059Samurai/* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 826059Samurai/* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 836059Samurai/* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 846059Samurai/* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 856059Samurai/* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 866059Samurai/* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 876059Samurai/* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 886059Samurai/* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 896059Samurai/* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 906059Samurai/* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 916059Samurai/* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 926059Samurai/* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 936059Samurai}; 946059Samurai 956059Samuraivoid 966059SamuraiHdlcInit() 976059Samurai{ 986059Samurai ifInOctets = ifOutOctets = 0; 996059Samurai ifInPackets = ifOutPackets = 0; 1006059Samurai ifOutLQRs = 0; 1016059Samurai} 1026059Samurai 1036059Samurai/* 1046059Samurai * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1056059Samurai * 2.27 for further details. 1066059Samurai */ 1076059Samuraiu_short 1086059SamuraiHdlcFcs(fcs, cp, len) 1096059Samuraiu_short fcs; 1106059Samuraiu_char *cp; 1116059Samuraiint len; 1126059Samurai{ 1136059Samurai while (len--) 1146059Samurai fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 1156059Samurai return(fcs); 1166059Samurai} 1176059Samurai 1186059Samuraivoid 1196059SamuraiHdlcOutput(int pri, u_short proto, struct mbuf *bp) 1206059Samurai{ 1216059Samurai struct mbuf *mhp, *mfcs; 1226059Samurai struct protostat *statp; 1236059Samurai struct lqrdata *lqr; 1246059Samurai u_char *cp; 1256059Samurai u_short fcs; 1266059Samurai 1276059Samurai if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 1286059Samurai if (CcpFsm.state == ST_OPENED) { 1296059Samurai Pred1Output(pri, proto, bp); 1306059Samurai return; 1316059Samurai } 1326059Samurai } 1336735Samurai if (DEV_IS_SYNC) 1346735Samurai mfcs = NULLBUFF; 1356735Samurai else 1366735Samurai mfcs = mballoc(2, MB_HDLCOUT); 1376059Samurai mhp = mballoc(4, MB_HDLCOUT); 1386059Samurai mhp->cnt = 0; 1396059Samurai cp = MBUF_CTOP(mhp); 1406059Samurai if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 1416059Samurai *cp++ = HDLC_ADDR; 1426059Samurai *cp++ = HDLC_UI; 1436059Samurai mhp->cnt += 2; 1446059Samurai } 1456059Samurai /* 1466059Samurai * If possible, compress protocol field. 1476059Samurai */ 1486059Samurai if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 1496059Samurai *cp++ = proto; 1506059Samurai mhp->cnt++; 1516059Samurai } else { 1526059Samurai *cp++ = proto >> 8; 1536059Samurai *cp = proto & 0377; 1546059Samurai mhp->cnt += 2; 1556059Samurai } 1566059Samurai mhp->next = bp; 1576059Samurai bp->next = mfcs; 1586059Samurai 1596059Samurai lqr = &MyLqrData; 1606059Samurai lqr->PeerOutPackets = ifOutPackets++; 1616059Samurai ifOutOctets += plength(mhp) + 1; 1626059Samurai lqr->PeerOutOctets = ifOutOctets; 1636059Samurai 1646059Samurai if (proto == PROTO_LQR) { 1656059Samurai lqr->MagicNumber = LcpInfo.want_magic; 1666059Samurai lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 1676059Samurai lqr->LastOutPackets = HisLqrData.PeerOutPackets; 1686059Samurai lqr->LastOutOctets = HisLqrData.PeerOutOctets; 1696059Samurai lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 1706059Samurai lqr->PeerInPackets = HisLqrSave.SaveInPackets; 1716059Samurai lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 1726059Samurai lqr->PeerInErrors = HisLqrSave.SaveInErrors; 1736059Samurai lqr->PeerInOctets = HisLqrSave.SaveInOctets; 1746059Samurai lqr->PeerOutLQRs = ++ifOutLQRs; 1756059Samurai LqrDump("LqrOutput", lqr); 1766059Samurai LqrChangeOrder(lqr, (struct lqrdata *)(MBUF_CTOP(bp))); 1776059Samurai } 1786059Samurai 1796735Samurai if (!DEV_IS_SYNC) { 1806735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 1816735Samurai fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 1826735Samurai fcs = ~fcs; 1836735Samurai cp = MBUF_CTOP(mfcs); 1846735Samurai *cp++ = fcs & 0377; /* Low byte first!! */ 1856735Samurai *cp++ = fcs >> 8; 1866735Samurai } 1876059Samurai 1886059Samurai LogDumpBp(LOG_HDLC, "HdlcOutput", mhp); 1896059Samurai for (statp = ProtocolStat; statp->number; statp++) 1906059Samurai if (statp->number == proto) 1916059Samurai break; 1926059Samurai statp->out_count++; 1936735Samurai if (DEV_IS_SYNC) 1946735Samurai ModemOutput(pri, mhp); 1956735Samurai else 1966735Samurai AsyncOutput(pri, mhp, proto); 1976059Samurai} 1986059Samurai 1996059SamuraiDecodePacket(proto, bp) 2006059Samuraiu_short proto; 2016059Samuraistruct mbuf *bp; 2026059Samurai{ 2036059Samurai#ifdef DEBUG 2046059Samurai logprintf("proto = %04x\n", proto); 2056059Samurai#endif 2069448Samurai u_char *cp; 2079448Samurai 2086059Samurai switch (proto) { 2096059Samurai case PROTO_LCP: 2106059Samurai LcpInput(bp); 2116059Samurai break; 2126059Samurai case PROTO_PAP: 2136059Samurai PapInput(bp); 2146059Samurai break; 2156059Samurai case PROTO_LQR: 2166059Samurai HisLqrSave.SaveInLQRs++; 2176059Samurai LqrInput(bp); 2186059Samurai break; 2196059Samurai case PROTO_CHAP: 2206059Samurai ChapInput(bp); 2216059Samurai break; 2226059Samurai case PROTO_VJUNCOMP: 2236059Samurai case PROTO_VJCOMP: 2246059Samurai bp = VjCompInput(bp, proto); 2256735Samurai if (bp == NULLBUFF) { 2266735Samurai break; 2276735Samurai } 2286059Samurai /* fall down */ 2296059Samurai case PROTO_IP: 2306059Samurai IpInput(bp); 2316059Samurai break; 2326059Samurai case PROTO_IPCP: 2336059Samurai IpcpInput(bp); 2346059Samurai break; 2356059Samurai case PROTO_CCP: 2366059Samurai CcpInput(bp); 2376059Samurai break; 2386059Samurai case PROTO_COMPD: 2396059Samurai Pred1Input(bp); 2406059Samurai break; 2416059Samurai default: 2429448Samurai LogPrintf(LOG_PHASE, "Unknown protocol 0x%04x\n", proto); 2439448Samurai bp->offset -= 2; 2449448Samurai bp->cnt += 2; 2459448Samurai cp = MBUF_CTOP(bp); 2469448Samurai LcpSendProtoRej(cp, bp->cnt); 2476059Samurai HisLqrSave.SaveInDiscards++; 2486059Samurai HdlcStat.unknownproto++; 2496059Samurai pfree(bp); 2506059Samurai break; 2516059Samurai } 2526059Samurai} 2536059Samurai 2546059Samuraiint 2556059SamuraiReportProtStatus() 2566059Samurai{ 2576059Samurai struct protostat *statp; 2586059Samurai int cnt; 2596059Samurai 2606059Samurai statp = ProtocolStat; 2616059Samurai statp--; 2626059Samurai cnt = 0; 2636059Samurai printf(" Protocol in out Protocol in out\n"); 2646059Samurai do { 2656059Samurai statp++; 2666059Samurai printf(" %-9s: %8u, %8u", 2676059Samurai statp->name, statp->in_count, statp->out_count); 2686059Samurai if (++cnt == 2) { 2696059Samurai printf("\n"); 2706059Samurai cnt = 0; 2716059Samurai } 2726059Samurai } while (statp->number); 2736059Samurai if (cnt) 2746059Samurai printf("\n"); 2756059Samurai return(1); 2766059Samurai} 2776059Samurai 2786059Samuraiint 2796059SamuraiReportHdlcStatus() 2806059Samurai{ 2816059Samurai struct hdlcstat *hp = &HdlcStat; 2826059Samurai 2836059Samurai printf("HDLC level errors\n\n"); 2846059Samurai printf("FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 2856059Samurai hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 2866059Samurai return(1); 2876059Samurai} 2886059Samurai 2896059Samuraistatic struct hdlcstat laststat; 2906059Samurai 2916059Samuraivoid 2926059SamuraiHdlcErrorCheck() 2936059Samurai{ 2946059Samurai struct hdlcstat *hp = &HdlcStat; 2956059Samurai struct hdlcstat *op = &laststat; 2966059Samurai 2976059Samurai if (bcmp(hp, op, sizeof(laststat))) { 2986059Samurai LogPrintf(LOG_PHASE, "HDLC errros -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 2996059Samurai hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 3006059Samurai hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 3016059Samurai } 3026059Samurai laststat = HdlcStat; 3036059Samurai} 3046059Samurai 3056059Samuraivoid 3066059SamuraiHdlcInput(struct mbuf *bp) 3076059Samurai{ 3086059Samurai u_short fcs, proto; 3096059Samurai u_char *cp, addr, ctrl; 3106059Samurai struct protostat *statp; 3116059Samurai 3126059Samurai LogDumpBp(LOG_HDLC, "HdlcInput:", bp); 3136735Samurai if (DEV_IS_SYNC) 3146735Samurai fcs = GOODFCS; 3156735Samurai else 3166735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 3176059Samurai HisLqrSave.SaveInOctets += bp->cnt + 1; 3186059Samurai 3196059Samurai#ifdef DEBUG 3206059Samurai logprintf("fcs = %04x (%s)\n", fcs, (fcs == GOODFCS)? "good" : "bad"); 3216059Samurai#endif 3226059Samurai if (fcs != GOODFCS) { 3236059Samurai HisLqrSave.SaveInErrors++; 3246059Samurai#ifdef DEBUG 3256059Samurai logprintf("Bad FCS\n"); 3266059Samurai#endif 3276059Samurai HdlcStat.badfcs++; 3286059Samurai pfree(bp); 3296059Samurai return; 3306059Samurai } 3316735Samurai if (!DEV_IS_SYNC) 3326735Samurai bp->cnt -= 2; /* discard FCS part */ 3336059Samurai cp = MBUF_CTOP(bp); 3346059Samurai 3356059Samurai ifInPackets++; 3366059Samurai ifInOctets += bp->cnt; 3376059Samurai 3386059Samurai if (!LcpInfo.want_acfcomp) { 3396059Samurai /* 3406059Samurai * We expect that packet is not compressed. 3416059Samurai */ 3426059Samurai addr = *cp++; 3436059Samurai if (addr != HDLC_ADDR) { 3446059Samurai HisLqrSave.SaveInErrors++; 3456059Samurai HdlcStat.badaddr++; 3466059Samurai#ifdef DEBUG 3476059Samurai logprintf("addr %02x\n", *cp); 3486059Samurai#endif 3496059Samurai pfree(bp); 3506059Samurai return; 3516059Samurai } 3526059Samurai 3536059Samurai ctrl = *cp++; 3546059Samurai if (ctrl != HDLC_UI) { 3556059Samurai HisLqrSave.SaveInErrors++; 3566059Samurai HdlcStat.badcommand++; 3576059Samurai#ifdef DEBUG 3586059Samurai logprintf("command %02x\n", *cp); 3596059Samurai#endif 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