1995-01-31 06:29:58 +00:00
|
|
|
/*
|
|
|
|
* PPP Async HDLC Module
|
|
|
|
*
|
|
|
|
* Written by Toshiharu OHNO (tony-o@iij.ad.jp)
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the Internet Initiative Japan, Inc. The name of the
|
|
|
|
* IIJ may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
1999-08-28 01:35:59 +00:00
|
|
|
* $FreeBSD$
|
1995-05-30 03:57:47 +00:00
|
|
|
*
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "layer.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "mbuf.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "timer.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "fsm.h"
|
1998-03-13 00:44:26 +00:00
|
|
|
#include "lqr.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
#include "hdlc.h"
|
|
|
|
#include "lcp.h"
|
1999-05-08 11:07:56 +00:00
|
|
|
#include "proto.h"
|
1997-10-26 01:04:02 +00:00
|
|
|
#include "async.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "throughput.h"
|
1998-04-03 19:21:56 +00:00
|
|
|
#include "ccp.h"
|
1998-01-30 19:46:07 +00:00
|
|
|
#include "link.h"
|
1998-02-09 19:21:11 +00:00
|
|
|
#include "descriptor.h"
|
1998-01-29 00:49:32 +00:00
|
|
|
#include "physical.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define MODE_HUNT 0x01
|
|
|
|
#define MODE_ESC 0x02
|
|
|
|
|
|
|
|
void
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Init(struct async *async)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode = MODE_HUNT;
|
|
|
|
async->length = 0;
|
|
|
|
async->my_accmap = async->his_accmap = 0xffffffff;
|
1998-04-19 01:18:51 +00:00
|
|
|
memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-02-08 11:05:01 +00:00
|
|
|
async_SetLinkParams(struct async *async, struct lcp *lcp)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-02-02 19:33:40 +00:00
|
|
|
async->my_accmap = lcp->want_accmap;
|
1999-04-11 08:51:04 +00:00
|
|
|
async->his_accmap = lcp->his_accmap | lcp->want_accmap;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-05-08 11:07:56 +00:00
|
|
|
* Encode into async HDLC byte code
|
1995-01-31 06:29:58 +00:00
|
|
|
*/
|
|
|
|
static void
|
1999-05-08 11:07:56 +00:00
|
|
|
async_Encode(struct async *async, u_char **cp, u_char c, int proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
u_char *wp;
|
|
|
|
|
|
|
|
wp = *cp;
|
1998-02-02 19:33:40 +00:00
|
|
|
if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
|
1997-08-25 00:29:32 +00:00
|
|
|
|| (c == HDLC_ESC) || (c == HDLC_SYN)) {
|
1995-01-31 06:29:58 +00:00
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
1998-02-18 19:35:20 +00:00
|
|
|
if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
|
1995-01-31 06:29:58 +00:00
|
|
|
*wp++ = HDLC_ESC;
|
|
|
|
c ^= HDLC_XOR;
|
|
|
|
}
|
|
|
|
*wp++ = c;
|
|
|
|
*cp = wp;
|
|
|
|
}
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
static struct mbuf *
|
|
|
|
async_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
|
|
|
|
int pri, u_short *proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-05-08 11:07:56 +00:00
|
|
|
struct physical *p = link2physical(l);
|
1995-01-31 06:29:58 +00:00
|
|
|
u_char *cp, *sp, *ep;
|
|
|
|
struct mbuf *wp;
|
|
|
|
int cnt;
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
if (!p || mbuf_Length(bp) > HDLCSIZE) {
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
cp = p->async.xbuff;
|
1995-01-31 06:29:58 +00:00
|
|
|
ep = cp + HDLCSIZE - 10;
|
|
|
|
wp = bp;
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp++ = HDLC_SYN;
|
1995-01-31 06:29:58 +00:00
|
|
|
while (wp) {
|
|
|
|
sp = MBUF_CTOP(wp);
|
|
|
|
for (cnt = wp->cnt; cnt > 0; cnt--) {
|
1999-05-08 11:07:56 +00:00
|
|
|
async_Encode(&p->async, &cp, *sp++, *proto);
|
1995-01-31 06:29:58 +00:00
|
|
|
if (cp >= ep) {
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
wp = wp->next;
|
|
|
|
}
|
1997-08-25 00:29:32 +00:00
|
|
|
*cp++ = HDLC_SYN;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
cnt = cp - p->async.xbuff;
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Free(bp);
|
1999-06-02 15:59:09 +00:00
|
|
|
bp = mbuf_Alloc(cnt, MB_ASYNCOUT);
|
1999-05-08 11:07:56 +00:00
|
|
|
memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
|
1999-05-12 09:49:12 +00:00
|
|
|
log_DumpBp(LogASYNC, "Write", bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
return bp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1997-10-26 01:04:02 +00:00
|
|
|
static struct mbuf *
|
1998-02-02 19:33:40 +00:00
|
|
|
async_Decode(struct async *async, u_char c)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1998-02-02 19:33:40 +00:00
|
|
|
if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
|
1998-01-21 02:15:33 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case HDLC_SYN:
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode &= ~MODE_HUNT;
|
|
|
|
if (async->length) { /* packet is ready. */
|
1999-06-02 15:59:09 +00:00
|
|
|
bp = mbuf_Alloc(async->length, MB_ASYNCIN);
|
1998-05-01 19:26:12 +00:00
|
|
|
mbuf_Write(bp, async->hbuff, async->length);
|
1998-02-02 19:33:40 +00:00
|
|
|
async->length = 0;
|
1998-01-21 02:15:33 +00:00
|
|
|
return bp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HDLC_ESC:
|
1998-02-02 19:33:40 +00:00
|
|
|
if (!(async->mode & MODE_ESC)) {
|
|
|
|
async->mode |= MODE_ESC;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Fall into ... */
|
|
|
|
default:
|
1998-02-02 19:33:40 +00:00
|
|
|
if (async->length >= HDLCSIZE) {
|
1995-01-31 06:29:58 +00:00
|
|
|
/* packet is too large, discard it */
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
|
|
|
|
async->length);
|
1998-02-02 19:33:40 +00:00
|
|
|
async->length = 0;
|
|
|
|
async->mode = MODE_HUNT;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-02-02 19:33:40 +00:00
|
|
|
if (async->mode & MODE_ESC) {
|
1995-01-31 06:29:58 +00:00
|
|
|
c ^= HDLC_XOR;
|
1998-02-02 19:33:40 +00:00
|
|
|
async->mode &= ~MODE_ESC;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-02-02 19:33:40 +00:00
|
|
|
async->hbuff[async->length++] = c;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-01-21 02:15:33 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
static struct mbuf *
|
|
|
|
async_LayerPull(struct bundle *b, struct link *l, struct mbuf *bp,
|
|
|
|
u_short *proto)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1999-05-08 11:07:56 +00:00
|
|
|
struct mbuf *nbp, **last;
|
|
|
|
struct physical *p = link2physical(l);
|
|
|
|
u_char *ch;
|
|
|
|
size_t cnt;
|
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
log_Printf(LogERROR, "Can't Pull an async packet from a logical link\n");
|
|
|
|
return bp;
|
|
|
|
}
|
1995-01-31 06:29:58 +00:00
|
|
|
|
1999-05-08 11:07:56 +00:00
|
|
|
last = &nbp;
|
|
|
|
|
1999-05-12 09:49:12 +00:00
|
|
|
log_DumpBp(LogASYNC, "Read", bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
while (bp) {
|
|
|
|
ch = MBUF_CTOP(bp);
|
|
|
|
for (cnt = bp->cnt; cnt; cnt--) {
|
|
|
|
*last = async_Decode(&p->async, *ch++);
|
|
|
|
if (*last != NULL)
|
|
|
|
last = &(*last)->pnext;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
bp = mbuf_FreeSeg(bp);
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
return nbp;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1999-05-08 11:07:56 +00:00
|
|
|
|
|
|
|
struct layer asynclayer =
|
|
|
|
{ LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };
|