hdlc.c revision 37210
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 * 2037210Sbrian * $Id: hdlc.c,v 1.33 1998/06/16 19:40:37 brian Exp $ 218857Srgrimes * 226059Samurai * TODO: 236059Samurai */ 2436285Sbrian#include <sys/types.h> 2530715Sbrian#include <netinet/in.h> 2636285Sbrian#include <netinet/in_systm.h> 2736285Sbrian#include <netinet/ip.h> 2836285Sbrian#include <sys/un.h> 2930715Sbrian 3030715Sbrian#include <stdio.h> 3130715Sbrian#include <string.h> 3230715Sbrian#include <termios.h> 3330715Sbrian 3437009Sbrian#include "defs.h" 3531343Sbrian#include "command.h" 3630715Sbrian#include "mbuf.h" 3730715Sbrian#include "log.h" 3830715Sbrian#include "timer.h" 396059Samurai#include "fsm.h" 4036285Sbrian#include "lqr.h" 416059Samurai#include "hdlc.h" 426059Samurai#include "lcpproto.h" 4336285Sbrian#include "iplist.h" 4436285Sbrian#include "throughput.h" 4536285Sbrian#include "slcompress.h" 4630733Sbrian#include "ipcp.h" 4730733Sbrian#include "ip.h" 4830733Sbrian#include "vjcomp.h" 4936285Sbrian#include "auth.h" 5030733Sbrian#include "pap.h" 5130733Sbrian#include "chap.h" 526059Samurai#include "lcp.h" 5331343Sbrian#include "async.h" 5413389Sphk#include "ccp.h" 5536285Sbrian#include "link.h" 5636285Sbrian#include "descriptor.h" 5736285Sbrian#include "physical.h" 5836285Sbrian#include "prompt.h" 5936285Sbrian#include "chat.h" 6036285Sbrian#include "mp.h" 6136285Sbrian#include "datalink.h" 6236285Sbrian#include "filter.h" 6336285Sbrian#include "bundle.h" 646059Samurai 6537210Sbrianstatic u_int16_t const fcstab[256] = { 6628679Sbrian /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 6728679Sbrian /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 6828679Sbrian /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 6928679Sbrian /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 7028679Sbrian /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 7128679Sbrian /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 7228679Sbrian /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 7328679Sbrian /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 7428679Sbrian /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 7528679Sbrian /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 7628679Sbrian /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 7728679Sbrian /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 7828679Sbrian /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 7928679Sbrian /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 8028679Sbrian /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 8128679Sbrian /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 8228679Sbrian /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 8328679Sbrian /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 8428679Sbrian /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 8528679Sbrian /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 8628679Sbrian /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 8728679Sbrian /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 8828679Sbrian /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 8928679Sbrian /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 9028679Sbrian /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 9128679Sbrian /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 9228679Sbrian /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 9328679Sbrian /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 9428679Sbrian /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 9528679Sbrian /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 9628679Sbrian /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 9728679Sbrian /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 986059Samurai}; 996059Samurai 1006059Samuraivoid 10136285Sbrianhdlc_Init(struct hdlc *hdlc, struct lcp *lcp) 1026059Samurai{ 10336285Sbrian memset(hdlc, '\0', sizeof(struct hdlc)); 10436285Sbrian hdlc->lqm.owner = lcp; 1056059Samurai} 1066059Samurai 1076059Samurai/* 1086059Samurai * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 1096059Samurai * 2.27 for further details. 1106059Samurai */ 11128679Sbrianinline u_short 11236285Sbrianhdlc_Fcs(u_short fcs, u_char * cp, int len) 1136059Samurai{ 1146059Samurai while (len--) 1156059Samurai fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 11628679Sbrian return (fcs); 1176059Samurai} 1186059Samurai 11932663Sbrianstatic inline u_short 12031514SbrianHdlcFcsBuf(u_short fcs, struct mbuf *m) 12131514Sbrian{ 12231514Sbrian int len; 12331514Sbrian u_char *pos, *end; 12431514Sbrian 12536285Sbrian len = mbuf_Length(m); 12631514Sbrian pos = MBUF_CTOP(m); 12731514Sbrian end = pos + m->cnt; 12831514Sbrian while (len--) { 12931514Sbrian fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff]; 13031514Sbrian if (pos == end && len) { 13131514Sbrian m = m->next; 13231514Sbrian pos = MBUF_CTOP(m); 13331514Sbrian end = pos + m->cnt; 13431514Sbrian } 13531514Sbrian } 13631514Sbrian return (fcs); 13731514Sbrian} 13831514Sbrian 1396059Samuraivoid 14036285Sbrianhdlc_Output(struct link *l, int pri, u_short proto, struct mbuf *bp) 1416059Samurai{ 14236285Sbrian struct physical *p = link2physical(l); 1436059Samurai struct mbuf *mhp, *mfcs; 1446059Samurai u_char *cp; 1456059Samurai u_short fcs; 1466059Samurai 14736285Sbrian if (!p || physical_IsSync(p)) 14832663Sbrian mfcs = NULL; 1496735Samurai else 15036285Sbrian mfcs = mbuf_Alloc(2, MB_HDLCOUT); 15131514Sbrian 15236285Sbrian mhp = mbuf_Alloc(4, MB_HDLCOUT); 1536059Samurai mhp->cnt = 0; 1546059Samurai cp = MBUF_CTOP(mhp); 15536285Sbrian if (p && (proto == PROTO_LCP || l->lcp.his_acfcomp == 0)) { 1566059Samurai *cp++ = HDLC_ADDR; 1576059Samurai *cp++ = HDLC_UI; 1586059Samurai mhp->cnt += 2; 1596059Samurai } 16028679Sbrian 1616059Samurai /* 16228679Sbrian * If possible, compress protocol field. 1636059Samurai */ 16436285Sbrian if (l->lcp.his_protocomp && (proto & 0xff00) == 0) { 1656059Samurai *cp++ = proto; 1666059Samurai mhp->cnt++; 1676059Samurai } else { 1686059Samurai *cp++ = proto >> 8; 1696059Samurai *cp = proto & 0377; 1706059Samurai mhp->cnt += 2; 1716059Samurai } 17236285Sbrian 1736059Samurai mhp->next = bp; 17436285Sbrian 17536285Sbrian if (!p) { 17636285Sbrian /* 17736285Sbrian * This is where we multiplex the data over our available physical 17836285Sbrian * links. We don't frame our logical link data. Instead we wait 17936285Sbrian * for the logical link implementation to chop our data up and pile 18036285Sbrian * it into the physical links by re-calling this function with the 18136285Sbrian * encapsulated fragments. 18236285Sbrian */ 18336285Sbrian link_Output(l, pri, mhp); 18436285Sbrian return; 18536285Sbrian } 18636285Sbrian 18736285Sbrian /* Tack mfcs onto the end, then set bp back to the start of the data */ 18831514Sbrian while (bp->next != NULL) 18931514Sbrian bp = bp->next; 1906059Samurai bp->next = mfcs; 19131514Sbrian bp = mhp->next; 1926059Samurai 19336285Sbrian p->hdlc.lqm.OutOctets += mbuf_Length(mhp) + 1; 19436285Sbrian p->hdlc.lqm.OutPackets++; 1956059Samurai 1966059Samurai if (proto == PROTO_LQR) { 19736285Sbrian /* Overwrite the entire packet */ 19836285Sbrian struct lqrdata lqr; 19936285Sbrian 20036285Sbrian lqr.MagicNumber = p->link.lcp.want_magic; 20136285Sbrian lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs; 20236285Sbrian lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets; 20336285Sbrian lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets; 20436285Sbrian lqr.PeerInLQRs = p->hdlc.lqm.lqr.SaveInLQRs; 20536285Sbrian lqr.PeerInPackets = p->hdlc.lqm.SaveInPackets; 20636285Sbrian lqr.PeerInDiscards = p->hdlc.lqm.SaveInDiscards; 20736285Sbrian lqr.PeerInErrors = p->hdlc.lqm.SaveInErrors; 20836285Sbrian lqr.PeerInOctets = p->hdlc.lqm.SaveInOctets; 20936285Sbrian lqr.PeerOutPackets = p->hdlc.lqm.OutPackets; 21036285Sbrian lqr.PeerOutOctets = p->hdlc.lqm.OutOctets; 21136285Sbrian if (p->hdlc.lqm.lqr.peer.LastOutLQRs == p->hdlc.lqm.lqr.OutLQRs) { 21234494Sbrian /* 21334494Sbrian * only increment if it's the first time or we've got a reply 21434494Sbrian * from the last one 21534494Sbrian */ 21636285Sbrian lqr.PeerOutLQRs = ++p->hdlc.lqm.lqr.OutLQRs; 21736285Sbrian lqr_Dump(l->name, "Output", &lqr); 21834494Sbrian } else { 21936285Sbrian lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs; 22036285Sbrian lqr_Dump(l->name, "Output (again)", &lqr); 22134494Sbrian } 22236285Sbrian lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp)); 2236059Samurai } 22436285Sbrian 22536285Sbrian if (mfcs) { 22632381Sbrian mfcs->cnt = 0; 22732381Sbrian fcs = HdlcFcsBuf(INITFCS, mhp); 2286735Samurai fcs = ~fcs; 2296735Samurai cp = MBUF_CTOP(mfcs); 23028679Sbrian *cp++ = fcs & 0377; /* Low byte first!! */ 2316735Samurai *cp++ = fcs >> 8; 23232381Sbrian mfcs->cnt = 2; 2336735Samurai } 23431514Sbrian 23536285Sbrian log_DumpBp(LogHDLC, "hdlc_Output", mhp); 23631514Sbrian 23736285Sbrian link_ProtocolRecord(l, proto, PROTO_OUT); 23836285Sbrian log_Printf(LogDEBUG, "hdlc_Output: proto = 0x%04x\n", proto); 23936285Sbrian 24036285Sbrian if (physical_IsSync(p)) 24136285Sbrian link_Output(l, pri, mhp); /* Send it raw */ 2426735Samurai else 24336285Sbrian async_Output(pri, mhp, proto, p); 2446059Samurai} 2456059Samurai 24631537Sbrian/* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */ 24731537Sbrianstatic struct { 24831537Sbrian u_short from; 24931537Sbrian u_short to; 25031537Sbrian const char *name; 25131537Sbrian} protocols[] = { 25231537Sbrian { 0x0001, 0x0001, "Padding Protocol" }, 25331537Sbrian { 0x0003, 0x001f, "reserved (transparency inefficient)" }, 25431537Sbrian { 0x0021, 0x0021, "Internet Protocol" }, 25531537Sbrian { 0x0023, 0x0023, "OSI Network Layer" }, 25631537Sbrian { 0x0025, 0x0025, "Xerox NS IDP" }, 25731537Sbrian { 0x0027, 0x0027, "DECnet Phase IV" }, 25831537Sbrian { 0x0029, 0x0029, "Appletalk" }, 25931537Sbrian { 0x002b, 0x002b, "Novell IPX" }, 26031537Sbrian { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" }, 26131537Sbrian { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" }, 26231537Sbrian { 0x0031, 0x0031, "Bridging PDU" }, 26331537Sbrian { 0x0033, 0x0033, "Stream Protocol (ST-II)" }, 26431537Sbrian { 0x0035, 0x0035, "Banyan Vines" }, 26531537Sbrian { 0x0037, 0x0037, "reserved (until 1993)" }, 26631537Sbrian { 0x0039, 0x0039, "AppleTalk EDDP" }, 26731537Sbrian { 0x003b, 0x003b, "AppleTalk SmartBuffered" }, 26831537Sbrian { 0x003d, 0x003d, "Multi-Link" }, 26931537Sbrian { 0x003f, 0x003f, "NETBIOS Framing" }, 27031537Sbrian { 0x0041, 0x0041, "Cisco Systems" }, 27131537Sbrian { 0x0043, 0x0043, "Ascom Timeplex" }, 27231537Sbrian { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" }, 27331537Sbrian { 0x0047, 0x0047, "DCA Remote Lan" }, 27431537Sbrian { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" }, 27531537Sbrian { 0x004b, 0x004b, "SNA over 802.2" }, 27631537Sbrian { 0x004d, 0x004d, "SNA" }, 27731537Sbrian { 0x004f, 0x004f, "IP6 Header Compression" }, 27831537Sbrian { 0x0051, 0x0051, "KNX Bridging Data" }, 27931537Sbrian { 0x0053, 0x0053, "Encryption" }, 28031537Sbrian { 0x0055, 0x0055, "Individual Link Encryption" }, 28131537Sbrian { 0x006f, 0x006f, "Stampede Bridging" }, 28231537Sbrian { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" }, 28331537Sbrian { 0x0073, 0x0073, "MP+ Protocol" }, 28431537Sbrian { 0x007d, 0x007d, "reserved (Control Escape)" }, 28531537Sbrian { 0x007f, 0x007f, "reserved (compression inefficient)" }, 28631537Sbrian { 0x00cf, 0x00cf, "reserved (PPP NLPID)" }, 28731537Sbrian { 0x00fb, 0x00fb, "compression on single link in multilink group" }, 28831537Sbrian { 0x00fd, 0x00fd, "1st choice compression" }, 28931537Sbrian { 0x00ff, 0x00ff, "reserved (compression inefficient)" }, 29031537Sbrian { 0x0200, 0x02ff, "(compression inefficient)" }, 29131537Sbrian { 0x0201, 0x0201, "802.1d Hello Packets" }, 29231537Sbrian { 0x0203, 0x0203, "IBM Source Routing BPDU" }, 29331537Sbrian { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" }, 29431537Sbrian { 0x0207, 0x0207, "Cisco Discovery Protocol" }, 29531537Sbrian { 0x0209, 0x0209, "Netcs Twin Routing" }, 29631537Sbrian { 0x0231, 0x0231, "Luxcom" }, 29731537Sbrian { 0x0233, 0x0233, "Sigma Network Systems" }, 29831537Sbrian { 0x0235, 0x0235, "Apple Client Server Protocol" }, 29931537Sbrian { 0x1e00, 0x1eff, "(compression inefficient)" }, 30031537Sbrian { 0x4001, 0x4001, "Cray Communications Control Protocol" }, 30131537Sbrian { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" }, 30231537Sbrian { 0x4021, 0x4021, "Stacker LZS" }, 30331537Sbrian { 0x8001, 0x801f, "Not Used - reserved" }, 30431537Sbrian { 0x8021, 0x8021, "Internet Protocol Control Protocol" }, 30531537Sbrian { 0x8023, 0x8023, "OSI Network Layer Control Protocol" }, 30631537Sbrian { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" }, 30731537Sbrian { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" }, 30831537Sbrian { 0x8029, 0x8029, "Appletalk Control Protocol" }, 30931537Sbrian { 0x802b, 0x802b, "Novell IPX Control Protocol" }, 31031537Sbrian { 0x802d, 0x802d, "reserved" }, 31131537Sbrian { 0x802f, 0x802f, "reserved" }, 31231537Sbrian { 0x8031, 0x8031, "Bridging NCP" }, 31331537Sbrian { 0x8033, 0x8033, "Stream Protocol Control Protocol" }, 31431537Sbrian { 0x8035, 0x8035, "Banyan Vines Control Protocol" }, 31531537Sbrian { 0x8037, 0x8037, "reserved till 1993" }, 31631537Sbrian { 0x8039, 0x8039, "reserved" }, 31731537Sbrian { 0x803b, 0x803b, "reserved" }, 31831537Sbrian { 0x803d, 0x803d, "Multi-Link Control Protocol" }, 31931537Sbrian { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" }, 32031537Sbrian { 0x8041, 0x8041, "Cisco Systems Control Protocol" }, 32131537Sbrian { 0x8043, 0x8043, "Ascom Timeplex" }, 32231537Sbrian { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" }, 32331537Sbrian { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" }, 32431537Sbrian { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" }, 32531537Sbrian { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" }, 32631537Sbrian { 0x804d, 0x804d, "SNA Control Protocol" }, 32731537Sbrian { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" }, 32831537Sbrian { 0x8051, 0x8051, "KNX Bridging Control Protocol" }, 32931537Sbrian { 0x8053, 0x8053, "Encryption Control Protocol" }, 33031537Sbrian { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" }, 33131537Sbrian { 0x806f, 0x806f, "Stampede Bridging Control Protocol" }, 33231537Sbrian { 0x8073, 0x8073, "MP+ Control Protocol" }, 33331537Sbrian { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" }, 33431537Sbrian { 0x807d, 0x807d, "Not Used - reserved" }, 33531537Sbrian { 0x80cf, 0x80cf, "Not Used - reserved" }, 33631537Sbrian { 0x80fb, 0x80fb, "compression on single link in multilink group control" }, 33731537Sbrian { 0x80fd, 0x80fd, "Compression Control Protocol" }, 33831537Sbrian { 0x80ff, 0x80ff, "Not Used - reserved" }, 33931537Sbrian { 0x8207, 0x8207, "Cisco Discovery Protocol Control" }, 34031537Sbrian { 0x8209, 0x8209, "Netcs Twin Routing" }, 34131537Sbrian { 0x8235, 0x8235, "Apple Client Server Protocol Control" }, 34231537Sbrian { 0xc021, 0xc021, "Link Control Protocol" }, 34331537Sbrian { 0xc023, 0xc023, "Password Authentication Protocol" }, 34431537Sbrian { 0xc025, 0xc025, "Link Quality Report" }, 34531537Sbrian { 0xc027, 0xc027, "Shiva Password Authentication Protocol" }, 34631537Sbrian { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" }, 34731537Sbrian { 0xc081, 0xc081, "Container Control Protocol" }, 34831537Sbrian { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" }, 34931537Sbrian { 0xc225, 0xc225, "RSA Authentication Protocol" }, 35031537Sbrian { 0xc227, 0xc227, "Extensible Authentication Protocol" }, 35131537Sbrian { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" }, 35231537Sbrian { 0xc281, 0xc281, "Proprietary Authentication Protocol" }, 35331537Sbrian { 0xc283, 0xc283, "Proprietary Authentication Protocol" }, 35431537Sbrian { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" } 35531537Sbrian}; 35631537Sbrian 35731962Sbrian#define NPROTOCOLS (sizeof protocols/sizeof protocols[0]) 35831537Sbrian 35936285Sbrianconst char * 36036285Sbrianhdlc_Protocol2Nam(u_short proto) 36131537Sbrian{ 36231537Sbrian int f; 36331537Sbrian 36431537Sbrian for (f = 0; f < NPROTOCOLS; f++) 36531537Sbrian if (proto >= protocols[f].from && proto <= protocols[f].to) 36631537Sbrian return protocols[f].name; 36731537Sbrian else if (proto < protocols[f].from) 36831537Sbrian break; 36931537Sbrian return "unrecognised protocol"; 37031537Sbrian} 37131537Sbrian 37236285Sbrianvoid 37336285Sbrianhdlc_DecodePacket(struct bundle *bundle, u_short proto, struct mbuf * bp, 37436285Sbrian struct link *l) 3756059Samurai{ 37636285Sbrian struct physical *p = link2physical(l); 3779448Samurai u_char *cp; 3789448Samurai 37936285Sbrian log_Printf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto); 38026516Sbrian 38136285Sbrian /* decompress everything. CCP needs uncompressed data too */ 38236285Sbrian if ((bp = ccp_Decompress(&l->ccp, &proto, bp)) == NULL) 38336285Sbrian return; 38431514Sbrian 3856059Samurai switch (proto) { 3866059Samurai case PROTO_LCP: 38736285Sbrian lcp_Input(&l->lcp, bp); 3886059Samurai break; 3896059Samurai case PROTO_PAP: 39036285Sbrian if (p) 39136285Sbrian pap_Input(bundle, bp, p); 39236285Sbrian else { 39336285Sbrian log_Printf(LogERROR, "DecodePacket: PAP: Not a physical link !\n"); 39436285Sbrian mbuf_Free(bp); 39536285Sbrian } 3966059Samurai break; 3976059Samurai case PROTO_LQR: 39836285Sbrian if (p) { 39936285Sbrian p->hdlc.lqm.lqr.SaveInLQRs++; 40036285Sbrian lqr_Input(p, bp); 40136285Sbrian } else { 40236285Sbrian log_Printf(LogERROR, "DecodePacket: LQR: Not a physical link !\n"); 40336285Sbrian mbuf_Free(bp); 40436285Sbrian } 4056059Samurai break; 4066059Samurai case PROTO_CHAP: 40736285Sbrian if (p) 40836285Sbrian chap_Input(bundle, bp, p); 40936285Sbrian else { 41036285Sbrian log_Printf(LogERROR, "DecodePacket: CHAP: Not a physical link !\n"); 41136285Sbrian mbuf_Free(bp); 41236285Sbrian } 4136059Samurai break; 4146059Samurai case PROTO_VJUNCOMP: 4156059Samurai case PROTO_VJCOMP: 41636285Sbrian bp = vj_Input(&bundle->ncp.ipcp, bp, proto); 41736285Sbrian if (bp == NULL) 4186735Samurai break; 4196059Samurai /* fall down */ 4206059Samurai case PROTO_IP: 42136285Sbrian ip_Input(bundle, bp); 4226059Samurai break; 4236059Samurai case PROTO_IPCP: 42436285Sbrian ipcp_Input(&bundle->ncp.ipcp, bundle, bp); 4256059Samurai break; 4266059Samurai case PROTO_CCP: 42736285Sbrian ccp_Input(&l->ccp, bundle, bp); 4286059Samurai break; 42936285Sbrian case PROTO_MP: 43036285Sbrian if (bundle->ncp.mp.active) { 43136285Sbrian if (p) 43236285Sbrian mp_Input(&bundle->ncp.mp, bp, p); 43336285Sbrian else { 43437019Sbrian log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n"); 43536285Sbrian mbuf_Free(bp); 43636285Sbrian } 43736285Sbrian break; 43836285Sbrian } 43936285Sbrian /* Fall through */ 4406059Samurai default: 44136285Sbrian log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n", 44236285Sbrian proto == PROTO_MP ? "Unexpected" : "Unknown", 44336285Sbrian proto, hdlc_Protocol2Nam(proto)); 4449448Samurai bp->offset -= 2; 4459448Samurai bp->cnt += 2; 4469448Samurai cp = MBUF_CTOP(bp); 44736285Sbrian lcp_SendProtoRej(&l->lcp, cp, bp->cnt); 44836285Sbrian if (p) { 44936285Sbrian p->hdlc.lqm.SaveInDiscards++; 45036285Sbrian p->hdlc.stats.unknownproto++; 45136285Sbrian } 45236285Sbrian mbuf_Free(bp); 4536059Samurai break; 4546059Samurai } 4556059Samurai} 4566059Samurai 45736285Sbrianstatic int 45836285Sbrianhdlc_GetProto(const u_char *cp, u_short *proto) 4596059Samurai{ 46036285Sbrian *proto = *cp; 46136285Sbrian if (!(*proto & 1)) { 46236285Sbrian *proto = (*proto << 8) | cp[1]; 46336285Sbrian return 2; 46426516Sbrian } 46536285Sbrian return 1; 4666059Samurai} 4676059Samurai 4686059Samuraivoid 46936285Sbrianhdlc_Input(struct bundle *bundle, struct mbuf * bp, struct physical *physical) 4706059Samurai{ 4716059Samurai u_short fcs, proto; 4726059Samurai u_char *cp, addr, ctrl; 47336285Sbrian int n; 4746059Samurai 47536285Sbrian log_DumpBp(LogHDLC, "hdlc_Input:", bp); 47636285Sbrian if (physical_IsSync(physical)) 4776735Samurai fcs = GOODFCS; 4786735Samurai else 47936285Sbrian fcs = hdlc_Fcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 48036285Sbrian physical->hdlc.lqm.SaveInOctets += bp->cnt + 1; 4816059Samurai 48236285Sbrian log_Printf(LogDEBUG, "%s: hdlc_Input: fcs = %04x (%s)\n", 48336285Sbrian physical->link.name, fcs, (fcs == GOODFCS) ? "good" : "BAD!"); 4846059Samurai if (fcs != GOODFCS) { 48536285Sbrian physical->hdlc.lqm.SaveInErrors++; 48636285Sbrian physical->hdlc.stats.badfcs++; 48736285Sbrian mbuf_Free(bp); 4886059Samurai return; 4896059Samurai } 49036285Sbrian if (!physical_IsSync(physical)) 4916735Samurai bp->cnt -= 2; /* discard FCS part */ 49225019Sphk 49325019Sphk if (bp->cnt < 2) { /* XXX: raise this bar ? */ 49436285Sbrian mbuf_Free(bp); 49528679Sbrian return; 49625019Sphk } 4976059Samurai cp = MBUF_CTOP(bp); 4986059Samurai 49936285Sbrian if (!physical->link.lcp.want_acfcomp) { 50036285Sbrian /* We expect the packet not to be compressed */ 5016059Samurai addr = *cp++; 5026059Samurai if (addr != HDLC_ADDR) { 50336285Sbrian physical->hdlc.lqm.SaveInErrors++; 50436285Sbrian physical->hdlc.stats.badaddr++; 50536285Sbrian log_Printf(LogDEBUG, "hdlc_Input: addr %02x\n", *cp); 50636285Sbrian mbuf_Free(bp); 5076059Samurai return; 5086059Samurai } 5096059Samurai ctrl = *cp++; 5106059Samurai if (ctrl != HDLC_UI) { 51136285Sbrian physical->hdlc.lqm.SaveInErrors++; 51236285Sbrian physical->hdlc.stats.badcommand++; 51336285Sbrian log_Printf(LogDEBUG, "hdlc_Input: %02x\n", *cp); 51436285Sbrian mbuf_Free(bp); 5156059Samurai return; 5166059Samurai } 5176059Samurai bp->offset += 2; 5186059Samurai bp->cnt -= 2; 5196059Samurai } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 5206059Samurai /* 52136285Sbrian * We can receive compressed packets, but the peer still sends 52236285Sbrian * uncompressed packets ! 5236059Samurai */ 5246059Samurai cp += 2; 5256059Samurai bp->offset += 2; 5266059Samurai bp->cnt -= 2; 5276059Samurai } 5286059Samurai 52936285Sbrian n = hdlc_GetProto(cp, &proto); 53036285Sbrian bp->offset += n; 53136285Sbrian bp->cnt -= n; 53236285Sbrian if (!physical->link.lcp.want_protocomp && n == 1) 53336285Sbrian log_Printf(LogHDLC, "%s: Warning: received a proto-compressed packet !\n", 53436285Sbrian physical->link.name); 53536285Sbrian 53636285Sbrian link_ProtocolRecord(&physical->link, proto, PROTO_IN); 53736285Sbrian physical->hdlc.lqm.SaveInPackets++; 53836285Sbrian 53936285Sbrian hdlc_DecodePacket(bundle, proto, bp, &physical->link); 54036285Sbrian} 54136285Sbrian 54236285Sbrian/* 54336285Sbrian * Detect a HDLC frame 54436285Sbrian */ 54536285Sbrian 54636285Sbrianstatic const char *FrameHeaders[] = { 54736285Sbrian "\176\377\003\300\041", 54836285Sbrian "\176\377\175\043\300\041", 54936285Sbrian "\176\177\175\043\100\041", 55036285Sbrian "\176\175\337\175\043\300\041", 55136285Sbrian "\176\175\137\175\043\100\041", 55236285Sbrian NULL, 55336285Sbrian}; 55436285Sbrian 55536285Sbrianu_char * 55636285Sbrianhdlc_Detect(struct physical *physical, u_char *cp, int n) 55736285Sbrian{ 55836285Sbrian const char *fp, **hp; 55936285Sbrian char *ptr; 56036285Sbrian 56136285Sbrian cp[n] = '\0'; /* be sure to null terminate */ 56236285Sbrian ptr = NULL; 56336285Sbrian for (hp = FrameHeaders; *hp; hp++) { 56436285Sbrian fp = *hp; 56536285Sbrian if (physical_IsSync(physical)) 56636285Sbrian fp++; 56736285Sbrian ptr = strstr((char *)cp, fp); /* XXX: cp may have embedded NULs */ 56836285Sbrian if (ptr) 5696059Samurai break; 57036285Sbrian } 57136285Sbrian return (u_char *)ptr; 57236285Sbrian} 5736059Samurai 57436285Sbrianint 57536285Sbrianhdlc_ReportStatus(struct cmdargs const *arg) 57636285Sbrian{ 57736285Sbrian struct hdlc *hdlc = &arg->cx->physical->hdlc; 57836285Sbrian 57936285Sbrian prompt_Printf(arg->prompt, "%s HDLC level errors:\n", arg->cx->name); 58036285Sbrian prompt_Printf(arg->prompt, " Bad Frame Check Sequence fields: %u\n", 58136285Sbrian hdlc->stats.badfcs); 58236285Sbrian prompt_Printf(arg->prompt, " Bad address (!= 0x%02x) fields: %u\n", 58336285Sbrian HDLC_ADDR, hdlc->stats.badaddr); 58436285Sbrian prompt_Printf(arg->prompt, " Bad command (!= 0x%02x) fields: %u\n", 58536285Sbrian HDLC_UI, hdlc->stats.badcommand); 58636285Sbrian prompt_Printf(arg->prompt, " Unrecognised protocol fields: %u\n", 58736285Sbrian hdlc->stats.unknownproto); 58836285Sbrian return 0; 5896059Samurai} 59036285Sbrian 59136285Sbrianstatic void 59236285Sbrianhdlc_ReportTime(void *v) 59336285Sbrian{ 59436285Sbrian /* Moan about HDLC errors */ 59536285Sbrian struct hdlc *hdlc = (struct hdlc *)v; 59636285Sbrian 59736285Sbrian timer_Stop(&hdlc->ReportTimer); 59836285Sbrian 59936285Sbrian if (memcmp(&hdlc->laststats, &hdlc->stats, sizeof hdlc->stats)) { 60036285Sbrian log_Printf(LogPHASE, 60136285Sbrian "%s: HDLC errors -> FCS: %u, ADDR: %u, COMD: %u, PROTO: %u\n", 60236285Sbrian hdlc->lqm.owner->fsm.link->name, 60336285Sbrian hdlc->stats.badfcs - hdlc->laststats.badfcs, 60436285Sbrian hdlc->stats.badaddr - hdlc->laststats.badaddr, 60536285Sbrian hdlc->stats.badcommand - hdlc->laststats.badcommand, 60636285Sbrian hdlc->stats.unknownproto - hdlc->laststats.unknownproto); 60736285Sbrian hdlc->laststats = hdlc->stats; 60836285Sbrian } 60936285Sbrian 61036285Sbrian timer_Start(&hdlc->ReportTimer); 61136285Sbrian} 61236285Sbrian 61336285Sbrianvoid 61436285Sbrianhdlc_StartTimer(struct hdlc *hdlc) 61536285Sbrian{ 61636285Sbrian timer_Stop(&hdlc->ReportTimer); 61736285Sbrian hdlc->ReportTimer.load = 60 * SECTICKS; 61836285Sbrian hdlc->ReportTimer.arg = hdlc; 61936285Sbrian hdlc->ReportTimer.func = hdlc_ReportTime; 62036285Sbrian hdlc->ReportTimer.name = "hdlc"; 62136285Sbrian timer_Start(&hdlc->ReportTimer); 62236285Sbrian} 62336285Sbrian 62436285Sbrianvoid 62536285Sbrianhdlc_StopTimer(struct hdlc *hdlc) 62636285Sbrian{ 62736285Sbrian timer_Stop(&hdlc->ReportTimer); 62836285Sbrian} 629