hdlc.c revision 31537
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 *
2031537Sbrian * $Id: hdlc.c,v 1.23 1997/12/03 10:23:47 brian Exp $
218857Srgrimes *
226059Samurai *	TODO:
236059Samurai */
2430715Sbrian#include <sys/param.h>
2530715Sbrian#include <netinet/in.h>
2630715Sbrian
2730715Sbrian#include <stdio.h>
2830715Sbrian#include <string.h>
2930715Sbrian#include <termios.h>
3030715Sbrian
3131343Sbrian#include "command.h"
3230715Sbrian#include "mbuf.h"
3330715Sbrian#include "log.h"
3430715Sbrian#include "defs.h"
3530715Sbrian#include "timer.h"
366059Samurai#include "fsm.h"
376059Samurai#include "hdlc.h"
386059Samurai#include "lcpproto.h"
3930733Sbrian#include "ipcp.h"
4030733Sbrian#include "ip.h"
4130733Sbrian#include "vjcomp.h"
4230733Sbrian#include "pap.h"
4330733Sbrian#include "chap.h"
446059Samurai#include "lcp.h"
4531343Sbrian#include "async.h"
466059Samurai#include "lqr.h"
4726142Sbrian#include "loadalias.h"
486735Samurai#include "vars.h"
4913389Sphk#include "modem.h"
5013389Sphk#include "ccp.h"
516059Samurai
526059Samuraistruct hdlcstat {
5328679Sbrian  int badfcs;
5428679Sbrian  int badaddr;
5528679Sbrian  int badcommand;
5628679Sbrian  int unknownproto;
5728679Sbrian}        HdlcStat;
586059Samurai
5930715Sbrianstatic int ifOutPackets;
6030715Sbrianstatic int ifOutOctets;
6130715Sbrianstatic int ifOutLQRs;
6230715Sbrianstatic int ifInPackets;
6330715Sbrianstatic int ifInOctets;
646059Samurai
656059Samuraistruct protostat {
666059Samurai  u_short number;
6731343Sbrian  const char *name;
686059Samurai  u_long in_count;
696059Samurai  u_long out_count;
7031537Sbrian} ProtocolStat[] = {
7131343Sbrian  { PROTO_IP, "IP" },
7231343Sbrian  { PROTO_VJUNCOMP, "VJ_UNCOMP" },
7331343Sbrian  { PROTO_VJCOMP, "VJ_COMP" },
7431343Sbrian  { PROTO_COMPD, "COMPD" },
7531343Sbrian  { PROTO_LCP, "LCP" },
7631343Sbrian  { PROTO_IPCP, "IPCP" },
7731343Sbrian  { PROTO_CCP, "CCP" },
7831343Sbrian  { PROTO_PAP, "PAP" },
7931343Sbrian  { PROTO_LQR, "LQR" },
8031343Sbrian  { PROTO_CHAP, "CHAP" },
8131343Sbrian  { 0, "Others" }
826059Samurai};
836059Samurai
8413760Sphkstatic u_short const fcstab[256] = {
8528679Sbrian   /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
8628679Sbrian   /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
8728679Sbrian   /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
8828679Sbrian   /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
8928679Sbrian   /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
9028679Sbrian   /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
9128679Sbrian   /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
9228679Sbrian   /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
9328679Sbrian   /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
9428679Sbrian   /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
9528679Sbrian   /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
9628679Sbrian   /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
9728679Sbrian   /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
9828679Sbrian   /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
9928679Sbrian   /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
10028679Sbrian   /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
10128679Sbrian   /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
10228679Sbrian   /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
10328679Sbrian   /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
10428679Sbrian   /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
10528679Sbrian   /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
10628679Sbrian   /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
10728679Sbrian   /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
10828679Sbrian   /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
10928679Sbrian   /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
11028679Sbrian   /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
11128679Sbrian   /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
11228679Sbrian   /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
11328679Sbrian   /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
11428679Sbrian   /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
11528679Sbrian   /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
11628679Sbrian   /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
1176059Samurai};
1186059Samurai
11928679Sbrianu_char EscMap[33];
12028679Sbrian
1216059Samuraivoid
1226059SamuraiHdlcInit()
1236059Samurai{
1246059Samurai  ifInOctets = ifOutOctets = 0;
1256059Samurai  ifInPackets = ifOutPackets = 0;
1266059Samurai  ifOutLQRs = 0;
1276059Samurai}
1286059Samurai
1296059Samurai/*
1306059Samurai *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1316059Samurai *  2.27 for further details.
1326059Samurai */
13328679Sbrianinline u_short
13428679SbrianHdlcFcs(u_short fcs, u_char * cp, int len)
1356059Samurai{
1366059Samurai  while (len--)
1376059Samurai    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
13828679Sbrian  return (fcs);
1396059Samurai}
1406059Samurai
14131514Sbrianinline u_short
14231514SbrianHdlcFcsBuf(u_short fcs, struct mbuf *m)
14331514Sbrian{
14431514Sbrian  int len;
14531514Sbrian  u_char *pos, *end;
14631514Sbrian
14731514Sbrian  len = plength(m);
14831514Sbrian  pos = MBUF_CTOP(m);
14931514Sbrian  end = pos + m->cnt;
15031514Sbrian  while (len--) {
15131514Sbrian    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
15231514Sbrian    if (pos == end && len) {
15331514Sbrian      m = m->next;
15431514Sbrian      pos = MBUF_CTOP(m);
15531514Sbrian      end = pos + m->cnt;
15631514Sbrian    }
15731514Sbrian  }
15831514Sbrian  return (fcs);
15931514Sbrian}
16031514Sbrian
1616059Samuraivoid
16228679SbrianHdlcOutput(int pri, u_short proto, struct mbuf * bp)
1636059Samurai{
1646059Samurai  struct mbuf *mhp, *mfcs;
1656059Samurai  struct protostat *statp;
1666059Samurai  struct lqrdata *lqr;
1676059Samurai  u_char *cp;
1686059Samurai  u_short fcs;
1696059Samurai
17031514Sbrian  if ((proto & 0xfff1) == 0x21)		/* Network Layer protocol */
17131514Sbrian    if (CcpFsm.state == ST_OPENED)
17231514Sbrian      if (CcpOutput(pri, proto, bp))
17329294Sbrian        return;
17431514Sbrian
1756735Samurai  if (DEV_IS_SYNC)
1766735Samurai    mfcs = NULLBUFF;
1776735Samurai  else
1786735Samurai    mfcs = mballoc(2, MB_HDLCOUT);
17931514Sbrian
1806059Samurai  mhp = mballoc(4, MB_HDLCOUT);
1816059Samurai  mhp->cnt = 0;
1826059Samurai  cp = MBUF_CTOP(mhp);
1836059Samurai  if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) {
1846059Samurai    *cp++ = HDLC_ADDR;
1856059Samurai    *cp++ = HDLC_UI;
1866059Samurai    mhp->cnt += 2;
1876059Samurai  }
18828679Sbrian
1896059Samurai  /*
19028679Sbrian   * If possible, compress protocol field.
1916059Samurai   */
1926059Samurai  if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) {
1936059Samurai    *cp++ = proto;
1946059Samurai    mhp->cnt++;
1956059Samurai  } else {
1966059Samurai    *cp++ = proto >> 8;
1976059Samurai    *cp = proto & 0377;
1986059Samurai    mhp->cnt += 2;
1996059Samurai  }
2006059Samurai  mhp->next = bp;
20131514Sbrian  while (bp->next != NULL)
20231514Sbrian    bp = bp->next;
2036059Samurai  bp->next = mfcs;
20431514Sbrian  bp = mhp->next;
2056059Samurai
2066059Samurai  lqr = &MyLqrData;
2076059Samurai  lqr->PeerOutPackets = ifOutPackets++;
2086059Samurai  ifOutOctets += plength(mhp) + 1;
2096059Samurai  lqr->PeerOutOctets = ifOutOctets;
2106059Samurai
2116059Samurai  if (proto == PROTO_LQR) {
2126059Samurai    lqr->MagicNumber = LcpInfo.want_magic;
2136059Samurai    lqr->LastOutLQRs = HisLqrData.PeerOutLQRs;
2146059Samurai    lqr->LastOutPackets = HisLqrData.PeerOutPackets;
2156059Samurai    lqr->LastOutOctets = HisLqrData.PeerOutOctets;
2166059Samurai    lqr->PeerInLQRs = HisLqrSave.SaveInLQRs;
2176059Samurai    lqr->PeerInPackets = HisLqrSave.SaveInPackets;
2186059Samurai    lqr->PeerInDiscards = HisLqrSave.SaveInDiscards;
2196059Samurai    lqr->PeerInErrors = HisLqrSave.SaveInErrors;
2206059Samurai    lqr->PeerInOctets = HisLqrSave.SaveInOctets;
2216059Samurai    lqr->PeerOutLQRs = ++ifOutLQRs;
2226059Samurai    LqrDump("LqrOutput", lqr);
22328679Sbrian    LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp)));
2246059Samurai  }
2256735Samurai  if (!DEV_IS_SYNC) {
2266735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt);
2276735Samurai    fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt);
2286735Samurai    fcs = ~fcs;
2296735Samurai    cp = MBUF_CTOP(mfcs);
23028679Sbrian    *cp++ = fcs & 0377;		/* Low byte first!! */
2316735Samurai    *cp++ = fcs >> 8;
2326735Samurai  }
23326516Sbrian  LogDumpBp(LogHDLC, "HdlcOutput", mhp);
2346059Samurai  for (statp = ProtocolStat; statp->number; statp++)
2356059Samurai    if (statp->number == proto)
2366059Samurai      break;
2376059Samurai  statp->out_count++;
23831514Sbrian
23931514Sbrian  LogPrintf(LogDEBUG, "HdlcOutput: proto = 0x%04x\n", proto);
24031514Sbrian
2416735Samurai  if (DEV_IS_SYNC)
2426735Samurai    ModemOutput(pri, mhp);
2436735Samurai  else
2446735Samurai    AsyncOutput(pri, mhp, proto);
2456059Samurai}
2466059Samurai
24731537Sbrian/* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
24831537Sbrianstatic struct {
24931537Sbrian  u_short from;
25031537Sbrian  u_short to;
25131537Sbrian  const char *name;
25231537Sbrian} protocols[] = {
25331537Sbrian  { 0x0001, 0x0001, "Padding Protocol" },
25431537Sbrian  { 0x0003, 0x001f, "reserved (transparency inefficient)" },
25531537Sbrian  { 0x0021, 0x0021, "Internet Protocol" },
25631537Sbrian  { 0x0023, 0x0023, "OSI Network Layer" },
25731537Sbrian  { 0x0025, 0x0025, "Xerox NS IDP" },
25831537Sbrian  { 0x0027, 0x0027, "DECnet Phase IV" },
25931537Sbrian  { 0x0029, 0x0029, "Appletalk" },
26031537Sbrian  { 0x002b, 0x002b, "Novell IPX" },
26131537Sbrian  { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" },
26231537Sbrian  { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" },
26331537Sbrian  { 0x0031, 0x0031, "Bridging PDU" },
26431537Sbrian  { 0x0033, 0x0033, "Stream Protocol (ST-II)" },
26531537Sbrian  { 0x0035, 0x0035, "Banyan Vines" },
26631537Sbrian  { 0x0037, 0x0037, "reserved (until 1993)" },
26731537Sbrian  { 0x0039, 0x0039, "AppleTalk EDDP" },
26831537Sbrian  { 0x003b, 0x003b, "AppleTalk SmartBuffered" },
26931537Sbrian  { 0x003d, 0x003d, "Multi-Link" },
27031537Sbrian  { 0x003f, 0x003f, "NETBIOS Framing" },
27131537Sbrian  { 0x0041, 0x0041, "Cisco Systems" },
27231537Sbrian  { 0x0043, 0x0043, "Ascom Timeplex" },
27331537Sbrian  { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" },
27431537Sbrian  { 0x0047, 0x0047, "DCA Remote Lan" },
27531537Sbrian  { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" },
27631537Sbrian  { 0x004b, 0x004b, "SNA over 802.2" },
27731537Sbrian  { 0x004d, 0x004d, "SNA" },
27831537Sbrian  { 0x004f, 0x004f, "IP6 Header Compression" },
27931537Sbrian  { 0x0051, 0x0051, "KNX Bridging Data" },
28031537Sbrian  { 0x0053, 0x0053, "Encryption" },
28131537Sbrian  { 0x0055, 0x0055, "Individual Link Encryption" },
28231537Sbrian  { 0x006f, 0x006f, "Stampede Bridging" },
28331537Sbrian  { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" },
28431537Sbrian  { 0x0073, 0x0073, "MP+ Protocol" },
28531537Sbrian  { 0x007d, 0x007d, "reserved (Control Escape)" },
28631537Sbrian  { 0x007f, 0x007f, "reserved (compression inefficient)" },
28731537Sbrian  { 0x00cf, 0x00cf, "reserved (PPP NLPID)" },
28831537Sbrian  { 0x00fb, 0x00fb, "compression on single link in multilink group" },
28931537Sbrian  { 0x00fd, 0x00fd, "1st choice compression" },
29031537Sbrian  { 0x00ff, 0x00ff, "reserved (compression inefficient)" },
29131537Sbrian  { 0x0200, 0x02ff, "(compression inefficient)" },
29231537Sbrian  { 0x0201, 0x0201, "802.1d Hello Packets" },
29331537Sbrian  { 0x0203, 0x0203, "IBM Source Routing BPDU" },
29431537Sbrian  { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" },
29531537Sbrian  { 0x0207, 0x0207, "Cisco Discovery Protocol" },
29631537Sbrian  { 0x0209, 0x0209, "Netcs Twin Routing" },
29731537Sbrian  { 0x0231, 0x0231, "Luxcom" },
29831537Sbrian  { 0x0233, 0x0233, "Sigma Network Systems" },
29931537Sbrian  { 0x0235, 0x0235, "Apple Client Server Protocol" },
30031537Sbrian  { 0x1e00, 0x1eff, "(compression inefficient)" },
30131537Sbrian  { 0x4001, 0x4001, "Cray Communications Control Protocol" },
30231537Sbrian  { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" },
30331537Sbrian  { 0x4021, 0x4021, "Stacker LZS" },
30431537Sbrian  { 0x8001, 0x801f, "Not Used - reserved" },
30531537Sbrian  { 0x8021, 0x8021, "Internet Protocol Control Protocol" },
30631537Sbrian  { 0x8023, 0x8023, "OSI Network Layer Control Protocol" },
30731537Sbrian  { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" },
30831537Sbrian  { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" },
30931537Sbrian  { 0x8029, 0x8029, "Appletalk Control Protocol" },
31031537Sbrian  { 0x802b, 0x802b, "Novell IPX Control Protocol" },
31131537Sbrian  { 0x802d, 0x802d, "reserved" },
31231537Sbrian  { 0x802f, 0x802f, "reserved" },
31331537Sbrian  { 0x8031, 0x8031, "Bridging NCP" },
31431537Sbrian  { 0x8033, 0x8033, "Stream Protocol Control Protocol" },
31531537Sbrian  { 0x8035, 0x8035, "Banyan Vines Control Protocol" },
31631537Sbrian  { 0x8037, 0x8037, "reserved till 1993" },
31731537Sbrian  { 0x8039, 0x8039, "reserved" },
31831537Sbrian  { 0x803b, 0x803b, "reserved" },
31931537Sbrian  { 0x803d, 0x803d, "Multi-Link Control Protocol" },
32031537Sbrian  { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" },
32131537Sbrian  { 0x8041, 0x8041, "Cisco Systems Control Protocol" },
32231537Sbrian  { 0x8043, 0x8043, "Ascom Timeplex" },
32331537Sbrian  { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" },
32431537Sbrian  { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
32531537Sbrian  { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
32631537Sbrian  { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" },
32731537Sbrian  { 0x804d, 0x804d, "SNA Control Protocol" },
32831537Sbrian  { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" },
32931537Sbrian  { 0x8051, 0x8051, "KNX Bridging Control Protocol" },
33031537Sbrian  { 0x8053, 0x8053, "Encryption Control Protocol" },
33131537Sbrian  { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" },
33231537Sbrian  { 0x806f, 0x806f, "Stampede Bridging Control Protocol" },
33331537Sbrian  { 0x8073, 0x8073, "MP+ Control Protocol" },
33431537Sbrian  { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" },
33531537Sbrian  { 0x807d, 0x807d, "Not Used - reserved" },
33631537Sbrian  { 0x80cf, 0x80cf, "Not Used - reserved" },
33731537Sbrian  { 0x80fb, 0x80fb, "compression on single link in multilink group control" },
33831537Sbrian  { 0x80fd, 0x80fd, "Compression Control Protocol" },
33931537Sbrian  { 0x80ff, 0x80ff, "Not Used - reserved" },
34031537Sbrian  { 0x8207, 0x8207, "Cisco Discovery Protocol Control" },
34131537Sbrian  { 0x8209, 0x8209, "Netcs Twin Routing" },
34231537Sbrian  { 0x8235, 0x8235, "Apple Client Server Protocol Control" },
34331537Sbrian  { 0xc021, 0xc021, "Link Control Protocol" },
34431537Sbrian  { 0xc023, 0xc023, "Password Authentication Protocol" },
34531537Sbrian  { 0xc025, 0xc025, "Link Quality Report" },
34631537Sbrian  { 0xc027, 0xc027, "Shiva Password Authentication Protocol" },
34731537Sbrian  { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" },
34831537Sbrian  { 0xc081, 0xc081, "Container Control Protocol" },
34931537Sbrian  { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" },
35031537Sbrian  { 0xc225, 0xc225, "RSA Authentication Protocol" },
35131537Sbrian  { 0xc227, 0xc227, "Extensible Authentication Protocol" },
35231537Sbrian  { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" },
35331537Sbrian  { 0xc281, 0xc281, "Proprietary Authentication Protocol" },
35431537Sbrian  { 0xc283, 0xc283, "Proprietary Authentication Protocol" },
35531537Sbrian  { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" }
35631537Sbrian};
35731537Sbrian
35831537Sbrian#define NPROTOCOLS (sizeof(protocols)/sizeof(protocols[0]))
35931537Sbrian
36031537Sbrianstatic const char *
36131537SbrianProtocol2Nam(u_short proto)
36231537Sbrian{
36331537Sbrian  int f;
36431537Sbrian
36531537Sbrian  for (f = 0; f < NPROTOCOLS; f++)
36631537Sbrian    if (proto >= protocols[f].from && proto <= protocols[f].to)
36731537Sbrian      return protocols[f].name;
36831537Sbrian    else if (proto < protocols[f].from)
36931537Sbrian      break;
37031537Sbrian  return "unrecognised protocol";
37131537Sbrian}
37231537Sbrian
37313379Sphkvoid
37428679SbrianDecodePacket(u_short proto, struct mbuf * bp)
3756059Samurai{
3769448Samurai  u_char *cp;
3779448Samurai
37831514Sbrian  LogPrintf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto);
37926516Sbrian
38031514Sbrian  /*
38131514Sbrian   * If proto isn't PROTO_COMPD, we still want to pass it to the
38231514Sbrian   * decompression routines so that the dictionary's updated
38331514Sbrian   */
38431514Sbrian  if (proto == PROTO_COMPD) {
38531514Sbrian    if ((bp = CompdInput(&proto, bp)) == NULL)
38631514Sbrian      return;
38731514Sbrian  } else if ((proto & 0xfff1) == 0x21)		/* Network Layer protocol */
38831514Sbrian    if (CcpFsm.state == ST_OPENED)
38931514Sbrian      CcpDictSetup(proto, bp);
39031514Sbrian
3916059Samurai  switch (proto) {
3926059Samurai  case PROTO_LCP:
3936059Samurai    LcpInput(bp);
3946059Samurai    break;
3956059Samurai  case PROTO_PAP:
3966059Samurai    PapInput(bp);
3976059Samurai    break;
3986059Samurai  case PROTO_LQR:
3996059Samurai    HisLqrSave.SaveInLQRs++;
4006059Samurai    LqrInput(bp);
4016059Samurai    break;
4026059Samurai  case PROTO_CHAP:
4036059Samurai    ChapInput(bp);
4046059Samurai    break;
4056059Samurai  case PROTO_VJUNCOMP:
4066059Samurai  case PROTO_VJCOMP:
4076059Samurai    bp = VjCompInput(bp, proto);
4086735Samurai    if (bp == NULLBUFF) {
4096735Samurai      break;
4106735Samurai    }
4116059Samurai    /* fall down */
4126059Samurai  case PROTO_IP:
4136059Samurai    IpInput(bp);
4146059Samurai    break;
4156059Samurai  case PROTO_IPCP:
4166059Samurai    IpcpInput(bp);
4176059Samurai    break;
4186059Samurai  case PROTO_CCP:
4196059Samurai    CcpInput(bp);
4206059Samurai    break;
4216059Samurai  default:
42231537Sbrian    LogPrintf(LogPHASE, "Unknown protocol 0x%04x (%s)\n",
42331537Sbrian              proto, Protocol2Nam(proto));
4249448Samurai    bp->offset -= 2;
4259448Samurai    bp->cnt += 2;
4269448Samurai    cp = MBUF_CTOP(bp);
42728679Sbrian    LcpSendProtoRej(cp, bp->cnt);
4286059Samurai    HisLqrSave.SaveInDiscards++;
4296059Samurai    HdlcStat.unknownproto++;
4306059Samurai    pfree(bp);
4316059Samurai    break;
4326059Samurai  }
4336059Samurai}
4346059Samurai
4356059Samuraiint
43631343SbrianReportProtStatus(struct cmdargs const *arg)
4376059Samurai{
4386059Samurai  struct protostat *statp;
4396059Samurai  int cnt;
4406059Samurai
4416059Samurai  statp = ProtocolStat;
4426059Samurai  statp--;
4436059Samurai  cnt = 0;
44426516Sbrian  fprintf(VarTerm, "    Protocol     in        out      Protocol      in       out\n");
4456059Samurai  do {
4466059Samurai    statp++;
44726516Sbrian    fprintf(VarTerm, "   %-9s: %8lu, %8lu",
44828679Sbrian	    statp->name, statp->in_count, statp->out_count);
4496059Samurai    if (++cnt == 2) {
45026516Sbrian      fprintf(VarTerm, "\n");
4516059Samurai      cnt = 0;
4526059Samurai    }
4536059Samurai  } while (statp->number);
4546059Samurai  if (cnt)
45528679Sbrian    fprintf(VarTerm, "\n");
45628679Sbrian  return (1);
4576059Samurai}
4586059Samurai
4596059Samuraiint
46031343SbrianReportHdlcStatus(struct cmdargs const *arg)
4616059Samurai{
4626059Samurai  struct hdlcstat *hp = &HdlcStat;
4636059Samurai
46426516Sbrian  if (VarTerm) {
46526516Sbrian    fprintf(VarTerm, "HDLC level errors\n\n");
46626516Sbrian    fprintf(VarTerm, "FCS: %u  ADDR: %u  COMMAND: %u  PROTO: %u\n",
46728679Sbrian	    hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto);
46826516Sbrian  }
46926516Sbrian  return 0;
4706059Samurai}
4716059Samurai
4726059Samuraistatic struct hdlcstat laststat;
4736059Samurai
4746059Samuraivoid
4756059SamuraiHdlcErrorCheck()
4766059Samurai{
4776059Samurai  struct hdlcstat *hp = &HdlcStat;
4786059Samurai  struct hdlcstat *op = &laststat;
4796059Samurai
48030715Sbrian  if (memcmp(hp, op, sizeof(laststat))) {
48126516Sbrian    LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n",
48228679Sbrian	      hp->badfcs - op->badfcs, hp->badaddr - op->badaddr,
48328679Sbrian      hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto);
4846059Samurai  }
4856059Samurai  laststat = HdlcStat;
4866059Samurai}
4876059Samurai
4886059Samuraivoid
48928679SbrianHdlcInput(struct mbuf * bp)
4906059Samurai{
4916059Samurai  u_short fcs, proto;
4926059Samurai  u_char *cp, addr, ctrl;
4936059Samurai  struct protostat *statp;
4946059Samurai
49526516Sbrian  LogDumpBp(LogHDLC, "HdlcInput:", bp);
4966735Samurai  if (DEV_IS_SYNC)
4976735Samurai    fcs = GOODFCS;
4986735Samurai  else
4996735Samurai    fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
5006059Samurai  HisLqrSave.SaveInOctets += bp->cnt + 1;
5016059Samurai
50226516Sbrian  LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n",
50328679Sbrian	    fcs, (fcs == GOODFCS) ? "good" : "bad");
5046059Samurai  if (fcs != GOODFCS) {
5056059Samurai    HisLqrSave.SaveInErrors++;
50626516Sbrian    LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n");
5076059Samurai    HdlcStat.badfcs++;
5086059Samurai    pfree(bp);
5096059Samurai    return;
5106059Samurai  }
5116735Samurai  if (!DEV_IS_SYNC)
5126735Samurai    bp->cnt -= 2;		/* discard FCS part */
51325019Sphk
51425019Sphk  if (bp->cnt < 2) {		/* XXX: raise this bar ? */
51528679Sbrian    pfree(bp);
51628679Sbrian    return;
51725019Sphk  }
5186059Samurai  cp = MBUF_CTOP(bp);
5196059Samurai
5206059Samurai  ifInPackets++;
5216059Samurai  ifInOctets += bp->cnt;
5226059Samurai
5236059Samurai  if (!LcpInfo.want_acfcomp) {
52428679Sbrian
5256059Samurai    /*
52628679Sbrian     * We expect that packet is not compressed.
5276059Samurai     */
5286059Samurai    addr = *cp++;
5296059Samurai    if (addr != HDLC_ADDR) {
5306059Samurai      HisLqrSave.SaveInErrors++;
5316059Samurai      HdlcStat.badaddr++;
53226516Sbrian      LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp);
5336059Samurai      pfree(bp);
5346059Samurai      return;
5356059Samurai    }
5366059Samurai    ctrl = *cp++;
5376059Samurai    if (ctrl != HDLC_UI) {
5386059Samurai      HisLqrSave.SaveInErrors++;
5396059Samurai      HdlcStat.badcommand++;
54026516Sbrian      LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp);
5416059Samurai      pfree(bp);
5426059Samurai      return;
5436059Samurai    }
5446059Samurai    bp->offset += 2;
5456059Samurai    bp->cnt -= 2;
5466059Samurai  } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
54728679Sbrian
5486059Samurai    /*
54928679Sbrian     * We can receive compressed packet, but peer still send uncompressed
55028679Sbrian     * packet to me.
5516059Samurai     */
5526059Samurai    cp += 2;
5536059Samurai    bp->offset += 2;
5546059Samurai    bp->cnt -= 2;
5556059Samurai  }
5566059Samurai  if (LcpInfo.want_protocomp) {
5576059Samurai    proto = 0;
5586059Samurai    cp--;
5596059Samurai    do {
56028679Sbrian      cp++;
56128679Sbrian      bp->offset++;
56228679Sbrian      bp->cnt--;
5636059Samurai      proto = proto << 8;
5646059Samurai      proto += *cp;
5656059Samurai    } while (!(proto & 1));
5666059Samurai  } else {
5676059Samurai    proto = *cp++ << 8;
5686059Samurai    proto |= *cp++;
5696059Samurai    bp->offset += 2;
5706059Samurai    bp->cnt -= 2;
5716059Samurai  }
5726059Samurai
5736059Samurai  for (statp = ProtocolStat; statp->number; statp++)
5746059Samurai    if (statp->number == proto)
5756059Samurai      break;
5766059Samurai  statp->in_count++;
5776059Samurai  HisLqrSave.SaveInPackets++;
5786059Samurai
5796059Samurai  DecodePacket(proto, bp);
5806059Samurai}
581