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

201 lines
4.6 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.
*
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
*/
#include <sys/types.h>
#include <string.h>
#include <termios.h>
#include "layer.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 "proto.h"
#include "async.h"
#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
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
1995-01-31 06:29:58 +00:00
*/
static void
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;
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;
}
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
{
struct physical *p = link2physical(l);
1995-01-31 06:29:58 +00:00
u_char *cp, *sp, *ep;
struct mbuf *wp;
int cnt;
if (!p || m_length(bp) > HDLCSIZE) {
m_freem(bp);
return NULL;
1995-01-31 06:29:58 +00:00
}
cp = p->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->m_len; cnt > 0; cnt--) {
async_Encode(&p->async, &cp, *sp++, *proto);
1995-01-31 06:29:58 +00:00
if (cp >= ep) {
m_freem(bp);
return NULL;
1995-01-31 06:29:58 +00:00
}
}
wp = wp->m_next;
1995-01-31 06:29:58 +00:00
}
*cp++ = HDLC_SYN;
1995-01-31 06:29:58 +00:00
cnt = cp - p->async.xbuff;
m_freem(bp);
bp = m_get(cnt, MB_ASYNCOUT);
memcpy(MBUF_CTOP(bp), p->async.xbuff, cnt);
Allow ``host:port/udp'' devices and support ``host:port/tcp'' as being the same as the previous (still supported) ``host:port'' syntax for tcp socket devices. A udp device uses synchronous ppp rather than async, and avoids the double-retransmit overhead that comes with ppp over tcp (it's usually a bad idea to transport IP over a reliable transport that itself is using an unreliable transport). PPP over UDP provides througput of ** 1.5Mb per second ** with all compression disabled, maxing out a PPro/200 when running ppp twice, back-to-back. This proves that PPPoE is plausable in userland.... This change adds a few more handler functions to struct device and allows derivations of struct device (which may contain their own data etc) to pass themselves through the unix domain socket for MP. ** At last **, struct physical has lost all the tty crud ! iov2physical() is now smart enough to restore the correct stack of layers so that MP servers will work again. The version number has bumped as our MP link transfer contents have changed (they now may contain a `struct device'). Don't extract the protocol twice in MP mode (resulting in protocol rejects for every MP packet). This was broken with my original layering changes. Add ``Physical'' and ``Sync'' log levels for logging the relevent raw packets and add protocol-tracking LogDEBUG stuff in various LayerPush & LayerPull functions. Assign our physical device name for incoming tcp connections by calling getpeername(). Assign our physical device name for incoming udp connections from the address retrieved by the first recvfrom().
1999-05-12 09:49:12 +00:00
log_DumpBp(LogASYNC, "Write", bp);
return 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 = m_get(async->length, MB_ASYNCIN);
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
}
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
{
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
last = &nbp;
Allow ``host:port/udp'' devices and support ``host:port/tcp'' as being the same as the previous (still supported) ``host:port'' syntax for tcp socket devices. A udp device uses synchronous ppp rather than async, and avoids the double-retransmit overhead that comes with ppp over tcp (it's usually a bad idea to transport IP over a reliable transport that itself is using an unreliable transport). PPP over UDP provides througput of ** 1.5Mb per second ** with all compression disabled, maxing out a PPro/200 when running ppp twice, back-to-back. This proves that PPPoE is plausable in userland.... This change adds a few more handler functions to struct device and allows derivations of struct device (which may contain their own data etc) to pass themselves through the unix domain socket for MP. ** At last **, struct physical has lost all the tty crud ! iov2physical() is now smart enough to restore the correct stack of layers so that MP servers will work again. The version number has bumped as our MP link transfer contents have changed (they now may contain a `struct device'). Don't extract the protocol twice in MP mode (resulting in protocol rejects for every MP packet). This was broken with my original layering changes. Add ``Physical'' and ``Sync'' log levels for logging the relevent raw packets and add protocol-tracking LogDEBUG stuff in various LayerPush & LayerPull functions. Assign our physical device name for incoming tcp connections by calling getpeername(). Assign our physical device name for incoming udp connections from the address retrieved by the first recvfrom().
1999-05-12 09:49:12 +00:00
log_DumpBp(LogASYNC, "Read", bp);
while (bp) {
ch = MBUF_CTOP(bp);
for (cnt = bp->m_len; cnt; cnt--) {
*last = async_Decode(&p->async, *ch++);
if (*last != NULL)
last = &(*last)->m_nextpkt;
}
bp = m_free(bp);
1995-01-31 06:29:58 +00:00
}
return nbp;
1995-01-31 06:29:58 +00:00
}
struct layer asynclayer =
{ LAYER_ASYNC, "async", async_LayerPush, async_LayerPull };