hdlc.c revision 30715
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 * 2030715Sbrian * $Id: hdlc.c,v 1.19 1997/09/10 21:36:54 brian Exp $ 218857Srgrimes * 226059Samurai * TODO: 236059Samurai */ 2430715Sbrian#include <sys/param.h> 2530715Sbrian#include <netinet/in.h> 2630715Sbrian 2730715Sbrian#include <stdio.h> 2830715Sbrian#include <string.h> 2930715Sbrian#include <termios.h> 3030715Sbrian 3130715Sbrian#include "mbuf.h" 3230715Sbrian#include "log.h" 3330715Sbrian#include "defs.h" 3430715Sbrian#include "timer.h" 356059Samurai#include "fsm.h" 366059Samurai#include "hdlc.h" 376059Samurai#include "lcpproto.h" 386059Samurai#include "lcp.h" 396059Samurai#include "lqr.h" 4026142Sbrian#include "loadalias.h" 4130715Sbrian#include "command.h" 426735Samurai#include "vars.h" 4313389Sphk#include "pred.h" 4413389Sphk#include "modem.h" 4513389Sphk#include "ccp.h" 466059Samurai 476059Samuraistruct hdlcstat { 4828679Sbrian int badfcs; 4928679Sbrian int badaddr; 5028679Sbrian int badcommand; 5128679Sbrian int unknownproto; 5228679Sbrian} HdlcStat; 536059Samurai 5430715Sbrianstatic int ifOutPackets; 5530715Sbrianstatic int ifOutOctets; 5630715Sbrianstatic int ifOutLQRs; 5730715Sbrianstatic int ifInPackets; 5830715Sbrianstatic int ifInOctets; 596059Samurai 606059Samuraistruct protostat { 616059Samurai u_short number; 626059Samurai char *name; 636059Samurai u_long in_count; 646059Samurai u_long out_count; 6528679Sbrian} ProtocolStat[] = { 6628679Sbrian 6728679Sbrian { 6828679Sbrian PROTO_IP, "IP" 6928679Sbrian }, 7028679Sbrian { 7128679Sbrian PROTO_VJUNCOMP, "VJ_UNCOMP" 7228679Sbrian }, 7328679Sbrian { 7428679Sbrian PROTO_VJCOMP, "VJ_COMP" 7528679Sbrian }, 7628679Sbrian { 7728679Sbrian PROTO_COMPD, "COMPD" 7828679Sbrian }, 7928679Sbrian { 8028679Sbrian PROTO_LCP, "LCP" 8128679Sbrian }, 8228679Sbrian { 8328679Sbrian PROTO_IPCP, "IPCP" 8428679Sbrian }, 8528679Sbrian { 8628679Sbrian PROTO_CCP, "CCP" 8728679Sbrian }, 8828679Sbrian { 8928679Sbrian PROTO_PAP, "PAP" 9028679Sbrian }, 9128679Sbrian { 9228679Sbrian PROTO_LQR, "LQR" 9328679Sbrian }, 9428679Sbrian { 9528679Sbrian PROTO_CHAP, "CHAP" 9628679Sbrian }, 9728679Sbrian { 9828679Sbrian 0, "Others" 9928679Sbrian }, 1006059Samurai}; 1016059Samurai 10213760Sphkstatic u_short const fcstab[256] = { 10328679Sbrian /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 10428679Sbrian /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 10528679Sbrian /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 10628679Sbrian /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 10728679Sbrian /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 10828679Sbrian /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 10928679Sbrian /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 11028679Sbrian /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 11128679Sbrian /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 11228679Sbrian /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 11328679Sbrian /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 11428679Sbrian /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 11528679Sbrian /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 11628679Sbrian /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 11728679Sbrian /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 11828679Sbrian /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 11928679Sbrian /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 12028679Sbrian /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 12128679Sbrian /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 12228679Sbrian /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 12328679Sbrian /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 12428679Sbrian /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 12528679Sbrian /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 12628679Sbrian /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 12728679Sbrian /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 12828679Sbrian /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 12928679Sbrian /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 13028679Sbrian /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 13128679Sbrian /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 13228679Sbrian /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 13328679Sbrian /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 13428679Sbrian /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 1356059Samurai}; 1366059Samurai 13728679Sbrianu_char EscMap[33]; 13828679Sbrian 1396059Samuraivoid 1406059SamuraiHdlcInit() 1416059Samurai{ 1426059Samurai ifInOctets = ifOutOctets = 0; 1436059Samurai ifInPackets = ifOutPackets = 0; 1446059Samurai ifOutLQRs = 0; 1456059Samurai} 1466059Samurai 1476059Samurai/* 1486059Samurai * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1496059Samurai * 2.27 for further details. 1506059Samurai */ 15128679Sbrianinline u_short 15228679SbrianHdlcFcs(u_short fcs, u_char * cp, int len) 1536059Samurai{ 1546059Samurai while (len--) 1556059Samurai fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 15628679Sbrian return (fcs); 1576059Samurai} 1586059Samurai 1596059Samuraivoid 16028679SbrianHdlcOutput(int pri, u_short proto, struct mbuf * bp) 1616059Samurai{ 1626059Samurai struct mbuf *mhp, *mfcs; 1636059Samurai struct protostat *statp; 1646059Samurai struct lqrdata *lqr; 1656059Samurai u_char *cp; 1666059Samurai u_short fcs; 1676059Samurai 1686059Samurai if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 1696059Samurai if (CcpFsm.state == ST_OPENED) { 17029295Sbrian if (CcpInfo.want_proto == TY_PRED1) { 17129294Sbrian Pred1Output(pri, proto, bp); 17229294Sbrian return; 17329294Sbrian } 1746059Samurai } 1756059Samurai } 1766735Samurai if (DEV_IS_SYNC) 1776735Samurai mfcs = NULLBUFF; 1786735Samurai else 1796735Samurai mfcs = mballoc(2, MB_HDLCOUT); 1806059Samurai mhp = mballoc(4, MB_HDLCOUT); 1816059Samurai mhp->cnt = 0; 1826059Samurai cp = MBUF_CTOP(mhp); 1836059Samurai if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 1846059Samurai *cp++ = HDLC_ADDR; 1856059Samurai *cp++ = HDLC_UI; 1866059Samurai mhp->cnt += 2; 1876059Samurai } 18828679Sbrian 1896059Samurai /* 19028679Sbrian * If possible, compress protocol field. 1916059Samurai */ 1926059Samurai if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 1936059Samurai *cp++ = proto; 1946059Samurai mhp->cnt++; 1956059Samurai } else { 1966059Samurai *cp++ = proto >> 8; 1976059Samurai *cp = proto & 0377; 1986059Samurai mhp->cnt += 2; 1996059Samurai } 2006059Samurai mhp->next = bp; 2016059Samurai bp->next = mfcs; 2026059Samurai 2036059Samurai lqr = &MyLqrData; 2046059Samurai lqr->PeerOutPackets = ifOutPackets++; 2056059Samurai ifOutOctets += plength(mhp) + 1; 2066059Samurai lqr->PeerOutOctets = ifOutOctets; 2076059Samurai 2086059Samurai if (proto == PROTO_LQR) { 2096059Samurai lqr->MagicNumber = LcpInfo.want_magic; 2106059Samurai lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 2116059Samurai lqr->LastOutPackets = HisLqrData.PeerOutPackets; 2126059Samurai lqr->LastOutOctets = HisLqrData.PeerOutOctets; 2136059Samurai lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 2146059Samurai lqr->PeerInPackets = HisLqrSave.SaveInPackets; 2156059Samurai lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 2166059Samurai lqr->PeerInErrors = HisLqrSave.SaveInErrors; 2176059Samurai lqr->PeerInOctets = HisLqrSave.SaveInOctets; 2186059Samurai lqr->PeerOutLQRs = ++ifOutLQRs; 2196059Samurai LqrDump("LqrOutput", lqr); 22028679Sbrian LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 2216059Samurai } 2226735Samurai if (!DEV_IS_SYNC) { 2236735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 2246735Samurai fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 2256735Samurai fcs = ~fcs; 2266735Samurai cp = MBUF_CTOP(mfcs); 22728679Sbrian *cp++ = fcs & 0377; /* Low byte first!! */ 2286735Samurai *cp++ = fcs >> 8; 2296735Samurai } 23026516Sbrian LogDumpBp(LogHDLC, "HdlcOutput", mhp); 2316059Samurai for (statp = ProtocolStat; statp->number; statp++) 2326059Samurai if (statp->number == proto) 2336059Samurai break; 2346059Samurai statp->out_count++; 2356735Samurai if (DEV_IS_SYNC) 2366735Samurai ModemOutput(pri, mhp); 2376735Samurai else 2386735Samurai AsyncOutput(pri, mhp, proto); 2396059Samurai} 2406059Samurai 24113379Sphkvoid 24228679SbrianDecodePacket(u_short proto, struct mbuf * bp) 2436059Samurai{ 2449448Samurai u_char *cp; 2459448Samurai 24626516Sbrian LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto); 24726516Sbrian 2486059Samurai switch (proto) { 2496059Samurai case PROTO_LCP: 2506059Samurai LcpInput(bp); 2516059Samurai break; 2526059Samurai case PROTO_PAP: 2536059Samurai PapInput(bp); 2546059Samurai break; 2556059Samurai case PROTO_LQR: 2566059Samurai HisLqrSave.SaveInLQRs++; 2576059Samurai LqrInput(bp); 2586059Samurai break; 2596059Samurai case PROTO_CHAP: 2606059Samurai ChapInput(bp); 2616059Samurai break; 2626059Samurai case PROTO_VJUNCOMP: 2636059Samurai case PROTO_VJCOMP: 2646059Samurai bp = VjCompInput(bp, proto); 2656735Samurai if (bp == NULLBUFF) { 2666735Samurai break; 2676735Samurai } 2686059Samurai /* fall down */ 2696059Samurai case PROTO_IP: 2706059Samurai IpInput(bp); 2716059Samurai break; 2726059Samurai case PROTO_IPCP: 2736059Samurai IpcpInput(bp); 2746059Samurai break; 2756059Samurai case PROTO_CCP: 2766059Samurai CcpInput(bp); 2776059Samurai break; 2786059Samurai case PROTO_COMPD: 2796059Samurai Pred1Input(bp); 2806059Samurai break; 2816059Samurai default: 28226516Sbrian LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto); 2839448Samurai bp->offset -= 2; 2849448Samurai bp->cnt += 2; 2859448Samurai cp = MBUF_CTOP(bp); 28628679Sbrian LcpSendProtoRej(cp, bp->cnt); 2876059Samurai HisLqrSave.SaveInDiscards++; 2886059Samurai HdlcStat.unknownproto++; 2896059Samurai pfree(bp); 2906059Samurai break; 2916059Samurai } 2926059Samurai} 2936059Samurai 2946059Samuraiint 2956059SamuraiReportProtStatus() 2966059Samurai{ 2976059Samurai struct protostat *statp; 2986059Samurai int cnt; 2996059Samurai 3006059Samurai statp = ProtocolStat; 3016059Samurai statp--; 3026059Samurai cnt = 0; 30326516Sbrian fprintf(VarTerm, " Protocol in out Protocol in out\n"); 3046059Samurai do { 3056059Samurai statp++; 30626516Sbrian fprintf(VarTerm, " %-9s: %8lu, %8lu", 30728679Sbrian statp->name, statp->in_count, statp->out_count); 3086059Samurai if (++cnt == 2) { 30926516Sbrian fprintf(VarTerm, "\n"); 3106059Samurai cnt = 0; 3116059Samurai } 3126059Samurai } while (statp->number); 3136059Samurai if (cnt) 31428679Sbrian fprintf(VarTerm, "\n"); 31528679Sbrian return (1); 3166059Samurai} 3176059Samurai 3186059Samuraiint 3196059SamuraiReportHdlcStatus() 3206059Samurai{ 3216059Samurai struct hdlcstat *hp = &HdlcStat; 3226059Samurai 32326516Sbrian if (VarTerm) { 32426516Sbrian fprintf(VarTerm, "HDLC level errors\n\n"); 32526516Sbrian fprintf(VarTerm, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 32628679Sbrian hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 32726516Sbrian } 32826516Sbrian return 0; 3296059Samurai} 3306059Samurai 3316059Samuraistatic struct hdlcstat laststat; 3326059Samurai 3336059Samuraivoid 3346059SamuraiHdlcErrorCheck() 3356059Samurai{ 3366059Samurai struct hdlcstat *hp = &HdlcStat; 3376059Samurai struct hdlcstat *op = &laststat; 3386059Samurai 33930715Sbrian if (memcmp(hp, op, sizeof(laststat))) { 34026516Sbrian LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 34128679Sbrian hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 34228679Sbrian hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 3436059Samurai } 3446059Samurai laststat = HdlcStat; 3456059Samurai} 3466059Samurai 3476059Samuraivoid 34828679SbrianHdlcInput(struct mbuf * bp) 3496059Samurai{ 3506059Samurai u_short fcs, proto; 3516059Samurai u_char *cp, addr, ctrl; 3526059Samurai struct protostat *statp; 3536059Samurai 35426516Sbrian LogDumpBp(LogHDLC, "HdlcInput:", bp); 3556735Samurai if (DEV_IS_SYNC) 3566735Samurai fcs = GOODFCS; 3576735Samurai else 3586735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 3596059Samurai HisLqrSave.SaveInOctets += bp->cnt + 1; 3606059Samurai 36126516Sbrian LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 36228679Sbrian fcs, (fcs == GOODFCS) ? "good" : "bad"); 3636059Samurai if (fcs != GOODFCS) { 3646059Samurai HisLqrSave.SaveInErrors++; 36526516Sbrian LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 3666059Samurai HdlcStat.badfcs++; 3676059Samurai pfree(bp); 3686059Samurai return; 3696059Samurai } 3706735Samurai if (!DEV_IS_SYNC) 3716735Samurai bp->cnt -= 2; /* discard FCS part */ 37225019Sphk 37325019Sphk if (bp->cnt < 2) { /* XXX: raise this bar ? */ 37428679Sbrian pfree(bp); 37528679Sbrian return; 37625019Sphk } 3776059Samurai cp = MBUF_CTOP(bp); 3786059Samurai 3796059Samurai ifInPackets++; 3806059Samurai ifInOctets += bp->cnt; 3816059Samurai 3826059Samurai if (!LcpInfo.want_acfcomp) { 38328679Sbrian 3846059Samurai /* 38528679Sbrian * We expect that packet is not compressed. 3866059Samurai */ 3876059Samurai addr = *cp++; 3886059Samurai if (addr != HDLC_ADDR) { 3896059Samurai HisLqrSave.SaveInErrors++; 3906059Samurai HdlcStat.badaddr++; 39126516Sbrian LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 3926059Samurai pfree(bp); 3936059Samurai return; 3946059Samurai } 3956059Samurai ctrl = *cp++; 3966059Samurai if (ctrl != HDLC_UI) { 3976059Samurai HisLqrSave.SaveInErrors++; 3986059Samurai HdlcStat.badcommand++; 39926516Sbrian LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 4006059Samurai pfree(bp); 4016059Samurai return; 4026059Samurai } 4036059Samurai bp->offset += 2; 4046059Samurai bp->cnt -= 2; 4056059Samurai } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 40628679Sbrian 4076059Samurai /* 40828679Sbrian * We can receive compressed packet, but peer still send uncompressed 40928679Sbrian * packet to me. 4106059Samurai */ 4116059Samurai cp += 2; 4126059Samurai bp->offset += 2; 4136059Samurai bp->cnt -= 2; 4146059Samurai } 4156059Samurai if (LcpInfo.want_protocomp) { 4166059Samurai proto = 0; 4176059Samurai cp--; 4186059Samurai do { 41928679Sbrian cp++; 42028679Sbrian bp->offset++; 42128679Sbrian bp->cnt--; 4226059Samurai proto = proto << 8; 4236059Samurai proto += *cp; 4246059Samurai } while (!(proto & 1)); 4256059Samurai } else { 4266059Samurai proto = *cp++ << 8; 4276059Samurai proto |= *cp++; 4286059Samurai bp->offset += 2; 4296059Samurai bp->cnt -= 2; 4306059Samurai } 4316059Samurai 4326059Samurai for (statp = ProtocolStat; statp->number; statp++) 4336059Samurai if (statp->number == proto) 4346059Samurai break; 4356059Samurai statp->in_count++; 4366059Samurai HisLqrSave.SaveInPackets++; 4376059Samurai 4386059Samurai DecodePacket(proto, bp); 4396059Samurai} 440