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

183 lines
4.3 KiB
C
Raw Normal View History

/*
* 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.15.2.10 1998/04/06 09:12:22 brian Exp $
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <termios.h>
#include "mbuf.h"
#include "log.h"
#include "defs.h"
#include "timer.h"
#include "fsm.h"
#include "lqr.h"
#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"
#define MODE_HUNT 0x01
#define MODE_ESC 0x02
void
async_Init(struct async *async)
{
async->mode = MODE_HUNT;
async->length = 0;
async->my_accmap = async->his_accmap = 0xffffffff;
}
void
async_SetLinkParams(struct async *async, struct lcp *lcp)
{
async->my_accmap = lcp->want_accmap;
async->his_accmap = lcp->his_accmap;
}
/*
* Encode into async HDLC byte code if necessary
*/
static void
HdlcPutByte(struct async *async, u_char **cp, u_char c, int proto)
{
u_char *wp;
wp = *cp;
if ((c < 0x20 && (proto == PROTO_LCP || (async->his_accmap & (1 << c))))
|| (c == HDLC_ESC) || (c == HDLC_SYN)) {
*wp++ = HDLC_ESC;
c ^= HDLC_XOR;
}
if (async->cfg.EscMap[32] && async->cfg.EscMap[c >> 3] & (1 << (c & 7))) {
*wp++ = HDLC_ESC;
c ^= HDLC_XOR;
}
*wp++ = c;
*cp = wp;
}
void
async_Output(int pri, struct mbuf *bp, int proto, struct physical *physical)
{
u_char *cp, *sp, *ep;
struct mbuf *wp;
int cnt;
if (plength(bp) > HDLCSIZE) {
pfree(bp);
return;
}
cp = physical->async.xbuff;
ep = cp + HDLCSIZE - 10;
wp = bp;
*cp++ = HDLC_SYN;
while (wp) {
sp = MBUF_CTOP(wp);
for (cnt = wp->cnt; cnt > 0; cnt--) {
HdlcPutByte(&physical->async, &cp, *sp++, proto);
if (cp >= ep) {
pfree(bp);
return;
}
}
wp = wp->next;
}
*cp++ = HDLC_SYN;
cnt = cp - physical->async.xbuff;
LogDumpBuff(LogASYNC, "WriteModem", physical->async.xbuff, cnt);
link_Write(physical2link(physical), pri, (char *)physical->async.xbuff, cnt);
link_AddOutOctets(physical2link(physical), cnt);
pfree(bp);
}
static struct mbuf *
async_Decode(struct async *async, u_char c)
{
struct mbuf *bp;
if ((async->mode & MODE_HUNT) && c != HDLC_SYN)
return NULL;
switch (c) {
case HDLC_SYN:
async->mode &= ~MODE_HUNT;
if (async->length) { /* packet is ready. */
bp = mballoc(async->length, MB_ASYNC);
mbwrite(bp, async->hbuff, async->length);
async->length = 0;
return bp;
}
break;
case HDLC_ESC:
if (!(async->mode & MODE_ESC)) {
async->mode |= MODE_ESC;
break;
}
/* Fall into ... */
default:
if (async->length >= HDLCSIZE) {
/* packet is too large, discard it */
LogPrintf(LogERROR, "Packet too large (%d), discarding.\n", async->length);
async->length = 0;
async->mode = MODE_HUNT;
break;
}
if (async->mode & MODE_ESC) {
c ^= HDLC_XOR;
async->mode &= ~MODE_ESC;
}
async->hbuff[async->length++] = c;
break;
}
return NULL;
}
void
async_Input(struct bundle *bundle, u_char *buff, int cnt,
struct physical *physical)
{
struct mbuf *bp;
link_AddInOctets(physical2link(physical), cnt);
if (Physical_IsSync(physical)) {
bp = mballoc(cnt, MB_ASYNC);
memcpy(MBUF_CTOP(bp), buff, cnt);
bp->cnt = cnt;
HdlcInput(bundle, bp, physical);
} else {
while (cnt > 0) {
bp = async_Decode(&physical->async, *buff++);
if (bp)
HdlcInput(bundle, bp, physical);
cnt--;
}
}
}