hdlc.c revision 31343
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 * 2031343Sbrian * $Id: hdlc.c,v 1.21 1997/10/26 12:42:10 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 3131343Sbrian#include "command.h" 3230715Sbrian#include "mbuf.h" 3330715Sbrian#include "log.h" 3430715Sbrian#include "defs.h" 3530715Sbrian#include "timer.h" 366059Samurai#include "fsm.h" 376059Samurai#include "hdlc.h" 386059Samurai#include "lcpproto.h" 3930733Sbrian#include "ipcp.h" 4030733Sbrian#include "ip.h" 4130733Sbrian#include "vjcomp.h" 4230733Sbrian#include "pap.h" 4330733Sbrian#include "chap.h" 446059Samurai#include "lcp.h" 4531343Sbrian#include "async.h" 466059Samurai#include "lqr.h" 4726142Sbrian#include "loadalias.h" 486735Samurai#include "vars.h" 4913389Sphk#include "pred.h" 5013389Sphk#include "modem.h" 5113389Sphk#include "ccp.h" 526059Samurai 536059Samuraistruct hdlcstat { 5428679Sbrian int badfcs; 5528679Sbrian int badaddr; 5628679Sbrian int badcommand; 5728679Sbrian int unknownproto; 5828679Sbrian} HdlcStat; 596059Samurai 6030715Sbrianstatic int ifOutPackets; 6130715Sbrianstatic int ifOutOctets; 6230715Sbrianstatic int ifOutLQRs; 6330715Sbrianstatic int ifInPackets; 6430715Sbrianstatic int ifInOctets; 656059Samurai 666059Samuraistruct protostat { 676059Samurai u_short number; 6831343Sbrian const char *name; 696059Samurai u_long in_count; 706059Samurai u_long out_count; 7128679Sbrian} ProtocolStat[] = { 7228679Sbrian 7331343Sbrian { PROTO_IP, "IP" }, 7431343Sbrian { PROTO_VJUNCOMP, "VJ_UNCOMP" }, 7531343Sbrian { PROTO_VJCOMP, "VJ_COMP" }, 7631343Sbrian { PROTO_COMPD, "COMPD" }, 7731343Sbrian { PROTO_LCP, "LCP" }, 7831343Sbrian { PROTO_IPCP, "IPCP" }, 7931343Sbrian { PROTO_CCP, "CCP" }, 8031343Sbrian { PROTO_PAP, "PAP" }, 8131343Sbrian { PROTO_LQR, "LQR" }, 8231343Sbrian { PROTO_CHAP, "CHAP" }, 8331343Sbrian { 0, "Others" } 846059Samurai}; 856059Samurai 8613760Sphkstatic u_short const fcstab[256] = { 8728679Sbrian /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 8828679Sbrian /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 8928679Sbrian /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 9028679Sbrian /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 9128679Sbrian /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 9228679Sbrian /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 9328679Sbrian /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 9428679Sbrian /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 9528679Sbrian /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 9628679Sbrian /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 9728679Sbrian /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 9828679Sbrian /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 9928679Sbrian /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 10028679Sbrian /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 10128679Sbrian /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 10228679Sbrian /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 10328679Sbrian /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 10428679Sbrian /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 10528679Sbrian /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 10628679Sbrian /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 10728679Sbrian /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 10828679Sbrian /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 10928679Sbrian /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 11028679Sbrian /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 11128679Sbrian /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 11228679Sbrian /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 11328679Sbrian /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 11428679Sbrian /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 11528679Sbrian /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 11628679Sbrian /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 11728679Sbrian /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 11828679Sbrian /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 1196059Samurai}; 1206059Samurai 12128679Sbrianu_char EscMap[33]; 12228679Sbrian 1236059Samuraivoid 1246059SamuraiHdlcInit() 1256059Samurai{ 1266059Samurai ifInOctets = ifOutOctets = 0; 1276059Samurai ifInPackets = ifOutPackets = 0; 1286059Samurai ifOutLQRs = 0; 1296059Samurai} 1306059Samurai 1316059Samurai/* 1326059Samurai * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1336059Samurai * 2.27 for further details. 1346059Samurai */ 13528679Sbrianinline u_short 13628679SbrianHdlcFcs(u_short fcs, u_char * cp, int len) 1376059Samurai{ 1386059Samurai while (len--) 1396059Samurai fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 14028679Sbrian return (fcs); 1416059Samurai} 1426059Samurai 1436059Samuraivoid 14428679SbrianHdlcOutput(int pri, u_short proto, struct mbuf * bp) 1456059Samurai{ 1466059Samurai struct mbuf *mhp, *mfcs; 1476059Samurai struct protostat *statp; 1486059Samurai struct lqrdata *lqr; 1496059Samurai u_char *cp; 1506059Samurai u_short fcs; 1516059Samurai 1526059Samurai if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 1536059Samurai if (CcpFsm.state == ST_OPENED) { 15429295Sbrian if (CcpInfo.want_proto == TY_PRED1) { 15529294Sbrian Pred1Output(pri, proto, bp); 15629294Sbrian return; 15729294Sbrian } 1586059Samurai } 1596059Samurai } 1606735Samurai if (DEV_IS_SYNC) 1616735Samurai mfcs = NULLBUFF; 1626735Samurai else 1636735Samurai mfcs = mballoc(2, MB_HDLCOUT); 1646059Samurai mhp = mballoc(4, MB_HDLCOUT); 1656059Samurai mhp->cnt = 0; 1666059Samurai cp = MBUF_CTOP(mhp); 1676059Samurai if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 1686059Samurai *cp++ = HDLC_ADDR; 1696059Samurai *cp++ = HDLC_UI; 1706059Samurai mhp->cnt += 2; 1716059Samurai } 17228679Sbrian 1736059Samurai /* 17428679Sbrian * If possible, compress protocol field. 1756059Samurai */ 1766059Samurai if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 1776059Samurai *cp++ = proto; 1786059Samurai mhp->cnt++; 1796059Samurai } else { 1806059Samurai *cp++ = proto >> 8; 1816059Samurai *cp = proto & 0377; 1826059Samurai mhp->cnt += 2; 1836059Samurai } 1846059Samurai mhp->next = bp; 1856059Samurai bp->next = mfcs; 1866059Samurai 1876059Samurai lqr = &MyLqrData; 1886059Samurai lqr->PeerOutPackets = ifOutPackets++; 1896059Samurai ifOutOctets += plength(mhp) + 1; 1906059Samurai lqr->PeerOutOctets = ifOutOctets; 1916059Samurai 1926059Samurai if (proto == PROTO_LQR) { 1936059Samurai lqr->MagicNumber = LcpInfo.want_magic; 1946059Samurai lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 1956059Samurai lqr->LastOutPackets = HisLqrData.PeerOutPackets; 1966059Samurai lqr->LastOutOctets = HisLqrData.PeerOutOctets; 1976059Samurai lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 1986059Samurai lqr->PeerInPackets = HisLqrSave.SaveInPackets; 1996059Samurai lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 2006059Samurai lqr->PeerInErrors = HisLqrSave.SaveInErrors; 2016059Samurai lqr->PeerInOctets = HisLqrSave.SaveInOctets; 2026059Samurai lqr->PeerOutLQRs = ++ifOutLQRs; 2036059Samurai LqrDump("LqrOutput", lqr); 20428679Sbrian LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 2056059Samurai } 2066735Samurai if (!DEV_IS_SYNC) { 2076735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 2086735Samurai fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 2096735Samurai fcs = ~fcs; 2106735Samurai cp = MBUF_CTOP(mfcs); 21128679Sbrian *cp++ = fcs & 0377; /* Low byte first!! */ 2126735Samurai *cp++ = fcs >> 8; 2136735Samurai } 21426516Sbrian LogDumpBp(LogHDLC, "HdlcOutput", mhp); 2156059Samurai for (statp = ProtocolStat; statp->number; statp++) 2166059Samurai if (statp->number == proto) 2176059Samurai break; 2186059Samurai statp->out_count++; 2196735Samurai if (DEV_IS_SYNC) 2206735Samurai ModemOutput(pri, mhp); 2216735Samurai else 2226735Samurai AsyncOutput(pri, mhp, proto); 2236059Samurai} 2246059Samurai 22513379Sphkvoid 22628679SbrianDecodePacket(u_short proto, struct mbuf * bp) 2276059Samurai{ 2289448Samurai u_char *cp; 2299448Samurai 23026516Sbrian LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto); 23126516Sbrian 2326059Samurai switch (proto) { 2336059Samurai case PROTO_LCP: 2346059Samurai LcpInput(bp); 2356059Samurai break; 2366059Samurai case PROTO_PAP: 2376059Samurai PapInput(bp); 2386059Samurai break; 2396059Samurai case PROTO_LQR: 2406059Samurai HisLqrSave.SaveInLQRs++; 2416059Samurai LqrInput(bp); 2426059Samurai break; 2436059Samurai case PROTO_CHAP: 2446059Samurai ChapInput(bp); 2456059Samurai break; 2466059Samurai case PROTO_VJUNCOMP: 2476059Samurai case PROTO_VJCOMP: 2486059Samurai bp = VjCompInput(bp, proto); 2496735Samurai if (bp == NULLBUFF) { 2506735Samurai break; 2516735Samurai } 2526059Samurai /* fall down */ 2536059Samurai case PROTO_IP: 2546059Samurai IpInput(bp); 2556059Samurai break; 2566059Samurai case PROTO_IPCP: 2576059Samurai IpcpInput(bp); 2586059Samurai break; 2596059Samurai case PROTO_CCP: 2606059Samurai CcpInput(bp); 2616059Samurai break; 2626059Samurai case PROTO_COMPD: 2636059Samurai Pred1Input(bp); 2646059Samurai break; 2656059Samurai default: 26626516Sbrian LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto); 2679448Samurai bp->offset -= 2; 2689448Samurai bp->cnt += 2; 2699448Samurai cp = MBUF_CTOP(bp); 27028679Sbrian LcpSendProtoRej(cp, bp->cnt); 2716059Samurai HisLqrSave.SaveInDiscards++; 2726059Samurai HdlcStat.unknownproto++; 2736059Samurai pfree(bp); 2746059Samurai break; 2756059Samurai } 2766059Samurai} 2776059Samurai 2786059Samuraiint 27931343SbrianReportProtStatus(struct cmdargs const *arg) 2806059Samurai{ 2816059Samurai struct protostat *statp; 2826059Samurai int cnt; 2836059Samurai 2846059Samurai statp = ProtocolStat; 2856059Samurai statp--; 2866059Samurai cnt = 0; 28726516Sbrian fprintf(VarTerm, " Protocol in out Protocol in out\n"); 2886059Samurai do { 2896059Samurai statp++; 29026516Sbrian fprintf(VarTerm, " %-9s: %8lu, %8lu", 29128679Sbrian statp->name, statp->in_count, statp->out_count); 2926059Samurai if (++cnt == 2) { 29326516Sbrian fprintf(VarTerm, "\n"); 2946059Samurai cnt = 0; 2956059Samurai } 2966059Samurai } while (statp->number); 2976059Samurai if (cnt) 29828679Sbrian fprintf(VarTerm, "\n"); 29928679Sbrian return (1); 3006059Samurai} 3016059Samurai 3026059Samuraiint 30331343SbrianReportHdlcStatus(struct cmdargs const *arg) 3046059Samurai{ 3056059Samurai struct hdlcstat *hp = &HdlcStat; 3066059Samurai 30726516Sbrian if (VarTerm) { 30826516Sbrian fprintf(VarTerm, "HDLC level errors\n\n"); 30926516Sbrian fprintf(VarTerm, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 31028679Sbrian hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 31126516Sbrian } 31226516Sbrian return 0; 3136059Samurai} 3146059Samurai 3156059Samuraistatic struct hdlcstat laststat; 3166059Samurai 3176059Samuraivoid 3186059SamuraiHdlcErrorCheck() 3196059Samurai{ 3206059Samurai struct hdlcstat *hp = &HdlcStat; 3216059Samurai struct hdlcstat *op = &laststat; 3226059Samurai 32330715Sbrian if (memcmp(hp, op, sizeof(laststat))) { 32426516Sbrian LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 32528679Sbrian hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 32628679Sbrian hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 3276059Samurai } 3286059Samurai laststat = HdlcStat; 3296059Samurai} 3306059Samurai 3316059Samuraivoid 33228679SbrianHdlcInput(struct mbuf * bp) 3336059Samurai{ 3346059Samurai u_short fcs, proto; 3356059Samurai u_char *cp, addr, ctrl; 3366059Samurai struct protostat *statp; 3376059Samurai 33826516Sbrian LogDumpBp(LogHDLC, "HdlcInput:", bp); 3396735Samurai if (DEV_IS_SYNC) 3406735Samurai fcs = GOODFCS; 3416735Samurai else 3426735Samurai fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 3436059Samurai HisLqrSave.SaveInOctets += bp->cnt + 1; 3446059Samurai 34526516Sbrian LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 34628679Sbrian fcs, (fcs == GOODFCS) ? "good" : "bad"); 3476059Samurai if (fcs != GOODFCS) { 3486059Samurai HisLqrSave.SaveInErrors++; 34926516Sbrian LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 3506059Samurai HdlcStat.badfcs++; 3516059Samurai pfree(bp); 3526059Samurai return; 3536059Samurai } 3546735Samurai if (!DEV_IS_SYNC) 3556735Samurai bp->cnt -= 2; /* discard FCS part */ 35625019Sphk 35725019Sphk if (bp->cnt < 2) { /* XXX: raise this bar ? */ 35828679Sbrian pfree(bp); 35928679Sbrian return; 36025019Sphk } 3616059Samurai cp = MBUF_CTOP(bp); 3626059Samurai 3636059Samurai ifInPackets++; 3646059Samurai ifInOctets += bp->cnt; 3656059Samurai 3666059Samurai if (!LcpInfo.want_acfcomp) { 36728679Sbrian 3686059Samurai /* 36928679Sbrian * We expect that packet is not compressed. 3706059Samurai */ 3716059Samurai addr = *cp++; 3726059Samurai if (addr != HDLC_ADDR) { 3736059Samurai HisLqrSave.SaveInErrors++; 3746059Samurai HdlcStat.badaddr++; 37526516Sbrian LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 3766059Samurai pfree(bp); 3776059Samurai return; 3786059Samurai } 3796059Samurai ctrl = *cp++; 3806059Samurai if (ctrl != HDLC_UI) { 3816059Samurai HisLqrSave.SaveInErrors++; 3826059Samurai HdlcStat.badcommand++; 38326516Sbrian LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 3846059Samurai pfree(bp); 3856059Samurai return; 3866059Samurai } 3876059Samurai bp->offset += 2; 3886059Samurai bp->cnt -= 2; 3896059Samurai } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 39028679Sbrian 3916059Samurai /* 39228679Sbrian * We can receive compressed packet, but peer still send uncompressed 39328679Sbrian * packet to me. 3946059Samurai */ 3956059Samurai cp += 2; 3966059Samurai bp->offset += 2; 3976059Samurai bp->cnt -= 2; 3986059Samurai } 3996059Samurai if (LcpInfo.want_protocomp) { 4006059Samurai proto = 0; 4016059Samurai cp--; 4026059Samurai do { 40328679Sbrian cp++; 40428679Sbrian bp->offset++; 40528679Sbrian bp->cnt--; 4066059Samurai proto = proto << 8; 4076059Samurai proto += *cp; 4086059Samurai } while (!(proto & 1)); 4096059Samurai } else { 4106059Samurai proto = *cp++ << 8; 4116059Samurai proto |= *cp++; 4126059Samurai bp->offset += 2; 4136059Samurai bp->cnt -= 2; 4146059Samurai } 4156059Samurai 4166059Samurai for (statp = ProtocolStat; statp->number; statp++) 4176059Samurai if (statp->number == proto) 4186059Samurai break; 4196059Samurai statp->in_count++; 4206059Samurai HisLqrSave.SaveInPackets++; 4216059Samurai 4226059Samurai DecodePacket(proto, bp); 4236059Samurai} 424