11ae349f5Scvs2svn /* 21ae349f5Scvs2svn * PPP High Level Link Control (HDLC) Module 31ae349f5Scvs2svn * 41ae349f5Scvs2svn * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 51ae349f5Scvs2svn * 61ae349f5Scvs2svn * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 71ae349f5Scvs2svn * 81ae349f5Scvs2svn * Redistribution and use in source and binary forms are permitted 91ae349f5Scvs2svn * provided that the above copyright notice and this paragraph are 101ae349f5Scvs2svn * duplicated in all such forms and that any documentation, 111ae349f5Scvs2svn * advertising materials, and other materials related to such 121ae349f5Scvs2svn * distribution and use acknowledge that the software was developed 131ae349f5Scvs2svn * by the Internet Initiative Japan, Inc. The name of the 141ae349f5Scvs2svn * IIJ may not be used to endorse or promote products derived 151ae349f5Scvs2svn * from this software without specific prior written permission. 161ae349f5Scvs2svn * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 171ae349f5Scvs2svn * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 181ae349f5Scvs2svn * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 191ae349f5Scvs2svn * 2085b542cfSBrian Somers * $Id: hdlc.c,v 1.28.2.8 1998/02/09 19:20:47 brian Exp $ 211ae349f5Scvs2svn * 221ae349f5Scvs2svn * TODO: 231ae349f5Scvs2svn */ 241ae349f5Scvs2svn #include <sys/param.h> 251ae349f5Scvs2svn #include <netinet/in.h> 261ae349f5Scvs2svn 271ae349f5Scvs2svn #include <stdio.h> 281ae349f5Scvs2svn #include <string.h> 291ae349f5Scvs2svn #include <termios.h> 301ae349f5Scvs2svn 311ae349f5Scvs2svn #include "command.h" 321ae349f5Scvs2svn #include "mbuf.h" 331ae349f5Scvs2svn #include "log.h" 341ae349f5Scvs2svn #include "defs.h" 351ae349f5Scvs2svn #include "timer.h" 361ae349f5Scvs2svn #include "fsm.h" 371ae349f5Scvs2svn #include "hdlc.h" 381ae349f5Scvs2svn #include "lcpproto.h" 3929e275ceSBrian Somers #include "iplist.h" 4029e275ceSBrian Somers #include "throughput.h" 411ae349f5Scvs2svn #include "ipcp.h" 421ae349f5Scvs2svn #include "ip.h" 431ae349f5Scvs2svn #include "vjcomp.h" 441ae349f5Scvs2svn #include "pap.h" 451ae349f5Scvs2svn #include "chap.h" 461ae349f5Scvs2svn #include "lcp.h" 471ae349f5Scvs2svn #include "async.h" 481ae349f5Scvs2svn #include "lqr.h" 491ae349f5Scvs2svn #include "loadalias.h" 501ae349f5Scvs2svn #include "vars.h" 511ae349f5Scvs2svn #include "modem.h" 521ae349f5Scvs2svn #include "ccp.h" 538c07a7b2SBrian Somers #include "link.h" 5442d4d396SBrian Somers #include "descriptor.h" 5563b73463SBrian Somers #include "physical.h" 5685b542cfSBrian Somers #include "prompt.h" 571ae349f5Scvs2svn 581ae349f5Scvs2svn static struct hdlcstat { 591ae349f5Scvs2svn int badfcs; 601ae349f5Scvs2svn int badaddr; 611ae349f5Scvs2svn int badcommand; 621ae349f5Scvs2svn int unknownproto; 631ae349f5Scvs2svn } HdlcStat; 641ae349f5Scvs2svn 651ae349f5Scvs2svn static int ifOutPackets; 661ae349f5Scvs2svn static int ifOutOctets; 671ae349f5Scvs2svn static int ifOutLQRs; 681ae349f5Scvs2svn 691ae349f5Scvs2svn static u_short const fcstab[256] = { 701ae349f5Scvs2svn /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 711ae349f5Scvs2svn /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 721ae349f5Scvs2svn /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 731ae349f5Scvs2svn /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 741ae349f5Scvs2svn /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 751ae349f5Scvs2svn /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 761ae349f5Scvs2svn /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 771ae349f5Scvs2svn /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 781ae349f5Scvs2svn /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 791ae349f5Scvs2svn /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 801ae349f5Scvs2svn /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 811ae349f5Scvs2svn /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 821ae349f5Scvs2svn /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 831ae349f5Scvs2svn /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 841ae349f5Scvs2svn /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 851ae349f5Scvs2svn /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 861ae349f5Scvs2svn /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 871ae349f5Scvs2svn /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 881ae349f5Scvs2svn /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 891ae349f5Scvs2svn /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 901ae349f5Scvs2svn /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 911ae349f5Scvs2svn /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 921ae349f5Scvs2svn /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 931ae349f5Scvs2svn /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 941ae349f5Scvs2svn /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 951ae349f5Scvs2svn /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 961ae349f5Scvs2svn /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 971ae349f5Scvs2svn /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 981ae349f5Scvs2svn /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 991ae349f5Scvs2svn /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 1001ae349f5Scvs2svn /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 1011ae349f5Scvs2svn /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 1021ae349f5Scvs2svn }; 1031ae349f5Scvs2svn 1041ae349f5Scvs2svn u_char EscMap[33]; 1051ae349f5Scvs2svn 1061ae349f5Scvs2svn void 1071ae349f5Scvs2svn HdlcInit() 1081ae349f5Scvs2svn { 1091ae349f5Scvs2svn ifOutOctets = 0; 1101ae349f5Scvs2svn ifOutPackets = 0; 1111ae349f5Scvs2svn ifOutLQRs = 0; 1121ae349f5Scvs2svn } 1131ae349f5Scvs2svn 1141ae349f5Scvs2svn /* 1151ae349f5Scvs2svn * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1161ae349f5Scvs2svn * 2.27 for further details. 1171ae349f5Scvs2svn */ 1181ae349f5Scvs2svn inline u_short 1191ae349f5Scvs2svn HdlcFcs(u_short fcs, u_char * cp, int len) 1201ae349f5Scvs2svn { 1211ae349f5Scvs2svn while (len--) 1221ae349f5Scvs2svn fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 1231ae349f5Scvs2svn return (fcs); 1241ae349f5Scvs2svn } 1251ae349f5Scvs2svn 1261ae349f5Scvs2svn static inline u_short 1271ae349f5Scvs2svn HdlcFcsBuf(u_short fcs, struct mbuf *m) 1281ae349f5Scvs2svn { 1291ae349f5Scvs2svn int len; 1301ae349f5Scvs2svn u_char *pos, *end; 1311ae349f5Scvs2svn 1321ae349f5Scvs2svn len = plength(m); 1331ae349f5Scvs2svn pos = MBUF_CTOP(m); 1341ae349f5Scvs2svn end = pos + m->cnt; 1351ae349f5Scvs2svn while (len--) { 1361ae349f5Scvs2svn fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff]; 1371ae349f5Scvs2svn if (pos == end && len) { 1381ae349f5Scvs2svn m = m->next; 1391ae349f5Scvs2svn pos = MBUF_CTOP(m); 1401ae349f5Scvs2svn end = pos + m->cnt; 1411ae349f5Scvs2svn } 1421ae349f5Scvs2svn } 1431ae349f5Scvs2svn return (fcs); 1441ae349f5Scvs2svn } 1451ae349f5Scvs2svn 1461ae349f5Scvs2svn void 1478c07a7b2SBrian Somers HdlcOutput(struct link *l, int pri, u_short proto, struct mbuf *bp) 1481ae349f5Scvs2svn { 1498c07a7b2SBrian Somers struct physical *p = link2physical(l); 1501ae349f5Scvs2svn struct mbuf *mhp, *mfcs; 1511ae349f5Scvs2svn struct lqrdata *lqr; 1521ae349f5Scvs2svn u_char *cp; 1531ae349f5Scvs2svn u_short fcs; 1541ae349f5Scvs2svn 1551ae349f5Scvs2svn if ((proto & 0xfff1) == 0x21) /* Network Layer protocol */ 1567308ec68SBrian Somers if (CcpInfo.fsm.state == ST_OPENED) 1578c07a7b2SBrian Somers if (CcpOutput(l, pri, proto, bp)) 1581ae349f5Scvs2svn return; 1591ae349f5Scvs2svn 1608c07a7b2SBrian Somers if (!p) { 1618c07a7b2SBrian Somers /* 1628c07a7b2SBrian Somers * This is where we multiplex the data over our available physical 1638c07a7b2SBrian Somers * links. We don't frame our logical link data. Instead we wait 1648c07a7b2SBrian Somers * for the logical link implementation to chop our data up and pile 1658c07a7b2SBrian Somers * it into the physical links by re-calling this function with the 1668c07a7b2SBrian Somers * encapsulated fragments. 1678c07a7b2SBrian Somers */ 1688c07a7b2SBrian Somers link_Output(l, pri, bp); 1698c07a7b2SBrian Somers return; 1708c07a7b2SBrian Somers } 1718c07a7b2SBrian Somers 1728c07a7b2SBrian Somers if (Physical_IsSync(p)) 1731ae349f5Scvs2svn mfcs = NULL; 1741ae349f5Scvs2svn else 1751ae349f5Scvs2svn mfcs = mballoc(2, MB_HDLCOUT); 1761ae349f5Scvs2svn 1771ae349f5Scvs2svn mhp = mballoc(4, MB_HDLCOUT); 1781ae349f5Scvs2svn mhp->cnt = 0; 1791ae349f5Scvs2svn cp = MBUF_CTOP(mhp); 1801ae349f5Scvs2svn if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 1811ae349f5Scvs2svn *cp++ = HDLC_ADDR; 1821ae349f5Scvs2svn *cp++ = HDLC_UI; 1831ae349f5Scvs2svn mhp->cnt += 2; 1841ae349f5Scvs2svn } 1851ae349f5Scvs2svn 1861ae349f5Scvs2svn /* 1871ae349f5Scvs2svn * If possible, compress protocol field. 1881ae349f5Scvs2svn */ 1891ae349f5Scvs2svn if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 1901ae349f5Scvs2svn *cp++ = proto; 1911ae349f5Scvs2svn mhp->cnt++; 1921ae349f5Scvs2svn } else { 1931ae349f5Scvs2svn *cp++ = proto >> 8; 1941ae349f5Scvs2svn *cp = proto & 0377; 1951ae349f5Scvs2svn mhp->cnt += 2; 1961ae349f5Scvs2svn } 1978c07a7b2SBrian Somers 1981ae349f5Scvs2svn mhp->next = bp; 1991ae349f5Scvs2svn while (bp->next != NULL) 2001ae349f5Scvs2svn bp = bp->next; 2011ae349f5Scvs2svn bp->next = mfcs; 2021ae349f5Scvs2svn bp = mhp->next; 2031ae349f5Scvs2svn 2041ae349f5Scvs2svn lqr = &MyLqrData; 2051ae349f5Scvs2svn lqr->PeerOutPackets = ifOutPackets++; 2061ae349f5Scvs2svn ifOutOctets += plength(mhp) + 1; 2071ae349f5Scvs2svn lqr->PeerOutOctets = ifOutOctets; 2081ae349f5Scvs2svn 2091ae349f5Scvs2svn if (proto == PROTO_LQR) { 2101ae349f5Scvs2svn lqr->MagicNumber = LcpInfo.want_magic; 2111ae349f5Scvs2svn lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 2121ae349f5Scvs2svn lqr->LastOutPackets = HisLqrData.PeerOutPackets; 2131ae349f5Scvs2svn lqr->LastOutOctets = HisLqrData.PeerOutOctets; 2141ae349f5Scvs2svn lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 2151ae349f5Scvs2svn lqr->PeerInPackets = HisLqrSave.SaveInPackets; 2161ae349f5Scvs2svn lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 2171ae349f5Scvs2svn lqr->PeerInErrors = HisLqrSave.SaveInErrors; 2181ae349f5Scvs2svn lqr->PeerInOctets = HisLqrSave.SaveInOctets; 2191ae349f5Scvs2svn lqr->PeerOutLQRs = ++ifOutLQRs; 2201ae349f5Scvs2svn LqrDump("LqrOutput", lqr); 2211ae349f5Scvs2svn LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 2221ae349f5Scvs2svn } 2238c07a7b2SBrian Somers 2248c07a7b2SBrian Somers if (mfcs) { 2251ae349f5Scvs2svn mfcs->cnt = 0; 2261ae349f5Scvs2svn fcs = HdlcFcsBuf(INITFCS, mhp); 2271ae349f5Scvs2svn fcs = ~fcs; 2281ae349f5Scvs2svn cp = MBUF_CTOP(mfcs); 2291ae349f5Scvs2svn *cp++ = fcs & 0377; /* Low byte first!! */ 2301ae349f5Scvs2svn *cp++ = fcs >> 8; 2311ae349f5Scvs2svn mfcs->cnt = 2; 2321ae349f5Scvs2svn } 2331ae349f5Scvs2svn 2348c07a7b2SBrian Somers LogDumpBp(LogHDLC, "HdlcOutput", mhp); 2358c07a7b2SBrian Somers 2368c07a7b2SBrian Somers link_ProtocolRecord(l, proto, PROTO_OUT); 2371ae349f5Scvs2svn LogPrintf(LogDEBUG, "HdlcOutput: proto = 0x%04x\n", proto); 2381ae349f5Scvs2svn 2398c07a7b2SBrian Somers if (Physical_IsSync(p)) 2408c07a7b2SBrian Somers link_Output(l, pri, mhp); /* Send it raw */ 2411ae349f5Scvs2svn else 2426140ba11SBrian Somers async_Output(pri, mhp, proto, p); 2431ae349f5Scvs2svn } 2441ae349f5Scvs2svn 2451ae349f5Scvs2svn /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */ 2461ae349f5Scvs2svn static struct { 2471ae349f5Scvs2svn u_short from; 2481ae349f5Scvs2svn u_short to; 2491ae349f5Scvs2svn const char *name; 2501ae349f5Scvs2svn } protocols[] = { 2511ae349f5Scvs2svn { 0x0001, 0x0001, "Padding Protocol" }, 2521ae349f5Scvs2svn { 0x0003, 0x001f, "reserved (transparency inefficient)" }, 2531ae349f5Scvs2svn { 0x0021, 0x0021, "Internet Protocol" }, 2541ae349f5Scvs2svn { 0x0023, 0x0023, "OSI Network Layer" }, 2551ae349f5Scvs2svn { 0x0025, 0x0025, "Xerox NS IDP" }, 2561ae349f5Scvs2svn { 0x0027, 0x0027, "DECnet Phase IV" }, 2571ae349f5Scvs2svn { 0x0029, 0x0029, "Appletalk" }, 2581ae349f5Scvs2svn { 0x002b, 0x002b, "Novell IPX" }, 2591ae349f5Scvs2svn { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" }, 2601ae349f5Scvs2svn { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" }, 2611ae349f5Scvs2svn { 0x0031, 0x0031, "Bridging PDU" }, 2621ae349f5Scvs2svn { 0x0033, 0x0033, "Stream Protocol (ST-II)" }, 2631ae349f5Scvs2svn { 0x0035, 0x0035, "Banyan Vines" }, 2641ae349f5Scvs2svn { 0x0037, 0x0037, "reserved (until 1993)" }, 2651ae349f5Scvs2svn { 0x0039, 0x0039, "AppleTalk EDDP" }, 2661ae349f5Scvs2svn { 0x003b, 0x003b, "AppleTalk SmartBuffered" }, 2671ae349f5Scvs2svn { 0x003d, 0x003d, "Multi-Link" }, 2681ae349f5Scvs2svn { 0x003f, 0x003f, "NETBIOS Framing" }, 2691ae349f5Scvs2svn { 0x0041, 0x0041, "Cisco Systems" }, 2701ae349f5Scvs2svn { 0x0043, 0x0043, "Ascom Timeplex" }, 2711ae349f5Scvs2svn { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" }, 2721ae349f5Scvs2svn { 0x0047, 0x0047, "DCA Remote Lan" }, 2731ae349f5Scvs2svn { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" }, 2741ae349f5Scvs2svn { 0x004b, 0x004b, "SNA over 802.2" }, 2751ae349f5Scvs2svn { 0x004d, 0x004d, "SNA" }, 2761ae349f5Scvs2svn { 0x004f, 0x004f, "IP6 Header Compression" }, 2771ae349f5Scvs2svn { 0x0051, 0x0051, "KNX Bridging Data" }, 2781ae349f5Scvs2svn { 0x0053, 0x0053, "Encryption" }, 2791ae349f5Scvs2svn { 0x0055, 0x0055, "Individual Link Encryption" }, 2801ae349f5Scvs2svn { 0x006f, 0x006f, "Stampede Bridging" }, 2811ae349f5Scvs2svn { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" }, 2821ae349f5Scvs2svn { 0x0073, 0x0073, "MP+ Protocol" }, 2831ae349f5Scvs2svn { 0x007d, 0x007d, "reserved (Control Escape)" }, 2841ae349f5Scvs2svn { 0x007f, 0x007f, "reserved (compression inefficient)" }, 2851ae349f5Scvs2svn { 0x00cf, 0x00cf, "reserved (PPP NLPID)" }, 2861ae349f5Scvs2svn { 0x00fb, 0x00fb, "compression on single link in multilink group" }, 2871ae349f5Scvs2svn { 0x00fd, 0x00fd, "1st choice compression" }, 2881ae349f5Scvs2svn { 0x00ff, 0x00ff, "reserved (compression inefficient)" }, 2891ae349f5Scvs2svn { 0x0200, 0x02ff, "(compression inefficient)" }, 2901ae349f5Scvs2svn { 0x0201, 0x0201, "802.1d Hello Packets" }, 2911ae349f5Scvs2svn { 0x0203, 0x0203, "IBM Source Routing BPDU" }, 2921ae349f5Scvs2svn { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" }, 2931ae349f5Scvs2svn { 0x0207, 0x0207, "Cisco Discovery Protocol" }, 2941ae349f5Scvs2svn { 0x0209, 0x0209, "Netcs Twin Routing" }, 2951ae349f5Scvs2svn { 0x0231, 0x0231, "Luxcom" }, 2961ae349f5Scvs2svn { 0x0233, 0x0233, "Sigma Network Systems" }, 2971ae349f5Scvs2svn { 0x0235, 0x0235, "Apple Client Server Protocol" }, 2981ae349f5Scvs2svn { 0x1e00, 0x1eff, "(compression inefficient)" }, 2991ae349f5Scvs2svn { 0x4001, 0x4001, "Cray Communications Control Protocol" }, 3001ae349f5Scvs2svn { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" }, 3011ae349f5Scvs2svn { 0x4021, 0x4021, "Stacker LZS" }, 3021ae349f5Scvs2svn { 0x8001, 0x801f, "Not Used - reserved" }, 3031ae349f5Scvs2svn { 0x8021, 0x8021, "Internet Protocol Control Protocol" }, 3041ae349f5Scvs2svn { 0x8023, 0x8023, "OSI Network Layer Control Protocol" }, 3051ae349f5Scvs2svn { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" }, 3061ae349f5Scvs2svn { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" }, 3071ae349f5Scvs2svn { 0x8029, 0x8029, "Appletalk Control Protocol" }, 3081ae349f5Scvs2svn { 0x802b, 0x802b, "Novell IPX Control Protocol" }, 3091ae349f5Scvs2svn { 0x802d, 0x802d, "reserved" }, 3101ae349f5Scvs2svn { 0x802f, 0x802f, "reserved" }, 3111ae349f5Scvs2svn { 0x8031, 0x8031, "Bridging NCP" }, 3121ae349f5Scvs2svn { 0x8033, 0x8033, "Stream Protocol Control Protocol" }, 3131ae349f5Scvs2svn { 0x8035, 0x8035, "Banyan Vines Control Protocol" }, 3141ae349f5Scvs2svn { 0x8037, 0x8037, "reserved till 1993" }, 3151ae349f5Scvs2svn { 0x8039, 0x8039, "reserved" }, 3161ae349f5Scvs2svn { 0x803b, 0x803b, "reserved" }, 3171ae349f5Scvs2svn { 0x803d, 0x803d, "Multi-Link Control Protocol" }, 3181ae349f5Scvs2svn { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" }, 3191ae349f5Scvs2svn { 0x8041, 0x8041, "Cisco Systems Control Protocol" }, 3201ae349f5Scvs2svn { 0x8043, 0x8043, "Ascom Timeplex" }, 3211ae349f5Scvs2svn { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" }, 3221ae349f5Scvs2svn { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, 3231ae349f5Scvs2svn { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, 3241ae349f5Scvs2svn { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" }, 3251ae349f5Scvs2svn { 0x804d, 0x804d, "SNA Control Protocol" }, 3261ae349f5Scvs2svn { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" }, 3271ae349f5Scvs2svn { 0x8051, 0x8051, "KNX Bridging Control Protocol" }, 3281ae349f5Scvs2svn { 0x8053, 0x8053, "Encryption Control Protocol" }, 3291ae349f5Scvs2svn { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" }, 3301ae349f5Scvs2svn { 0x806f, 0x806f, "Stampede Bridging Control Protocol" }, 3311ae349f5Scvs2svn { 0x8073, 0x8073, "MP+ Control Protocol" }, 3321ae349f5Scvs2svn { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" }, 3331ae349f5Scvs2svn { 0x807d, 0x807d, "Not Used - reserved" }, 3341ae349f5Scvs2svn { 0x80cf, 0x80cf, "Not Used - reserved" }, 3351ae349f5Scvs2svn { 0x80fb, 0x80fb, "compression on single link in multilink group control" }, 3361ae349f5Scvs2svn { 0x80fd, 0x80fd, "Compression Control Protocol" }, 3371ae349f5Scvs2svn { 0x80ff, 0x80ff, "Not Used - reserved" }, 3381ae349f5Scvs2svn { 0x8207, 0x8207, "Cisco Discovery Protocol Control" }, 3391ae349f5Scvs2svn { 0x8209, 0x8209, "Netcs Twin Routing" }, 3401ae349f5Scvs2svn { 0x8235, 0x8235, "Apple Client Server Protocol Control" }, 3411ae349f5Scvs2svn { 0xc021, 0xc021, "Link Control Protocol" }, 3421ae349f5Scvs2svn { 0xc023, 0xc023, "Password Authentication Protocol" }, 3431ae349f5Scvs2svn { 0xc025, 0xc025, "Link Quality Report" }, 3441ae349f5Scvs2svn { 0xc027, 0xc027, "Shiva Password Authentication Protocol" }, 3451ae349f5Scvs2svn { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" }, 3461ae349f5Scvs2svn { 0xc081, 0xc081, "Container Control Protocol" }, 3471ae349f5Scvs2svn { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" }, 3481ae349f5Scvs2svn { 0xc225, 0xc225, "RSA Authentication Protocol" }, 3491ae349f5Scvs2svn { 0xc227, 0xc227, "Extensible Authentication Protocol" }, 3501ae349f5Scvs2svn { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" }, 3511ae349f5Scvs2svn { 0xc281, 0xc281, "Proprietary Authentication Protocol" }, 3521ae349f5Scvs2svn { 0xc283, 0xc283, "Proprietary Authentication Protocol" }, 3531ae349f5Scvs2svn { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" } 3541ae349f5Scvs2svn }; 3551ae349f5Scvs2svn 3561ae349f5Scvs2svn #define NPROTOCOLS (sizeof protocols/sizeof protocols[0]) 3571ae349f5Scvs2svn 3581ae349f5Scvs2svn static const char * 3591ae349f5Scvs2svn Protocol2Nam(u_short proto) 3601ae349f5Scvs2svn { 3611ae349f5Scvs2svn int f; 3621ae349f5Scvs2svn 3631ae349f5Scvs2svn for (f = 0; f < NPROTOCOLS; f++) 3641ae349f5Scvs2svn if (proto >= protocols[f].from && proto <= protocols[f].to) 3651ae349f5Scvs2svn return protocols[f].name; 3661ae349f5Scvs2svn else if (proto < protocols[f].from) 3671ae349f5Scvs2svn break; 3681ae349f5Scvs2svn return "unrecognised protocol"; 3691ae349f5Scvs2svn } 3701ae349f5Scvs2svn 3711ae349f5Scvs2svn static void 3727a6f8720SBrian Somers DecodePacket(struct bundle *bundle, u_short proto, struct mbuf * bp, 3737a6f8720SBrian Somers struct link *l) 3741ae349f5Scvs2svn { 3758c07a7b2SBrian Somers struct physical *p = link2physical(l); 3761ae349f5Scvs2svn u_char *cp; 3771ae349f5Scvs2svn 3781ae349f5Scvs2svn LogPrintf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto); 3791ae349f5Scvs2svn 3801ae349f5Scvs2svn /* 3811ae349f5Scvs2svn * If proto isn't PROTO_COMPD, we still want to pass it to the 3821ae349f5Scvs2svn * decompression routines so that the dictionary's updated 3831ae349f5Scvs2svn */ 3847308ec68SBrian Somers if (CcpInfo.fsm.state == ST_OPENED) 3851ae349f5Scvs2svn if (proto == PROTO_COMPD) { 3861ae349f5Scvs2svn if ((bp = CompdInput(&proto, bp)) == NULL) 3871ae349f5Scvs2svn return; 3881ae349f5Scvs2svn } else if ((proto & 0xfff1) == 0x21) /* Network Layer protocol */ 3891ae349f5Scvs2svn CcpDictSetup(proto, bp); 3901ae349f5Scvs2svn 3911ae349f5Scvs2svn switch (proto) { 3921ae349f5Scvs2svn case PROTO_LCP: 3931ae349f5Scvs2svn LcpInput(bp); 3941ae349f5Scvs2svn break; 3951ae349f5Scvs2svn case PROTO_PAP: 3968c07a7b2SBrian Somers if (p) 3977a6f8720SBrian Somers PapInput(bundle, bp, p); 3988c07a7b2SBrian Somers else { 3998c07a7b2SBrian Somers LogPrintf(LogERROR, "DecodePacket: PAP: Not a physical link !\n"); 4008c07a7b2SBrian Somers pfree(bp); 4018c07a7b2SBrian Somers } 4021ae349f5Scvs2svn break; 4031ae349f5Scvs2svn case PROTO_LQR: 4041ae349f5Scvs2svn HisLqrSave.SaveInLQRs++; 4058c07a7b2SBrian Somers if (p) 4068c07a7b2SBrian Somers LqrInput(p, bp); 4078c07a7b2SBrian Somers else { 4088c07a7b2SBrian Somers LogPrintf(LogERROR, "DecodePacket: LQR: Not a physical link !\n"); 4098c07a7b2SBrian Somers pfree(bp); 4108c07a7b2SBrian Somers } 4111ae349f5Scvs2svn break; 4121ae349f5Scvs2svn case PROTO_CHAP: 4138c07a7b2SBrian Somers if (p) 4147a6f8720SBrian Somers ChapInput(bundle, bp, p); 4158c07a7b2SBrian Somers else { 4168c07a7b2SBrian Somers LogPrintf(LogERROR, "DecodePacket: CHAP: Not a physical link !\n"); 4178c07a7b2SBrian Somers pfree(bp); 4188c07a7b2SBrian Somers } 4191ae349f5Scvs2svn break; 4201ae349f5Scvs2svn case PROTO_VJUNCOMP: 4211ae349f5Scvs2svn case PROTO_VJCOMP: 4221ae349f5Scvs2svn bp = VjCompInput(bp, proto); 4238c07a7b2SBrian Somers if (bp == NULL) 4241ae349f5Scvs2svn break; 4251ae349f5Scvs2svn /* fall down */ 4261ae349f5Scvs2svn case PROTO_IP: 4277a6f8720SBrian Somers IpInput(bundle, bp); 4281ae349f5Scvs2svn break; 4291ae349f5Scvs2svn case PROTO_IPCP: 4301ae349f5Scvs2svn IpcpInput(bp); 4311ae349f5Scvs2svn break; 4321ae349f5Scvs2svn case PROTO_CCP: 433455aabc3SBrian Somers CcpInput(bundle, bp); 4341ae349f5Scvs2svn break; 4351ae349f5Scvs2svn default: 4361ae349f5Scvs2svn LogPrintf(LogPHASE, "Unknown protocol 0x%04x (%s)\n", 4371ae349f5Scvs2svn proto, Protocol2Nam(proto)); 4381ae349f5Scvs2svn bp->offset -= 2; 4391ae349f5Scvs2svn bp->cnt += 2; 4401ae349f5Scvs2svn cp = MBUF_CTOP(bp); 4411ae349f5Scvs2svn LcpSendProtoRej(cp, bp->cnt); 4421ae349f5Scvs2svn HisLqrSave.SaveInDiscards++; 4431ae349f5Scvs2svn HdlcStat.unknownproto++; 4441ae349f5Scvs2svn pfree(bp); 4451ae349f5Scvs2svn break; 4461ae349f5Scvs2svn } 4471ae349f5Scvs2svn } 4481ae349f5Scvs2svn 4491ae349f5Scvs2svn int 4501ae349f5Scvs2svn ReportHdlcStatus(struct cmdargs const *arg) 4511ae349f5Scvs2svn { 45285b542cfSBrian Somers prompt_Printf(&prompt, "HDLC level errors\n\n"); 45385b542cfSBrian Somers prompt_Printf(&prompt, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 45485b542cfSBrian Somers HdlcStat.badfcs, HdlcStat.badaddr, 45585b542cfSBrian Somers HdlcStat.badcommand, HdlcStat.unknownproto); 4561ae349f5Scvs2svn return 0; 4571ae349f5Scvs2svn } 4581ae349f5Scvs2svn 4591ae349f5Scvs2svn static struct hdlcstat laststat; 4601ae349f5Scvs2svn 4611ae349f5Scvs2svn void 4621ae349f5Scvs2svn HdlcErrorCheck() 4631ae349f5Scvs2svn { 46485b542cfSBrian Somers if (memcmp(&HdlcStat, &laststat, sizeof laststat)) { 4651ae349f5Scvs2svn LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 46685b542cfSBrian Somers HdlcStat.badfcs - laststat.badfcs, 46785b542cfSBrian Somers HdlcStat.badaddr - laststat.badaddr, 46885b542cfSBrian Somers HdlcStat.badcommand - laststat.badcommand, 46985b542cfSBrian Somers HdlcStat.unknownproto - laststat.unknownproto); 4701ae349f5Scvs2svn laststat = HdlcStat; 4711ae349f5Scvs2svn } 47285b542cfSBrian Somers } 4731ae349f5Scvs2svn 4741ae349f5Scvs2svn void 4757a6f8720SBrian Somers HdlcInput(struct bundle *bundle, struct mbuf * bp, struct physical *physical) 4761ae349f5Scvs2svn { 4771ae349f5Scvs2svn u_short fcs, proto; 4781ae349f5Scvs2svn u_char *cp, addr, ctrl; 4791ae349f5Scvs2svn 4801ae349f5Scvs2svn LogDumpBp(LogHDLC, "HdlcInput:", bp); 48163b73463SBrian Somers if (Physical_IsSync(physical)) 4821ae349f5Scvs2svn fcs = GOODFCS; 4831ae349f5Scvs2svn else 4841ae349f5Scvs2svn fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 4851ae349f5Scvs2svn HisLqrSave.SaveInOctets += bp->cnt + 1; 4861ae349f5Scvs2svn 4871ae349f5Scvs2svn LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 4881ae349f5Scvs2svn fcs, (fcs == GOODFCS) ? "good" : "bad"); 4891ae349f5Scvs2svn if (fcs != GOODFCS) { 4901ae349f5Scvs2svn HisLqrSave.SaveInErrors++; 4911ae349f5Scvs2svn LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 4921ae349f5Scvs2svn HdlcStat.badfcs++; 4931ae349f5Scvs2svn pfree(bp); 4941ae349f5Scvs2svn return; 4951ae349f5Scvs2svn } 49663b73463SBrian Somers if (!Physical_IsSync(physical)) 4971ae349f5Scvs2svn bp->cnt -= 2; /* discard FCS part */ 4981ae349f5Scvs2svn 4991ae349f5Scvs2svn if (bp->cnt < 2) { /* XXX: raise this bar ? */ 5001ae349f5Scvs2svn pfree(bp); 5011ae349f5Scvs2svn return; 5021ae349f5Scvs2svn } 5031ae349f5Scvs2svn cp = MBUF_CTOP(bp); 5041ae349f5Scvs2svn 5051ae349f5Scvs2svn if (!LcpInfo.want_acfcomp) { 5061ae349f5Scvs2svn 5071ae349f5Scvs2svn /* 5081ae349f5Scvs2svn * We expect that packet is not compressed. 5091ae349f5Scvs2svn */ 5101ae349f5Scvs2svn addr = *cp++; 5111ae349f5Scvs2svn if (addr != HDLC_ADDR) { 5121ae349f5Scvs2svn HisLqrSave.SaveInErrors++; 5131ae349f5Scvs2svn HdlcStat.badaddr++; 5141ae349f5Scvs2svn LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 5151ae349f5Scvs2svn pfree(bp); 5161ae349f5Scvs2svn return; 5171ae349f5Scvs2svn } 5181ae349f5Scvs2svn ctrl = *cp++; 5191ae349f5Scvs2svn if (ctrl != HDLC_UI) { 5201ae349f5Scvs2svn HisLqrSave.SaveInErrors++; 5211ae349f5Scvs2svn HdlcStat.badcommand++; 5221ae349f5Scvs2svn LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 5231ae349f5Scvs2svn pfree(bp); 5241ae349f5Scvs2svn return; 5251ae349f5Scvs2svn } 5261ae349f5Scvs2svn bp->offset += 2; 5271ae349f5Scvs2svn bp->cnt -= 2; 5281ae349f5Scvs2svn } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 5291ae349f5Scvs2svn 5301ae349f5Scvs2svn /* 5311ae349f5Scvs2svn * We can receive compressed packet, but peer still send uncompressed 5321ae349f5Scvs2svn * packet to me. 5331ae349f5Scvs2svn */ 5341ae349f5Scvs2svn cp += 2; 5351ae349f5Scvs2svn bp->offset += 2; 5361ae349f5Scvs2svn bp->cnt -= 2; 5371ae349f5Scvs2svn } 5381ae349f5Scvs2svn if (LcpInfo.want_protocomp) { 5391ae349f5Scvs2svn proto = 0; 5401ae349f5Scvs2svn cp--; 5411ae349f5Scvs2svn do { 5421ae349f5Scvs2svn cp++; 5431ae349f5Scvs2svn bp->offset++; 5441ae349f5Scvs2svn bp->cnt--; 5451ae349f5Scvs2svn proto = proto << 8; 5461ae349f5Scvs2svn proto += *cp; 5471ae349f5Scvs2svn } while (!(proto & 1)); 5481ae349f5Scvs2svn } else { 5491ae349f5Scvs2svn proto = *cp++ << 8; 5501ae349f5Scvs2svn proto |= *cp++; 5511ae349f5Scvs2svn bp->offset += 2; 5521ae349f5Scvs2svn bp->cnt -= 2; 5531ae349f5Scvs2svn } 5541ae349f5Scvs2svn 5558c07a7b2SBrian Somers link_ProtocolRecord(physical2link(physical), proto, PROTO_IN); 5561ae349f5Scvs2svn HisLqrSave.SaveInPackets++; 5571ae349f5Scvs2svn 5587a6f8720SBrian Somers DecodePacket(bundle, proto, bp, physical2link(physical)); 5591ae349f5Scvs2svn } 56042d4d396SBrian Somers 56142d4d396SBrian Somers /* 56242d4d396SBrian Somers * Detect a HDLC frame 56342d4d396SBrian Somers */ 56442d4d396SBrian Somers 56542d4d396SBrian Somers static const char *FrameHeaders[] = { 56642d4d396SBrian Somers "\176\377\003\300\041", 56742d4d396SBrian Somers "\176\377\175\043\300\041", 56842d4d396SBrian Somers "\176\177\175\043\100\041", 56942d4d396SBrian Somers "\176\175\337\175\043\300\041", 57042d4d396SBrian Somers "\176\175\137\175\043\100\041", 57142d4d396SBrian Somers NULL, 57242d4d396SBrian Somers }; 57342d4d396SBrian Somers 57442d4d396SBrian Somers u_char * 57542d4d396SBrian Somers HdlcDetect(struct physical *physical, u_char *cp, int n) 57642d4d396SBrian Somers { 57742d4d396SBrian Somers const char *ptr, *fp, **hp; 57842d4d396SBrian Somers 57942d4d396SBrian Somers cp[n] = '\0'; /* be sure to null terminated */ 58042d4d396SBrian Somers ptr = NULL; 58142d4d396SBrian Somers for (hp = FrameHeaders; *hp; hp++) { 58242d4d396SBrian Somers fp = *hp; 58342d4d396SBrian Somers if (Physical_IsSync(physical)) 58442d4d396SBrian Somers fp++; 58542d4d396SBrian Somers ptr = strstr((char *) cp, fp); 58642d4d396SBrian Somers if (ptr) 58742d4d396SBrian Somers break; 58842d4d396SBrian Somers } 58942d4d396SBrian Somers return (u_char *)ptr; 59042d4d396SBrian Somers } 591