xref: /freebsd/usr.sbin/ppp/hdlc.c (revision 63b7346316afa0c20b6cf22506bcc53abc9f08f8)
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  *
2063b73463SBrian Somers  * $Id: hdlc.c,v 1.28 1998/01/21 02:15:15 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"
391ae349f5Scvs2svn #include "ipcp.h"
401ae349f5Scvs2svn #include "ip.h"
411ae349f5Scvs2svn #include "vjcomp.h"
421ae349f5Scvs2svn #include "pap.h"
431ae349f5Scvs2svn #include "chap.h"
441ae349f5Scvs2svn #include "lcp.h"
451ae349f5Scvs2svn #include "async.h"
461ae349f5Scvs2svn #include "lqr.h"
471ae349f5Scvs2svn #include "loadalias.h"
481ae349f5Scvs2svn #include "vars.h"
491ae349f5Scvs2svn #include "modem.h"
501ae349f5Scvs2svn #include "ccp.h"
5163b73463SBrian Somers #include "physical.h"
521ae349f5Scvs2svn 
531ae349f5Scvs2svn static struct hdlcstat {
541ae349f5Scvs2svn   int badfcs;
551ae349f5Scvs2svn   int badaddr;
561ae349f5Scvs2svn   int badcommand;
571ae349f5Scvs2svn   int unknownproto;
581ae349f5Scvs2svn }        HdlcStat;
591ae349f5Scvs2svn 
601ae349f5Scvs2svn static int ifOutPackets;
611ae349f5Scvs2svn static int ifOutOctets;
621ae349f5Scvs2svn static int ifOutLQRs;
631ae349f5Scvs2svn 
641ae349f5Scvs2svn static struct protostat {
651ae349f5Scvs2svn   u_short number;
661ae349f5Scvs2svn   const char *name;
671ae349f5Scvs2svn   u_long in_count;
681ae349f5Scvs2svn   u_long out_count;
691ae349f5Scvs2svn } ProtocolStat[] = {
701ae349f5Scvs2svn   { PROTO_IP, "IP" },
711ae349f5Scvs2svn   { PROTO_VJUNCOMP, "VJ_UNCOMP" },
721ae349f5Scvs2svn   { PROTO_VJCOMP, "VJ_COMP" },
731ae349f5Scvs2svn   { PROTO_COMPD, "COMPD" },
741ae349f5Scvs2svn   { PROTO_LCP, "LCP" },
751ae349f5Scvs2svn   { PROTO_IPCP, "IPCP" },
761ae349f5Scvs2svn   { PROTO_CCP, "CCP" },
771ae349f5Scvs2svn   { PROTO_PAP, "PAP" },
781ae349f5Scvs2svn   { PROTO_LQR, "LQR" },
791ae349f5Scvs2svn   { PROTO_CHAP, "CHAP" },
801ae349f5Scvs2svn   { 0, "Others" }
811ae349f5Scvs2svn };
821ae349f5Scvs2svn 
831ae349f5Scvs2svn static u_short const fcstab[256] = {
841ae349f5Scvs2svn    /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
851ae349f5Scvs2svn    /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
861ae349f5Scvs2svn    /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
871ae349f5Scvs2svn    /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
881ae349f5Scvs2svn    /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
891ae349f5Scvs2svn    /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
901ae349f5Scvs2svn    /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
911ae349f5Scvs2svn    /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
921ae349f5Scvs2svn    /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
931ae349f5Scvs2svn    /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
941ae349f5Scvs2svn    /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
951ae349f5Scvs2svn    /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
961ae349f5Scvs2svn    /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
971ae349f5Scvs2svn    /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
981ae349f5Scvs2svn    /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
991ae349f5Scvs2svn    /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
1001ae349f5Scvs2svn    /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
1011ae349f5Scvs2svn    /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
1021ae349f5Scvs2svn    /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
1031ae349f5Scvs2svn    /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
1041ae349f5Scvs2svn    /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
1051ae349f5Scvs2svn    /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
1061ae349f5Scvs2svn    /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
1071ae349f5Scvs2svn    /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
1081ae349f5Scvs2svn    /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
1091ae349f5Scvs2svn    /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
1101ae349f5Scvs2svn    /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
1111ae349f5Scvs2svn    /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
1121ae349f5Scvs2svn    /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
1131ae349f5Scvs2svn    /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
1141ae349f5Scvs2svn    /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
1151ae349f5Scvs2svn    /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
1161ae349f5Scvs2svn };
1171ae349f5Scvs2svn 
1181ae349f5Scvs2svn u_char EscMap[33];
1191ae349f5Scvs2svn 
1201ae349f5Scvs2svn void
1211ae349f5Scvs2svn HdlcInit()
1221ae349f5Scvs2svn {
1231ae349f5Scvs2svn   ifOutOctets = 0;
1241ae349f5Scvs2svn   ifOutPackets = 0;
1251ae349f5Scvs2svn   ifOutLQRs = 0;
1261ae349f5Scvs2svn }
1271ae349f5Scvs2svn 
1281ae349f5Scvs2svn /*
1291ae349f5Scvs2svn  *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1301ae349f5Scvs2svn  *  2.27 for further details.
1311ae349f5Scvs2svn  */
1321ae349f5Scvs2svn inline u_short
1331ae349f5Scvs2svn HdlcFcs(u_short fcs, u_char * cp, int len)
1341ae349f5Scvs2svn {
1351ae349f5Scvs2svn   while (len--)
1361ae349f5Scvs2svn     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
1371ae349f5Scvs2svn   return (fcs);
1381ae349f5Scvs2svn }
1391ae349f5Scvs2svn 
1401ae349f5Scvs2svn static inline u_short
1411ae349f5Scvs2svn HdlcFcsBuf(u_short fcs, struct mbuf *m)
1421ae349f5Scvs2svn {
1431ae349f5Scvs2svn   int len;
1441ae349f5Scvs2svn   u_char *pos, *end;
1451ae349f5Scvs2svn 
1461ae349f5Scvs2svn   len = plength(m);
1471ae349f5Scvs2svn   pos = MBUF_CTOP(m);
1481ae349f5Scvs2svn   end = pos + m->cnt;
1491ae349f5Scvs2svn   while (len--) {
1501ae349f5Scvs2svn     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
1511ae349f5Scvs2svn     if (pos == end && len) {
1521ae349f5Scvs2svn       m = m->next;
1531ae349f5Scvs2svn       pos = MBUF_CTOP(m);
1541ae349f5Scvs2svn       end = pos + m->cnt;
1551ae349f5Scvs2svn     }
1561ae349f5Scvs2svn   }
1571ae349f5Scvs2svn   return (fcs);
1581ae349f5Scvs2svn }
1591ae349f5Scvs2svn 
1601ae349f5Scvs2svn void
16163b73463SBrian Somers HdlcOutput(struct physical *physical,
16263b73463SBrian Somers 		   int pri, u_short proto, struct mbuf * bp)
1631ae349f5Scvs2svn {
1641ae349f5Scvs2svn   struct mbuf *mhp, *mfcs;
1651ae349f5Scvs2svn   struct protostat *statp;
1661ae349f5Scvs2svn   struct lqrdata *lqr;
1671ae349f5Scvs2svn   u_char *cp;
1681ae349f5Scvs2svn   u_short fcs;
1691ae349f5Scvs2svn 
1701ae349f5Scvs2svn   if ((proto & 0xfff1) == 0x21)		/* Network Layer protocol */
1711ae349f5Scvs2svn     if (CcpFsm.state == ST_OPENED)
17263b73463SBrian Somers       if (CcpOutput(physical, pri, proto, bp))
1731ae349f5Scvs2svn         return;
1741ae349f5Scvs2svn 
17563b73463SBrian Somers   if (Physical_IsSync(physical))
1761ae349f5Scvs2svn     mfcs = NULL;
1771ae349f5Scvs2svn   else
1781ae349f5Scvs2svn     mfcs = mballoc(2, MB_HDLCOUT);
1791ae349f5Scvs2svn 
1801ae349f5Scvs2svn   mhp = mballoc(4, MB_HDLCOUT);
1811ae349f5Scvs2svn   mhp->cnt = 0;
1821ae349f5Scvs2svn   cp = MBUF_CTOP(mhp);
1831ae349f5Scvs2svn   if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) {
1841ae349f5Scvs2svn     *cp++ = HDLC_ADDR;
1851ae349f5Scvs2svn     *cp++ = HDLC_UI;
1861ae349f5Scvs2svn     mhp->cnt += 2;
1871ae349f5Scvs2svn   }
1881ae349f5Scvs2svn 
1891ae349f5Scvs2svn   /*
1901ae349f5Scvs2svn    * If possible, compress protocol field.
1911ae349f5Scvs2svn    */
1921ae349f5Scvs2svn   if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) {
1931ae349f5Scvs2svn     *cp++ = proto;
1941ae349f5Scvs2svn     mhp->cnt++;
1951ae349f5Scvs2svn   } else {
1961ae349f5Scvs2svn     *cp++ = proto >> 8;
1971ae349f5Scvs2svn     *cp = proto & 0377;
1981ae349f5Scvs2svn     mhp->cnt += 2;
1991ae349f5Scvs2svn   }
2001ae349f5Scvs2svn   mhp->next = bp;
2011ae349f5Scvs2svn   while (bp->next != NULL)
2021ae349f5Scvs2svn     bp = bp->next;
2031ae349f5Scvs2svn   bp->next = mfcs;
2041ae349f5Scvs2svn   bp = mhp->next;
2051ae349f5Scvs2svn 
2061ae349f5Scvs2svn   lqr = &MyLqrData;
2071ae349f5Scvs2svn   lqr->PeerOutPackets = ifOutPackets++;
2081ae349f5Scvs2svn   ifOutOctets += plength(mhp) + 1;
2091ae349f5Scvs2svn   lqr->PeerOutOctets = ifOutOctets;
2101ae349f5Scvs2svn 
2111ae349f5Scvs2svn   if (proto == PROTO_LQR) {
2121ae349f5Scvs2svn     lqr->MagicNumber = LcpInfo.want_magic;
2131ae349f5Scvs2svn     lqr->LastOutLQRs = HisLqrData.PeerOutLQRs;
2141ae349f5Scvs2svn     lqr->LastOutPackets = HisLqrData.PeerOutPackets;
2151ae349f5Scvs2svn     lqr->LastOutOctets = HisLqrData.PeerOutOctets;
2161ae349f5Scvs2svn     lqr->PeerInLQRs = HisLqrSave.SaveInLQRs;
2171ae349f5Scvs2svn     lqr->PeerInPackets = HisLqrSave.SaveInPackets;
2181ae349f5Scvs2svn     lqr->PeerInDiscards = HisLqrSave.SaveInDiscards;
2191ae349f5Scvs2svn     lqr->PeerInErrors = HisLqrSave.SaveInErrors;
2201ae349f5Scvs2svn     lqr->PeerInOctets = HisLqrSave.SaveInOctets;
2211ae349f5Scvs2svn     lqr->PeerOutLQRs = ++ifOutLQRs;
2221ae349f5Scvs2svn     LqrDump("LqrOutput", lqr);
2231ae349f5Scvs2svn     LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp)));
2241ae349f5Scvs2svn   }
22563b73463SBrian Somers   if (!Physical_IsSync(physical)) {
2261ae349f5Scvs2svn     mfcs->cnt = 0;
2271ae349f5Scvs2svn     fcs = HdlcFcsBuf(INITFCS, mhp);
2281ae349f5Scvs2svn     fcs = ~fcs;
2291ae349f5Scvs2svn     cp = MBUF_CTOP(mfcs);
2301ae349f5Scvs2svn     *cp++ = fcs & 0377;		/* Low byte first!! */
2311ae349f5Scvs2svn     *cp++ = fcs >> 8;
2321ae349f5Scvs2svn     mfcs->cnt = 2;
2331ae349f5Scvs2svn   }
2341ae349f5Scvs2svn   LogDumpBp(LogHDLC, "HdlcOutput", mhp);
2351ae349f5Scvs2svn   for (statp = ProtocolStat; statp->number; statp++)
2361ae349f5Scvs2svn     if (statp->number == proto)
2371ae349f5Scvs2svn       break;
2381ae349f5Scvs2svn   statp->out_count++;
2391ae349f5Scvs2svn 
2401ae349f5Scvs2svn   LogPrintf(LogDEBUG, "HdlcOutput: proto = 0x%04x\n", proto);
2411ae349f5Scvs2svn 
24263b73463SBrian Somers   if (Physical_IsSync(physical))
24363b73463SBrian Somers     ModemOutput(physical, pri, mhp);
2441ae349f5Scvs2svn   else
24563b73463SBrian Somers     AsyncOutput(pri, mhp, proto, physical);
2461ae349f5Scvs2svn }
2471ae349f5Scvs2svn 
2481ae349f5Scvs2svn /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
2491ae349f5Scvs2svn static struct {
2501ae349f5Scvs2svn   u_short from;
2511ae349f5Scvs2svn   u_short to;
2521ae349f5Scvs2svn   const char *name;
2531ae349f5Scvs2svn } protocols[] = {
2541ae349f5Scvs2svn   { 0x0001, 0x0001, "Padding Protocol" },
2551ae349f5Scvs2svn   { 0x0003, 0x001f, "reserved (transparency inefficient)" },
2561ae349f5Scvs2svn   { 0x0021, 0x0021, "Internet Protocol" },
2571ae349f5Scvs2svn   { 0x0023, 0x0023, "OSI Network Layer" },
2581ae349f5Scvs2svn   { 0x0025, 0x0025, "Xerox NS IDP" },
2591ae349f5Scvs2svn   { 0x0027, 0x0027, "DECnet Phase IV" },
2601ae349f5Scvs2svn   { 0x0029, 0x0029, "Appletalk" },
2611ae349f5Scvs2svn   { 0x002b, 0x002b, "Novell IPX" },
2621ae349f5Scvs2svn   { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" },
2631ae349f5Scvs2svn   { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" },
2641ae349f5Scvs2svn   { 0x0031, 0x0031, "Bridging PDU" },
2651ae349f5Scvs2svn   { 0x0033, 0x0033, "Stream Protocol (ST-II)" },
2661ae349f5Scvs2svn   { 0x0035, 0x0035, "Banyan Vines" },
2671ae349f5Scvs2svn   { 0x0037, 0x0037, "reserved (until 1993)" },
2681ae349f5Scvs2svn   { 0x0039, 0x0039, "AppleTalk EDDP" },
2691ae349f5Scvs2svn   { 0x003b, 0x003b, "AppleTalk SmartBuffered" },
2701ae349f5Scvs2svn   { 0x003d, 0x003d, "Multi-Link" },
2711ae349f5Scvs2svn   { 0x003f, 0x003f, "NETBIOS Framing" },
2721ae349f5Scvs2svn   { 0x0041, 0x0041, "Cisco Systems" },
2731ae349f5Scvs2svn   { 0x0043, 0x0043, "Ascom Timeplex" },
2741ae349f5Scvs2svn   { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" },
2751ae349f5Scvs2svn   { 0x0047, 0x0047, "DCA Remote Lan" },
2761ae349f5Scvs2svn   { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" },
2771ae349f5Scvs2svn   { 0x004b, 0x004b, "SNA over 802.2" },
2781ae349f5Scvs2svn   { 0x004d, 0x004d, "SNA" },
2791ae349f5Scvs2svn   { 0x004f, 0x004f, "IP6 Header Compression" },
2801ae349f5Scvs2svn   { 0x0051, 0x0051, "KNX Bridging Data" },
2811ae349f5Scvs2svn   { 0x0053, 0x0053, "Encryption" },
2821ae349f5Scvs2svn   { 0x0055, 0x0055, "Individual Link Encryption" },
2831ae349f5Scvs2svn   { 0x006f, 0x006f, "Stampede Bridging" },
2841ae349f5Scvs2svn   { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" },
2851ae349f5Scvs2svn   { 0x0073, 0x0073, "MP+ Protocol" },
2861ae349f5Scvs2svn   { 0x007d, 0x007d, "reserved (Control Escape)" },
2871ae349f5Scvs2svn   { 0x007f, 0x007f, "reserved (compression inefficient)" },
2881ae349f5Scvs2svn   { 0x00cf, 0x00cf, "reserved (PPP NLPID)" },
2891ae349f5Scvs2svn   { 0x00fb, 0x00fb, "compression on single link in multilink group" },
2901ae349f5Scvs2svn   { 0x00fd, 0x00fd, "1st choice compression" },
2911ae349f5Scvs2svn   { 0x00ff, 0x00ff, "reserved (compression inefficient)" },
2921ae349f5Scvs2svn   { 0x0200, 0x02ff, "(compression inefficient)" },
2931ae349f5Scvs2svn   { 0x0201, 0x0201, "802.1d Hello Packets" },
2941ae349f5Scvs2svn   { 0x0203, 0x0203, "IBM Source Routing BPDU" },
2951ae349f5Scvs2svn   { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" },
2961ae349f5Scvs2svn   { 0x0207, 0x0207, "Cisco Discovery Protocol" },
2971ae349f5Scvs2svn   { 0x0209, 0x0209, "Netcs Twin Routing" },
2981ae349f5Scvs2svn   { 0x0231, 0x0231, "Luxcom" },
2991ae349f5Scvs2svn   { 0x0233, 0x0233, "Sigma Network Systems" },
3001ae349f5Scvs2svn   { 0x0235, 0x0235, "Apple Client Server Protocol" },
3011ae349f5Scvs2svn   { 0x1e00, 0x1eff, "(compression inefficient)" },
3021ae349f5Scvs2svn   { 0x4001, 0x4001, "Cray Communications Control Protocol" },
3031ae349f5Scvs2svn   { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" },
3041ae349f5Scvs2svn   { 0x4021, 0x4021, "Stacker LZS" },
3051ae349f5Scvs2svn   { 0x8001, 0x801f, "Not Used - reserved" },
3061ae349f5Scvs2svn   { 0x8021, 0x8021, "Internet Protocol Control Protocol" },
3071ae349f5Scvs2svn   { 0x8023, 0x8023, "OSI Network Layer Control Protocol" },
3081ae349f5Scvs2svn   { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" },
3091ae349f5Scvs2svn   { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" },
3101ae349f5Scvs2svn   { 0x8029, 0x8029, "Appletalk Control Protocol" },
3111ae349f5Scvs2svn   { 0x802b, 0x802b, "Novell IPX Control Protocol" },
3121ae349f5Scvs2svn   { 0x802d, 0x802d, "reserved" },
3131ae349f5Scvs2svn   { 0x802f, 0x802f, "reserved" },
3141ae349f5Scvs2svn   { 0x8031, 0x8031, "Bridging NCP" },
3151ae349f5Scvs2svn   { 0x8033, 0x8033, "Stream Protocol Control Protocol" },
3161ae349f5Scvs2svn   { 0x8035, 0x8035, "Banyan Vines Control Protocol" },
3171ae349f5Scvs2svn   { 0x8037, 0x8037, "reserved till 1993" },
3181ae349f5Scvs2svn   { 0x8039, 0x8039, "reserved" },
3191ae349f5Scvs2svn   { 0x803b, 0x803b, "reserved" },
3201ae349f5Scvs2svn   { 0x803d, 0x803d, "Multi-Link Control Protocol" },
3211ae349f5Scvs2svn   { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" },
3221ae349f5Scvs2svn   { 0x8041, 0x8041, "Cisco Systems Control Protocol" },
3231ae349f5Scvs2svn   { 0x8043, 0x8043, "Ascom Timeplex" },
3241ae349f5Scvs2svn   { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" },
3251ae349f5Scvs2svn   { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
3261ae349f5Scvs2svn   { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
3271ae349f5Scvs2svn   { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" },
3281ae349f5Scvs2svn   { 0x804d, 0x804d, "SNA Control Protocol" },
3291ae349f5Scvs2svn   { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" },
3301ae349f5Scvs2svn   { 0x8051, 0x8051, "KNX Bridging Control Protocol" },
3311ae349f5Scvs2svn   { 0x8053, 0x8053, "Encryption Control Protocol" },
3321ae349f5Scvs2svn   { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" },
3331ae349f5Scvs2svn   { 0x806f, 0x806f, "Stampede Bridging Control Protocol" },
3341ae349f5Scvs2svn   { 0x8073, 0x8073, "MP+ Control Protocol" },
3351ae349f5Scvs2svn   { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" },
3361ae349f5Scvs2svn   { 0x807d, 0x807d, "Not Used - reserved" },
3371ae349f5Scvs2svn   { 0x80cf, 0x80cf, "Not Used - reserved" },
3381ae349f5Scvs2svn   { 0x80fb, 0x80fb, "compression on single link in multilink group control" },
3391ae349f5Scvs2svn   { 0x80fd, 0x80fd, "Compression Control Protocol" },
3401ae349f5Scvs2svn   { 0x80ff, 0x80ff, "Not Used - reserved" },
3411ae349f5Scvs2svn   { 0x8207, 0x8207, "Cisco Discovery Protocol Control" },
3421ae349f5Scvs2svn   { 0x8209, 0x8209, "Netcs Twin Routing" },
3431ae349f5Scvs2svn   { 0x8235, 0x8235, "Apple Client Server Protocol Control" },
3441ae349f5Scvs2svn   { 0xc021, 0xc021, "Link Control Protocol" },
3451ae349f5Scvs2svn   { 0xc023, 0xc023, "Password Authentication Protocol" },
3461ae349f5Scvs2svn   { 0xc025, 0xc025, "Link Quality Report" },
3471ae349f5Scvs2svn   { 0xc027, 0xc027, "Shiva Password Authentication Protocol" },
3481ae349f5Scvs2svn   { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" },
3491ae349f5Scvs2svn   { 0xc081, 0xc081, "Container Control Protocol" },
3501ae349f5Scvs2svn   { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" },
3511ae349f5Scvs2svn   { 0xc225, 0xc225, "RSA Authentication Protocol" },
3521ae349f5Scvs2svn   { 0xc227, 0xc227, "Extensible Authentication Protocol" },
3531ae349f5Scvs2svn   { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" },
3541ae349f5Scvs2svn   { 0xc281, 0xc281, "Proprietary Authentication Protocol" },
3551ae349f5Scvs2svn   { 0xc283, 0xc283, "Proprietary Authentication Protocol" },
3561ae349f5Scvs2svn   { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" }
3571ae349f5Scvs2svn };
3581ae349f5Scvs2svn 
3591ae349f5Scvs2svn #define NPROTOCOLS (sizeof protocols/sizeof protocols[0])
3601ae349f5Scvs2svn 
3611ae349f5Scvs2svn static const char *
3621ae349f5Scvs2svn Protocol2Nam(u_short proto)
3631ae349f5Scvs2svn {
3641ae349f5Scvs2svn   int f;
3651ae349f5Scvs2svn 
3661ae349f5Scvs2svn   for (f = 0; f < NPROTOCOLS; f++)
3671ae349f5Scvs2svn     if (proto >= protocols[f].from && proto <= protocols[f].to)
3681ae349f5Scvs2svn       return protocols[f].name;
3691ae349f5Scvs2svn     else if (proto < protocols[f].from)
3701ae349f5Scvs2svn       break;
3711ae349f5Scvs2svn   return "unrecognised protocol";
3721ae349f5Scvs2svn }
3731ae349f5Scvs2svn 
3741ae349f5Scvs2svn static void
37563b73463SBrian Somers DecodePacket(u_short proto, struct mbuf * bp, struct physical *physical)
3761ae349f5Scvs2svn {
3771ae349f5Scvs2svn   u_char *cp;
3781ae349f5Scvs2svn 
3791ae349f5Scvs2svn   LogPrintf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto);
3801ae349f5Scvs2svn 
3811ae349f5Scvs2svn   /*
3821ae349f5Scvs2svn    * If proto isn't PROTO_COMPD, we still want to pass it to the
3831ae349f5Scvs2svn    * decompression routines so that the dictionary's updated
3841ae349f5Scvs2svn    */
3851ae349f5Scvs2svn   if (CcpFsm.state == ST_OPENED)
3861ae349f5Scvs2svn     if (proto == PROTO_COMPD) {
3871ae349f5Scvs2svn       if ((bp = CompdInput(&proto, bp)) == NULL)
3881ae349f5Scvs2svn         return;
3891ae349f5Scvs2svn     } else if ((proto & 0xfff1) == 0x21)	/* Network Layer protocol */
3901ae349f5Scvs2svn       CcpDictSetup(proto, bp);
3911ae349f5Scvs2svn 
3921ae349f5Scvs2svn   switch (proto) {
3931ae349f5Scvs2svn   case PROTO_LCP:
3941ae349f5Scvs2svn     LcpInput(bp);
3951ae349f5Scvs2svn     break;
3961ae349f5Scvs2svn   case PROTO_PAP:
39763b73463SBrian Somers     PapInput(bp, physical);
3981ae349f5Scvs2svn     break;
3991ae349f5Scvs2svn   case PROTO_LQR:
4001ae349f5Scvs2svn     HisLqrSave.SaveInLQRs++;
40163b73463SBrian Somers     LqrInput(physical, bp);
4021ae349f5Scvs2svn     break;
4031ae349f5Scvs2svn   case PROTO_CHAP:
40463b73463SBrian Somers     ChapInput(bp, physical);
4051ae349f5Scvs2svn     break;
4061ae349f5Scvs2svn   case PROTO_VJUNCOMP:
4071ae349f5Scvs2svn   case PROTO_VJCOMP:
4081ae349f5Scvs2svn     bp = VjCompInput(bp, proto);
4091ae349f5Scvs2svn     if (bp == NULL) {
4101ae349f5Scvs2svn       break;
4111ae349f5Scvs2svn     }
4121ae349f5Scvs2svn     /* fall down */
4131ae349f5Scvs2svn   case PROTO_IP:
4141ae349f5Scvs2svn     IpInput(bp);
4151ae349f5Scvs2svn     break;
4161ae349f5Scvs2svn   case PROTO_IPCP:
4171ae349f5Scvs2svn     IpcpInput(bp);
4181ae349f5Scvs2svn     break;
4191ae349f5Scvs2svn   case PROTO_CCP:
4201ae349f5Scvs2svn     CcpInput(bp);
4211ae349f5Scvs2svn     break;
4221ae349f5Scvs2svn   default:
4231ae349f5Scvs2svn     LogPrintf(LogPHASE, "Unknown protocol 0x%04x (%s)\n",
4241ae349f5Scvs2svn               proto, Protocol2Nam(proto));
4251ae349f5Scvs2svn     bp->offset -= 2;
4261ae349f5Scvs2svn     bp->cnt += 2;
4271ae349f5Scvs2svn     cp = MBUF_CTOP(bp);
4281ae349f5Scvs2svn     LcpSendProtoRej(cp, bp->cnt);
4291ae349f5Scvs2svn     HisLqrSave.SaveInDiscards++;
4301ae349f5Scvs2svn     HdlcStat.unknownproto++;
4311ae349f5Scvs2svn     pfree(bp);
4321ae349f5Scvs2svn     break;
4331ae349f5Scvs2svn   }
4341ae349f5Scvs2svn }
4351ae349f5Scvs2svn 
4361ae349f5Scvs2svn int
4371ae349f5Scvs2svn ReportProtStatus(struct cmdargs const *arg)
4381ae349f5Scvs2svn {
4391ae349f5Scvs2svn   struct protostat *statp;
4401ae349f5Scvs2svn   int cnt;
4411ae349f5Scvs2svn 
4421ae349f5Scvs2svn   statp = ProtocolStat;
4431ae349f5Scvs2svn   statp--;
4441ae349f5Scvs2svn   cnt = 0;
4451ae349f5Scvs2svn   fprintf(VarTerm, "    Protocol     in        out      Protocol      in       out\n");
4461ae349f5Scvs2svn   do {
4471ae349f5Scvs2svn     statp++;
4481ae349f5Scvs2svn     fprintf(VarTerm, "   %-9s: %8lu, %8lu",
4491ae349f5Scvs2svn 	    statp->name, statp->in_count, statp->out_count);
4501ae349f5Scvs2svn     if (++cnt == 2) {
4511ae349f5Scvs2svn       fprintf(VarTerm, "\n");
4521ae349f5Scvs2svn       cnt = 0;
4531ae349f5Scvs2svn     }
4541ae349f5Scvs2svn   } while (statp->number);
4551ae349f5Scvs2svn   if (cnt)
4561ae349f5Scvs2svn     fprintf(VarTerm, "\n");
4571ae349f5Scvs2svn   return (0);
4581ae349f5Scvs2svn }
4591ae349f5Scvs2svn 
4601ae349f5Scvs2svn int
4611ae349f5Scvs2svn ReportHdlcStatus(struct cmdargs const *arg)
4621ae349f5Scvs2svn {
4631ae349f5Scvs2svn   struct hdlcstat *hp = &HdlcStat;
4641ae349f5Scvs2svn 
4651ae349f5Scvs2svn   if (VarTerm) {
4661ae349f5Scvs2svn     fprintf(VarTerm, "HDLC level errors\n\n");
4671ae349f5Scvs2svn     fprintf(VarTerm, "FCS: %u  ADDR: %u  COMMAND: %u  PROTO: %u\n",
4681ae349f5Scvs2svn 	    hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto);
4691ae349f5Scvs2svn   }
4701ae349f5Scvs2svn   return 0;
4711ae349f5Scvs2svn }
4721ae349f5Scvs2svn 
4731ae349f5Scvs2svn static struct hdlcstat laststat;
4741ae349f5Scvs2svn 
4751ae349f5Scvs2svn void
4761ae349f5Scvs2svn HdlcErrorCheck()
4771ae349f5Scvs2svn {
4781ae349f5Scvs2svn   struct hdlcstat *hp = &HdlcStat;
4791ae349f5Scvs2svn   struct hdlcstat *op = &laststat;
4801ae349f5Scvs2svn 
4811ae349f5Scvs2svn   if (memcmp(hp, op, sizeof laststat)) {
4821ae349f5Scvs2svn     LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n",
4831ae349f5Scvs2svn 	      hp->badfcs - op->badfcs, hp->badaddr - op->badaddr,
4841ae349f5Scvs2svn       hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto);
4851ae349f5Scvs2svn   }
4861ae349f5Scvs2svn   laststat = HdlcStat;
4871ae349f5Scvs2svn }
4881ae349f5Scvs2svn 
4891ae349f5Scvs2svn void
49063b73463SBrian Somers HdlcInput(struct mbuf * bp, struct physical *physical)
4911ae349f5Scvs2svn {
4921ae349f5Scvs2svn   u_short fcs, proto;
4931ae349f5Scvs2svn   u_char *cp, addr, ctrl;
4941ae349f5Scvs2svn   struct protostat *statp;
4951ae349f5Scvs2svn 
4961ae349f5Scvs2svn   LogDumpBp(LogHDLC, "HdlcInput:", bp);
49763b73463SBrian Somers   if (Physical_IsSync(physical))
4981ae349f5Scvs2svn     fcs = GOODFCS;
4991ae349f5Scvs2svn   else
5001ae349f5Scvs2svn     fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
5011ae349f5Scvs2svn   HisLqrSave.SaveInOctets += bp->cnt + 1;
5021ae349f5Scvs2svn 
5031ae349f5Scvs2svn   LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n",
5041ae349f5Scvs2svn 	    fcs, (fcs == GOODFCS) ? "good" : "bad");
5051ae349f5Scvs2svn   if (fcs != GOODFCS) {
5061ae349f5Scvs2svn     HisLqrSave.SaveInErrors++;
5071ae349f5Scvs2svn     LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n");
5081ae349f5Scvs2svn     HdlcStat.badfcs++;
5091ae349f5Scvs2svn     pfree(bp);
5101ae349f5Scvs2svn     return;
5111ae349f5Scvs2svn   }
51263b73463SBrian Somers   if (!Physical_IsSync(physical))
5131ae349f5Scvs2svn     bp->cnt -= 2;		/* discard FCS part */
5141ae349f5Scvs2svn 
5151ae349f5Scvs2svn   if (bp->cnt < 2) {		/* XXX: raise this bar ? */
5161ae349f5Scvs2svn     pfree(bp);
5171ae349f5Scvs2svn     return;
5181ae349f5Scvs2svn   }
5191ae349f5Scvs2svn   cp = MBUF_CTOP(bp);
5201ae349f5Scvs2svn 
5211ae349f5Scvs2svn   if (!LcpInfo.want_acfcomp) {
5221ae349f5Scvs2svn 
5231ae349f5Scvs2svn     /*
5241ae349f5Scvs2svn      * We expect that packet is not compressed.
5251ae349f5Scvs2svn      */
5261ae349f5Scvs2svn     addr = *cp++;
5271ae349f5Scvs2svn     if (addr != HDLC_ADDR) {
5281ae349f5Scvs2svn       HisLqrSave.SaveInErrors++;
5291ae349f5Scvs2svn       HdlcStat.badaddr++;
5301ae349f5Scvs2svn       LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp);
5311ae349f5Scvs2svn       pfree(bp);
5321ae349f5Scvs2svn       return;
5331ae349f5Scvs2svn     }
5341ae349f5Scvs2svn     ctrl = *cp++;
5351ae349f5Scvs2svn     if (ctrl != HDLC_UI) {
5361ae349f5Scvs2svn       HisLqrSave.SaveInErrors++;
5371ae349f5Scvs2svn       HdlcStat.badcommand++;
5381ae349f5Scvs2svn       LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp);
5391ae349f5Scvs2svn       pfree(bp);
5401ae349f5Scvs2svn       return;
5411ae349f5Scvs2svn     }
5421ae349f5Scvs2svn     bp->offset += 2;
5431ae349f5Scvs2svn     bp->cnt -= 2;
5441ae349f5Scvs2svn   } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
5451ae349f5Scvs2svn 
5461ae349f5Scvs2svn     /*
5471ae349f5Scvs2svn      * We can receive compressed packet, but peer still send uncompressed
5481ae349f5Scvs2svn      * packet to me.
5491ae349f5Scvs2svn      */
5501ae349f5Scvs2svn     cp += 2;
5511ae349f5Scvs2svn     bp->offset += 2;
5521ae349f5Scvs2svn     bp->cnt -= 2;
5531ae349f5Scvs2svn   }
5541ae349f5Scvs2svn   if (LcpInfo.want_protocomp) {
5551ae349f5Scvs2svn     proto = 0;
5561ae349f5Scvs2svn     cp--;
5571ae349f5Scvs2svn     do {
5581ae349f5Scvs2svn       cp++;
5591ae349f5Scvs2svn       bp->offset++;
5601ae349f5Scvs2svn       bp->cnt--;
5611ae349f5Scvs2svn       proto = proto << 8;
5621ae349f5Scvs2svn       proto += *cp;
5631ae349f5Scvs2svn     } while (!(proto & 1));
5641ae349f5Scvs2svn   } else {
5651ae349f5Scvs2svn     proto = *cp++ << 8;
5661ae349f5Scvs2svn     proto |= *cp++;
5671ae349f5Scvs2svn     bp->offset += 2;
5681ae349f5Scvs2svn     bp->cnt -= 2;
5691ae349f5Scvs2svn   }
5701ae349f5Scvs2svn 
5711ae349f5Scvs2svn   for (statp = ProtocolStat; statp->number; statp++)
5721ae349f5Scvs2svn     if (statp->number == proto)
5731ae349f5Scvs2svn       break;
5741ae349f5Scvs2svn   statp->in_count++;
5751ae349f5Scvs2svn   HisLqrSave.SaveInPackets++;
5761ae349f5Scvs2svn 
57763b73463SBrian Somers   DecodePacket(proto, bp, physical);
5781ae349f5Scvs2svn }
579