hdlc.c revision 32663
156893Sfenner/*
256893Sfenner *	     PPP High Level Link Control (HDLC) Module
356893Sfenner *
456893Sfenner *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
556893Sfenner *
656893Sfenner *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
756893Sfenner *
856893Sfenner * Redistribution and use in source and binary forms are permitted
956893Sfenner * provided that the above copyright notice and this paragraph are
1056893Sfenner * duplicated in all such forms and that any documentation,
1156893Sfenner * advertising materials, and other materials related to such
1256893Sfenner * distribution and use acknowledge that the software was developed
1356893Sfenner * by the Internet Initiative Japan, Inc.  The name of the
1456893Sfenner * IIJ may not be used to endorse or promote products derived
1556893Sfenner * from this software without specific prior written permission.
1656893Sfenner * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1756893Sfenner * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1856893Sfenner * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1956893Sfenner *
2056893Sfenner * $Id: hdlc.c,v 1.27 1998/01/10 01:55:10 brian Exp $
2156893Sfenner *
2256893Sfenner *	TODO:
23127668Sbms */
24214478Srpaulo#include <sys/param.h>
2556893Sfenner#include <netinet/in.h>
2656893Sfenner
2756893Sfenner#include <stdio.h>
2856893Sfenner#include <string.h>
2956893Sfenner#include <termios.h>
3056893Sfenner
3156893Sfenner#include "command.h"
3256893Sfenner#include "mbuf.h"
33127668Sbms#include "log.h"
3456893Sfenner#include "defs.h"
3556893Sfenner#include "timer.h"
3698524Sfenner#include "fsm.h"
3756893Sfenner#include "hdlc.h"
38147899Ssam#include "lcpproto.h"
39147899Ssam#include "ipcp.h"
40147899Ssam#include "ip.h"
41147899Ssam#include "vjcomp.h"
4275115Sfenner#include "pap.h"
4375115Sfenner#include "chap.h"
44127668Sbms#include "lcp.h"
4556893Sfenner#include "async.h"
4675115Sfenner#include "lqr.h"
4775115Sfenner#include "loadalias.h"
4875115Sfenner#include "vars.h"
4998524Sfenner#include "modem.h"
5098524Sfenner#include "ccp.h"
5198524Sfenner
52127668Sbmsstatic struct hdlcstat {
53127668Sbms  int badfcs;
54146773Ssam  int badaddr;
55146773Ssam  int badcommand;
56127668Sbms  int unknownproto;
5775115Sfenner}        HdlcStat;
58127668Sbms
59127668Sbmsstatic int ifOutPackets;
6056893Sfennerstatic int ifOutOctets;
6175115Sfennerstatic int ifOutLQRs;
6275115Sfenner
6375115Sfennerstatic struct protostat {
6475115Sfenner  u_short number;
65214478Srpaulo  const char *name;
66214478Srpaulo  u_long in_count;
67214478Srpaulo  u_long out_count;
68146773Ssam} ProtocolStat[] = {
69146773Ssam  { PROTO_IP, "IP" },
70146773Ssam  { PROTO_VJUNCOMP, "VJ_UNCOMP" },
71146773Ssam  { PROTO_VJCOMP, "VJ_COMP" },
72146773Ssam  { PROTO_COMPD, "COMPD" },
73146773Ssam  { PROTO_LCP, "LCP" },
74146773Ssam  { PROTO_IPCP, "IPCP" },
75147899Ssam  { PROTO_CCP, "CCP" },
76147899Ssam  { PROTO_PAP, "PAP" },
77147899Ssam  { PROTO_LQR, "LQR" },
78147899Ssam  { PROTO_CHAP, "CHAP" },
79146773Ssam  { 0, "Others" }
80146773Ssam};
81162017Ssam
82146773Ssamstatic u_short const fcstab[256] = {
83146773Ssam   /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
84146773Ssam   /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
85146773Ssam   /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
86147899Ssam   /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
87146773Ssam   /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
88146773Ssam   /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
89146773Ssam   /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
90146773Ssam   /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
91146773Ssam   /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
92146773Ssam   /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
93146773Ssam   /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
94146773Ssam   /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
95146773Ssam   /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
96146773Ssam   /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
97214478Srpaulo   /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
98146773Ssam   /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
99146773Ssam   /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
100146773Ssam   /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
101146773Ssam   /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
102146773Ssam   /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
103146773Ssam   /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
104146773Ssam   /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
105146773Ssam   /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
106146773Ssam   /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
107146773Ssam   /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
108146773Ssam   /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
109146773Ssam   /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
110146773Ssam   /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
111146773Ssam   /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
112146773Ssam   /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
113146773Ssam   /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
114146773Ssam   /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
115146773Ssam};
116146773Ssam
117146773Ssamu_char EscMap[33];
118146773Ssam
119146773Ssamvoid
120146773SsamHdlcInit()
121146773Ssam{
122146773Ssam  ifOutOctets = 0;
123146773Ssam  ifOutPackets = 0;
124146773Ssam  ifOutLQRs = 0;
125146773Ssam}
126146773Ssam
127146773Ssam/*
128146773Ssam *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
129146773Ssam *  2.27 for further details.
130146773Ssam */
131146773Ssaminline u_short
132146773SsamHdlcFcs(u_short fcs, u_char * cp, int len)
133146773Ssam{
134146773Ssam  while (len--)
135146773Ssam    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
136146773Ssam  return (fcs);
137146773Ssam}
138214478Srpaulo
139162017Ssamstatic inline u_short
140146773SsamHdlcFcsBuf(u_short fcs, struct mbuf *m)
141146773Ssam{
142146773Ssam  int len;
143146773Ssam  u_char *pos, *end;
144146773Ssam
145146773Ssam  len = plength(m);
146146773Ssam  pos = MBUF_CTOP(m);
147146773Ssam  end = pos + m->cnt;
148146773Ssam  while (len--) {
149146773Ssam    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
150146773Ssam    if (pos == end && len) {
151146773Ssam      m = m->next;
152146773Ssam      pos = MBUF_CTOP(m);
153146773Ssam      end = pos + m->cnt;
154146773Ssam    }
155146773Ssam  }
15698524Sfenner  return (fcs);
15798524Sfenner}
15898524Sfenner
15998524Sfennervoid
16098524SfennerHdlcOutput(int pri, u_short proto, struct mbuf * bp)
16198524Sfenner{
16298524Sfenner  struct mbuf *mhp, *mfcs;
16398524Sfenner  struct protostat *statp;
16498524Sfenner  struct lqrdata *lqr;
16598524Sfenner  u_char *cp;
16698524Sfenner  u_short fcs;
16798524Sfenner
16898524Sfenner  if ((proto & 0xfff1) == 0x21)		/* Network Layer protocol */
16998524Sfenner    if (CcpFsm.state == ST_OPENED)
17098524Sfenner      if (CcpOutput(pri, proto, bp))
17198524Sfenner        return;
17298524Sfenner
17398524Sfenner  if (DEV_IS_SYNC)
17498524Sfenner    mfcs = NULL;
17598524Sfenner  else
17698524Sfenner    mfcs = mballoc(2, MB_HDLCOUT);
17798524Sfenner
17898524Sfenner  mhp = mballoc(4, MB_HDLCOUT);
17998524Sfenner  mhp->cnt = 0;
18098524Sfenner  cp = MBUF_CTOP(mhp);
18198524Sfenner  if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) {
18298524Sfenner    *cp++ = HDLC_ADDR;
18398524Sfenner    *cp++ = HDLC_UI;
18498524Sfenner    mhp->cnt += 2;
18598524Sfenner  }
18698524Sfenner
18798524Sfenner  /*
18898524Sfenner   * If possible, compress protocol field.
18998524Sfenner   */
19098524Sfenner  if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) {
19198524Sfenner    *cp++ = proto;
19298524Sfenner    mhp->cnt++;
19398524Sfenner  } else {
19498524Sfenner    *cp++ = proto >> 8;
19598524Sfenner    *cp = proto & 0377;
19698524Sfenner    mhp->cnt += 2;
197127668Sbms  }
198127668Sbms  mhp->next = bp;
199127668Sbms  while (bp->next != NULL)
200127668Sbms    bp = bp->next;
201127668Sbms  bp->next = mfcs;
202127668Sbms  bp = mhp->next;
203127668Sbms
204127668Sbms  lqr = &MyLqrData;
205127668Sbms  lqr->PeerOutPackets = ifOutPackets++;
206127668Sbms  ifOutOctets += plength(mhp) + 1;
207127668Sbms  lqr->PeerOutOctets = ifOutOctets;
208127668Sbms
209127668Sbms  if (proto == PROTO_LQR) {
210127668Sbms    lqr->MagicNumber = LcpInfo.want_magic;
211127668Sbms    lqr->LastOutLQRs = HisLqrData.PeerOutLQRs;
212127668Sbms    lqr->LastOutPackets = HisLqrData.PeerOutPackets;
213127668Sbms    lqr->LastOutOctets = HisLqrData.PeerOutOctets;
214127668Sbms    lqr->PeerInLQRs = HisLqrSave.SaveInLQRs;
215127668Sbms    lqr->PeerInPackets = HisLqrSave.SaveInPackets;
216127668Sbms    lqr->PeerInDiscards = HisLqrSave.SaveInDiscards;
217127668Sbms    lqr->PeerInErrors = HisLqrSave.SaveInErrors;
218127668Sbms    lqr->PeerInOctets = HisLqrSave.SaveInOctets;
219127668Sbms    lqr->PeerOutLQRs = ++ifOutLQRs;
220127668Sbms    LqrDump("LqrOutput", lqr);
221127668Sbms    LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp)));
222127668Sbms  }
223127668Sbms  if (!DEV_IS_SYNC) {
224127668Sbms    mfcs->cnt = 0;
225127668Sbms    fcs = HdlcFcsBuf(INITFCS, mhp);
226127668Sbms    fcs = ~fcs;
227127668Sbms    cp = MBUF_CTOP(mfcs);
228127668Sbms    *cp++ = fcs & 0377;		/* Low byte first!! */
229127668Sbms    *cp++ = fcs >> 8;
230127668Sbms    mfcs->cnt = 2;
231127668Sbms  }
232127668Sbms  LogDumpBp(LogHDLC, "HdlcOutput", mhp);
233127668Sbms  for (statp = ProtocolStat; statp->number; statp++)
234127668Sbms    if (statp->number == proto)
235127668Sbms      break;
236127668Sbms  statp->out_count++;
237127668Sbms
238127668Sbms  LogPrintf(LogDEBUG, "HdlcOutput: proto = 0x%04x\n", proto);
239127668Sbms
240214478Srpaulo  if (DEV_IS_SYNC)
241214478Srpaulo    ModemOutput(pri, mhp);
242214478Srpaulo  else
243214478Srpaulo    AsyncOutput(pri, mhp, proto);
244214478Srpaulo}
245214478Srpaulo
246214478Srpaulo/* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
247214478Srpaulostatic struct {
248214478Srpaulo  u_short from;
249214478Srpaulo  u_short to;
250214478Srpaulo  const char *name;
251214478Srpaulo} protocols[] = {
252214478Srpaulo  { 0x0001, 0x0001, "Padding Protocol" },
253214478Srpaulo  { 0x0003, 0x001f, "reserved (transparency inefficient)" },
254214478Srpaulo  { 0x0021, 0x0021, "Internet Protocol" },
255214478Srpaulo  { 0x0023, 0x0023, "OSI Network Layer" },
256214478Srpaulo  { 0x0025, 0x0025, "Xerox NS IDP" },
257214478Srpaulo  { 0x0027, 0x0027, "DECnet Phase IV" },
258214478Srpaulo  { 0x0029, 0x0029, "Appletalk" },
259214478Srpaulo  { 0x002b, 0x002b, "Novell IPX" },
260214478Srpaulo  { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" },
261214478Srpaulo  { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" },
262214478Srpaulo  { 0x0031, 0x0031, "Bridging PDU" },
263214478Srpaulo  { 0x0033, 0x0033, "Stream Protocol (ST-II)" },
264214478Srpaulo  { 0x0035, 0x0035, "Banyan Vines" },
265214478Srpaulo  { 0x0037, 0x0037, "reserved (until 1993)" },
266214478Srpaulo  { 0x0039, 0x0039, "AppleTalk EDDP" },
267214478Srpaulo  { 0x003b, 0x003b, "AppleTalk SmartBuffered" },
268214478Srpaulo  { 0x003d, 0x003d, "Multi-Link" },
269214478Srpaulo  { 0x003f, 0x003f, "NETBIOS Framing" },
270214478Srpaulo  { 0x0041, 0x0041, "Cisco Systems" },
271214478Srpaulo  { 0x0043, 0x0043, "Ascom Timeplex" },
272214478Srpaulo  { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" },
273214478Srpaulo  { 0x0047, 0x0047, "DCA Remote Lan" },
274214478Srpaulo  { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" },
275214478Srpaulo  { 0x004b, 0x004b, "SNA over 802.2" },
276214478Srpaulo  { 0x004d, 0x004d, "SNA" },
277214478Srpaulo  { 0x004f, 0x004f, "IP6 Header Compression" },
278214478Srpaulo  { 0x0051, 0x0051, "KNX Bridging Data" },
279214478Srpaulo  { 0x0053, 0x0053, "Encryption" },
280214478Srpaulo  { 0x0055, 0x0055, "Individual Link Encryption" },
281214478Srpaulo  { 0x006f, 0x006f, "Stampede Bridging" },
282214478Srpaulo  { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" },
283214478Srpaulo  { 0x0073, 0x0073, "MP+ Protocol" },
284214478Srpaulo  { 0x007d, 0x007d, "reserved (Control Escape)" },
285214478Srpaulo  { 0x007f, 0x007f, "reserved (compression inefficient)" },
286214478Srpaulo  { 0x00cf, 0x00cf, "reserved (PPP NLPID)" },
287214478Srpaulo  { 0x00fb, 0x00fb, "compression on single link in multilink group" },
288214478Srpaulo  { 0x00fd, 0x00fd, "1st choice compression" },
289214478Srpaulo  { 0x00ff, 0x00ff, "reserved (compression inefficient)" },
290214478Srpaulo  { 0x0200, 0x02ff, "(compression inefficient)" },
291214478Srpaulo  { 0x0201, 0x0201, "802.1d Hello Packets" },
292214478Srpaulo  { 0x0203, 0x0203, "IBM Source Routing BPDU" },
293214478Srpaulo  { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" },
294214478Srpaulo  { 0x0207, 0x0207, "Cisco Discovery Protocol" },
295214478Srpaulo  { 0x0209, 0x0209, "Netcs Twin Routing" },
296214478Srpaulo  { 0x0231, 0x0231, "Luxcom" },
297214478Srpaulo  { 0x0233, 0x0233, "Sigma Network Systems" },
298214478Srpaulo  { 0x0235, 0x0235, "Apple Client Server Protocol" },
299214478Srpaulo  { 0x1e00, 0x1eff, "(compression inefficient)" },
300214478Srpaulo  { 0x4001, 0x4001, "Cray Communications Control Protocol" },
301214478Srpaulo  { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" },
302214478Srpaulo  { 0x4021, 0x4021, "Stacker LZS" },
303214478Srpaulo  { 0x8001, 0x801f, "Not Used - reserved" },
304214478Srpaulo  { 0x8021, 0x8021, "Internet Protocol Control Protocol" },
305214478Srpaulo  { 0x8023, 0x8023, "OSI Network Layer Control Protocol" },
306214478Srpaulo  { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" },
307214478Srpaulo  { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" },
308214478Srpaulo  { 0x8029, 0x8029, "Appletalk Control Protocol" },
309214478Srpaulo  { 0x802b, 0x802b, "Novell IPX Control Protocol" },
310214478Srpaulo  { 0x802d, 0x802d, "reserved" },
311214478Srpaulo  { 0x802f, 0x802f, "reserved" },
312214478Srpaulo  { 0x8031, 0x8031, "Bridging NCP" },
313214478Srpaulo  { 0x8033, 0x8033, "Stream Protocol Control Protocol" },
314214478Srpaulo  { 0x8035, 0x8035, "Banyan Vines Control Protocol" },
315214478Srpaulo  { 0x8037, 0x8037, "reserved till 1993" },
316214478Srpaulo  { 0x8039, 0x8039, "reserved" },
317214478Srpaulo  { 0x803b, 0x803b, "reserved" },
31856893Sfenner  { 0x803d, 0x803d, "Multi-Link Control Protocol" },
319214478Srpaulo  { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" },
320214478Srpaulo  { 0x8041, 0x8041, "Cisco Systems Control Protocol" },
32156893Sfenner  { 0x8043, 0x8043, "Ascom Timeplex" },
32275115Sfenner  { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" },
32398524Sfenner  { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
32498524Sfenner  { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
32598524Sfenner  { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" },
32698524Sfenner  { 0x804d, 0x804d, "SNA Control Protocol" },
32798524Sfenner  { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" },
328127668Sbms  { 0x8051, 0x8051, "KNX Bridging Control Protocol" },
32956893Sfenner  { 0x8053, 0x8053, "Encryption Control Protocol" },
33056893Sfenner  { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" },
33156893Sfenner  { 0x806f, 0x806f, "Stampede Bridging Control Protocol" },
33256893Sfenner  { 0x8073, 0x8073, "MP+ Control Protocol" },
33375115Sfenner  { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" },
33456893Sfenner  { 0x807d, 0x807d, "Not Used - reserved" },
33556893Sfenner  { 0x80cf, 0x80cf, "Not Used - reserved" },
336127668Sbms  { 0x80fb, 0x80fb, "compression on single link in multilink group control" },
337127668Sbms  { 0x80fd, 0x80fd, "Compression Control Protocol" },
338127668Sbms  { 0x80ff, 0x80ff, "Not Used - reserved" },
339127668Sbms  { 0x8207, 0x8207, "Cisco Discovery Protocol Control" },
340127668Sbms  { 0x8209, 0x8209, "Netcs Twin Routing" },
341127668Sbms  { 0x8235, 0x8235, "Apple Client Server Protocol Control" },
342127668Sbms  { 0xc021, 0xc021, "Link Control Protocol" },
343127668Sbms  { 0xc023, 0xc023, "Password Authentication Protocol" },
344127668Sbms  { 0xc025, 0xc025, "Link Quality Report" },
345127668Sbms  { 0xc027, 0xc027, "Shiva Password Authentication Protocol" },
346127668Sbms  { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" },
347127668Sbms  { 0xc081, 0xc081, "Container Control Protocol" },
348127668Sbms  { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" },
349127668Sbms  { 0xc225, 0xc225, "RSA Authentication Protocol" },
350146773Ssam  { 0xc227, 0xc227, "Extensible Authentication Protocol" },
351146773Ssam  { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" },
352146773Ssam  { 0xc281, 0xc281, "Proprietary Authentication Protocol" },
353146773Ssam  { 0xc283, 0xc283, "Proprietary Authentication Protocol" },
354146773Ssam  { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" }
355146773Ssam};
356146773Ssam
357146773Ssam#define NPROTOCOLS (sizeof protocols/sizeof protocols[0])
358146773Ssam
359146773Ssamstatic const char *
360146773SsamProtocol2Nam(u_short proto)
361146773Ssam{
362146773Ssam  int f;
36375115Sfenner
36456893Sfenner  for (f = 0; f < NPROTOCOLS; f++)
36556893Sfenner    if (proto >= protocols[f].from && proto <= protocols[f].to)
366146773Ssam      return protocols[f].name;
36756893Sfenner    else if (proto < protocols[f].from)
368146773Ssam      break;
369146773Ssam  return "unrecognised protocol";
37056893Sfenner}
371146773Ssam
372146773Ssamstatic void
373146773SsamDecodePacket(u_short proto, struct mbuf * bp)
37456893Sfenner{
375146773Ssam  u_char *cp;
37656893Sfenner
37756893Sfenner  LogPrintf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto);
37856893Sfenner
37956893Sfenner  /*
38075115Sfenner   * If proto isn't PROTO_COMPD, we still want to pass it to the
38175115Sfenner   * decompression routines so that the dictionary's updated
38275115Sfenner   */
38375115Sfenner  if (CcpFsm.state == ST_OPENED)
384127668Sbms    if (proto == PROTO_COMPD) {
38575115Sfenner      if ((bp = CompdInput(&proto, bp)) == NULL)
38656893Sfenner        return;
387146773Ssam    } else if ((proto & 0xfff1) == 0x21)	/* Network Layer protocol */
38856893Sfenner      CcpDictSetup(proto, bp);
38956893Sfenner
39056893Sfenner  switch (proto) {
39156893Sfenner  case PROTO_LCP:
392146773Ssam    LcpInput(bp);
39356893Sfenner    break;
39456893Sfenner  case PROTO_PAP:
39556893Sfenner    PapInput(bp);
39656893Sfenner    break;
397146773Ssam  case PROTO_LQR:
39856893Sfenner    HisLqrSave.SaveInLQRs++;
39956893Sfenner    LqrInput(bp);
40056893Sfenner    break;
40156893Sfenner  case PROTO_CHAP:
40256893Sfenner    ChapInput(bp);
40356893Sfenner    break;
404146773Ssam  case PROTO_VJUNCOMP:
405146773Ssam  case PROTO_VJCOMP:
406146773Ssam    bp = VjCompInput(bp, proto);
407146773Ssam    if (bp == NULL) {
408146773Ssam      break;
40956893Sfenner    }
41056893Sfenner    /* fall down */
41156893Sfenner  case PROTO_IP:
41256893Sfenner    IpInput(bp);
413146773Ssam    break;
41456893Sfenner  case PROTO_IPCP:
41556893Sfenner    IpcpInput(bp);
41656893Sfenner    break;
41756893Sfenner  case PROTO_CCP:
41856893Sfenner    CcpInput(bp);
419146773Ssam    break;
42056893Sfenner  default:
42156893Sfenner    LogPrintf(LogPHASE, "Unknown protocol 0x%04x (%s)\n",
42256893Sfenner              proto, Protocol2Nam(proto));
423146773Ssam    bp->offset -= 2;
42456893Sfenner    bp->cnt += 2;
42556893Sfenner    cp = MBUF_CTOP(bp);
426146773Ssam    LcpSendProtoRej(cp, bp->cnt);
42756893Sfenner    HisLqrSave.SaveInDiscards++;
42856893Sfenner    HdlcStat.unknownproto++;
42956893Sfenner    pfree(bp);
43056893Sfenner    break;
43156893Sfenner  }
43256893Sfenner}
43356893Sfenner
434146773Ssamint
43556893SfennerReportProtStatus(struct cmdargs const *arg)
43656893Sfenner{
437146773Ssam  struct protostat *statp;
43856893Sfenner  int cnt;
43956893Sfenner
440146773Ssam  statp = ProtocolStat;
44156893Sfenner  statp--;
44256893Sfenner  cnt = 0;
443146773Ssam  fprintf(VarTerm, "    Protocol     in        out      Protocol      in       out\n");
44456893Sfenner  do {
44556893Sfenner    statp++;
44656893Sfenner    fprintf(VarTerm, "   %-9s: %8lu, %8lu",
44756893Sfenner	    statp->name, statp->in_count, statp->out_count);
44856893Sfenner    if (++cnt == 2) {
44956893Sfenner      fprintf(VarTerm, "\n");
450127668Sbms      cnt = 0;
451146773Ssam    }
45256893Sfenner  } while (statp->number);
45356893Sfenner  if (cnt)
454146773Ssam    fprintf(VarTerm, "\n");
455146773Ssam  return (0);
456146773Ssam}
457146773Ssam
458146773Ssamint
459146773SsamReportHdlcStatus(struct cmdargs const *arg)
460146773Ssam{
461146773Ssam  struct hdlcstat *hp = &HdlcStat;
46256893Sfenner
46356893Sfenner  if (VarTerm) {
46456893Sfenner    fprintf(VarTerm, "HDLC level errors\n\n");
46556893Sfenner    fprintf(VarTerm, "FCS: %u  ADDR: %u  COMMAND: %u  PROTO: %u\n",
46656893Sfenner	    hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto);
46756893Sfenner  }
46856893Sfenner  return 0;
46956893Sfenner}
470146773Ssam
47156893Sfennerstatic struct hdlcstat laststat;
47275115Sfenner
473127668Sbmsvoid
47456893SfennerHdlcErrorCheck()
47556893Sfenner{
47656893Sfenner  struct hdlcstat *hp = &HdlcStat;
477146773Ssam  struct hdlcstat *op = &laststat;
47856893Sfenner
47956893Sfenner  if (memcmp(hp, op, sizeof laststat)) {
48056893Sfenner    LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n",
48156893Sfenner	      hp->badfcs - op->badfcs, hp->badaddr - op->badaddr,
48256893Sfenner      hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto);
483146773Ssam  }
484146773Ssam  laststat = HdlcStat;
485146773Ssam}
486146773Ssam
487146773Ssamvoid
488146773SsamHdlcInput(struct mbuf * bp)
489146773Ssam{
490146773Ssam  u_short fcs, proto;
49198524Sfenner  u_char *cp, addr, ctrl;
49275115Sfenner  struct protostat *statp;
493127668Sbms
49456893Sfenner  LogDumpBp(LogHDLC, "HdlcInput:", bp);
49556893Sfenner  if (DEV_IS_SYNC)
49656893Sfenner    fcs = GOODFCS;
49756893Sfenner  else
49856893Sfenner    fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
49956893Sfenner  HisLqrSave.SaveInOctets += bp->cnt + 1;
50056893Sfenner
501146773Ssam  LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n",
50256893Sfenner	    fcs, (fcs == GOODFCS) ? "good" : "bad");
50356893Sfenner  if (fcs != GOODFCS) {
50475115Sfenner    HisLqrSave.SaveInErrors++;
505127668Sbms    LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n");
50656893Sfenner    HdlcStat.badfcs++;
50756893Sfenner    pfree(bp);
50856893Sfenner    return;
50956893Sfenner  }
51056893Sfenner  if (!DEV_IS_SYNC)
51156893Sfenner    bp->cnt -= 2;		/* discard FCS part */
51256893Sfenner
51356893Sfenner  if (bp->cnt < 2) {		/* XXX: raise this bar ? */
51456893Sfenner    pfree(bp);
515146773Ssam    return;
51656893Sfenner  }
51775115Sfenner  cp = MBUF_CTOP(bp);
518146773Ssam
519146773Ssam  if (!LcpInfo.want_acfcomp) {
520146773Ssam
521146773Ssam    /*
52256893Sfenner     * We expect that packet is not compressed.
52375115Sfenner     */
524127668Sbms    addr = *cp++;
52575115Sfenner    if (addr != HDLC_ADDR) {
52656893Sfenner      HisLqrSave.SaveInErrors++;
52756893Sfenner      HdlcStat.badaddr++;
52856893Sfenner      LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp);
52956893Sfenner      pfree(bp);
53056893Sfenner      return;
53156893Sfenner    }
532146773Ssam    ctrl = *cp++;
533127668Sbms    if (ctrl != HDLC_UI) {
53475115Sfenner      HisLqrSave.SaveInErrors++;
53575115Sfenner      HdlcStat.badcommand++;
53656893Sfenner      LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp);
53756893Sfenner      pfree(bp);
53856893Sfenner      return;
539127668Sbms    }
54056893Sfenner    bp->offset += 2;
54156893Sfenner    bp->cnt -= 2;
54275115Sfenner  } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
54375115Sfenner
54456893Sfenner    /*
545127668Sbms     * We can receive compressed packet, but peer still send uncompressed
54656893Sfenner     * packet to me.
54775115Sfenner     */
54875115Sfenner    cp += 2;
549127668Sbms    bp->offset += 2;
55056893Sfenner    bp->cnt -= 2;
551146773Ssam  }
552146773Ssam  if (LcpInfo.want_protocomp) {
553146773Ssam    proto = 0;
554146773Ssam    cp--;
555146773Ssam    do {
556146773Ssam      cp++;
557146773Ssam      bp->offset++;
558127668Sbms      bp->cnt--;
559146773Ssam      proto = proto << 8;
560146773Ssam      proto += *cp;
561146773Ssam    } while (!(proto & 1));
562127668Sbms  } else {
563127668Sbms    proto = *cp++ << 8;
564127668Sbms    proto |= *cp++;
565127668Sbms    bp->offset += 2;
566127668Sbms    bp->cnt -= 2;
567127668Sbms  }
568146773Ssam
569127668Sbms  for (statp = ProtocolStat; statp->number; statp++)
570127668Sbms    if (statp->number == proto)
571127668Sbms      break;
572127668Sbms  statp->in_count++;
573127668Sbms  HisLqrSave.SaveInPackets++;
574127668Sbms
575127668Sbms  DecodePacket(proto, bp);
576127668Sbms}
577127668Sbms