hdlc.c revision 9797
119370Spst/* 298944Sobrien * PPP High Level Link Control (HDLC) Module 398944Sobrien * 419370Spst * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 519370Spst * 698944Sobrien * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 719370Spst * 898944Sobrien * Redistribution and use in source and binary forms are permitted 998944Sobrien * provided that the above copyright notice and this paragraph are 1098944Sobrien * duplicated in all such forms and that any documentation, 1198944Sobrien * advertising materials, and other materials related to such 1219370Spst * distribution and use acknowledge that the software was developed 1398944Sobrien * by the Internet Initiative Japan, Inc. The name of the 1498944Sobrien * IIJ may not be used to endorse or promote products derived 1598944Sobrien * from this software without specific prior written permission. 1698944Sobrien * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1719370Spst * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1898944Sobrien * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1998944Sobrien * 2098944Sobrien * $Id: hdlc.c,v 1.4 1995/07/08 17:46:49 amurai Exp $ 2198944Sobrien * 2219370Spst * TODO: 2319370Spst */ 2419370Spst#include "fsm.h" 2546283Sdfr#include "hdlc.h" 2619370Spst#include "lcpproto.h" 2719370Spst#include "lcp.h" 2898944Sobrien#include "lqr.h" 2998944Sobrien#include "vars.h" 3019370Spst 3119370Spststruct hdlcstat { 3246283Sdfr int badfcs; 3398944Sobrien int badaddr; 3419370Spst int badcommand; 3519370Spst int unknownproto; 3619370Spst} HdlcStat; 3746283Sdfr 3819370Spststatic int ifOutPackets, ifOutOctets, ifOutLQRs; 3919370Spststatic int ifInPackets, ifInOctets; 4019370Spst 4119370Spststruct protostat { 4246283Sdfr u_short number; 4346283Sdfr char *name; 4446283Sdfr u_long in_count; 4546283Sdfr u_long out_count; 4646283Sdfr} ProtocolStat[] = { 4746283Sdfr { PROTO_IP, "IP" }, 4846283Sdfr { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 4946283Sdfr { PROTO_VJCOMP, "VJ_COMP" }, 5046283Sdfr { PROTO_COMPD, "COMPD" }, 5198944Sobrien { PROTO_LCP, "LCP" }, 5246283Sdfr { PROTO_IPCP, "IPCP" }, 5346283Sdfr { PROTO_CCP, "CCP" }, 5446283Sdfr { PROTO_PAP, "PAP" }, 5546283Sdfr { PROTO_LQR, "LQR" }, 5646283Sdfr { PROTO_CHAP, "CHAP" }, 5746283Sdfr { 0, "Others" }, 5846283Sdfr}; 5946283Sdfr 6046283Sdfrstatic u_short fcstab[256] = { 6146283Sdfr/* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 6246283Sdfr/* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 6346283Sdfr/* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 6446283Sdfr/* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 6546283Sdfr/* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 6646283Sdfr/* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 6746283Sdfr/* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 6846283Sdfr/* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 6946283Sdfr/* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 7046283Sdfr/* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 7146283Sdfr/* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 7246283Sdfr/* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 7346283Sdfr/* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 7446283Sdfr/* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 7546283Sdfr/* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 7646283Sdfr/* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 7746283Sdfr/* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 7846283Sdfr/* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 7946283Sdfr/* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 8046283Sdfr/* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 8146283Sdfr/* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 8246283Sdfr/* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 8346283Sdfr/* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 8446283Sdfr/* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 8546283Sdfr/* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 8646283Sdfr/* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 8746283Sdfr/* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 8846283Sdfr/* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 8946283Sdfr/* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 9046283Sdfr/* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 91130803Smarcel/* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 92130803Smarcel/* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 93130803Smarcel}; 9446283Sdfr 95130803Smarcelvoid 96130803SmarcelHdlcInit() 97130803Smarcel{ 98130803Smarcel ifInOctets = ifOutOctets = 0; 99130803Smarcel ifInPackets = ifOutPackets = 0; 100130803Smarcel ifOutLQRs = 0; 101130803Smarcel} 102130803Smarcel 103130803Smarcel/* 104130803Smarcel * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 105130803Smarcel * 2.27 for further details. 106130803Smarcel */ 107130803Smarcelu_short 108130803SmarcelHdlcFcs(fcs, cp, len) 109130803Smarcelu_short fcs; 110130803Smarcelu_char *cp; 111130803Smarcelint len; 112130803Smarcel{ 113130803Smarcel while (len--) 114130803Smarcel fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 11598944Sobrien return(fcs); 11619370Spst} 11719370Spst 11819370Spstvoid 11919370SpstHdlcOutput(int pri, u_short proto, struct mbuf *bp) 12019370Spst{ 12198944Sobrien struct mbuf *mhp, *mfcs; 12298944Sobrien struct protostat *statp; 12398944Sobrien struct lqrdata *lqr; 12419370Spst u_char *cp; 12598944Sobrien u_short fcs; 12698944Sobrien 12798944Sobrien if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 12819370Spst if (CcpFsm.state == ST_OPENED) { 12919370Spst Pred1Output(pri, proto, bp); 13019370Spst return; 13119370Spst } 13219370Spst } 13319370Spst if (DEV_IS_SYNC) 13419370Spst mfcs = NULLBUFF; 13519370Spst else 13619370Spst mfcs = mballoc(2, MB_HDLCOUT); 13719370Spst mhp = mballoc(4, MB_HDLCOUT); 13898944Sobrien mhp->cnt = 0; 13998944Sobrien cp = MBUF_CTOP(mhp); 14019370Spst if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 14146283Sdfr *cp++ = HDLC_ADDR; 14298944Sobrien *cp++ = HDLC_UI; 14319370Spst mhp->cnt += 2; 14419370Spst } 14519370Spst /* 14698944Sobrien * If possible, compress protocol field. 14719370Spst */ 14846283Sdfr if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 14919370Spst *cp++ = proto; 15046283Sdfr mhp->cnt++; 15146283Sdfr } else { 15246283Sdfr *cp++ = proto >> 8; 15346283Sdfr *cp = proto & 0377; 15498944Sobrien mhp->cnt += 2; 15546283Sdfr } 15646283Sdfr mhp->next = bp; 15746283Sdfr bp->next = mfcs; 15846283Sdfr 15946283Sdfr lqr = &MyLqrData; 16046283Sdfr lqr->PeerOutPackets = ifOutPackets++; 16146283Sdfr ifOutOctets += plength(mhp) + 1; 16246283Sdfr lqr->PeerOutOctets = ifOutOctets; 16346283Sdfr 16419370Spst if (proto == PROTO_LQR) { 16519370Spst lqr->MagicNumber = LcpInfo.want_magic; 16619370Spst lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 16719370Spst lqr->LastOutPackets = HisLqrData.PeerOutPackets; 16898944Sobrien lqr->LastOutOctets = HisLqrData.PeerOutOctets; 16919370Spst lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 17019370Spst lqr->PeerInPackets = HisLqrSave.SaveInPackets; 17119370Spst lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 17219370Spst lqr->PeerInErrors = HisLqrSave.SaveInErrors; 17319370Spst lqr->PeerInOctets = HisLqrSave.SaveInOctets; 17419370Spst lqr->PeerOutLQRs = ++ifOutLQRs; 17519370Spst LqrDump("LqrOutput", lqr); 17619370Spst LqrChangeOrder(lqr, (struct lqrdata *)(MBUF_CTOP(bp))); 17719370Spst } 17819370Spst 17946283Sdfr if (!DEV_IS_SYNC) { 18046283Sdfr fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 18146283Sdfr fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 18246283Sdfr fcs = ~fcs; 18346283Sdfr cp = MBUF_CTOP(mfcs); 18446283Sdfr *cp++ = fcs & 0377; /* Low byte first!! */ 18546283Sdfr *cp++ = fcs >> 8; 18619370Spst } 18746283Sdfr 18846283Sdfr LogDumpBp(LOG_HDLC, "HdlcOutput", mhp); 18946283Sdfr for (statp = ProtocolStat; statp->number; statp++) 19046283Sdfr if (statp->number == proto) 19146283Sdfr break; 19246283Sdfr statp->out_count++; 19346283Sdfr if (DEV_IS_SYNC) 19446283Sdfr ModemOutput(pri, mhp); 19546283Sdfr else 19646283Sdfr AsyncOutput(pri, mhp, proto); 197130803Smarcel} 19846283Sdfr 19946283SdfrDecodePacket(proto, bp) 20046283Sdfru_short proto; 20146283Sdfrstruct mbuf *bp; 20298944Sobrien{ 20398944Sobrien#ifdef DEBUG 20446283Sdfr logprintf("proto = %04x\n", proto); 20546283Sdfr#endif 20646283Sdfr u_char *cp; 20746283Sdfr 20846283Sdfr switch (proto) { 20946283Sdfr case PROTO_LCP: 21098944Sobrien LcpInput(bp); 21146283Sdfr break; 21246283Sdfr case PROTO_PAP: 21346283Sdfr PapInput(bp); 21446283Sdfr break; 21546283Sdfr case PROTO_LQR: 21646283Sdfr HisLqrSave.SaveInLQRs++; 21746283Sdfr LqrInput(bp); 21846283Sdfr break; 21946283Sdfr case PROTO_CHAP: 22046283Sdfr ChapInput(bp); 22146283Sdfr break; 22246283Sdfr case PROTO_VJUNCOMP: 22346283Sdfr case PROTO_VJCOMP: 22419370Spst bp = VjCompInput(bp, proto); 22546283Sdfr if (bp == NULLBUFF) { 22646283Sdfr break; 22746283Sdfr } 22819370Spst /* fall down */ 22946283Sdfr case PROTO_IP: 23046283Sdfr IpInput(bp); 23146283Sdfr break; 23246283Sdfr case PROTO_IPCP: 23346283Sdfr IpcpInput(bp); 23446283Sdfr break; 23546283Sdfr case PROTO_CCP: 23646283Sdfr CcpInput(bp); 23746283Sdfr break; 23846283Sdfr case PROTO_COMPD: 23946283Sdfr Pred1Input(bp); 24046283Sdfr break; 24146283Sdfr default: 242130803Smarcel LogPrintf(LOG_PHASE, "Unknown protocol 0x%04x\n", proto); 24398944Sobrien bp->offset -= 2; 24446283Sdfr bp->cnt += 2; 24546283Sdfr cp = MBUF_CTOP(bp); 24646283Sdfr LcpSendProtoRej(cp, bp->cnt); 24746283Sdfr HisLqrSave.SaveInDiscards++; 24846283Sdfr HdlcStat.unknownproto++; 24946283Sdfr pfree(bp); 25046283Sdfr break; 25119370Spst } 25246283Sdfr} 25346283Sdfr 25419370Spstint 25546283SdfrReportProtStatus() 25646283Sdfr{ 25719370Spst struct protostat *statp; 25819370Spst int cnt; 25919370Spst 26019370Spst statp = ProtocolStat; 26146283Sdfr statp--; 26219370Spst cnt = 0; 26319370Spst printf(" Protocol in out Protocol in out\n"); 26419370Spst do { 26519370Spst statp++; 26619370Spst printf(" %-9s: %8u, %8u", 26719370Spst statp->name, statp->in_count, statp->out_count); 26819370Spst if (++cnt == 2) { 26919370Spst printf("\n"); 27019370Spst cnt = 0; 27119370Spst } 27219370Spst } while (statp->number); 27319370Spst if (cnt) 27419370Spst printf("\n"); 27519370Spst return(1); 27619370Spst} 27746283Sdfr 27898944Sobrienint 27946283SdfrReportHdlcStatus() 28046283Sdfr{ 28146283Sdfr struct hdlcstat *hp = &HdlcStat; 28246283Sdfr 28398944Sobrien printf("HDLC level errors\n\n"); 28498944Sobrien printf("FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 28598944Sobrien hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 28619370Spst return(1); 28719370Spst} 28819370Spst 28919370Spststatic struct hdlcstat laststat; 29019370Spst 29119370Spstvoid 29219370SpstHdlcErrorCheck() 29319370Spst{ 29419370Spst struct hdlcstat *hp = &HdlcStat; 29519370Spst struct hdlcstat *op = &laststat; 29646283Sdfr 29719370Spst if (bcmp(hp, op, sizeof(laststat))) { 29819370Spst LogPrintf(LOG_PHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 29919370Spst hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 30019370Spst hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 30119370Spst } 30246283Sdfr laststat = HdlcStat; 30319370Spst} 30419370Spst 30598944Sobrienvoid 30619370SpstHdlcInput(struct mbuf *bp) 30719370Spst{ 30819370Spst u_short fcs, proto; 30919370Spst u_char *cp, addr, ctrl; 31098944Sobrien struct protostat *statp; 31198944Sobrien 31298944Sobrien LogDumpBp(LOG_HDLC, "HdlcInput:", bp); 31398944Sobrien if (DEV_IS_SYNC) 31419370Spst fcs = GOODFCS; 31519370Spst else 31619370Spst fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 31746283Sdfr HisLqrSave.SaveInOctets += bp->cnt + 1; 31846283Sdfr 31946283Sdfr#ifdef DEBUG 32046283Sdfr logprintf("fcs = %04x (%s)\n", fcs, (fcs == GOODFCS)? "good" : "bad"); 32146283Sdfr#endif 32246283Sdfr if (fcs != GOODFCS) { 32346283Sdfr HisLqrSave.SaveInErrors++; 32446283Sdfr#ifdef DEBUG 32519370Spst logprintf("Bad FCS\n"); 32619370Spst#endif 32798944Sobrien HdlcStat.badfcs++; 32898944Sobrien pfree(bp); 32998944Sobrien return; 33098944Sobrien } 33119370Spst if (!DEV_IS_SYNC) 33219370Spst bp->cnt -= 2; /* discard FCS part */ 33346283Sdfr cp = MBUF_CTOP(bp); 33446283Sdfr 33546283Sdfr ifInPackets++; 33646283Sdfr ifInOctets += bp->cnt; 33746283Sdfr 33846283Sdfr if (!LcpInfo.want_acfcomp) { 33946283Sdfr /* 34046283Sdfr * We expect that packet is not compressed. 34146283Sdfr */ 34246283Sdfr addr = *cp++; 34346283Sdfr if (addr != HDLC_ADDR) { 34446283Sdfr HisLqrSave.SaveInErrors++; 34546283Sdfr HdlcStat.badaddr++; 34646283Sdfr#ifdef DEBUG 34746283Sdfr logprintf("addr %02x\n", *cp); 34846283Sdfr#endif 34946283Sdfr pfree(bp); 35046283Sdfr return; 35146283Sdfr } 35246283Sdfr 35346283Sdfr ctrl = *cp++; 35446283Sdfr if (ctrl != HDLC_UI) { 35546283Sdfr HisLqrSave.SaveInErrors++; 35698944Sobrien HdlcStat.badcommand++; 35746283Sdfr#ifdef DEBUG 35846283Sdfr logprintf("command %02x\n", *cp); 35946283Sdfr#endif 36046283Sdfr pfree(bp); 36146283Sdfr return; 36246283Sdfr } 36346283Sdfr bp->offset += 2; 36446283Sdfr bp->cnt -= 2; 36546283Sdfr } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 36646283Sdfr /* 36746283Sdfr * We can receive compressed packet, but peer still send 36846283Sdfr * uncompressed packet to me. 36946283Sdfr */ 37046283Sdfr cp += 2; 37146283Sdfr bp->offset += 2; 37246283Sdfr bp->cnt -= 2; 37398944Sobrien } 37498944Sobrien 37546283Sdfr if (LcpInfo.want_protocomp) { 37646283Sdfr proto = 0; 37746283Sdfr cp--; 37819370Spst do { 37919370Spst cp++; bp->offset++; bp->cnt--; 38019370Spst proto = proto << 8; 38119370Spst proto += *cp; 38219370Spst } while (!(proto & 1)); 38346283Sdfr } else { 38419370Spst proto = *cp++ << 8; 38598944Sobrien proto |= *cp++; 38619370Spst bp->offset += 2; 38719370Spst bp->cnt -= 2; 38819370Spst } 38919370Spst 39046283Sdfr for (statp = ProtocolStat; statp->number; statp++) 39146283Sdfr if (statp->number == proto) 39219370Spst break; 39319370Spst statp->in_count++; 39419370Spst HisLqrSave.SaveInPackets++; 39519370Spst 39698944Sobrien DecodePacket(proto, bp); 39719370Spst} 39819370Spst