2001-06-13 21:52:19 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
|
|
|
|
* Internet Initiative Japan, Inc (IIJ)
|
|
|
|
* All rights reserved.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
2001-06-13 21:52:19 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1995-01-31 06:29:58 +00:00
|
|
|
*
|
|
|
|
*/
|
2001-06-13 21:52:19 +00:00
|
|
|
|
2002-08-25 13:30:43 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-05-21 21:49:08 +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-05-21 21:49:08 +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-05-21 21:49:08 +00:00
|
|
|
#include "throughput.h"
|
|
|
|
#include "ccp.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "descriptor.h"
|
|
|
|
#include "physical.h"
|
1995-01-31 06:29:58 +00:00
|
|
|
|
|
|
|
#define MODE_HUNT 0x01
|
|
|
|
#define MODE_ESC 0x02
|
|
|
|
|
|
|
|
void
|
1998-05-21 21:49:08 +00:00
|
|
|
async_Init(struct async *async)
|
2002-03-04 10:08:57 +00:00
|
|
|
{
|
|
|
|
async_Setup(async);
|
|
|
|
memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
async_Setup(struct async *async)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
1998-05-21 21:49:08 +00:00
|
|
|
async->mode = MODE_HUNT;
|
|
|
|
async->length = 0;
|
|
|
|
async->my_accmap = async->his_accmap = 0xffffffff;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-30 12:30:09 +00:00
|
|
|
async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
2002-03-30 12:30:09 +00:00
|
|
|
async->my_accmap = mymap;
|
|
|
|
async->his_accmap = hismap | mymap;
|
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-05-21 21:49:08 +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-05-21 21:49:08 +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-12-20 20:29:47 +00:00
|
|
|
if (!p || m_length(bp) > HDLCSIZE) {
|
|
|
|
m_freem(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);
|
1999-12-20 20:29:47 +00:00
|
|
|
for (cnt = wp->m_len; 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) {
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(bp);
|
1999-05-08 11:07:56 +00:00
|
|
|
return NULL;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
wp = wp->m_next;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
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;
|
1999-12-20 20:29:47 +00:00
|
|
|
m_freem(bp);
|
|
|
|
bp = m_get(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-05-21 21:49:08 +00:00
|
|
|
async_Decode(struct async *async, u_char c)
|
1995-01-31 06:29:58 +00:00
|
|
|
{
|
|
|
|
struct mbuf *bp;
|
|
|
|
|
1998-05-21 21:49:08 +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-05-21 21:49:08 +00:00
|
|
|
async->mode &= ~MODE_HUNT;
|
|
|
|
if (async->length) { /* packet is ready. */
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_get(async->length, MB_ASYNCIN);
|
1998-05-21 21:49:08 +00:00
|
|
|
mbuf_Write(bp, async->hbuff, async->length);
|
|
|
|
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-05-21 21:49:08 +00:00
|
|
|
if (!(async->mode & MODE_ESC)) {
|
|
|
|
async->mode |= MODE_ESC;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-08-25 13:30:43 +00:00
|
|
|
/* FALLTHROUGH */
|
1995-01-31 06:29:58 +00:00
|
|
|
default:
|
1998-05-21 21:49:08 +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-05-21 21:49:08 +00:00
|
|
|
async->length = 0;
|
|
|
|
async->mode = MODE_HUNT;
|
1995-01-31 06:29:58 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-05-21 21:49:08 +00:00
|
|
|
if (async->mode & MODE_ESC) {
|
1995-01-31 06:29:58 +00:00
|
|
|
c ^= HDLC_XOR;
|
1998-05-21 21:49:08 +00:00
|
|
|
async->mode &= ~MODE_ESC;
|
1995-01-31 06:29:58 +00:00
|
|
|
}
|
1998-05-21 21:49:08 +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);
|
1999-12-20 20:29:47 +00:00
|
|
|
for (cnt = bp->m_len; cnt; cnt--) {
|
1999-05-08 11:07:56 +00:00
|
|
|
*last = async_Decode(&p->async, *ch++);
|
|
|
|
if (*last != NULL)
|
1999-12-20 20:29:47 +00:00
|
|
|
last = &(*last)->m_nextpkt;
|
1995-02-26 12:18:08 +00:00
|
|
|
}
|
1999-12-20 20:29:47 +00:00
|
|
|
bp = m_free(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 };
|