freebsd-src/usr.sbin/ppp/async.c

185 lines
4.4 KiB
C
Raw Normal View History

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.
*
* $Id: async.c,v 1.17 1998/06/16 19:40:34 brian Exp $
1995-05-30 03:57:47 +00:00
*
1995-01-31 06:29:58 +00:00
*/
#include <sys/types.h>
#include <string.h>
#include <termios.h>
#include "mbuf.h"
#include "log.h"
#include "defs.h"
#include "timer.h"
1995-01-31 06:29:58 +00:00
#include "fsm.h"
#include "lqr.h"
1995-01-31 06:29:58 +00:00
#include "hdlc.h"
#include "lcp.h"
#include "lcpproto.h"
#include "async.h"
#include "throughput.h"
o Move struct lcp and struct ccp into struct link. o Remove bundle2lcp(), bundle2ccp() and bundle2link(). They're too resource-hungry and we have `owner pointers' to do their job. o Make our FSM understand LCPs that are always ST_OPENED (with a minimum code that != 1). o Send FSM code rejects for invalid codes. o Make our bundle fsm_parent deal with multiple links. o Make timer diagnostics pretty and allow access via ~t in `term' mode (not just when logging debug) and `show timers'. Only show timers every second in debug mode, otherwise we get too many diagnostics to be useful (we probably still do). Also, don't restrict ~m in term mode to depend on debug logging. o Rationalise our bundles' phases. o Create struct mp (multilink protocol). This is both an NCP and a type of struct link. It feeds off other NCPs for output, passing fragmented packets into the queues of available datalinks. It also gets PROTO_MP input, reassembles the fragments into ppp frames, and passes them back to the HDLC layer that the fragments were passed from. ** It's not yet possible to enter multilink mode :-( ** o Add `set weight' (requires context) for deciding on a links weighting in multilink mode. Weighting is simplistic (and probably badly implemented) for now. o Remove the function pointers in struct link. They ended up only applying to physical links. o Configure our tun device with an MTU equal to the MRU from struct mp's LCP and a speed equal to the sum of our link speeds. o `show {lcp,ccp,proto}' and `set deflate' now have optional context and use ChooseLink() to decide on which `struct link' to use. This allows behaviour as before when in non-multilink mode, and allows access to the MP logical link in multilink mode. o Ignore reconnect and redial values when in -direct mode and when cleaning up. Always redial when in -ddial or -dedicated mode (unless cleaning up). o Tell our links to `staydown' when we close them due to a signal. o Remove remaining `#ifdef SIGALRM's (ppp doesn't function without alarms). o Don't bother strdup()ing our physical link name. o Various other cosmetic changes.
1998-04-03 19:21:56 +00:00
#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
async_Init(struct async *async)
1995-01-31 06:29:58 +00:00
{
async->mode = MODE_HUNT;
async->length = 0;
async->my_accmap = async->his_accmap = 0xffffffff;
memset(async->cfg.EscMap, '\0', sizeof async->cfg.EscMap);
1995-01-31 06:29:58 +00:00
}
void
async_SetLinkParams(struct async *async, struct lcp *lcp)
1995-01-31 06:29:58 +00:00
{
async->my_accmap = lcp->want_accmap;
async->his_accmap = lcp->his_accmap | lcp->want_accmap;
1995-01-31 06:29:58 +00:00
}
/*
* Encode into async HDLC byte code if necessary
*/
static void
HdlcPutByte(struct async *async, u_char **cp, u_char c, int proto)
1995-01-31 06:29:58 +00:00
{
u_char *wp;
wp = *cp;
if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
|| (c == HDLC_ESC) || (c == HDLC_SYN)) {
1995-01-31 06:29:58 +00:00
*wp++ = HDLC_ESC;
c ^= HDLC_XOR;
}
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;
}
void
async_Output(int pri, struct mbuf *bp, int proto, struct physical *physical)
1995-01-31 06:29:58 +00:00
{
u_char *cp, *sp, *ep;
struct mbuf *wp;
int cnt;
if (mbuf_Length(bp) > HDLCSIZE) {
mbuf_Free(bp);
1995-01-31 06:29:58 +00:00
return;
}
cp = physical->async.xbuff;
1995-01-31 06:29:58 +00:00
ep = cp + HDLCSIZE - 10;
wp = bp;
*cp++ = HDLC_SYN;
1995-01-31 06:29:58 +00:00
while (wp) {
sp = MBUF_CTOP(wp);
for (cnt = wp->cnt; cnt > 0; cnt--) {
HdlcPutByte(&physical->async, &cp, *sp++, proto);
1995-01-31 06:29:58 +00:00
if (cp >= ep) {
mbuf_Free(bp);
1995-01-31 06:29:58 +00:00
return;
}
}
wp = wp->next;
}
*cp++ = HDLC_SYN;
1995-01-31 06:29:58 +00:00
cnt = cp - physical->async.xbuff;
log_DumpBuff(LogASYNC, "WriteModem", physical->async.xbuff, cnt);
link_Write(&physical->link, pri, (char *)physical->async.xbuff, cnt);
link_AddOutOctets(&physical->link, cnt);
mbuf_Free(bp);
1995-01-31 06:29:58 +00:00
}
static struct mbuf *
async_Decode(struct async *async, u_char c)
1995-01-31 06:29:58 +00:00
{
struct mbuf *bp;
if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
return NULL;
1995-01-31 06:29:58 +00:00
switch (c) {
case HDLC_SYN:
async->mode &= ~MODE_HUNT;
if (async->length) { /* packet is ready. */
bp = mbuf_Alloc(async->length, MB_ASYNC);
mbuf_Write(bp, async->hbuff, async->length);
async->length = 0;
return bp;
1995-01-31 06:29:58 +00:00
}
break;
case HDLC_ESC:
if (!(async->mode & MODE_ESC)) {
async->mode |= MODE_ESC;
1995-01-31 06:29:58 +00:00
break;
}
/* Fall into ... */
default:
if (async->length >= HDLCSIZE) {
1995-01-31 06:29:58 +00:00
/* packet is too large, discard it */
log_Printf(LogWARN, "Packet too large (%d), discarding.\n",
async->length);
async->length = 0;
async->mode = MODE_HUNT;
1995-01-31 06:29:58 +00:00
break;
}
if (async->mode & MODE_ESC) {
1995-01-31 06:29:58 +00:00
c ^= HDLC_XOR;
async->mode &= ~MODE_ESC;
1995-01-31 06:29:58 +00:00
}
async->hbuff[async->length++] = c;
1995-01-31 06:29:58 +00:00
break;
}
return NULL;
1995-01-31 06:29:58 +00:00
}
void
async_Input(struct bundle *bundle, u_char *buff, int cnt,
struct physical *physical)
1995-01-31 06:29:58 +00:00
{
struct mbuf *bp;
link_AddInOctets(&physical->link, cnt);
if (physical_IsSync(physical)) {
bp = mbuf_Alloc(cnt, MB_ASYNC);
memcpy(MBUF_CTOP(bp), buff, cnt);
bp->cnt = cnt;
hdlc_Input(bundle, bp, physical);
} else {
while (cnt > 0) {
bp = async_Decode(&physical->async, *buff++);
if (bp)
hdlc_Input(bundle, bp, physical);
cnt--;
}
1995-01-31 06:29:58 +00:00
}
}