hdlc.c revision 29295
1139749Simp/* 2129794Stackerman * PPP High Level Link Control (HDLC) Module 3129794Stackerman * 4129794Stackerman * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5129794Stackerman * 6129794Stackerman * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7129794Stackerman * 8129794Stackerman * Redistribution and use in source and binary forms are permitted 9129794Stackerman * provided that the above copyright notice and this paragraph are 10129794Stackerman * duplicated in all such forms and that any documentation, 11129794Stackerman * advertising materials, and other materials related to such 12129794Stackerman * distribution and use acknowledge that the software was developed 13129794Stackerman * by the Internet Initiative Japan, Inc. The name of the 14129794Stackerman * IIJ may not be used to endorse or promote products derived 15129794Stackerman * from this software without specific prior written permission. 16129794Stackerman * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17129794Stackerman * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18129794Stackerman * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19129794Stackerman * 20129794Stackerman * $Id: hdlc.c,v 1.18 1997/09/10 21:33:32 brian Exp $ 21129794Stackerman * 22129794Stackerman * TODO: 23129794Stackerman */ 24129794Stackerman#include "fsm.h" 25129794Stackerman#include "hdlc.h" 26129794Stackerman#include "lcpproto.h" 27129794Stackerman#include "lcp.h" 28129794Stackerman#include "lqr.h" 29129794Stackerman#include "loadalias.h" 30129794Stackerman#include "vars.h" 31129794Stackerman#include "pred.h" 32129794Stackerman#include "modem.h" 33129794Stackerman#include "ccp.h" 34129794Stackerman 35129794Stackermanstruct hdlcstat { 36150968Sglebius int badfcs; 37150968Sglebius int badaddr; 38150968Sglebius int badcommand; 39150968Sglebius int unknownproto; 40129794Stackerman} HdlcStat; 41129794Stackerman 42129794Stackermanstatic int ifOutPackets, ifOutOctets, ifOutLQRs; 43129794Stackermanstatic int ifInPackets, ifInOctets; 44129794Stackerman 45129794Stackermanstruct protostat { 46129794Stackerman u_short number; 47129794Stackerman char *name; 48129794Stackerman u_long in_count; 49129794Stackerman u_long out_count; 50129794Stackerman} ProtocolStat[] = { 51129794Stackerman 52129794Stackerman { 53129794Stackerman PROTO_IP, "IP" 54129794Stackerman }, 55129794Stackerman { 56129794Stackerman PROTO_VJUNCOMP, "VJ_UNCOMP" 57129794Stackerman }, 58129794Stackerman { 59129794Stackerman PROTO_VJCOMP, "VJ_COMP" 60129794Stackerman }, 61129794Stackerman { 62129794Stackerman PROTO_COMPD, "COMPD" 63129794Stackerman }, 64129794Stackerman { 65129794Stackerman PROTO_LCP, "LCP" 66129794Stackerman }, 67129794Stackerman { 68129794Stackerman PROTO_IPCP, "IPCP" 69129794Stackerman }, 70129794Stackerman { 71129794Stackerman PROTO_CCP, "CCP" 72129794Stackerman }, 73129794Stackerman { 74129794Stackerman PROTO_PAP, "PAP" 75253102Sjkim }, 76253102Sjkim { 77129794Stackerman PROTO_LQR, "LQR" 78129794Stackerman }, 79129794Stackerman { 80129794Stackerman PROTO_CHAP, "CHAP" 81129794Stackerman }, 82129794Stackerman { 83129794Stackerman 0, "Others" 84129794Stackerman }, 85129794Stackerman}; 86129794Stackerman 87129794Stackermanstatic u_short const fcstab[256] = { 88129794Stackerman /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 89129794Stackerman /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 90129794Stackerman /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 91129794Stackerman /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 92129794Stackerman /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 93129794Stackerman /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 94129794Stackerman /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 95129794Stackerman /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 96129794Stackerman /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 97129794Stackerman /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 98144651Salc /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 99129794Stackerman /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 100199539Sjhb /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 101129794Stackerman /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 102144651Salc /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 103129794Stackerman /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 104129794Stackerman /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 105129794Stackerman /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 106129794Stackerman /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 107129794Stackerman /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 108129794Stackerman /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 109129794Stackerman /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 110129794Stackerman /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 111211907Syongari /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 112129794Stackerman /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 113129794Stackerman /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 114129794Stackerman /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 115129794Stackerman /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 116129794Stackerman /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 117129794Stackerman /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 118129794Stackerman /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 119129794Stackerman /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 120129794Stackerman}; 121129794Stackerman 122129794Stackermanu_char EscMap[33]; 123129794Stackerman 124193096Sattiliovoid 125129794StackermanHdlcInit() 126129794Stackerman{ 127129794Stackerman ifInOctets = ifOutOctets = 0; 128129794Stackerman ifInPackets = ifOutPackets = 0; 129129794Stackerman ifOutLQRs = 0; 130129794Stackerman} 131129794Stackerman 132129794Stackerman/* 133129794Stackerman * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 134129794Stackerman * 2.27 for further details. 135129794Stackerman */ 136129794Stackermaninline u_short 137129794StackermanHdlcFcs(u_short fcs, u_char * cp, int len) 138129794Stackerman{ 139129794Stackerman while (len--) 140129794Stackerman fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 141129794Stackerman return (fcs); 142129794Stackerman} 143129794Stackerman 144129794Stackermanvoid 145129794StackermanHdlcOutput(int pri, u_short proto, struct mbuf * bp) 146129794Stackerman{ 147129794Stackerman struct mbuf *mhp, *mfcs; 148150789Sglebius struct protostat *statp; 149150789Sglebius struct lqrdata *lqr; 150150789Sglebius u_char *cp; 151129794Stackerman u_short fcs; 152129794Stackerman 153129794Stackerman if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 154129794Stackerman if (CcpFsm.state == ST_OPENED) { 155129794Stackerman if (CcpInfo.want_proto == TY_PRED1) { 156129794Stackerman Pred1Output(pri, proto, bp); 157129794Stackerman return; 158129794Stackerman } 159129794Stackerman } 160129794Stackerman } 161129794Stackerman if (DEV_IS_SYNC) 162246128Ssbz mfcs = NULLBUFF; 163246128Ssbz else 164129794Stackerman mfcs = mballoc(2, MB_HDLCOUT); 165129794Stackerman mhp = mballoc(4, MB_HDLCOUT); 166129794Stackerman mhp->cnt = 0; 167129794Stackerman cp = MBUF_CTOP(mhp); 168129794Stackerman if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 169129794Stackerman *cp++ = HDLC_ADDR; 170129794Stackerman *cp++ = HDLC_UI; 171192147Simp mhp->cnt += 2; 172129794Stackerman } 173192147Simp 174192147Simp /* 175144183Smux * If possible, compress protocol field. 176129794Stackerman */ 177129794Stackerman if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 178129794Stackerman *cp++ = proto; 179129794Stackerman mhp->cnt++; 180129794Stackerman } else { 181129794Stackerman *cp++ = proto >> 8; 182129794Stackerman *cp = proto & 0377; 183129794Stackerman mhp->cnt += 2; 184129794Stackerman } 185129794Stackerman mhp->next = bp; 186129794Stackerman bp->next = mfcs; 187129794Stackerman 188129794Stackerman lqr = &MyLqrData; 189129794Stackerman lqr->PeerOutPackets = ifOutPackets++; 190129794Stackerman ifOutOctets += plength(mhp) + 1; 191129794Stackerman lqr->PeerOutOctets = ifOutOctets; 192129794Stackerman 193129794Stackerman if (proto == PROTO_LQR) { 194129794Stackerman lqr->MagicNumber = LcpInfo.want_magic; 195129794Stackerman lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 196129794Stackerman lqr->LastOutPackets = HisLqrData.PeerOutPackets; 197129794Stackerman lqr->LastOutOctets = HisLqrData.PeerOutOctets; 198129794Stackerman lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 199129794Stackerman lqr->PeerInPackets = HisLqrSave.SaveInPackets; 200129794Stackerman lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 201129794Stackerman lqr->PeerInErrors = HisLqrSave.SaveInErrors; 202129794Stackerman lqr->PeerInOctets = HisLqrSave.SaveInOctets; 203129794Stackerman lqr->PeerOutLQRs = ++ifOutLQRs; 204129794Stackerman LqrDump("LqrOutput", lqr); 205129794Stackerman LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 206129794Stackerman } 207129794Stackerman if (!DEV_IS_SYNC) { 208129794Stackerman fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 209129794Stackerman fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 210129794Stackerman fcs = ~fcs; 211129794Stackerman cp = MBUF_CTOP(mfcs); 212129794Stackerman *cp++ = fcs & 0377; /* Low byte first!! */ 213129794Stackerman *cp++ = fcs >> 8; 214129794Stackerman } 215129794Stackerman LogDumpBp(LogHDLC, "HdlcOutput", mhp); 216129794Stackerman for (statp = ProtocolStat; statp->number; statp++) 217129794Stackerman if (statp->number == proto) 218129794Stackerman break; 219129794Stackerman statp->out_count++; 220129794Stackerman if (DEV_IS_SYNC) 221129794Stackerman ModemOutput(pri, mhp); 222129794Stackerman else 223129794Stackerman AsyncOutput(pri, mhp, proto); 224129794Stackerman} 225129794Stackerman 226129794Stackermanvoid 227129794StackermanDecodePacket(u_short proto, struct mbuf * bp) 228143160Simp{ 229129794Stackerman u_char *cp; 230129794Stackerman 231129794Stackerman LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto); 232129794Stackerman 233129794Stackerman switch (proto) { 234129794Stackerman case PROTO_LCP: 235129794Stackerman LcpInput(bp); 236129794Stackerman break; 237129794Stackerman case PROTO_PAP: 238129794Stackerman PapInput(bp); 239129794Stackerman break; 240129794Stackerman case PROTO_LQR: 241129794Stackerman HisLqrSave.SaveInLQRs++; 242129794Stackerman LqrInput(bp); 243129794Stackerman break; 244129794Stackerman case PROTO_CHAP: 245129794Stackerman ChapInput(bp); 246129794Stackerman break; 247129794Stackerman case PROTO_VJUNCOMP: 248129794Stackerman case PROTO_VJCOMP: 249129794Stackerman bp = VjCompInput(bp, proto); 250129794Stackerman if (bp == NULLBUFF) { 251129794Stackerman break; 252129794Stackerman } 253198987Sjhb /* fall down */ 254129794Stackerman case PROTO_IP: 255129794Stackerman IpInput(bp); 256129794Stackerman break; 257129794Stackerman case PROTO_IPCP: 258198987Sjhb IpcpInput(bp); 259129794Stackerman break; 260129794Stackerman case PROTO_CCP: 261129794Stackerman CcpInput(bp); 262129794Stackerman break; 263129794Stackerman case PROTO_COMPD: 264144651Salc Pred1Input(bp); 265129794Stackerman break; 266129794Stackerman default: 267129794Stackerman LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto); 268129794Stackerman bp->offset -= 2; 269129794Stackerman bp->cnt += 2; 270129794Stackerman cp = MBUF_CTOP(bp); 271129794Stackerman LcpSendProtoRej(cp, bp->cnt); 272144183Smux HisLqrSave.SaveInDiscards++; 273144183Smux HdlcStat.unknownproto++; 274129794Stackerman pfree(bp); 275129794Stackerman break; 276129794Stackerman } 277129794Stackerman} 278199539Sjhb 279129794Stackermanint 280129794StackermanReportProtStatus() 281129794Stackerman{ 282129794Stackerman struct protostat *statp; 283129794Stackerman int cnt; 284129794Stackerman 285129794Stackerman statp = ProtocolStat; 286129794Stackerman statp--; 287129794Stackerman cnt = 0; 288129794Stackerman fprintf(VarTerm, " Protocol in out Protocol in out\n"); 289129794Stackerman do { 290129794Stackerman statp++; 291129794Stackerman fprintf(VarTerm, " %-9s: %8lu, %8lu", 292129794Stackerman statp->name, statp->in_count, statp->out_count); 293129794Stackerman if (++cnt == 2) { 294129794Stackerman fprintf(VarTerm, "\n"); 295129794Stackerman cnt = 0; 296129794Stackerman } 297129794Stackerman } while (statp->number); 298129794Stackerman if (cnt) 299129794Stackerman fprintf(VarTerm, "\n"); 300129794Stackerman return (1); 301129794Stackerman} 302198987Sjhb 303129794Stackermanint 304129794StackermanReportHdlcStatus() 305129794Stackerman{ 306129794Stackerman struct hdlcstat *hp = &HdlcStat; 307129794Stackerman 308129794Stackerman if (VarTerm) { 309129794Stackerman fprintf(VarTerm, "HDLC level errors\n\n"); 310129794Stackerman fprintf(VarTerm, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 311198987Sjhb hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 312129794Stackerman } 313129794Stackerman return 0; 314129794Stackerman} 315129794Stackerman 316129794Stackermanstatic struct hdlcstat laststat; 317129794Stackerman 318129794Stackermanvoid 319129794StackermanHdlcErrorCheck() 320129794Stackerman{ 321129794Stackerman struct hdlcstat *hp = &HdlcStat; 322198987Sjhb struct hdlcstat *op = &laststat; 323129794Stackerman 324129794Stackerman if (bcmp(hp, op, sizeof(laststat))) { 325129794Stackerman LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 326129794Stackerman hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 327129794Stackerman hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 328211913Syongari } 329211913Syongari laststat = HdlcStat; 330211913Syongari} 331211913Syongari 332211913Syongarivoid 333211913SyongariHdlcInput(struct mbuf * bp) 334211913Syongari{ 335211913Syongari u_short fcs, proto; 336211913Syongari u_char *cp, addr, ctrl; 337129794Stackerman struct protostat *statp; 338129794Stackerman 339198987Sjhb LogDumpBp(LogHDLC, "HdlcInput:", bp); 340129794Stackerman if (DEV_IS_SYNC) 341129794Stackerman fcs = GOODFCS; 342129794Stackerman else 343129794Stackerman fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 344211907Syongari HisLqrSave.SaveInOctets += bp->cnt + 1; 345211907Syongari 346129794Stackerman LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 347129794Stackerman fcs, (fcs == GOODFCS) ? "good" : "bad"); 348129794Stackerman if (fcs != GOODFCS) { 349129794Stackerman HisLqrSave.SaveInErrors++; 350129794Stackerman LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 351129794Stackerman HdlcStat.badfcs++; 352129794Stackerman pfree(bp); 353129794Stackerman return; 354129794Stackerman } 355129794Stackerman if (!DEV_IS_SYNC) 356129794Stackerman bp->cnt -= 2; /* discard FCS part */ 357129794Stackerman 358129794Stackerman if (bp->cnt < 2) { /* XXX: raise this bar ? */ 359129794Stackerman pfree(bp); 360211907Syongari return; 361211907Syongari } 362129794Stackerman cp = MBUF_CTOP(bp); 363129794Stackerman 364211913Syongari ifInPackets++; 365129794Stackerman ifInOctets += bp->cnt; 366129794Stackerman 367129794Stackerman if (!LcpInfo.want_acfcomp) { 368129794Stackerman 369129794Stackerman /* 370129794Stackerman * We expect that packet is not compressed. 371129794Stackerman */ 372129794Stackerman addr = *cp++; 373129794Stackerman if (addr != HDLC_ADDR) { 374129794Stackerman HisLqrSave.SaveInErrors++; 375129794Stackerman HdlcStat.badaddr++; 376129794Stackerman LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 377129794Stackerman pfree(bp); 378129794Stackerman return; 379129794Stackerman } 380129794Stackerman ctrl = *cp++; 381129794Stackerman if (ctrl != HDLC_UI) { 382129794Stackerman HisLqrSave.SaveInErrors++; 383147256Sbrooks HdlcStat.badcommand++; 384129794Stackerman LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 385129794Stackerman pfree(bp); 386129794Stackerman return; 387150789Sglebius } 388150789Sglebius bp->offset += 2; 389150789Sglebius bp->cnt -= 2; 390150789Sglebius } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 391150789Sglebius 392144651Salc /* 393129794Stackerman * We can receive compressed packet, but peer still send uncompressed 394129794Stackerman * packet to me. 395129794Stackerman */ 396144651Salc cp += 2; 397129794Stackerman bp->offset += 2; 398129794Stackerman bp->cnt -= 2; 399199539Sjhb } 400129794Stackerman if (LcpInfo.want_protocomp) { 401199539Sjhb proto = 0; 402150306Simp cp--; 403199539Sjhb do { 404150306Simp cp++; 405150306Simp bp->offset++; 406199539Sjhb bp->cnt--; 407129794Stackerman proto = proto << 8; 408129794Stackerman proto += *cp; 409129794Stackerman } while (!(proto & 1)); 410129794Stackerman } else { 411129794Stackerman proto = *cp++ << 8; 412129794Stackerman proto |= *cp++; 413129794Stackerman bp->offset += 2; 414129794Stackerman bp->cnt -= 2; 415129794Stackerman } 416129794Stackerman 417129794Stackerman for (statp = ProtocolStat; statp->number; statp++) 418129794Stackerman if (statp->number == proto) 419129794Stackerman break; 420129794Stackerman statp->in_count++; 421129794Stackerman HisLqrSave.SaveInPackets++; 422129794Stackerman 423129794Stackerman DecodePacket(proto, bp); 424129794Stackerman} 425129794Stackerman