hdlc.c revision 30715
1/* 2 * PPP High Level Link Control (HDLC) Module 3 * 4 * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5 * 6 * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7 * 8 * Redistribution and use in source and binary forms are permitted 9 * provided that the above copyright notice and this paragraph are 10 * duplicated in all such forms and that any documentation, 11 * advertising materials, and other materials related to such 12 * distribution and use acknowledge that the software was developed 13 * by the Internet Initiative Japan, Inc. The name of the 14 * IIJ may not be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19 * 20 * $Id: hdlc.c,v 1.19 1997/09/10 21:36:54 brian Exp $ 21 * 22 * TODO: 23 */ 24#include <sys/param.h> 25#include <netinet/in.h> 26 27#include <stdio.h> 28#include <string.h> 29#include <termios.h> 30 31#include "mbuf.h" 32#include "log.h" 33#include "defs.h" 34#include "timer.h" 35#include "fsm.h" 36#include "hdlc.h" 37#include "lcpproto.h" 38#include "lcp.h" 39#include "lqr.h" 40#include "loadalias.h" 41#include "command.h" 42#include "vars.h" 43#include "pred.h" 44#include "modem.h" 45#include "ccp.h" 46 47struct hdlcstat { 48 int badfcs; 49 int badaddr; 50 int badcommand; 51 int unknownproto; 52} HdlcStat; 53 54static int ifOutPackets; 55static int ifOutOctets; 56static int ifOutLQRs; 57static int ifInPackets; 58static int ifInOctets; 59 60struct protostat { 61 u_short number; 62 char *name; 63 u_long in_count; 64 u_long out_count; 65} ProtocolStat[] = { 66 67 { 68 PROTO_IP, "IP" 69 }, 70 { 71 PROTO_VJUNCOMP, "VJ_UNCOMP" 72 }, 73 { 74 PROTO_VJCOMP, "VJ_COMP" 75 }, 76 { 77 PROTO_COMPD, "COMPD" 78 }, 79 { 80 PROTO_LCP, "LCP" 81 }, 82 { 83 PROTO_IPCP, "IPCP" 84 }, 85 { 86 PROTO_CCP, "CCP" 87 }, 88 { 89 PROTO_PAP, "PAP" 90 }, 91 { 92 PROTO_LQR, "LQR" 93 }, 94 { 95 PROTO_CHAP, "CHAP" 96 }, 97 { 98 0, "Others" 99 }, 100}; 101 102static u_short const fcstab[256] = { 103 /* 00 */ 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 104 /* 08 */ 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 105 /* 10 */ 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 106 /* 18 */ 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, 107 /* 20 */ 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, 108 /* 28 */ 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, 109 /* 30 */ 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, 110 /* 38 */ 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, 111 /* 40 */ 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, 112 /* 48 */ 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, 113 /* 50 */ 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, 114 /* 58 */ 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, 115 /* 60 */ 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, 116 /* 68 */ 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, 117 /* 70 */ 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, 118 /* 78 */ 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, 119 /* 80 */ 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, 120 /* 88 */ 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, 121 /* 90 */ 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, 122 /* 98 */ 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, 123 /* a0 */ 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, 124 /* a8 */ 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, 125 /* b0 */ 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, 126 /* b8 */ 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, 127 /* c0 */ 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, 128 /* c8 */ 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, 129 /* d0 */ 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, 130 /* d8 */ 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, 131 /* e0 */ 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, 132 /* e8 */ 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, 133 /* f0 */ 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 134 /* f8 */ 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 135}; 136 137u_char EscMap[33]; 138 139void 140HdlcInit() 141{ 142 ifInOctets = ifOutOctets = 0; 143 ifInPackets = ifOutPackets = 0; 144 ifOutLQRs = 0; 145} 146 147/* 148 * HDLC FCS computation. Read RFC 1171 Appendix B and CCITT X.25 section 149 * 2.27 for further details. 150 */ 151inline u_short 152HdlcFcs(u_short fcs, u_char * cp, int len) 153{ 154 while (len--) 155 fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff]; 156 return (fcs); 157} 158 159void 160HdlcOutput(int pri, u_short proto, struct mbuf * bp) 161{ 162 struct mbuf *mhp, *mfcs; 163 struct protostat *statp; 164 struct lqrdata *lqr; 165 u_char *cp; 166 u_short fcs; 167 168 if ((proto & 0xfff1) == 0x21) { /* Network Layer protocol */ 169 if (CcpFsm.state == ST_OPENED) { 170 if (CcpInfo.want_proto == TY_PRED1) { 171 Pred1Output(pri, proto, bp); 172 return; 173 } 174 } 175 } 176 if (DEV_IS_SYNC) 177 mfcs = NULLBUFF; 178 else 179 mfcs = mballoc(2, MB_HDLCOUT); 180 mhp = mballoc(4, MB_HDLCOUT); 181 mhp->cnt = 0; 182 cp = MBUF_CTOP(mhp); 183 if (proto == PROTO_LCP || LcpInfo.his_acfcomp == 0) { 184 *cp++ = HDLC_ADDR; 185 *cp++ = HDLC_UI; 186 mhp->cnt += 2; 187 } 188 189 /* 190 * If possible, compress protocol field. 191 */ 192 if (LcpInfo.his_protocomp && (proto & 0xff00) == 0) { 193 *cp++ = proto; 194 mhp->cnt++; 195 } else { 196 *cp++ = proto >> 8; 197 *cp = proto & 0377; 198 mhp->cnt += 2; 199 } 200 mhp->next = bp; 201 bp->next = mfcs; 202 203 lqr = &MyLqrData; 204 lqr->PeerOutPackets = ifOutPackets++; 205 ifOutOctets += plength(mhp) + 1; 206 lqr->PeerOutOctets = ifOutOctets; 207 208 if (proto == PROTO_LQR) { 209 lqr->MagicNumber = LcpInfo.want_magic; 210 lqr->LastOutLQRs = HisLqrData.PeerOutLQRs; 211 lqr->LastOutPackets = HisLqrData.PeerOutPackets; 212 lqr->LastOutOctets = HisLqrData.PeerOutOctets; 213 lqr->PeerInLQRs = HisLqrSave.SaveInLQRs; 214 lqr->PeerInPackets = HisLqrSave.SaveInPackets; 215 lqr->PeerInDiscards = HisLqrSave.SaveInDiscards; 216 lqr->PeerInErrors = HisLqrSave.SaveInErrors; 217 lqr->PeerInOctets = HisLqrSave.SaveInOctets; 218 lqr->PeerOutLQRs = ++ifOutLQRs; 219 LqrDump("LqrOutput", lqr); 220 LqrChangeOrder(lqr, (struct lqrdata *) (MBUF_CTOP(bp))); 221 } 222 if (!DEV_IS_SYNC) { 223 fcs = HdlcFcs(INITFCS, MBUF_CTOP(mhp), mhp->cnt); 224 fcs = HdlcFcs(fcs, MBUF_CTOP(bp), bp->cnt); 225 fcs = ~fcs; 226 cp = MBUF_CTOP(mfcs); 227 *cp++ = fcs & 0377; /* Low byte first!! */ 228 *cp++ = fcs >> 8; 229 } 230 LogDumpBp(LogHDLC, "HdlcOutput", mhp); 231 for (statp = ProtocolStat; statp->number; statp++) 232 if (statp->number == proto) 233 break; 234 statp->out_count++; 235 if (DEV_IS_SYNC) 236 ModemOutput(pri, mhp); 237 else 238 AsyncOutput(pri, mhp, proto); 239} 240 241void 242DecodePacket(u_short proto, struct mbuf * bp) 243{ 244 u_char *cp; 245 246 LogPrintf(LogDEBUG, "DecodePacket: proto = %04x\n", proto); 247 248 switch (proto) { 249 case PROTO_LCP: 250 LcpInput(bp); 251 break; 252 case PROTO_PAP: 253 PapInput(bp); 254 break; 255 case PROTO_LQR: 256 HisLqrSave.SaveInLQRs++; 257 LqrInput(bp); 258 break; 259 case PROTO_CHAP: 260 ChapInput(bp); 261 break; 262 case PROTO_VJUNCOMP: 263 case PROTO_VJCOMP: 264 bp = VjCompInput(bp, proto); 265 if (bp == NULLBUFF) { 266 break; 267 } 268 /* fall down */ 269 case PROTO_IP: 270 IpInput(bp); 271 break; 272 case PROTO_IPCP: 273 IpcpInput(bp); 274 break; 275 case PROTO_CCP: 276 CcpInput(bp); 277 break; 278 case PROTO_COMPD: 279 Pred1Input(bp); 280 break; 281 default: 282 LogPrintf(LogPHASE, "Unknown protocol 0x%04x\n", proto); 283 bp->offset -= 2; 284 bp->cnt += 2; 285 cp = MBUF_CTOP(bp); 286 LcpSendProtoRej(cp, bp->cnt); 287 HisLqrSave.SaveInDiscards++; 288 HdlcStat.unknownproto++; 289 pfree(bp); 290 break; 291 } 292} 293 294int 295ReportProtStatus() 296{ 297 struct protostat *statp; 298 int cnt; 299 300 statp = ProtocolStat; 301 statp--; 302 cnt = 0; 303 fprintf(VarTerm, " Protocol in out Protocol in out\n"); 304 do { 305 statp++; 306 fprintf(VarTerm, " %-9s: %8lu, %8lu", 307 statp->name, statp->in_count, statp->out_count); 308 if (++cnt == 2) { 309 fprintf(VarTerm, "\n"); 310 cnt = 0; 311 } 312 } while (statp->number); 313 if (cnt) 314 fprintf(VarTerm, "\n"); 315 return (1); 316} 317 318int 319ReportHdlcStatus() 320{ 321 struct hdlcstat *hp = &HdlcStat; 322 323 if (VarTerm) { 324 fprintf(VarTerm, "HDLC level errors\n\n"); 325 fprintf(VarTerm, "FCS: %u ADDR: %u COMMAND: %u PROTO: %u\n", 326 hp->badfcs, hp->badaddr, hp->badcommand, hp->unknownproto); 327 } 328 return 0; 329} 330 331static struct hdlcstat laststat; 332 333void 334HdlcErrorCheck() 335{ 336 struct hdlcstat *hp = &HdlcStat; 337 struct hdlcstat *op = &laststat; 338 339 if (memcmp(hp, op, sizeof(laststat))) { 340 LogPrintf(LogPHASE, "HDLC errors -> FCS: %u ADDR: %u COMD: %u PROTO: %u\n", 341 hp->badfcs - op->badfcs, hp->badaddr - op->badaddr, 342 hp->badcommand - op->badcommand, hp->unknownproto - op->unknownproto); 343 } 344 laststat = HdlcStat; 345} 346 347void 348HdlcInput(struct mbuf * bp) 349{ 350 u_short fcs, proto; 351 u_char *cp, addr, ctrl; 352 struct protostat *statp; 353 354 LogDumpBp(LogHDLC, "HdlcInput:", bp); 355 if (DEV_IS_SYNC) 356 fcs = GOODFCS; 357 else 358 fcs = HdlcFcs(INITFCS, MBUF_CTOP(bp), bp->cnt); 359 HisLqrSave.SaveInOctets += bp->cnt + 1; 360 361 LogPrintf(LogDEBUG, "HdlcInput: fcs = %04x (%s)\n", 362 fcs, (fcs == GOODFCS) ? "good" : "bad"); 363 if (fcs != GOODFCS) { 364 HisLqrSave.SaveInErrors++; 365 LogPrintf(LogDEBUG, "HdlcInput: Bad FCS\n"); 366 HdlcStat.badfcs++; 367 pfree(bp); 368 return; 369 } 370 if (!DEV_IS_SYNC) 371 bp->cnt -= 2; /* discard FCS part */ 372 373 if (bp->cnt < 2) { /* XXX: raise this bar ? */ 374 pfree(bp); 375 return; 376 } 377 cp = MBUF_CTOP(bp); 378 379 ifInPackets++; 380 ifInOctets += bp->cnt; 381 382 if (!LcpInfo.want_acfcomp) { 383 384 /* 385 * We expect that packet is not compressed. 386 */ 387 addr = *cp++; 388 if (addr != HDLC_ADDR) { 389 HisLqrSave.SaveInErrors++; 390 HdlcStat.badaddr++; 391 LogPrintf(LogDEBUG, "HdlcInput: addr %02x\n", *cp); 392 pfree(bp); 393 return; 394 } 395 ctrl = *cp++; 396 if (ctrl != HDLC_UI) { 397 HisLqrSave.SaveInErrors++; 398 HdlcStat.badcommand++; 399 LogPrintf(LogDEBUG, "HdlcInput: %02x\n", *cp); 400 pfree(bp); 401 return; 402 } 403 bp->offset += 2; 404 bp->cnt -= 2; 405 } else if (cp[0] == HDLC_ADDR && cp[1] == HDLC_UI) { 406 407 /* 408 * We can receive compressed packet, but peer still send uncompressed 409 * packet to me. 410 */ 411 cp += 2; 412 bp->offset += 2; 413 bp->cnt -= 2; 414 } 415 if (LcpInfo.want_protocomp) { 416 proto = 0; 417 cp--; 418 do { 419 cp++; 420 bp->offset++; 421 bp->cnt--; 422 proto = proto << 8; 423 proto += *cp; 424 } while (!(proto & 1)); 425 } else { 426 proto = *cp++ << 8; 427 proto |= *cp++; 428 bp->offset += 2; 429 bp->cnt -= 2; 430 } 431 432 for (statp = ProtocolStat; statp->number; statp++) 433 if (statp->number == proto) 434 break; 435 statp->in_count++; 436 HisLqrSave.SaveInPackets++; 437 438 DecodePacket(proto, bp); 439} 440