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

221 lines
5.3 KiB
C
Raw Normal View History

/*-
* 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
*
* 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
*
* 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
*
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#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"
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)
{
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
{
async->mode = MODE_HUNT;
async->length = 0;
async->my_accmap = async->his_accmap = 0xffffffff;
1995-01-31 06:29:58 +00:00
}
void
async_SetLinkParams(struct async *async, u_int32_t mymap, u_int32_t hismap)
1995-01-31 06:29:58 +00:00
{
async->my_accmap = mymap;
async->his_accmap = hismap | mymap;
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 *
2004-09-05 01:46:52 +00:00
async_LayerPush(struct bundle *b __unused, struct link *l, struct mbuf *bp,
int pri __unused, 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;
size_t oldcnt;
2004-09-05 01:46:52 +00:00
size_t cnt;
1995-01-31 06:29:58 +00:00
if (!p || m_length(bp) > HDLCSIZE) {
m_freem(bp);
return NULL;
1995-01-31 06:29:58 +00:00
}
oldcnt = m_length(bp);
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);
bp->priv = cnt - oldcnt;
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;
}
/* FALLTHROUGH */
1995-01-31 06:29:58 +00:00
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 *
2004-09-05 01:46:52 +00:00
async_LayerPull(struct bundle *b __unused, struct link *l, struct mbuf *bp,
u_short *proto __unused)
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 };