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

320 lines
9.7 KiB
C
Raw Normal View History

1995-01-31 06:29:58 +00:00
/*
* PPP Line Quality Monitoring (LQM) 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: lqr.c,v 1.28 1998/06/26 19:02:40 brian Exp $
1995-05-30 03:57:47 +00:00
*
* o LQR based on RFC1333
1995-01-31 06:29:58 +00:00
*
* TODO:
* o LQM policy
* o Allow user to configure LQM method and interval.
*/
#include <sys/types.h>
#include <sys/un.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 "lcpproto.h"
#include "lcp.h"
1995-01-31 06:29:58 +00:00
#include "lqr.h"
#include "hdlc.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"
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 "mp.h"
#include "chat.h"
#include "auth.h"
#include "chap.h"
#include "command.h"
#include "datalink.h"
1995-01-31 06:29:58 +00:00
struct echolqr {
u_int32_t magic;
u_int32_t signature;
u_int32_t sequence;
1995-01-31 06:29:58 +00:00
};
#define SIGNATURE 0x594e4f54
static void
SendEchoReq(struct lcp *lcp)
1995-01-31 06:29:58 +00:00
{
struct hdlc *hdlc = &link2physical(lcp->fsm.link)->hdlc;
struct echolqr echo;
echo.magic = htonl(lcp->want_magic);
echo.signature = htonl(SIGNATURE);
echo.sequence = htonl(hdlc->lqm.echo.seq_sent);
fsm_Output(&lcp->fsm, CODE_ECHOREQ, hdlc->lqm.echo.seq_sent++,
(u_char *)&echo, sizeof echo);
1995-01-31 06:29:58 +00:00
}
void
lqr_RecvEcho(struct fsm *fp, struct mbuf * bp)
1995-01-31 06:29:58 +00:00
{
struct hdlc *hdlc = &link2physical(fp->link)->hdlc;
1995-01-31 06:29:58 +00:00
struct echolqr *lqr;
u_int32_t seq;
1995-01-31 06:29:58 +00:00
if (mbuf_Length(bp) == sizeof(struct echolqr)) {
lqr = (struct echolqr *) MBUF_CTOP(bp);
if (ntohl(lqr->signature) == SIGNATURE) {
1995-01-31 06:29:58 +00:00
seq = ntohl(lqr->sequence);
/* careful not to update lqm.echo.seq_recv with older values */
if ((hdlc->lqm.echo.seq_recv > (u_int32_t)0 - 5 && seq < 5) ||
(hdlc->lqm.echo.seq_recv <= (u_int32_t)0 - 5 &&
seq > hdlc->lqm.echo.seq_recv))
hdlc->lqm.echo.seq_recv = seq;
} else
log_Printf(LogWARN, "lqr_RecvEcho: Got sig 0x%08lx, not 0x%08lx !\n",
(u_long)ntohl(lqr->signature), (u_long)SIGNATURE);
} else
log_Printf(LogWARN, "lqr_RecvEcho: Got packet size %d, expecting %ld !\n",
mbuf_Length(bp), (long)sizeof(struct echolqr));
1995-01-31 06:29:58 +00:00
}
void
lqr_ChangeOrder(struct lqrdata * src, struct lqrdata * dst)
1995-01-31 06:29:58 +00:00
{
1998-06-26 19:02:40 +00:00
u_int32_t *sp, *dp;
1995-01-31 06:29:58 +00:00
int n;
1998-06-26 19:02:40 +00:00
sp = (u_int32_t *) src;
dp = (u_int32_t *) dst;
for (n = 0; n < sizeof(struct lqrdata) / sizeof(u_int32_t); n++)
1995-01-31 06:29:58 +00:00
*dp++ = ntohl(*sp++);
}
static void
SendLqrData(struct lcp *lcp)
1995-01-31 06:29:58 +00:00
{
struct mbuf *bp;
bp = mbuf_Alloc(sizeof(struct lqrdata), MB_LQR);
hdlc_Output(lcp->fsm.link, PRI_LINK, PROTO_LQR, bp);
}
1995-01-31 06:29:58 +00:00
static void
SendLqrReport(void *v)
{
struct lcp *lcp = (struct lcp *)v;
struct physical *p = link2physical(lcp->fsm.link);
timer_Stop(&p->hdlc.lqm.timer);
if (p->hdlc.lqm.method & LQM_LQR) {
if (p->hdlc.lqm.lqr.resent > 5) {
/* XXX: Should implement LQM strategy */
log_Printf(LogPHASE, "%s: ** Too many LQR packets lost **\n",
lcp->fsm.link->name);
log_Printf(LogLQM, "%s: Too many LQR packets lost\n",
lcp->fsm.link->name);
p->hdlc.lqm.method = 0;
datalink_Down(p->dl, CLOSE_NORMAL);
1995-01-31 06:29:58 +00:00
} else {
SendLqrData(lcp);
p->hdlc.lqm.lqr.resent++;
1995-01-31 06:29:58 +00:00
}
} else if (p->hdlc.lqm.method & LQM_ECHO) {
if ((p->hdlc.lqm.echo.seq_sent > 5 &&
p->hdlc.lqm.echo.seq_sent - 5 > p->hdlc.lqm.echo.seq_recv) ||
(p->hdlc.lqm.echo.seq_sent <= 5 &&
p->hdlc.lqm.echo.seq_sent > p->hdlc.lqm.echo.seq_recv + 5)) {
log_Printf(LogPHASE, "%s: ** Too many ECHO LQR packets lost **\n",
lcp->fsm.link->name);
log_Printf(LogLQM, "%s: Too many ECHO LQR packets lost\n",
lcp->fsm.link->name);
p->hdlc.lqm.method = 0;
datalink_Down(p->dl, CLOSE_NORMAL);
1995-01-31 06:29:58 +00:00
} else
SendEchoReq(lcp);
1995-01-31 06:29:58 +00:00
}
if (p->hdlc.lqm.method && p->hdlc.lqm.timer.load)
timer_Start(&p->hdlc.lqm.timer);
1995-01-31 06:29:58 +00:00
}
void
lqr_Input(struct physical *physical, struct mbuf *bp)
1995-01-31 06:29:58 +00:00
{
int len;
len = mbuf_Length(bp);
if (len != sizeof(struct lqrdata))
log_Printf(LogWARN, "lqr_Input: Got packet size %d, expecting %ld !\n",
len, (long)sizeof(struct lqrdata));
else if (!IsAccepted(physical->link.lcp.cfg.lqr) &&
!(physical->hdlc.lqm.method & LQM_LQR)) {
1995-01-31 06:29:58 +00:00
bp->offset -= 2;
bp->cnt += 2;
lcp_SendProtoRej(physical->hdlc.lqm.owner, MBUF_CTOP(bp), bp->cnt);
1995-01-31 06:29:58 +00:00
} else {
struct lqrdata *lqr;
struct lcp *lcp;
u_int32_t lastLQR;
1995-01-31 06:29:58 +00:00
lqr = (struct lqrdata *)MBUF_CTOP(bp);
lcp = physical->hdlc.lqm.owner;
if (ntohl(lqr->MagicNumber) != physical->hdlc.lqm.owner->his_magic)
log_Printf(LogWARN, "lqr_Input: magic 0x%08lx is wrong,"
" expecting 0x%08lx\n",
(u_long)ntohl(lqr->MagicNumber),
(u_long)physical->hdlc.lqm.owner->his_magic);
else {
/*
* Remember our PeerInLQRs, then convert byte order and save
*/
lastLQR = physical->hdlc.lqm.lqr.peer.PeerInLQRs;
lqr_ChangeOrder(lqr, &physical->hdlc.lqm.lqr.peer);
lqr_Dump(physical->link.name, "Input", &physical->hdlc.lqm.lqr.peer);
/* we have received an LQR from peer */
physical->hdlc.lqm.lqr.resent = 0;
/*
* Generate an LQR response if we're not running an LQR timer OR
* two successive LQR's PeerInLQRs are the same OR we're not going to
* send our next one before the peers max timeout.
*/
if (physical->hdlc.lqm.timer.load == 0 ||
!(physical->hdlc.lqm.method & LQM_LQR) ||
(lastLQR && lastLQR == physical->hdlc.lqm.lqr.peer.PeerInLQRs) ||
(physical->hdlc.lqm.lqr.peer_timeout &&
physical->hdlc.lqm.timer.rest * 100 / SECTICKS >
physical->hdlc.lqm.lqr.peer_timeout))
SendLqrData(physical->hdlc.lqm.owner);
1995-01-31 06:29:58 +00:00
}
}
mbuf_Free(bp);
1995-01-31 06:29:58 +00:00
}
/*
* When LCP is reached to opened state, We'll start LQM activity.
*/
static void
lqr_Setup(struct lcp *lcp)
1995-01-31 06:29:58 +00:00
{
struct physical *physical = link2physical(lcp->fsm.link);
1995-01-31 06:29:58 +00:00
physical->hdlc.lqm.lqr.resent = 0;
physical->hdlc.lqm.echo.seq_sent = 0;
physical->hdlc.lqm.echo.seq_recv = 0;
memset(&physical->hdlc.lqm.lqr.peer, '\0',
sizeof physical->hdlc.lqm.lqr.peer);
physical->hdlc.lqm.method = LQM_ECHO;
if (IsEnabled(lcp->cfg.lqr) && !REJECTED(lcp, TY_QUALPROTO))
physical->hdlc.lqm.method |= LQM_LQR;
timer_Stop(&physical->hdlc.lqm.timer);
1995-01-31 06:29:58 +00:00
physical->hdlc.lqm.lqr.peer_timeout = lcp->his_lqrperiod;
if (lcp->his_lqrperiod)
log_Printf(LogLQM, "%s: Expecting LQR every %d.%02d secs\n",
physical->link.name, lcp->his_lqrperiod / 100,
lcp->his_lqrperiod % 100);
if (lcp->want_lqrperiod) {
log_Printf(LogLQM, "%s: Will send %s every %d.%02d secs\n",
physical->link.name,
physical->hdlc.lqm.method & LQM_LQR ? "LQR" : "ECHO LQR",
lcp->want_lqrperiod / 100, lcp->want_lqrperiod % 100);
physical->hdlc.lqm.timer.load = lcp->want_lqrperiod * SECTICKS / 100;
physical->hdlc.lqm.timer.func = SendLqrReport;
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
physical->hdlc.lqm.timer.name = "lqm";
physical->hdlc.lqm.timer.arg = lcp;
1995-01-31 06:29:58 +00:00
} else {
physical->hdlc.lqm.timer.load = 0;
if (!lcp->his_lqrperiod)
log_Printf(LogLQM, "%s: LQR/ECHO LQR not negotiated\n",
physical->link.name);
1995-01-31 06:29:58 +00:00
}
}
void
lqr_Start(struct lcp *lcp)
{
struct physical *p = link2physical(lcp->fsm.link);
lqr_Setup(lcp);
if (p->hdlc.lqm.timer.load)
SendLqrReport(lcp);
}
1995-01-31 06:29:58 +00:00
void
lqr_reStart(struct lcp *lcp)
1995-01-31 06:29:58 +00:00
{
struct physical *p = link2physical(lcp->fsm.link);
1995-01-31 06:29:58 +00:00
lqr_Setup(lcp);
if (p->hdlc.lqm.timer.load)
timer_Start(&p->hdlc.lqm.timer);
}
void
lqr_StopTimer(struct physical *physical)
{
timer_Stop(&physical->hdlc.lqm.timer);
}
void
lqr_Stop(struct physical *physical, int method)
{
1995-01-31 06:29:58 +00:00
if (method == LQM_LQR)
log_Printf(LogLQM, "%s: Stop sending LQR, Use LCP ECHO instead.\n",
physical->link.name);
1995-01-31 06:29:58 +00:00
if (method == LQM_ECHO)
log_Printf(LogLQM, "%s: Stop sending LCP ECHO.\n",
physical->link.name);
physical->hdlc.lqm.method &= ~method;
if (physical->hdlc.lqm.method)
SendLqrReport(physical->hdlc.lqm.owner);
1995-01-31 06:29:58 +00:00
else
timer_Stop(&physical->hdlc.lqm.timer);
1995-01-31 06:29:58 +00:00
}
void
lqr_Dump(const char *link, const char *message, const struct lqrdata *lqr)
1995-01-31 06:29:58 +00:00
{
if (log_IsKept(LogLQM)) {
log_Printf(LogLQM, "%s: %s:\n", link, message);
log_Printf(LogLQM, " Magic: %08x LastOutLQRs: %08x\n",
lqr->MagicNumber, lqr->LastOutLQRs);
log_Printf(LogLQM, " LastOutPackets: %08x LastOutOctets: %08x\n",
lqr->LastOutPackets, lqr->LastOutOctets);
log_Printf(LogLQM, " PeerInLQRs: %08x PeerInPackets: %08x\n",
lqr->PeerInLQRs, lqr->PeerInPackets);
log_Printf(LogLQM, " PeerInDiscards: %08x PeerInErrors: %08x\n",
lqr->PeerInDiscards, lqr->PeerInErrors);
log_Printf(LogLQM, " PeerInOctets: %08x PeerOutLQRs: %08x\n",
lqr->PeerInOctets, lqr->PeerOutLQRs);
log_Printf(LogLQM, " PeerOutPackets: %08x PeerOutOctets: %08x\n",
lqr->PeerOutPackets, lqr->PeerOutOctets);
1995-01-31 06:29:58 +00:00
}
}