hdlc.c revision 43313
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 *
2043313Sbrian * $Id: hdlc.c,v 1.36 1998/08/07 18:42:48 brian Exp $
218857Srgrimes *
226059Samurai *	TODO:
236059Samurai */
2443313Sbrian#include <sys/param.h>
2530715Sbrian#include <netinet/in.h>
2636285Sbrian#include <netinet/in_systm.h>
2736285Sbrian#include <netinet/ip.h>
2836285Sbrian#include <sys/un.h>
2930715Sbrian
3030715Sbrian#include <stdio.h>
3130715Sbrian#include <string.h>
3230715Sbrian#include <termios.h>
3330715Sbrian
3437009Sbrian#include "defs.h"
3531343Sbrian#include "command.h"
3630715Sbrian#include "mbuf.h"
3730715Sbrian#include "log.h"
3830715Sbrian#include "timer.h"
396059Samurai#include "fsm.h"
4036285Sbrian#include "lqr.h"
416059Samurai#include "hdlc.h"
426059Samurai#include "lcpproto.h"
4336285Sbrian#include "iplist.h"
4436285Sbrian#include "throughput.h"
4536285Sbrian#include "slcompress.h"
4630733Sbrian#include "ipcp.h"
4730733Sbrian#include "ip.h"
4830733Sbrian#include "vjcomp.h"
4936285Sbrian#include "auth.h"
5030733Sbrian#include "pap.h"
5130733Sbrian#include "chap.h"
526059Samurai#include "lcp.h"
5331343Sbrian#include "async.h"
5413389Sphk#include "ccp.h"
5536285Sbrian#include "link.h"
5636285Sbrian#include "descriptor.h"
5736285Sbrian#include "physical.h"
5836285Sbrian#include "prompt.h"
5936285Sbrian#include "chat.h"
6036285Sbrian#include "mp.h"
6138174Sbrian#include "cbcp.h"
6236285Sbrian#include "datalink.h"
6336285Sbrian#include "filter.h"
6443313Sbrian#ifndef NORADIUS
6543313Sbrian#include "radius.h"
6643313Sbrian#endif
6736285Sbrian#include "bundle.h"
686059Samurai
6937210Sbrianstatic u_int16_t const fcstab[256] = {
7028679Sbrian   /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
7128679Sbrian   /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
7228679Sbrian   /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
7328679Sbrian   /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
7428679Sbrian   /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
7528679Sbrian   /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
7628679Sbrian   /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
7728679Sbrian   /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
7828679Sbrian   /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
7928679Sbrian   /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
8028679Sbrian   /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
8128679Sbrian   /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
8228679Sbrian   /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
8328679Sbrian   /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
8428679Sbrian   /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
8528679Sbrian   /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
8628679Sbrian   /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
8728679Sbrian   /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
8828679Sbrian   /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
8928679Sbrian   /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
9028679Sbrian   /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
9128679Sbrian   /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
9228679Sbrian   /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
9328679Sbrian   /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
9428679Sbrian   /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
9528679Sbrian   /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
9628679Sbrian   /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
9728679Sbrian   /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
9828679Sbrian   /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
9928679Sbrian   /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
10028679Sbrian   /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
10128679Sbrian   /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
1026059Samurai};
1036059Samurai
1046059Samuraivoid
10536285Sbrianhdlc_Init(struct hdlc *hdlc, struct lcp *lcp)
1066059Samurai{
10736285Sbrian  memset(hdlc, '\0', sizeof(struct hdlc));
10836285Sbrian  hdlc->lqm.owner = lcp;
1096059Samurai}
1106059Samurai
1116059Samurai/*
1126059Samurai *  HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section
1136059Samurai *  2.27 for further details.
1146059Samurai */
11528679Sbrianinline u_short
11636285Sbrianhdlc_Fcs(u_short fcs, u_char * cp, int len)
1176059Samurai{
1186059Samurai  while (len--)
1196059Samurai    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
12028679Sbrian  return (fcs);
1216059Samurai}
1226059Samurai
12332663Sbrianstatic inline u_short
12431514SbrianHdlcFcsBuf(u_short fcs, struct mbuf *m)
12531514Sbrian{
12631514Sbrian  int len;
12731514Sbrian  u_char *pos, *end;
12831514Sbrian
12936285Sbrian  len = mbuf_Length(m);
13031514Sbrian  pos = MBUF_CTOP(m);
13131514Sbrian  end = pos + m->cnt;
13231514Sbrian  while (len--) {
13331514Sbrian    fcs = (fcs >> 8) ^ fcstab[(fcs ^ *pos++) & 0xff];
13431514Sbrian    if (pos == end && len) {
13531514Sbrian      m = m->next;
13631514Sbrian      pos = MBUF_CTOP(m);
13731514Sbrian      end = pos + m->cnt;
13831514Sbrian    }
13931514Sbrian  }
14031514Sbrian  return (fcs);
14131514Sbrian}
14231514Sbrian
1436059Samuraivoid
14436285Sbrianhdlc_Output(struct link *l, int pri, u_short proto, struct mbuf *bp)
1456059Samurai{
14636285Sbrian  struct physical *p = link2physical(l);
1476059Samurai  struct mbuf *mhp, *mfcs;
1486059Samurai  u_char *cp;
1496059Samurai  u_short fcs;
1506059Samurai
15136285Sbrian  if (!p || physical_IsSync(p))
15232663Sbrian    mfcs = NULL;
1536735Samurai  else
15436285Sbrian    mfcs = mbuf_Alloc(2, MB_HDLCOUT);
15531514Sbrian
15636285Sbrian  mhp = mbuf_Alloc(4, MB_HDLCOUT);
1576059Samurai  mhp->cnt = 0;
1586059Samurai  cp = MBUF_CTOP(mhp);
15936285Sbrian  if (p && (proto == PROTO_LCP || l->lcp.his_acfcomp == 0)) {
1606059Samurai    *cp++ = HDLC_ADDR;
1616059Samurai    *cp++ = HDLC_UI;
1626059Samurai    mhp->cnt += 2;
1636059Samurai  }
16428679Sbrian
1656059Samurai  /*
16628679Sbrian   * If possible, compress protocol field.
1676059Samurai   */
16836285Sbrian  if (l->lcp.his_protocomp && (proto & 0xff00) == 0) {
1696059Samurai    *cp++ = proto;
1706059Samurai    mhp->cnt++;
1716059Samurai  } else {
1726059Samurai    *cp++ = proto >> 8;
1736059Samurai    *cp = proto & 0377;
1746059Samurai    mhp->cnt += 2;
1756059Samurai  }
17636285Sbrian
1776059Samurai  mhp->next = bp;
17836285Sbrian
17936285Sbrian  if (!p) {
18036285Sbrian    /*
18136285Sbrian     * This is where we multiplex the data over our available physical
18236285Sbrian     * links.  We don't frame our logical link data.  Instead we wait
18336285Sbrian     * for the logical link implementation to chop our data up and pile
18436285Sbrian     * it into the physical links by re-calling this function with the
18536285Sbrian     * encapsulated fragments.
18636285Sbrian     */
18736285Sbrian    link_Output(l, pri, mhp);
18836285Sbrian    return;
18936285Sbrian  }
19036285Sbrian
19136285Sbrian  /* Tack mfcs onto the end, then set bp back to the start of the data */
19231514Sbrian  while (bp->next != NULL)
19331514Sbrian    bp = bp->next;
1946059Samurai  bp->next = mfcs;
19531514Sbrian  bp = mhp->next;
1966059Samurai
19736285Sbrian  p->hdlc.lqm.OutOctets += mbuf_Length(mhp) + 1;
19836285Sbrian  p->hdlc.lqm.OutPackets++;
1996059Samurai
2006059Samurai  if (proto == PROTO_LQR) {
20136285Sbrian    /* Overwrite the entire packet */
20236285Sbrian    struct lqrdata lqr;
20336285Sbrian
20436285Sbrian    lqr.MagicNumber = p->link.lcp.want_magic;
20536285Sbrian    lqr.LastOutLQRs = p->hdlc.lqm.lqr.peer.PeerOutLQRs;
20636285Sbrian    lqr.LastOutPackets = p->hdlc.lqm.lqr.peer.PeerOutPackets;
20736285Sbrian    lqr.LastOutOctets = p->hdlc.lqm.lqr.peer.PeerOutOctets;
20836285Sbrian    lqr.PeerInLQRs = p->hdlc.lqm.lqr.SaveInLQRs;
20936285Sbrian    lqr.PeerInPackets = p->hdlc.lqm.SaveInPackets;
21036285Sbrian    lqr.PeerInDiscards = p->hdlc.lqm.SaveInDiscards;
21136285Sbrian    lqr.PeerInErrors = p->hdlc.lqm.SaveInErrors;
21236285Sbrian    lqr.PeerInOctets = p->hdlc.lqm.SaveInOctets;
21336285Sbrian    lqr.PeerOutPackets = p->hdlc.lqm.OutPackets;
21436285Sbrian    lqr.PeerOutOctets = p->hdlc.lqm.OutOctets;
21536285Sbrian    if (p->hdlc.lqm.lqr.peer.LastOutLQRs == p->hdlc.lqm.lqr.OutLQRs) {
21634494Sbrian      /*
21734494Sbrian       * only increment if it's the first time or we've got a reply
21834494Sbrian       * from the last one
21934494Sbrian       */
22036285Sbrian      lqr.PeerOutLQRs = ++p->hdlc.lqm.lqr.OutLQRs;
22136285Sbrian      lqr_Dump(l->name, "Output", &lqr);
22234494Sbrian    } else {
22336285Sbrian      lqr.PeerOutLQRs = p->hdlc.lqm.lqr.OutLQRs;
22436285Sbrian      lqr_Dump(l->name, "Output (again)", &lqr);
22534494Sbrian    }
22636285Sbrian    lqr_ChangeOrder(&lqr, (struct lqrdata *)MBUF_CTOP(bp));
2276059Samurai  }
22836285Sbrian
22936285Sbrian  if (mfcs) {
23032381Sbrian    mfcs->cnt = 0;
23132381Sbrian    fcs = HdlcFcsBuf(INITFCS, mhp);
2326735Samurai    fcs = ~fcs;
2336735Samurai    cp = MBUF_CTOP(mfcs);
23428679Sbrian    *cp++ = fcs & 0377;		/* Low byte first!! */
2356735Samurai    *cp++ = fcs >> 8;
23632381Sbrian    mfcs->cnt = 2;
2376735Samurai  }
23831514Sbrian
23936285Sbrian  log_DumpBp(LogHDLC, "hdlc_Output", mhp);
24031514Sbrian
24136285Sbrian  link_ProtocolRecord(l, proto, PROTO_OUT);
24236285Sbrian  log_Printf(LogDEBUG, "hdlc_Output: proto = 0x%04x\n", proto);
24336285Sbrian
24436285Sbrian  if (physical_IsSync(p))
24536285Sbrian    link_Output(l, pri, mhp);          /* Send it raw */
2466735Samurai  else
24736285Sbrian    async_Output(pri, mhp, proto, p);
2486059Samurai}
2496059Samurai
25031537Sbrian/* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */
25131537Sbrianstatic struct {
25231537Sbrian  u_short from;
25331537Sbrian  u_short to;
25431537Sbrian  const char *name;
25531537Sbrian} protocols[] = {
25631537Sbrian  { 0x0001, 0x0001, "Padding Protocol" },
25731537Sbrian  { 0x0003, 0x001f, "reserved (transparency inefficient)" },
25831537Sbrian  { 0x0021, 0x0021, "Internet Protocol" },
25931537Sbrian  { 0x0023, 0x0023, "OSI Network Layer" },
26031537Sbrian  { 0x0025, 0x0025, "Xerox NS IDP" },
26131537Sbrian  { 0x0027, 0x0027, "DECnet Phase IV" },
26231537Sbrian  { 0x0029, 0x0029, "Appletalk" },
26331537Sbrian  { 0x002b, 0x002b, "Novell IPX" },
26431537Sbrian  { 0x002d, 0x002d, "Van Jacobson Compressed TCP/IP" },
26531537Sbrian  { 0x002f, 0x002f, "Van Jacobson Uncompressed TCP/IP" },
26631537Sbrian  { 0x0031, 0x0031, "Bridging PDU" },
26731537Sbrian  { 0x0033, 0x0033, "Stream Protocol (ST-II)" },
26831537Sbrian  { 0x0035, 0x0035, "Banyan Vines" },
26931537Sbrian  { 0x0037, 0x0037, "reserved (until 1993)" },
27031537Sbrian  { 0x0039, 0x0039, "AppleTalk EDDP" },
27131537Sbrian  { 0x003b, 0x003b, "AppleTalk SmartBuffered" },
27231537Sbrian  { 0x003d, 0x003d, "Multi-Link" },
27331537Sbrian  { 0x003f, 0x003f, "NETBIOS Framing" },
27431537Sbrian  { 0x0041, 0x0041, "Cisco Systems" },
27531537Sbrian  { 0x0043, 0x0043, "Ascom Timeplex" },
27631537Sbrian  { 0x0045, 0x0045, "Fujitsu Link Backup and Load Balancing (LBLB)" },
27731537Sbrian  { 0x0047, 0x0047, "DCA Remote Lan" },
27831537Sbrian  { 0x0049, 0x0049, "Serial Data Transport Protocol (PPP-SDTP)" },
27931537Sbrian  { 0x004b, 0x004b, "SNA over 802.2" },
28031537Sbrian  { 0x004d, 0x004d, "SNA" },
28131537Sbrian  { 0x004f, 0x004f, "IP6 Header Compression" },
28231537Sbrian  { 0x0051, 0x0051, "KNX Bridging Data" },
28331537Sbrian  { 0x0053, 0x0053, "Encryption" },
28431537Sbrian  { 0x0055, 0x0055, "Individual Link Encryption" },
28531537Sbrian  { 0x006f, 0x006f, "Stampede Bridging" },
28631537Sbrian  { 0x0071, 0x0071, "BAP Bandwidth Allocation Protocol" },
28731537Sbrian  { 0x0073, 0x0073, "MP+ Protocol" },
28831537Sbrian  { 0x007d, 0x007d, "reserved (Control Escape)" },
28931537Sbrian  { 0x007f, 0x007f, "reserved (compression inefficient)" },
29031537Sbrian  { 0x00cf, 0x00cf, "reserved (PPP NLPID)" },
29131537Sbrian  { 0x00fb, 0x00fb, "compression on single link in multilink group" },
29231537Sbrian  { 0x00fd, 0x00fd, "1st choice compression" },
29331537Sbrian  { 0x00ff, 0x00ff, "reserved (compression inefficient)" },
29431537Sbrian  { 0x0200, 0x02ff, "(compression inefficient)" },
29531537Sbrian  { 0x0201, 0x0201, "802.1d Hello Packets" },
29631537Sbrian  { 0x0203, 0x0203, "IBM Source Routing BPDU" },
29731537Sbrian  { 0x0205, 0x0205, "DEC LANBridge100 Spanning Tree" },
29831537Sbrian  { 0x0207, 0x0207, "Cisco Discovery Protocol" },
29931537Sbrian  { 0x0209, 0x0209, "Netcs Twin Routing" },
30031537Sbrian  { 0x0231, 0x0231, "Luxcom" },
30131537Sbrian  { 0x0233, 0x0233, "Sigma Network Systems" },
30231537Sbrian  { 0x0235, 0x0235, "Apple Client Server Protocol" },
30331537Sbrian  { 0x1e00, 0x1eff, "(compression inefficient)" },
30431537Sbrian  { 0x4001, 0x4001, "Cray Communications Control Protocol" },
30531537Sbrian  { 0x4003, 0x4003, "CDPD Mobile Network Registration Protocol" },
30631537Sbrian  { 0x4021, 0x4021, "Stacker LZS" },
30731537Sbrian  { 0x8001, 0x801f, "Not Used - reserved" },
30831537Sbrian  { 0x8021, 0x8021, "Internet Protocol Control Protocol" },
30931537Sbrian  { 0x8023, 0x8023, "OSI Network Layer Control Protocol" },
31031537Sbrian  { 0x8025, 0x8025, "Xerox NS IDP Control Protocol" },
31131537Sbrian  { 0x8027, 0x8027, "DECnet Phase IV Control Protocol" },
31231537Sbrian  { 0x8029, 0x8029, "Appletalk Control Protocol" },
31331537Sbrian  { 0x802b, 0x802b, "Novell IPX Control Protocol" },
31431537Sbrian  { 0x802d, 0x802d, "reserved" },
31531537Sbrian  { 0x802f, 0x802f, "reserved" },
31631537Sbrian  { 0x8031, 0x8031, "Bridging NCP" },
31731537Sbrian  { 0x8033, 0x8033, "Stream Protocol Control Protocol" },
31831537Sbrian  { 0x8035, 0x8035, "Banyan Vines Control Protocol" },
31931537Sbrian  { 0x8037, 0x8037, "reserved till 1993" },
32031537Sbrian  { 0x8039, 0x8039, "reserved" },
32131537Sbrian  { 0x803b, 0x803b, "reserved" },
32231537Sbrian  { 0x803d, 0x803d, "Multi-Link Control Protocol" },
32331537Sbrian  { 0x803f, 0x803f, "NETBIOS Framing Control Protocol" },
32431537Sbrian  { 0x8041, 0x8041, "Cisco Systems Control Protocol" },
32531537Sbrian  { 0x8043, 0x8043, "Ascom Timeplex" },
32631537Sbrian  { 0x8045, 0x8045, "Fujitsu LBLB Control Protocol" },
32731537Sbrian  { 0x8047, 0x8047, "DCA Remote Lan Network Control Protocol (RLNCP)" },
32831537Sbrian  { 0x8049, 0x8049, "Serial Data Control Protocol (PPP-SDCP)" },
32931537Sbrian  { 0x804b, 0x804b, "SNA over 802.2 Control Protocol" },
33031537Sbrian  { 0x804d, 0x804d, "SNA Control Protocol" },
33131537Sbrian  { 0x804f, 0x804f, "IP6 Header Compression Control Protocol" },
33231537Sbrian  { 0x8051, 0x8051, "KNX Bridging Control Protocol" },
33331537Sbrian  { 0x8053, 0x8053, "Encryption Control Protocol" },
33431537Sbrian  { 0x8055, 0x8055, "Individual Link Encryption Control Protocol" },
33531537Sbrian  { 0x806f, 0x806f, "Stampede Bridging Control Protocol" },
33631537Sbrian  { 0x8073, 0x8073, "MP+ Control Protocol" },
33731537Sbrian  { 0x8071, 0x8071, "BACP Bandwidth Allocation Control Protocol" },
33831537Sbrian  { 0x807d, 0x807d, "Not Used - reserved" },
33931537Sbrian  { 0x80cf, 0x80cf, "Not Used - reserved" },
34031537Sbrian  { 0x80fb, 0x80fb, "compression on single link in multilink group control" },
34131537Sbrian  { 0x80fd, 0x80fd, "Compression Control Protocol" },
34231537Sbrian  { 0x80ff, 0x80ff, "Not Used - reserved" },
34331537Sbrian  { 0x8207, 0x8207, "Cisco Discovery Protocol Control" },
34431537Sbrian  { 0x8209, 0x8209, "Netcs Twin Routing" },
34531537Sbrian  { 0x8235, 0x8235, "Apple Client Server Protocol Control" },
34631537Sbrian  { 0xc021, 0xc021, "Link Control Protocol" },
34731537Sbrian  { 0xc023, 0xc023, "Password Authentication Protocol" },
34831537Sbrian  { 0xc025, 0xc025, "Link Quality Report" },
34931537Sbrian  { 0xc027, 0xc027, "Shiva Password Authentication Protocol" },
35031537Sbrian  { 0xc029, 0xc029, "CallBack Control Protocol (CBCP)" },
35131537Sbrian  { 0xc081, 0xc081, "Container Control Protocol" },
35231537Sbrian  { 0xc223, 0xc223, "Challenge Handshake Authentication Protocol" },
35331537Sbrian  { 0xc225, 0xc225, "RSA Authentication Protocol" },
35431537Sbrian  { 0xc227, 0xc227, "Extensible Authentication Protocol" },
35531537Sbrian  { 0xc26f, 0xc26f, "Stampede Bridging Authorization Protocol" },
35631537Sbrian  { 0xc281, 0xc281, "Proprietary Authentication Protocol" },
35731537Sbrian  { 0xc283, 0xc283, "Proprietary Authentication Protocol" },
35831537Sbrian  { 0xc481, 0xc481, "Proprietary Node ID Authentication Protocol" }
35931537Sbrian};
36031537Sbrian
36131962Sbrian#define NPROTOCOLS (sizeof protocols/sizeof protocols[0])
36231537Sbrian
36336285Sbrianconst char *
36436285Sbrianhdlc_Protocol2Nam(u_short proto)
36531537Sbrian{
36631537Sbrian  int f;
36731537Sbrian
36831537Sbrian  for (f = 0; f < NPROTOCOLS; f++)
36931537Sbrian    if (proto >= protocols[f].from && proto <= protocols[f].to)
37031537Sbrian      return protocols[f].name;
37131537Sbrian    else if (proto < protocols[f].from)
37231537Sbrian      break;
37331537Sbrian  return "unrecognised protocol";
37431537Sbrian}
37531537Sbrian
37636285Sbrianvoid
37736285Sbrianhdlc_DecodePacket(struct bundle *bundle, u_short proto, struct mbuf * bp,
37836285Sbrian                  struct link *l)
3796059Samurai{
38036285Sbrian  struct physical *p = link2physical(l);
3819448Samurai  u_char *cp;
38237994Sbrian  const char *type;
3839448Samurai
38436285Sbrian  log_Printf(LogDEBUG, "DecodePacket: proto = 0x%04x\n", proto);
38526516Sbrian
38636285Sbrian  /* decompress everything.  CCP needs uncompressed data too */
38736285Sbrian  if ((bp = ccp_Decompress(&l->ccp, &proto, bp)) == NULL)
38836285Sbrian    return;
38931514Sbrian
3906059Samurai  switch (proto) {
3916059Samurai  case PROTO_LCP:
39236285Sbrian    lcp_Input(&l->lcp, bp);
3936059Samurai    break;
3946059Samurai  case PROTO_PAP:
39536285Sbrian    if (p)
39636285Sbrian      pap_Input(bundle, bp, p);
39736285Sbrian    else {
39836285Sbrian      log_Printf(LogERROR, "DecodePacket: PAP: Not a physical link !\n");
39936285Sbrian      mbuf_Free(bp);
40036285Sbrian    }
4016059Samurai    break;
40238174Sbrian  case PROTO_CBCP:
40338174Sbrian    if (p)
40438174Sbrian      cbcp_Input(p, bp);
40538174Sbrian    else {
40638174Sbrian      log_Printf(LogERROR, "DecodePacket: CBCP: Not a physical link !\n");
40738174Sbrian      mbuf_Free(bp);
40838174Sbrian    }
40938174Sbrian    break;
4106059Samurai  case PROTO_LQR:
41136285Sbrian    if (p) {
41236285Sbrian      p->hdlc.lqm.lqr.SaveInLQRs++;
41336285Sbrian      lqr_Input(p, bp);
41436285Sbrian    } else {
41536285Sbrian      log_Printf(LogERROR, "DecodePacket: LQR: Not a physical link !\n");
41636285Sbrian      mbuf_Free(bp);
41736285Sbrian    }
4186059Samurai    break;
4196059Samurai  case PROTO_CHAP:
42036285Sbrian    if (p)
42136285Sbrian      chap_Input(bundle, bp, p);
42236285Sbrian    else {
42336285Sbrian      log_Printf(LogERROR, "DecodePacket: CHAP: Not a physical link !\n");
42436285Sbrian      mbuf_Free(bp);
42536285Sbrian    }
4266059Samurai    break;
4276059Samurai  case PROTO_VJUNCOMP:
4286059Samurai  case PROTO_VJCOMP:
42936285Sbrian    bp = vj_Input(&bundle->ncp.ipcp, bp, proto);
43036285Sbrian    if (bp == NULL)
4316735Samurai      break;
4326059Samurai    /* fall down */
4336059Samurai  case PROTO_IP:
43436285Sbrian    ip_Input(bundle, bp);
4356059Samurai    break;
4366059Samurai  case PROTO_IPCP:
43736285Sbrian    ipcp_Input(&bundle->ncp.ipcp, bundle, bp);
4386059Samurai    break;
4396059Samurai  case PROTO_CCP:
44036285Sbrian    ccp_Input(&l->ccp, bundle, bp);
4416059Samurai    break;
44236285Sbrian  case PROTO_MP:
44336285Sbrian    if (bundle->ncp.mp.active) {
44436285Sbrian      if (p)
44536285Sbrian        mp_Input(&bundle->ncp.mp, bp, p);
44636285Sbrian      else {
44737019Sbrian        log_Printf(LogWARN, "DecodePacket: Can't do MP inside MP !\n");
44836285Sbrian        mbuf_Free(bp);
44936285Sbrian      }
45036285Sbrian      break;
45136285Sbrian    }
45236285Sbrian    /* Fall through */
4536059Samurai  default:
45437994Sbrian    switch (proto) {
45537994Sbrian      case PROTO_MP:
45637994Sbrian      case PROTO_COMPD:
45737994Sbrian      case PROTO_ICOMPD:
45837994Sbrian        type = "Unexpected";
45937994Sbrian        break;
46037994Sbrian      default:
46137994Sbrian        type = "Unknown";
46237994Sbrian        break;
46337994Sbrian    }
46437994Sbrian    log_Printf(LogPHASE, "%s protocol 0x%04x (%s)\n", type, proto,
46537994Sbrian               hdlc_Protocol2Nam(proto));
4669448Samurai    bp->offset -= 2;
4679448Samurai    bp->cnt += 2;
4689448Samurai    cp = MBUF_CTOP(bp);
46936285Sbrian    lcp_SendProtoRej(&l->lcp, cp, bp->cnt);
47036285Sbrian    if (p) {
47136285Sbrian      p->hdlc.lqm.SaveInDiscards++;
47236285Sbrian      p->hdlc.stats.unknownproto++;
47336285Sbrian    }
47436285Sbrian    mbuf_Free(bp);
4756059Samurai    break;
4766059Samurai  }
4776059Samurai}
4786059Samurai
47936285Sbrianstatic int
48036285Sbrianhdlc_GetProto(const u_char *cp, u_short *proto)
4816059Samurai{
48236285Sbrian  *proto = *cp;
48336285Sbrian  if (!(*proto & 1)) {
48436285Sbrian    *proto = (*proto << 8) | cp[1];
48536285Sbrian    return 2;
48626516Sbrian  }
48736285Sbrian  return 1;
4886059Samurai}
4896059Samurai
4906059Samuraivoid
49136285Sbrianhdlc_Input(struct bundle *bundle, struct mbuf * bp, struct physical *physical)
4926059Samurai{
4936059Samurai  u_short fcs, proto;
4946059Samurai  u_char *cp, addr, ctrl;
49536285Sbrian  int n;
4966059Samurai
49736285Sbrian  log_DumpBp(LogHDLC, "hdlc_Input:", bp);
49836285Sbrian  if (physical_IsSync(physical))
4996735Samurai    fcs = GOODFCS;
5006735Samurai  else
50136285Sbrian    fcs = hdlc_Fcs(INITFCS, MBUF_CTOP(bp), bp->cnt);
50236285Sbrian  physical->hdlc.lqm.SaveInOctets += bp->cnt + 1;
5036059Samurai
50436285Sbrian  log_Printf(LogDEBUG, "%s: hdlc_Input: fcs = %04x (%s)\n",
50536285Sbrian            physical->link.name, fcs, (fcs == GOODFCS) ? "good" : "BAD!");
5066059Samurai  if (fcs != GOODFCS) {
50736285Sbrian    physical->hdlc.lqm.SaveInErrors++;
50836285Sbrian    physical->hdlc.stats.badfcs++;
50936285Sbrian    mbuf_Free(bp);
5106059Samurai    return;
5116059Samurai  }
51236285Sbrian  if (!physical_IsSync(physical))
5136735Samurai    bp->cnt -= 2;		/* discard FCS part */
51425019Sphk
51525019Sphk  if (bp->cnt < 2) {		/* XXX: raise this bar ? */
51636285Sbrian    mbuf_Free(bp);
51728679Sbrian    return;
51825019Sphk  }
5196059Samurai  cp = MBUF_CTOP(bp);
5206059Samurai
52136285Sbrian  if (!physical->link.lcp.want_acfcomp) {
52236285Sbrian    /* We expect the packet not to be compressed */
5236059Samurai    addr = *cp++;
5246059Samurai    if (addr != HDLC_ADDR) {
52536285Sbrian      physical->hdlc.lqm.SaveInErrors++;
52636285Sbrian      physical->hdlc.stats.badaddr++;
52736285Sbrian      log_Printf(LogDEBUG, "hdlc_Input: addr %02x\n", *cp);
52836285Sbrian      mbuf_Free(bp);
5296059Samurai      return;
5306059Samurai    }
5316059Samurai    ctrl = *cp++;
5326059Samurai    if (ctrl != HDLC_UI) {
53336285Sbrian      physical->hdlc.lqm.SaveInErrors++;
53436285Sbrian      physical->hdlc.stats.badcommand++;
53536285Sbrian      log_Printf(LogDEBUG, "hdlc_Input: %02x\n", *cp);
53636285Sbrian      mbuf_Free(bp);
5376059Samurai      return;
5386059Samurai    }
5396059Samurai    bp->offset += 2;
5406059Samurai    bp->cnt -= 2;
5416059Samurai  } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) {
5426059Samurai    /*
54336285Sbrian     * We can receive compressed packets, but the peer still sends
54436285Sbrian     * uncompressed packets !
5456059Samurai     */
5466059Samurai    cp += 2;
5476059Samurai    bp->offset += 2;
5486059Samurai    bp->cnt -= 2;
5496059Samurai  }
5506059Samurai
55136285Sbrian  n = hdlc_GetProto(cp, &proto);
55236285Sbrian  bp->offset += n;
55336285Sbrian  bp->cnt -= n;
55436285Sbrian  if (!physical->link.lcp.want_protocomp && n == 1)
55536285Sbrian    log_Printf(LogHDLC, "%s: Warning: received a proto-compressed packet !\n",
55636285Sbrian               physical->link.name);
55736285Sbrian
55836285Sbrian  link_ProtocolRecord(&physical->link, proto, PROTO_IN);
55936285Sbrian  physical->hdlc.lqm.SaveInPackets++;
56036285Sbrian
56136285Sbrian  hdlc_DecodePacket(bundle, proto, bp, &physical->link);
56236285Sbrian}
56336285Sbrian
56436285Sbrian/*
56536285Sbrian *  Detect a HDLC frame
56636285Sbrian */
56736285Sbrian
56836285Sbrianstatic const char *FrameHeaders[] = {
56936285Sbrian  "\176\377\003\300\041",
57036285Sbrian  "\176\377\175\043\300\041",
57136285Sbrian  "\176\177\175\043\100\041",
57236285Sbrian  "\176\175\337\175\043\300\041",
57336285Sbrian  "\176\175\137\175\043\100\041",
57436285Sbrian  NULL,
57536285Sbrian};
57636285Sbrian
57736285Sbrianu_char *
57836285Sbrianhdlc_Detect(struct physical *physical, u_char *cp, int n)
57936285Sbrian{
58036285Sbrian  const char *fp, **hp;
58136285Sbrian  char *ptr;
58236285Sbrian
58336285Sbrian  cp[n] = '\0';				/* be sure to null terminate */
58436285Sbrian  ptr = NULL;
58536285Sbrian  for (hp = FrameHeaders; *hp; hp++) {
58636285Sbrian    fp = *hp;
58736285Sbrian    if (physical_IsSync(physical))
58836285Sbrian      fp++;
58936285Sbrian    ptr = strstr((char *)cp, fp);	/* XXX: cp may have embedded NULs */
59036285Sbrian    if (ptr)
5916059Samurai      break;
59236285Sbrian  }
59336285Sbrian  return (u_char *)ptr;
59436285Sbrian}
5956059Samurai
59636285Sbrianint
59736285Sbrianhdlc_ReportStatus(struct cmdargs const *arg)
59836285Sbrian{
59936285Sbrian  struct hdlc *hdlc = &arg->cx->physical->hdlc;
60036285Sbrian
60136285Sbrian  prompt_Printf(arg->prompt, "%s HDLC level errors:\n", arg->cx->name);
60236285Sbrian  prompt_Printf(arg->prompt, " Bad Frame Check Sequence fields: %u\n",
60336285Sbrian	        hdlc->stats.badfcs);
60436285Sbrian  prompt_Printf(arg->prompt, " Bad address (!= 0x%02x) fields:    %u\n",
60536285Sbrian	        HDLC_ADDR, hdlc->stats.badaddr);
60636285Sbrian  prompt_Printf(arg->prompt, " Bad command (!= 0x%02x) fields:    %u\n",
60736285Sbrian	        HDLC_UI, hdlc->stats.badcommand);
60836285Sbrian  prompt_Printf(arg->prompt, " Unrecognised protocol fields:    %u\n",
60936285Sbrian	        hdlc->stats.unknownproto);
61036285Sbrian  return 0;
6116059Samurai}
61236285Sbrian
61336285Sbrianstatic void
61436285Sbrianhdlc_ReportTime(void *v)
61536285Sbrian{
61636285Sbrian  /* Moan about HDLC errors */
61736285Sbrian  struct hdlc *hdlc = (struct hdlc *)v;
61836285Sbrian
61936285Sbrian  timer_Stop(&hdlc->ReportTimer);
62036285Sbrian
62136285Sbrian  if (memcmp(&hdlc->laststats, &hdlc->stats, sizeof hdlc->stats)) {
62236285Sbrian    log_Printf(LogPHASE,
62336285Sbrian              "%s: HDLC errors -> FCS: %u, ADDR: %u, COMD: %u, PROTO: %u\n",
62436285Sbrian              hdlc->lqm.owner->fsm.link->name,
62536285Sbrian	      hdlc->stats.badfcs - hdlc->laststats.badfcs,
62636285Sbrian              hdlc->stats.badaddr - hdlc->laststats.badaddr,
62736285Sbrian              hdlc->stats.badcommand - hdlc->laststats.badcommand,
62836285Sbrian              hdlc->stats.unknownproto - hdlc->laststats.unknownproto);
62936285Sbrian    hdlc->laststats = hdlc->stats;
63036285Sbrian  }
63136285Sbrian
63236285Sbrian  timer_Start(&hdlc->ReportTimer);
63336285Sbrian}
63436285Sbrian
63536285Sbrianvoid
63636285Sbrianhdlc_StartTimer(struct hdlc *hdlc)
63736285Sbrian{
63836285Sbrian  timer_Stop(&hdlc->ReportTimer);
63936285Sbrian  hdlc->ReportTimer.load = 60 * SECTICKS;
64036285Sbrian  hdlc->ReportTimer.arg = hdlc;
64136285Sbrian  hdlc->ReportTimer.func = hdlc_ReportTime;
64236285Sbrian  hdlc->ReportTimer.name = "hdlc";
64336285Sbrian  timer_Start(&hdlc->ReportTimer);
64436285Sbrian}
64536285Sbrian
64636285Sbrianvoid
64736285Sbrianhdlc_StopTimer(struct hdlc *hdlc)
64836285Sbrian{
64936285Sbrian  timer_Stop(&hdlc->ReportTimer);
65036285Sbrian}
651