xref: /freebsd/usr.sbin/ppp/hdlc.c (revision 643f49047ececa129a0e3f5e08ff3bfad942cb0c)
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  *
20643f4904SBrian Somers  * $Id: hdlc.c,v 1.28.2.28 1998/04/23 21:50:09 brian Exp $
211ae349f5Scvs2svn  *
221ae349f5Scvs2svn  *	TODO:
231ae349f5Scvs2svn  */
242764b86aSBrian Somers #include <sys/types.h>
251ae349f5Scvs2svn #include <netinet/in.h>
26eaa4df37SBrian Somers #include <netinet/in_systm.h>
27eaa4df37SBrian Somers #include <netinet/ip.h>
281ae349f5Scvs2svn 
291ae349f5Scvs2svn #include <stdio.h>
301ae349f5Scvs2svn #include <string.h>
311ae349f5Scvs2svn #include <termios.h>
321ae349f5Scvs2svn 
331ae349f5Scvs2svn #include "command.h"
341ae349f5Scvs2svn #include "mbuf.h"
351ae349f5Scvs2svn #include "log.h"
361ae349f5Scvs2svn #include "defs.h"
371ae349f5Scvs2svn #include "timer.h"
381ae349f5Scvs2svn #include "fsm.h"
39879ed6faSBrian Somers #include "lqr.h"
401ae349f5Scvs2svn #include "hdlc.h"
411ae349f5Scvs2svn #include "lcpproto.h"
4229e275ceSBrian Somers #include "iplist.h"
4329e275ceSBrian Somers #include "throughput.h"
44eaa4df37SBrian Somers #include "slcompress.h"
451ae349f5Scvs2svn #include "ipcp.h"
461ae349f5Scvs2svn #include "ip.h"
471ae349f5Scvs2svn #include "vjcomp.h"
48e2ebb036SBrian Somers #include "auth.h"
491ae349f5Scvs2svn #include "pap.h"
501ae349f5Scvs2svn #include "chap.h"
511ae349f5Scvs2svn #include "lcp.h"
521ae349f5Scvs2svn #include "async.h"
531ae349f5Scvs2svn #include "ccp.h"
548c07a7b2SBrian Somers #include "link.h"
5542d4d396SBrian Somers #include "descriptor.h"
5663b73463SBrian Somers #include "physical.h"
5785b542cfSBrian Somers #include "prompt.h"
5863258dccSBrian Somers #include "chat.h"
593b0f8d2eSBrian Somers #include "mp.h"
6063258dccSBrian Somers #include "datalink.h"
615ca5389aSBrian Somers #include "filter.h"
62503a7782SBrian Somers #include "bundle.h"
631ae349f5Scvs2svn 
641ae349f5Scvs2svn static u_short const fcstab[256] = {
651ae349f5Scvs2svn    /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
661ae349f5Scvs2svn    /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
671ae349f5Scvs2svn    /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
681ae349f5Scvs2svn    /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
691ae349f5Scvs2svn    /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
701ae349f5Scvs2svn    /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
711ae349f5Scvs2svn    /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
721ae349f5Scvs2svn    /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
731ae349f5Scvs2svn    /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
741ae349f5Scvs2svn    /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
751ae349f5Scvs2svn    /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
761ae349f5Scvs2svn    /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
771ae349f5Scvs2svn    /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
781ae349f5Scvs2svn    /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
791ae349f5Scvs2svn    /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
801ae349f5Scvs2svn    /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
811ae349f5Scvs2svn    /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
821ae349f5Scvs2svn    /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
831ae349f5Scvs2svn    /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
841ae349f5Scvs2svn    /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
851ae349f5Scvs2svn    /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
861ae349f5Scvs2svn    /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
871ae349f5Scvs2svn    /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
881ae349f5Scvs2svn    /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
891ae349f5Scvs2svn    /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
901ae349f5Scvs2svn    /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
911ae349f5Scvs2svn    /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
921ae349f5Scvs2svn    /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
931ae349f5Scvs2svn    /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
941ae349f5Scvs2svn    /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
951ae349f5Scvs2svn    /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
961ae349f5Scvs2svn    /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
971ae349f5Scvs2svn };
981ae349f5Scvs2svn 
991ae349f5Scvs2svn void
100643f4904SBrian Somers hdlc_Init(struct hdlc *hdlc, struct lcp *lcp)
1011ae349f5Scvs2svn {
10263258dccSBrian Somers   memset(hdlc, '\0', sizeof(struct hdlc));
103643f4904SBrian Somers   hdlc->lqm.owner = lcp;
1041ae349f5Scvs2svn }
1051ae349f5Scvs2svn 
1061ae349f5Scvs2svn /*
1071ae349f5Scvs2svn  *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1081ae349f5Scvs2svn  *  2.27 for further details.
1091ae349f5Scvs2svn  */
1101ae349f5Scvs2svn inline u_short
1111ae349f5Scvs2svn HdlcFcs(u_short fcs, u_char * cp, int len)
1121ae349f5Scvs2svn {
1131ae349f5Scvs2svn   while (len--)
1141ae349f5Scvs2svn     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
1151ae349f5Scvs2svn   return (fcs);
1161ae349f5Scvs2svn }
1171ae349f5Scvs2svn 
1181ae349f5Scvs2svn static inline u_short
1191ae349f5Scvs2svn HdlcFcsBuf(u_short fcs, struct mbuf *m)
1201ae349f5Scvs2svn {
1211ae349f5Scvs2svn   int len;
1221ae349f5Scvs2svn   u_char *pos, *end;
1231ae349f5Scvs2svn 
1241ae349f5Scvs2svn   len = plength(m);
1251ae349f5Scvs2svn   pos = MBUF_CTOP(m);
1261ae349f5Scvs2svn   end = pos + m->cnt;
1271ae349f5Scvs2svn   while (len--) {
1281ae349f5Scvs2svn     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
1291ae349f5Scvs2svn     if (pos == end && len) {
1301ae349f5Scvs2svn       m = m->next;
1311ae349f5Scvs2svn       pos = MBUF_CTOP(m);
1321ae349f5Scvs2svn       end = pos + m->cnt;
1331ae349f5Scvs2svn     }
1341ae349f5Scvs2svn   }
1351ae349f5Scvs2svn   return (fcs);
1361ae349f5Scvs2svn }
1371ae349f5Scvs2svn 
1381ae349f5Scvs2svn void
1398c07a7b2SBrian Somers HdlcOutput(struct link *l, int pri, u_short proto, struct mbuf *bp)
1401ae349f5Scvs2svn {
1418c07a7b2SBrian Somers   struct physical *p = link2physical(l);
1421ae349f5Scvs2svn   struct mbuf *mhp, *mfcs;
1431ae349f5Scvs2svn   u_char *cp;
1441ae349f5Scvs2svn   u_short fcs;
1451ae349f5Scvs2svn 
146673903ecSBrian Somers   if (!p || Physical_IsSync(p))
1471ae349f5Scvs2svn     mfcs = NULL;
1481ae349f5Scvs2svn   else
1491ae349f5Scvs2svn     mfcs = mballoc(2, MB_HDLCOUT);
1501ae349f5Scvs2svn 
1511ae349f5Scvs2svn   mhp = mballoc(4, MB_HDLCOUT);
1521ae349f5Scvs2svn   mhp->cnt = 0;
1531ae349f5Scvs2svn   cp = MBUF_CTOP(mhp);
154673903ecSBrian Somers   if (p && (proto == PROTO_LCP || l->lcp.his_acfcomp == 0)) {
1551ae349f5Scvs2svn     *cp++ = HDLC_ADDR;
1561ae349f5Scvs2svn     *cp++ = HDLC_UI;
1571ae349f5Scvs2svn     mhp->cnt += 2;
1581ae349f5Scvs2svn   }
1591ae349f5Scvs2svn 
1601ae349f5Scvs2svn   /*
1611ae349f5Scvs2svn    * If possible, compress protocol field.
1621ae349f5Scvs2svn    */
163673903ecSBrian Somers   if (l->lcp.his_protocomp && (proto & 0xff00) == 0) {
1641ae349f5Scvs2svn     *cp++ = proto;
1651ae349f5Scvs2svn     mhp->cnt++;
1661ae349f5Scvs2svn   } else {
1671ae349f5Scvs2svn     *cp++ = proto >> 8;
1681ae349f5Scvs2svn     *cp = proto & 0377;
1691ae349f5Scvs2svn     mhp->cnt += 2;
1701ae349f5Scvs2svn   }
1718c07a7b2SBrian Somers 
1721ae349f5Scvs2svn   mhp->next = bp;
173673903ecSBrian Somers 
174673903ecSBrian Somers   if (!p) {
175673903ecSBrian Somers     /*
176673903ecSBrian Somers      * This is where we multiplex the data over our available physical
177673903ecSBrian Somers      * links.  We don't frame our logical link data.  Instead we wait
178673903ecSBrian Somers      * for the logical link implementation to chop our data up and pile
179673903ecSBrian Somers      * it into the physical links by re-calling this function with the
180673903ecSBrian Somers      * encapsulated fragments.
181673903ecSBrian Somers      */
182673903ecSBrian Somers     link_Output(l, pri, mhp);
183673903ecSBrian Somers     return;
184673903ecSBrian Somers   }
185673903ecSBrian Somers 
186673903ecSBrian Somers   /* Tack mfcs onto the end, then set bp back to the start of the data */
1871ae349f5Scvs2svn   while (bp->next != NULL)
1881ae349f5Scvs2svn     bp = bp->next;
1891ae349f5Scvs2svn   bp->next = mfcs;
1901ae349f5Scvs2svn   bp = mhp->next;
1911ae349f5Scvs2svn 
192879ed6faSBrian Somers   p->hdlc.lqm.OutOctets += plength(mhp) + 1;
193879ed6faSBrian Somers   p->hdlc.lqm.OutPackets++;
1941ae349f5Scvs2svn 
1951ae349f5Scvs2svn   if (proto == PROTO_LQR) {
196879ed6faSBrian Somers     /* Overwrite the entire packet */
197879ed6faSBrian Somers     struct lqrdata lqr;
198879ed6faSBrian Somers 
1993b0f8d2eSBrian Somers     lqr.MagicNumber = p->link.lcp.want_magic;
200879ed6faSBrian Somers     lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
201879ed6faSBrian Somers     lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
202879ed6faSBrian Somers     lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
203879ed6faSBrian Somers     lqr.PeerInLQRs = p->hdlc.lqm.lqr.SaveInLQRs;
204879ed6faSBrian Somers     lqr.PeerInPackets = p->hdlc.lqm.SaveInPackets;
205879ed6faSBrian Somers     lqr.PeerInDiscards = p->hdlc.lqm.SaveInDiscards;
206879ed6faSBrian Somers     lqr.PeerInErrors = p->hdlc.lqm.SaveInErrors;
207879ed6faSBrian Somers     lqr.PeerInOctets = p->hdlc.lqm.SaveInOctets;
208879ed6faSBrian Somers     lqr.PeerOutPackets = p->hdlc.lqm.OutPackets;
209879ed6faSBrian Somers     lqr.PeerOutOctets = p->hdlc.lqm.OutOctets;
210879ed6faSBrian Somers     if (p->hdlc.lqm.lqr.peer.LastOutLQRs == p->hdlc.lqm.lqr.OutLQRs) {
211879ed6faSBrian Somers       /*
212879ed6faSBrian Somers        * only increment if it's the first time or we've got a reply
213879ed6faSBrian Somers        * from the last one
214879ed6faSBrian Somers        */
215879ed6faSBrian Somers       lqr.PeerOutLQRs = ++p->hdlc.lqm.lqr.OutLQRs;
216643f4904SBrian Somers       LqrDump(l->name, "Output", &lqr);
217879ed6faSBrian Somers     } else {
218879ed6faSBrian Somers       lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
219643f4904SBrian Somers       LqrDump(l->name, "Output (again)", &lqr);
220879ed6faSBrian Somers     }
221879ed6faSBrian Somers     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 
35863258dccSBrian Somers const char *
35963258dccSBrian Somers hdlc_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 
3713b0f8d2eSBrian Somers void
3723b0f8d2eSBrian Somers hdlc_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 
380ee6c193fSBrian Somers   /* decompress everything.  CCP needs uncompressed data too */
3813b0f8d2eSBrian Somers   if ((bp = ccp_Decompress(&l->ccp, &proto, bp)) == NULL)
3821ae349f5Scvs2svn     return;
3831ae349f5Scvs2svn 
3841ae349f5Scvs2svn   switch (proto) {
3851ae349f5Scvs2svn   case PROTO_LCP:
3863b0f8d2eSBrian Somers     LcpInput(&l->lcp, bp);
3871ae349f5Scvs2svn     break;
3881ae349f5Scvs2svn   case PROTO_PAP:
3898c07a7b2SBrian Somers     if (p)
3907a6f8720SBrian Somers       PapInput(bundle, bp, p);
3918c07a7b2SBrian Somers     else {
3928c07a7b2SBrian Somers       LogPrintf(LogERROR, "DecodePacket: PAP: Not a physical link !\n");
3938c07a7b2SBrian Somers       pfree(bp);
3948c07a7b2SBrian Somers     }
3951ae349f5Scvs2svn     break;
3961ae349f5Scvs2svn   case PROTO_LQR:
397879ed6faSBrian Somers     if (p) {
398879ed6faSBrian Somers       p->hdlc.lqm.lqr.SaveInLQRs++;
3998c07a7b2SBrian Somers       LqrInput(p, bp);
400879ed6faSBrian Somers     } else {
4018c07a7b2SBrian Somers       LogPrintf(LogERROR, "DecodePacket: LQR: Not a physical link !\n");
4028c07a7b2SBrian Somers       pfree(bp);
4038c07a7b2SBrian Somers     }
4041ae349f5Scvs2svn     break;
4051ae349f5Scvs2svn   case PROTO_CHAP:
4068c07a7b2SBrian Somers     if (p)
4077a6f8720SBrian Somers       ChapInput(bundle, bp, p);
4088c07a7b2SBrian Somers     else {
4098c07a7b2SBrian Somers       LogPrintf(LogERROR, "DecodePacket: CHAP: Not a physical link !\n");
4108c07a7b2SBrian Somers       pfree(bp);
4118c07a7b2SBrian Somers     }
4121ae349f5Scvs2svn     break;
4131ae349f5Scvs2svn   case PROTO_VJUNCOMP:
4141ae349f5Scvs2svn   case PROTO_VJCOMP:
415eaa4df37SBrian Somers     bp = VjCompInput(&bundle->ncp.ipcp, bp, proto);
4168c07a7b2SBrian Somers     if (bp == NULL)
4171ae349f5Scvs2svn       break;
4181ae349f5Scvs2svn     /* fall down */
4191ae349f5Scvs2svn   case PROTO_IP:
4207a6f8720SBrian Somers     IpInput(bundle, bp);
4211ae349f5Scvs2svn     break;
4221ae349f5Scvs2svn   case PROTO_IPCP:
4235828db6dSBrian Somers     IpcpInput(&bundle->ncp.ipcp, bp);
4241ae349f5Scvs2svn     break;
4251ae349f5Scvs2svn   case PROTO_CCP:
4263b0f8d2eSBrian Somers     CcpInput(&l->ccp, bundle, bp);
4271ae349f5Scvs2svn     break;
4283b0f8d2eSBrian Somers   case PROTO_MP:
4293b0f8d2eSBrian Somers     if (bundle->ncp.mp.active) {
4303b0f8d2eSBrian Somers       if (p)
4313b0f8d2eSBrian Somers         mp_Input(&bundle->ncp.mp, bp, p);
4323b0f8d2eSBrian Somers       else {
4333b0f8d2eSBrian Somers         LogPrintf(LogERROR, "DecodePacket: MP inside MP ?!\n");
4343b0f8d2eSBrian Somers         pfree(bp);
4353b0f8d2eSBrian Somers       }
4363b0f8d2eSBrian Somers       break;
4373b0f8d2eSBrian Somers     }
4383b0f8d2eSBrian Somers     /* Fall through */
4391ae349f5Scvs2svn   default:
4403b0f8d2eSBrian Somers     LogPrintf(LogPHASE, "%s protocol 0x%04x (%s)\n",
4413b0f8d2eSBrian Somers               proto == PROTO_MP ? "Unexpected" : "Unknown",
44263258dccSBrian Somers               proto, hdlc_Protocol2Nam(proto));
4431ae349f5Scvs2svn     bp->offset -= 2;
4441ae349f5Scvs2svn     bp->cnt += 2;
4451ae349f5Scvs2svn     cp = MBUF_CTOP(bp);
4463b0f8d2eSBrian Somers     lcp_SendProtoRej(&l->lcp, cp, bp->cnt);
4473b0f8d2eSBrian Somers     if (p) {
448879ed6faSBrian Somers       p->hdlc.lqm.SaveInDiscards++;
44963258dccSBrian Somers       p->hdlc.stats.unknownproto++;
4503b0f8d2eSBrian Somers     }
4511ae349f5Scvs2svn     pfree(bp);
4521ae349f5Scvs2svn     break;
4531ae349f5Scvs2svn   }
4541ae349f5Scvs2svn }
4551ae349f5Scvs2svn 
4561ae349f5Scvs2svn void
4577a6f8720SBrian Somers HdlcInput(struct bundle *bundle, struct mbuf * bp, struct physical *physical)
4581ae349f5Scvs2svn {
4591ae349f5Scvs2svn   u_short fcs, proto;
4601ae349f5Scvs2svn   u_char *cp, addr, ctrl;
4611ae349f5Scvs2svn 
4621ae349f5Scvs2svn   LogDumpBp(LogHDLC, "HdlcInput:", bp);
46363b73463SBrian Somers   if (Physical_IsSync(physical))
4641ae349f5Scvs2svn     fcs = GOODFCS;
4651ae349f5Scvs2svn   else
4661ae349f5Scvs2svn     fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
467879ed6faSBrian Somers   physical->hdlc.lqm.SaveInOctets += bp->cnt + 1;
4681ae349f5Scvs2svn 
4691ae349f5Scvs2svn   LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n",
4701ae349f5Scvs2svn 	    fcs, (fcs == GOODFCS) ? "good" : "bad");
4711ae349f5Scvs2svn   if (fcs != GOODFCS) {
472879ed6faSBrian Somers     physical->hdlc.lqm.SaveInErrors++;
4731ae349f5Scvs2svn     LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n");
47463258dccSBrian Somers     physical->hdlc.stats.badfcs++;
4751ae349f5Scvs2svn     pfree(bp);
4761ae349f5Scvs2svn     return;
4771ae349f5Scvs2svn   }
47863b73463SBrian Somers   if (!Physical_IsSync(physical))
4791ae349f5Scvs2svn     bp->cnt -= 2;		/* discard FCS part */
4801ae349f5Scvs2svn 
4811ae349f5Scvs2svn   if (bp->cnt < 2) {		/* XXX: raise this bar ? */
4821ae349f5Scvs2svn     pfree(bp);
4831ae349f5Scvs2svn     return;
4841ae349f5Scvs2svn   }
4851ae349f5Scvs2svn   cp = MBUF_CTOP(bp);
4861ae349f5Scvs2svn 
4873b0f8d2eSBrian Somers   if (!physical->link.lcp.want_acfcomp) {
4881ae349f5Scvs2svn     /*
4891ae349f5Scvs2svn      * We expect that packet is not compressed.
4901ae349f5Scvs2svn      */
4911ae349f5Scvs2svn     addr = *cp++;
4921ae349f5Scvs2svn     if (addr != HDLC_ADDR) {
493879ed6faSBrian Somers       physical->hdlc.lqm.SaveInErrors++;
49463258dccSBrian Somers       physical->hdlc.stats.badaddr++;
4951ae349f5Scvs2svn       LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp);
4961ae349f5Scvs2svn       pfree(bp);
4971ae349f5Scvs2svn       return;
4981ae349f5Scvs2svn     }
4991ae349f5Scvs2svn     ctrl = *cp++;
5001ae349f5Scvs2svn     if (ctrl != HDLC_UI) {
501879ed6faSBrian Somers       physical->hdlc.lqm.SaveInErrors++;
50263258dccSBrian Somers       physical->hdlc.stats.badcommand++;
5031ae349f5Scvs2svn       LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp);
5041ae349f5Scvs2svn       pfree(bp);
5051ae349f5Scvs2svn       return;
5061ae349f5Scvs2svn     }
5071ae349f5Scvs2svn     bp->offset += 2;
5081ae349f5Scvs2svn     bp->cnt -= 2;
5091ae349f5Scvs2svn   } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
5101ae349f5Scvs2svn 
5111ae349f5Scvs2svn     /*
5121ae349f5Scvs2svn      * We can receive compressed packet, but peer still send uncompressed
5131ae349f5Scvs2svn      * packet to me.
5141ae349f5Scvs2svn      */
5151ae349f5Scvs2svn     cp += 2;
5161ae349f5Scvs2svn     bp->offset += 2;
5171ae349f5Scvs2svn     bp->cnt -= 2;
5181ae349f5Scvs2svn   }
5193b0f8d2eSBrian Somers   if (physical->link.lcp.want_protocomp) {
5201ae349f5Scvs2svn     proto = 0;
5211ae349f5Scvs2svn     cp--;
5221ae349f5Scvs2svn     do {
5231ae349f5Scvs2svn       cp++;
5241ae349f5Scvs2svn       bp->offset++;
5251ae349f5Scvs2svn       bp->cnt--;
5261ae349f5Scvs2svn       proto = proto << 8;
5271ae349f5Scvs2svn       proto += *cp;
5281ae349f5Scvs2svn     } while (!(proto & 1));
5291ae349f5Scvs2svn   } else {
5301ae349f5Scvs2svn     proto = *cp++ << 8;
5311ae349f5Scvs2svn     proto |= *cp++;
5321ae349f5Scvs2svn     bp->offset += 2;
5331ae349f5Scvs2svn     bp->cnt -= 2;
5341ae349f5Scvs2svn   }
5351ae349f5Scvs2svn 
5368c07a7b2SBrian Somers   link_ProtocolRecord(physical2link(physical), proto, PROTO_IN);
537879ed6faSBrian Somers   physical->hdlc.lqm.SaveInPackets++;
5381ae349f5Scvs2svn 
5393b0f8d2eSBrian Somers   hdlc_DecodePacket(bundle, proto, bp, physical2link(physical));
5401ae349f5Scvs2svn }
54142d4d396SBrian Somers 
54242d4d396SBrian Somers /*
54342d4d396SBrian Somers  *  Detect a HDLC frame
54442d4d396SBrian Somers  */
54542d4d396SBrian Somers 
54642d4d396SBrian Somers static const char *FrameHeaders[] = {
54742d4d396SBrian Somers   "\176\377\003\300\041",
54842d4d396SBrian Somers   "\176\377\175\043\300\041",
54942d4d396SBrian Somers   "\176\177\175\043\100\041",
55042d4d396SBrian Somers   "\176\175\337\175\043\300\041",
55142d4d396SBrian Somers   "\176\175\137\175\043\100\041",
55242d4d396SBrian Somers   NULL,
55342d4d396SBrian Somers };
55442d4d396SBrian Somers 
55542d4d396SBrian Somers u_char *
55642d4d396SBrian Somers HdlcDetect(struct physical *physical, u_char *cp, int n)
55742d4d396SBrian Somers {
558cdbbb6b5SBrian Somers   const char *fp, **hp;
559cdbbb6b5SBrian Somers   char *ptr;
56042d4d396SBrian Somers 
56142d4d396SBrian Somers   cp[n] = '\0';			/* be sure to null terminated */
56242d4d396SBrian Somers   ptr = NULL;
56342d4d396SBrian Somers   for (hp = FrameHeaders; *hp; hp++) {
56442d4d396SBrian Somers     fp = *hp;
56542d4d396SBrian Somers     if (Physical_IsSync(physical))
56642d4d396SBrian Somers       fp++;
56742d4d396SBrian Somers     ptr = strstr((char *) cp, fp);
56842d4d396SBrian Somers     if (ptr)
56942d4d396SBrian Somers       break;
57042d4d396SBrian Somers   }
57142d4d396SBrian Somers   return (u_char *)ptr;
57242d4d396SBrian Somers }
57363258dccSBrian Somers 
57463258dccSBrian Somers int
57563258dccSBrian Somers hdlc_ReportStatus(struct cmdargs const *arg)
57663258dccSBrian Somers {
577b6217683SBrian Somers   struct hdlc *hdlc = &arg->cx->physical->hdlc;
578b6217683SBrian Somers 
579b6217683SBrian Somers   prompt_Printf(arg->prompt, "HDLC level errors:\n");
580b6217683SBrian Somers   prompt_Printf(arg->prompt, " Bad Frame Check Sequence fields: %u\n",
581b6217683SBrian Somers 	        hdlc->stats.badfcs);
582b6217683SBrian Somers   prompt_Printf(arg->prompt, " Bad address (!= 0x%02x) fields:    %u\n",
583b6217683SBrian Somers 	        HDLC_ADDR, hdlc->stats.badaddr);
584b6217683SBrian Somers   prompt_Printf(arg->prompt, " Bad command (!= 0x%02x) fields:    %u\n",
585b6217683SBrian Somers 	        HDLC_UI, hdlc->stats.badcommand);
586b6217683SBrian Somers   prompt_Printf(arg->prompt, " Unrecognised protocol fields:    %u\n",
587b6217683SBrian Somers 	        hdlc->stats.unknownproto);
58863258dccSBrian Somers   return 0;
58963258dccSBrian Somers }
59063258dccSBrian Somers 
59163258dccSBrian Somers static void
59263258dccSBrian Somers hdlc_ReportTime(void *v)
59363258dccSBrian Somers {
59463258dccSBrian Somers   /* Moan about HDLC errors */
59563258dccSBrian Somers   struct hdlc *hdlc = (struct hdlc *)v;
59663258dccSBrian Somers 
59763258dccSBrian Somers   StopTimer(&hdlc->ReportTimer);
59863258dccSBrian Somers 
59963258dccSBrian Somers   if (memcmp(&hdlc->laststats, &hdlc->stats, sizeof hdlc->stats)) {
60063258dccSBrian Somers     LogPrintf(LogPHASE,
60163258dccSBrian Somers               "HDLC errors -> FCS: %u, ADDR: %u, COMD: %u, PROTO: %u\n",
60263258dccSBrian Somers 	      hdlc->stats.badfcs - hdlc->laststats.badfcs,
60363258dccSBrian Somers               hdlc->stats.badaddr - hdlc->laststats.badaddr,
60463258dccSBrian Somers               hdlc->stats.badcommand - hdlc->laststats.badcommand,
60563258dccSBrian Somers               hdlc->stats.unknownproto - hdlc->laststats.unknownproto);
60663258dccSBrian Somers     hdlc->laststats = hdlc->stats;
60763258dccSBrian Somers   }
60863258dccSBrian Somers 
60963258dccSBrian Somers   StartTimer(&hdlc->ReportTimer);
61063258dccSBrian Somers }
61163258dccSBrian Somers 
61263258dccSBrian Somers void
61363258dccSBrian Somers hdlc_StartTimer(struct hdlc *hdlc)
61463258dccSBrian Somers {
61563258dccSBrian Somers   StopTimer(&hdlc->ReportTimer);
61663258dccSBrian Somers   hdlc->ReportTimer.load = 60 * SECTICKS;
61763258dccSBrian Somers   hdlc->ReportTimer.arg = hdlc;
61863258dccSBrian Somers   hdlc->ReportTimer.func = hdlc_ReportTime;
6193b0f8d2eSBrian Somers   hdlc->ReportTimer.name = "hdlc";
62063258dccSBrian Somers   StartTimer(&hdlc->ReportTimer);
62163258dccSBrian Somers }
62263258dccSBrian Somers 
62363258dccSBrian Somers void
62463258dccSBrian Somers hdlc_StopTimer(struct hdlc *hdlc)
62563258dccSBrian Somers {
62663258dccSBrian Somers   StopTimer(&hdlc->ReportTimer);
62763258dccSBrian Somers }
628