hdlc.c revision 29294
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 * 2029294Sbrian * $Id: hdlc.c,v 1.17 1997/08/25 00:29:13 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 { 3628679Sbrian int badfcs; 3728679Sbrian int badaddr; 3828679Sbrian int badcommand; 3928679Sbrian int unknownproto; 4028679Sbrian} 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; 5028679Sbrian} ProtocolStat[] = { 5128679Sbrian 5228679Sbrian { 5328679Sbrian PROTO_IP, "IP" 5428679Sbrian }, 5528679Sbrian { 5628679Sbrian PROTO_VJUNCOMP, "VJ_UNCOMP" 5728679Sbrian }, 5828679Sbrian { 5928679Sbrian PROTO_VJCOMP, "VJ_COMP" 6028679Sbrian }, 6128679Sbrian { 6228679Sbrian PROTO_COMPD, "COMPD" 6328679Sbrian }, 6428679Sbrian { 6528679Sbrian PROTO_LCP, "LCP" 6628679Sbrian }, 6728679Sbrian { 6828679Sbrian PROTO_IPCP, "IPCP" 6928679Sbrian }, 7028679Sbrian { 7128679Sbrian PROTO_CCP, "CCP" 7228679Sbrian }, 7328679Sbrian { 7428679Sbrian PROTO_PAP, "PAP" 7528679Sbrian }, 7628679Sbrian { 7728679Sbrian PROTO_LQR, "LQR" 7828679Sbrian }, 7928679Sbrian { 8028679Sbrian PROTO_CHAP, "CHAP" 8128679Sbrian }, 8228679Sbrian { 8328679Sbrian 0, "Others" 8428679Sbrian }, 856059Samurai}; 866059Samurai 8713760Sphkstatic u_short const fcstab[256] = { 8828679Sbrian /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 8928679Sbrian /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 9028679Sbrian /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 9128679Sbrian /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 9228679Sbrian /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 9328679Sbrian /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 9428679Sbrian /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 9528679Sbrian /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 9628679Sbrian /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 9728679Sbrian /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 9828679Sbrian /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 9928679Sbrian /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 10028679Sbrian /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 10128679Sbrian /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 10228679Sbrian /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 10328679Sbrian /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 10428679Sbrian /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 10528679Sbrian /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 10628679Sbrian /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 10728679Sbrian /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 10828679Sbrian /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 10928679Sbrian /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 11028679Sbrian /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 11128679Sbrian /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 11228679Sbrian /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 11328679Sbrian /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 11428679Sbrian /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 11528679Sbrian /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 11628679Sbrian /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 11728679Sbrian /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 11828679Sbrian /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 11928679Sbrian /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 1206059Samurai}; 1216059Samurai 12228679Sbrianu_char EscMap[33]; 12328679Sbrian 1246059Samuraivoid 1256059SamuraiHdlcInit() 1266059Samurai{ 1276059Samurai ifInOctets = ifOutOctets = 0; 1286059Samurai ifInPackets = ifOutPackets = 0; 1296059Samurai ifOutLQRs = 0; 1306059Samurai} 1316059Samurai 1326059Samurai/* 1336059Samurai * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1346059Samurai * 2.27 for further details. 1356059Samurai */ 13628679Sbrianinline u_short 13728679SbrianHdlcFcs(u_short fcs, u_char * cp, int len) 1386059Samurai{ 1396059Samurai while (len--) 1406059Samurai fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 14128679Sbrian return (fcs); 1426059Samurai} 1436059Samurai 1446059Samuraivoid 14528679SbrianHdlcOutput(int pri, u_short proto, struct mbuf * bp) 1466059Samurai{ 1476059Samurai struct mbuf *mhp, *mfcs; 1486059Samurai struct protostat *statp; 1496059Samurai struct lqrdata *lqr; 1506059Samurai u_char *cp; 1516059Samurai u_short fcs; 1526059Samurai 1536059Samurai if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 1546059Samurai if (CcpFsm.state == ST_OPENED) { 15529294Sbrian if (CcpInfo.want_proto == PRED1) { 15629294Sbrian Pred1Output(pri, proto, bp); 15729294Sbrian return; 15829294Sbrian } 1596059Samurai } 1606059Samurai } 1616735Samurai if (DEV_IS_SYNC) 1626735Samurai mfcs = NULLBUFF; 1636735Samurai else 1646735Samurai mfcs = mballoc(2, MB_HDLCOUT); 1656059Samurai mhp = mballoc(4, MB_HDLCOUT); 1666059Samurai mhp->cnt = 0; 1676059Samurai cp = MBUF_CTOP(mhp); 1686059Samurai if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 1696059Samurai *cp++ = HDLC_ADDR; 1706059Samurai *cp++ = HDLC_UI; 1716059Samurai mhp->cnt += 2; 1726059Samurai } 17328679Sbrian 1746059Samurai /* 17528679Sbrian * If possible, compress protocol field. 1766059Samurai */ 1776059Samurai if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 1786059Samurai *cp++ = proto; 1796059Samurai mhp->cnt++; 1806059Samurai } else { 1816059Samurai *cp++ = proto >> 8; 1826059Samurai *cp = proto & 0377; 1836059Samurai mhp->cnt += 2; 1846059Samurai } 1856059Samurai mhp->next = bp; 1866059Samurai bp->next = mfcs; 1876059Samurai 1886059Samurai lqr = &MyLqrData; 1896059Samurai lqr->PeerOutPackets = ifOutPackets++; 1906059Samurai ifOutOctets += plength(mhp) + 1; 1916059Samurai lqr->PeerOutOctets = ifOutOctets; 1926059Samurai 1936059Samurai if (proto == PROTO_LQR) { 1946059Samurai lqr->MagicNumber = LcpInfo.want_magic; 1956059Samurai lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 1966059Samurai lqr->LastOutPackets = HisLqrData.PeerOutPackets; 1976059Samurai lqr->LastOutOctets = HisLqrData.PeerOutOctets; 1986059Samurai lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 1996059Samurai lqr->PeerInPackets = HisLqrSave.SaveInPackets; 2006059Samurai lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 2016059Samurai lqr->PeerInErrors = HisLqrSave.SaveInErrors; 2026059Samurai lqr->PeerInOctets = HisLqrSave.SaveInOctets; 2036059Samurai lqr->PeerOutLQRs = ++ifOutLQRs; 2046059Samurai LqrDump("LqrOutput", lqr); 20528679Sbrian LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 2066059Samurai } 2076735Samurai if (!DEV_IS_SYNC) { 2086735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 2096735Samurai fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 2106735Samurai fcs = ~fcs; 2116735Samurai cp = MBUF_CTOP(mfcs); 21228679Sbrian *cp++ = fcs & 0377; /* Low byte first!! */ 2136735Samurai *cp++ = fcs >> 8; 2146735Samurai } 21526516Sbrian LogDumpBp(LogHDLC, "HdlcOutput", mhp); 2166059Samurai for (statp = ProtocolStat; statp->number; statp++) 2176059Samurai if (statp->number == proto) 2186059Samurai break; 2196059Samurai statp->out_count++; 2206735Samurai if (DEV_IS_SYNC) 2216735Samurai ModemOutput(pri, mhp); 2226735Samurai else 2236735Samurai AsyncOutput(pri, mhp, proto); 2246059Samurai} 2256059Samurai 22613379Sphkvoid 22728679SbrianDecodePacket(u_short proto, struct mbuf * bp) 2286059Samurai{ 2299448Samurai u_char *cp; 2309448Samurai 23126516Sbrian LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto); 23226516Sbrian 2336059Samurai switch (proto) { 2346059Samurai case PROTO_LCP: 2356059Samurai LcpInput(bp); 2366059Samurai break; 2376059Samurai case PROTO_PAP: 2386059Samurai PapInput(bp); 2396059Samurai break; 2406059Samurai case PROTO_LQR: 2416059Samurai HisLqrSave.SaveInLQRs++; 2426059Samurai LqrInput(bp); 2436059Samurai break; 2446059Samurai case PROTO_CHAP: 2456059Samurai ChapInput(bp); 2466059Samurai break; 2476059Samurai case PROTO_VJUNCOMP: 2486059Samurai case PROTO_VJCOMP: 2496059Samurai bp = VjCompInput(bp, proto); 2506735Samurai if (bp == NULLBUFF) { 2516735Samurai break; 2526735Samurai } 2536059Samurai /* fall down */ 2546059Samurai case PROTO_IP: 2556059Samurai IpInput(bp); 2566059Samurai break; 2576059Samurai case PROTO_IPCP: 2586059Samurai IpcpInput(bp); 2596059Samurai break; 2606059Samurai case PROTO_CCP: 2616059Samurai CcpInput(bp); 2626059Samurai break; 2636059Samurai case PROTO_COMPD: 2646059Samurai Pred1Input(bp); 2656059Samurai break; 2666059Samurai default: 26726516Sbrian LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto); 2689448Samurai bp->offset -= 2; 2699448Samurai bp->cnt += 2; 2709448Samurai cp = MBUF_CTOP(bp); 27128679Sbrian LcpSendProtoRej(cp, bp->cnt); 2726059Samurai HisLqrSave.SaveInDiscards++; 2736059Samurai HdlcStat.unknownproto++; 2746059Samurai pfree(bp); 2756059Samurai break; 2766059Samurai } 2776059Samurai} 2786059Samurai 2796059Samuraiint 2806059SamuraiReportProtStatus() 2816059Samurai{ 2826059Samurai struct protostat *statp; 2836059Samurai int cnt; 2846059Samurai 2856059Samurai statp = ProtocolStat; 2866059Samurai statp--; 2876059Samurai cnt = 0; 28826516Sbrian fprintf(VarTerm, " Protocol in out Protocol in out\n"); 2896059Samurai do { 2906059Samurai statp++; 29126516Sbrian fprintf(VarTerm, " %-9s: %8lu, %8lu", 29228679Sbrian statp->name, statp->in_count, statp->out_count); 2936059Samurai if (++cnt == 2) { 29426516Sbrian fprintf(VarTerm, "\n"); 2956059Samurai cnt = 0; 2966059Samurai } 2976059Samurai } while (statp->number); 2986059Samurai if (cnt) 29928679Sbrian fprintf(VarTerm, "\n"); 30028679Sbrian return (1); 3016059Samurai} 3026059Samurai 3036059Samuraiint 3046059SamuraiReportHdlcStatus() 3056059Samurai{ 3066059Samurai struct hdlcstat *hp = &HdlcStat; 3076059Samurai 30826516Sbrian if (VarTerm) { 30926516Sbrian fprintf(VarTerm, "HDLC level errors\n\n"); 31026516Sbrian fprintf(VarTerm, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 31128679Sbrian hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 31226516Sbrian } 31326516Sbrian return 0; 3146059Samurai} 3156059Samurai 3166059Samuraistatic struct hdlcstat laststat; 3176059Samurai 3186059Samuraivoid 3196059SamuraiHdlcErrorCheck() 3206059Samurai{ 3216059Samurai struct hdlcstat *hp = &HdlcStat; 3226059Samurai struct hdlcstat *op = &laststat; 3236059Samurai 3246059Samurai if (bcmp(hp, op, sizeof(laststat))) { 32526516Sbrian LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 32628679Sbrian hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 32728679Sbrian hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 3286059Samurai } 3296059Samurai laststat = HdlcStat; 3306059Samurai} 3316059Samurai 3326059Samuraivoid 33328679SbrianHdlcInput(struct mbuf * bp) 3346059Samurai{ 3356059Samurai u_short fcs, proto; 3366059Samurai u_char *cp, addr, ctrl; 3376059Samurai struct protostat *statp; 3386059Samurai 33926516Sbrian LogDumpBp(LogHDLC, "HdlcInput:", bp); 3406735Samurai if (DEV_IS_SYNC) 3416735Samurai fcs = GOODFCS; 3426735Samurai else 3436735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 3446059Samurai HisLqrSave.SaveInOctets += bp->cnt + 1; 3456059Samurai 34626516Sbrian LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 34728679Sbrian fcs, (fcs == GOODFCS) ? "good" : "bad"); 3486059Samurai if (fcs != GOODFCS) { 3496059Samurai HisLqrSave.SaveInErrors++; 35026516Sbrian LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 3516059Samurai HdlcStat.badfcs++; 3526059Samurai pfree(bp); 3536059Samurai return; 3546059Samurai } 3556735Samurai if (!DEV_IS_SYNC) 3566735Samurai bp->cnt -= 2; /* discard FCS part */ 35725019Sphk 35825019Sphk if (bp->cnt < 2) { /* XXX: raise this bar ? */ 35928679Sbrian pfree(bp); 36028679Sbrian return; 36125019Sphk } 3626059Samurai cp = MBUF_CTOP(bp); 3636059Samurai 3646059Samurai ifInPackets++; 3656059Samurai ifInOctets += bp->cnt; 3666059Samurai 3676059Samurai if (!LcpInfo.want_acfcomp) { 36828679Sbrian 3696059Samurai /* 37028679Sbrian * We expect that packet is not compressed. 3716059Samurai */ 3726059Samurai addr = *cp++; 3736059Samurai if (addr != HDLC_ADDR) { 3746059Samurai HisLqrSave.SaveInErrors++; 3756059Samurai HdlcStat.badaddr++; 37626516Sbrian LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 3776059Samurai pfree(bp); 3786059Samurai return; 3796059Samurai } 3806059Samurai ctrl = *cp++; 3816059Samurai if (ctrl != HDLC_UI) { 3826059Samurai HisLqrSave.SaveInErrors++; 3836059Samurai HdlcStat.badcommand++; 38426516Sbrian LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 3856059Samurai pfree(bp); 3866059Samurai return; 3876059Samurai } 3886059Samurai bp->offset += 2; 3896059Samurai bp->cnt -= 2; 3906059Samurai } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 39128679Sbrian 3926059Samurai /* 39328679Sbrian * We can receive compressed packet, but peer still send uncompressed 39428679Sbrian * packet to me. 3956059Samurai */ 3966059Samurai cp += 2; 3976059Samurai bp->offset += 2; 3986059Samurai bp->cnt -= 2; 3996059Samurai } 4006059Samurai if (LcpInfo.want_protocomp) { 4016059Samurai proto = 0; 4026059Samurai cp--; 4036059Samurai do { 40428679Sbrian cp++; 40528679Sbrian bp->offset++; 40628679Sbrian bp->cnt--; 4076059Samurai proto = proto << 8; 4086059Samurai proto += *cp; 4096059Samurai } while (!(proto & 1)); 4106059Samurai } else { 4116059Samurai proto = *cp++ << 8; 4126059Samurai proto |= *cp++; 4136059Samurai bp->offset += 2; 4146059Samurai bp->cnt -= 2; 4156059Samurai } 4166059Samurai 4176059Samurai for (statp = ProtocolStat; statp->number; statp++) 4186059Samurai if (statp->number == proto) 4196059Samurai break; 4206059Samurai statp->in_count++; 4216059Samurai HisLqrSave.SaveInPackets++; 4226059Samurai 4236059Samurai DecodePacket(proto, bp); 4246059Samurai} 425