From 7308ec6890afae98c7b0e239eeda8a11e452e198 Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Sat, 31 Jan 1998 02:48:30 +0000 Subject: Move the global FSMs into their relevent structures. IPCP, CCP and LCP are now just derived FSMs. Comment each of the FSM implementations so that we can tell what's going on. Revise the state transitions so that CCP and IPCP actually send terminate REQs when appropriate. The OS & IPCP layers are still like spagetti (next job). --- usr.sbin/ppp/ccp.c | 138 ++++++++++++++++++++++--------------- usr.sbin/ppp/ccp.h | 7 +- usr.sbin/ppp/chap.c | 4 +- usr.sbin/ppp/command.c | 44 ++++++------ usr.sbin/ppp/deflate.c | 12 ++-- usr.sbin/ppp/filter.c | 3 +- usr.sbin/ppp/fsm.c | 22 +++--- usr.sbin/ppp/fsm.h | 30 ++++---- usr.sbin/ppp/hdlc.c | 6 +- usr.sbin/ppp/ip.c | 6 +- usr.sbin/ppp/ipcp.c | 120 +++++++++++++++++++------------- usr.sbin/ppp/ipcp.h | 6 +- usr.sbin/ppp/lcp.c | 183 ++++++++++++++++++++++++++----------------------- usr.sbin/ppp/lcp.h | 4 +- usr.sbin/ppp/lqr.c | 11 ++- usr.sbin/ppp/main.c | 16 ++--- usr.sbin/ppp/os.c | 29 +++++--- usr.sbin/ppp/pap.c | 6 +- usr.sbin/ppp/phase.c | 3 +- usr.sbin/ppp/pred.c | 6 +- usr.sbin/ppp/route.c | 3 +- 21 files changed, 368 insertions(+), 291 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/ppp/ccp.c b/usr.sbin/ppp/ccp.c index 3e47385c549f..b4ab77dfcb20 100644 --- a/usr.sbin/ppp/ccp.c +++ b/usr.sbin/ppp/ccp.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: ccp.c,v 1.30.2.2 1998/01/29 20:45:13 brian Exp $ + * $Id: ccp.c,v 1.30.2.3 1998/01/30 19:45:27 brian Exp $ * * TODO: * o Support other compression protocols @@ -43,8 +43,6 @@ #include "pred.h" #include "deflate.h" -struct ccpstate CcpInfo = { -1, -1, -1, -1, -1, -1 }; - static void CcpSendConfigReq(struct fsm *); static void CcpSendTerminateReq(struct fsm *); static void CcpSendTerminateAck(struct fsm *); @@ -55,29 +53,30 @@ static void CcpLayerUp(struct fsm *); static void CcpLayerDown(struct fsm *); static void CcpInitRestartCounter(struct fsm *); -struct fsm CcpFsm = { - "CCP", - PROTO_CCP, - CCP_MAXCODE, - 0, - ST_INITIAL, - 0, 0, 0, - {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ - LogCCP, - - NULL, - - CcpLayerUp, - CcpLayerDown, - CcpLayerStart, - CcpLayerFinish, - CcpInitRestartCounter, - CcpSendConfigReq, - CcpSendTerminateReq, - CcpSendTerminateAck, - CcpDecodeConfig, +struct ccpstate CcpInfo = { + { + "CCP", + PROTO_CCP, + CCP_MAXCODE, + 0, + ST_INITIAL, + 0, 0, 0, + {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ + LogCCP, + NULL, + CcpLayerUp, + CcpLayerDown, + CcpLayerStart, + CcpLayerFinish, + CcpInitRestartCounter, + CcpSendConfigReq, + CcpSendTerminateReq, + CcpSendTerminateAck, + CcpDecodeConfig, + }, + -1, -1, -1, -1, -1, -1 }; static char const *cftypes[] = { @@ -125,7 +124,8 @@ int ReportCcpStatus(struct cmdargs const *arg) { if (VarTerm) { - fprintf(VarTerm, "%s [%s]\n", CcpFsm.name, StateNames[CcpFsm.state]); + fprintf(VarTerm, "%s [%s]\n", CcpInfo.fsm.name, + StateNames[CcpInfo.fsm.state]); fprintf(VarTerm, "My protocol = %s, His protocol = %s\n", protoname(CcpInfo.my_proto), protoname(CcpInfo.his_proto)); fprintf(VarTerm, "Output: %ld --> %ld, Input: %ld --> %ld\n", @@ -135,30 +135,25 @@ ReportCcpStatus(struct cmdargs const *arg) return 0; } -static void -ccpstateInit(void) -{ - if (CcpInfo.in_init) - (*algorithm[CcpInfo.in_algorithm]->i.Term)(); - if (CcpInfo.out_init) - (*algorithm[CcpInfo.out_algorithm]->o.Term)(); - memset(&CcpInfo, '\0', sizeof CcpInfo); - CcpInfo.in_algorithm = CcpInfo.out_algorithm = -1; - CcpInfo.his_proto = CcpInfo.my_proto = -1; - CcpInfo.reset_sent = CcpInfo.last_reset = -1; -} - void CcpInit(struct link *l) { - FsmInit(&CcpFsm, l); - ccpstateInit(); - CcpFsm.maxconfig = 10; + /* Initialise ourselves */ + FsmInit(&CcpInfo.fsm, l); + CcpInfo.his_proto = CcpInfo.my_proto = -1; + CcpInfo.reset_sent = CcpInfo.last_reset = -1; + CcpInfo.in_algorithm = CcpInfo.out_algorithm = -1; + CcpInfo.his_reject = CcpInfo.my_reject = 0; + CcpInfo.out_init = CcpInfo.in_init = 0; + CcpInfo.uncompout = CcpInfo.compout = 0; + CcpInfo.uncompin = CcpInfo.compin = 0; + CcpInfo.fsm.maxconfig = 10; } static void CcpInitRestartCounter(struct fsm *fp) { + /* Set fsm timer load */ fp->FsmTimer.load = VarRetryTimeout * SECTICKS; fp->restart = 5; } @@ -166,6 +161,7 @@ CcpInitRestartCounter(struct fsm *fp) static void CcpSendConfigReq(struct fsm *fp) { + /* Send config REQ please */ u_char *cp; int f; @@ -189,6 +185,7 @@ CcpSendConfigReq(struct fsm *fp) void CcpSendResetReq(struct fsm *fp) { + /* We can't read our input - ask peer to reset */ LogPrintf(LogCCP, "SendResetReq(%d)\n", fp->reqid); CcpInfo.reset_sent = fp->reqid; CcpInfo.last_reset = -1; @@ -198,12 +195,13 @@ CcpSendResetReq(struct fsm *fp) static void CcpSendTerminateReq(struct fsm *fp) { - /* Fsm has just send a terminate request */ + /* Term REQ just sent by FSM */ } static void CcpSendTerminateAck(struct fsm *fp) { + /* Send Term ACK please */ LogPrintf(LogCCP, "CcpSendTerminateAck\n"); FsmOutput(fp, CODE_TERMACK, fp->reqid++, NULL, 0); } @@ -211,6 +209,7 @@ CcpSendTerminateAck(struct fsm *fp) void CcpRecvResetReq(struct fsm *fp) { + /* Got a reset REQ, reset outgoing dictionary */ if (CcpInfo.out_init) (*algorithm[CcpInfo.out_algorithm]->o.Reset)(); } @@ -218,21 +217,30 @@ CcpRecvResetReq(struct fsm *fp) static void CcpLayerStart(struct fsm *fp) { + /* We're about to start up ! */ LogPrintf(LogCCP, "CcpLayerStart.\n"); } static void CcpLayerFinish(struct fsm *fp) { + /* We're now down */ LogPrintf(LogCCP, "CcpLayerFinish.\n"); - ccpstateInit(); + if (CcpInfo.in_init) { + (*algorithm[CcpInfo.in_algorithm]->i.Term)(); + CcpInfo.in_init = 0; + } + if (CcpInfo.out_init) { + (*algorithm[CcpInfo.out_algorithm]->o.Term)(); + CcpInfo.out_init = 0; + } } static void CcpLayerDown(struct fsm *fp) { + /* About to come down */ LogPrintf(LogCCP, "CcpLayerDown.\n"); - ccpstateInit(); } /* @@ -241,6 +249,7 @@ CcpLayerDown(struct fsm *fp) static void CcpLayerUp(struct fsm *fp) { + /* We're now up */ LogPrintf(LogCCP, "CcpLayerUp(%d).\n", fp->state); if (!CcpInfo.in_init && CcpInfo.in_algorithm >= 0 && CcpInfo.in_algorithm < NALGORITHMS) @@ -270,27 +279,40 @@ CcpLayerUp(struct fsm *fp) void CcpUp() { - FsmUp(&CcpFsm); + /* Lower layers are ready.... go */ + FsmUp(&CcpInfo.fsm); LogPrintf(LogCCP, "CCP Up event!!\n"); } +void +CcpDown() +{ + /* Physical link is gone - sudden death */ + if (CcpInfo.fsm.state >= ST_CLOSED) { + FsmDown(&CcpInfo.fsm); + /* FsmDown() results in a CcpLayerDown() if we're currently open. */ + CcpLayerFinish(&CcpInfo.fsm); + } +} + void CcpOpen() { + /* Start CCP please */ int f; for (f = 0; f < NALGORITHMS; f++) if (Enabled(algorithm[f]->Conf)) { - CcpFsm.open_mode = 0; - FsmOpen(&CcpFsm); + CcpInfo.fsm.open_mode = 0; + FsmOpen(&CcpInfo.fsm); break; } if (f == NALGORITHMS) for (f = 0; f < NALGORITHMS; f++) if (Acceptable(algorithm[f]->Conf)) { - CcpFsm.open_mode = OPEN_PASSIVE; - FsmOpen(&CcpFsm); + CcpInfo.fsm.open_mode = OPEN_PASSIVE; + FsmOpen(&CcpInfo.fsm); break; } } @@ -298,6 +320,7 @@ CcpOpen() static void CcpDecodeConfig(u_char *cp, int plen, int mode_type) { + /* Deal with incoming data */ int type, length; int f; @@ -384,11 +407,12 @@ CcpDecodeConfig(u_char *cp, int plen, int mode_type) void CcpInput(struct mbuf *bp) { + /* Got PROTO_CCP from link */ if (phase == PHASE_NETWORK) - FsmInput(&CcpFsm, bp); + FsmInput(&CcpInfo.fsm, bp); else { - if (phase > PHASE_NETWORK) - LogPrintf(LogCCP, "Error: Unexpected CCP in phase %d\n", phase); + if (phase < PHASE_NETWORK) + LogPrintf(LogCCP, "Error: Unexpected CCP in phase %d (ignored)\n", phase); pfree(bp); } } @@ -396,6 +420,7 @@ CcpInput(struct mbuf *bp) void CcpResetInput(u_char id) { + /* Got a reset ACK, reset incoming dictionary */ if (CcpInfo.reset_sent != -1) { if (id != CcpInfo.reset_sent) { LogPrintf(LogWARN, "CCP: Incorrect ResetAck (id %d, not %d) ignored\n", @@ -419,6 +444,7 @@ CcpResetInput(u_char id) int CcpOutput(struct link *l, int pri, u_short proto, struct mbuf *m) { + /* Compress outgoing data */ if (CcpInfo.out_init) return (*algorithm[CcpInfo.out_algorithm]->o.Write)(l, pri, proto, m); return 0; @@ -427,10 +453,11 @@ CcpOutput(struct link *l, int pri, u_short proto, struct mbuf *m) struct mbuf * CompdInput(u_short *proto, struct mbuf *m) { + /* Decompress incoming data */ if (CcpInfo.reset_sent != -1) { /* Send another REQ and put the packet in the bit bucket */ LogPrintf(LogCCP, "ReSendResetReq(%d)\n", CcpInfo.reset_sent); - FsmOutput(&CcpFsm, CODE_RESETREQ, CcpInfo.reset_sent, NULL, 0); + FsmOutput(&CcpInfo.fsm, CODE_RESETREQ, CcpInfo.reset_sent, NULL, 0); pfree(m); } else if (CcpInfo.in_init) return (*algorithm[CcpInfo.in_algorithm]->i.Read)(proto, m); @@ -440,6 +467,7 @@ CompdInput(u_short *proto, struct mbuf *m) void CcpDictSetup(u_short proto, struct mbuf *m) { + /* Add incoming data to the dictionary */ if (CcpInfo.in_init) (*algorithm[CcpInfo.in_algorithm]->i.DictSetup)(proto, m); } diff --git a/usr.sbin/ppp/ccp.h b/usr.sbin/ppp/ccp.h index 8f838ff12b3f..9e074480e0b6 100644 --- a/usr.sbin/ppp/ccp.h +++ b/usr.sbin/ppp/ccp.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: ccp.h,v 1.14.2.2 1998/01/29 20:45:14 brian Exp $ + * $Id: ccp.h,v 1.14.2.3 1998/01/30 19:45:29 brian Exp $ * * TODO: */ @@ -36,6 +36,8 @@ #define TY_DEFLATE 26 /* Deflate (gzip) - rfc 1979 */ struct ccpstate { + struct fsm fsm; /* The finite state machine */ + int his_proto; /* peer's compression protocol */ int my_proto; /* our compression protocol */ @@ -80,8 +82,6 @@ struct ccp_algorithm { } o; }; -extern struct fsm CcpFsm; - extern void CcpRecvResetReq(struct fsm *); extern void CcpSendResetReq(struct fsm *); extern void CcpInput(struct mbuf *); @@ -93,3 +93,4 @@ extern void CcpResetInput(u_char); extern int CcpOutput(struct link *, int, u_short, struct mbuf *); extern struct mbuf *CompdInput(u_short *, struct mbuf *); extern void CcpDictSetup(u_short, struct mbuf *); +extern void CcpDown(void); diff --git a/usr.sbin/ppp/chap.c b/usr.sbin/ppp/chap.c index a87a0aa83845..dc3f48772385 100644 --- a/usr.sbin/ppp/chap.c +++ b/usr.sbin/ppp/chap.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: chap.c,v 1.28.2.2 1998/01/30 01:33:44 brian Exp $ + * $Id: chap.c,v 1.28.2.3 1998/01/30 19:45:29 brian Exp $ * * TODO: */ @@ -256,7 +256,7 @@ RecvChapTalk(struct fsmheader *chp, struct mbuf *bp, struct physical *physical) */ ChapOutput(physical, CHAP_FAILURE, chp->id, "Invalid!!", 9); reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); break; } } diff --git a/usr.sbin/ppp/command.c b/usr.sbin/ppp/command.c index efdefbf99ca0..30d7134cc124 100644 --- a/usr.sbin/ppp/command.c +++ b/usr.sbin/ppp/command.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: command.c,v 1.131.2.2 1998/01/29 23:11:33 brian Exp $ + * $Id: command.c,v 1.131.2.3 1998/01/30 19:45:32 brian Exp $ * */ #include @@ -170,9 +170,9 @@ DialCommand(struct cmdargs const *arg) int tries; int res; - if (LcpFsm.state > ST_CLOSED) { + if (LcpInfo.fsm.state > ST_CLOSED) { if (VarTerm) - fprintf(VarTerm, "LCP state is [%s]\n", StateNames[LcpFsm.state]); + fprintf(VarTerm, "LCP state is [%s]\n", StateNames[LcpInfo.fsm.state]); return 0; } @@ -482,22 +482,22 @@ ShowStopped(struct cmdargs const *arg) return 0; fprintf(VarTerm, " Stopped Timer: LCP: "); - if (!LcpFsm.StoppedTimer.load) + if (!LcpInfo.fsm.StoppedTimer.load) fprintf(VarTerm, "Disabled"); else - fprintf(VarTerm, "%ld secs", LcpFsm.StoppedTimer.load / SECTICKS); + fprintf(VarTerm, "%ld secs", LcpInfo.fsm.StoppedTimer.load / SECTICKS); fprintf(VarTerm, ", IPCP: "); - if (!IpcpFsm.StoppedTimer.load) + if (!IpcpInfo.fsm.StoppedTimer.load) fprintf(VarTerm, "Disabled"); else - fprintf(VarTerm, "%ld secs", IpcpFsm.StoppedTimer.load / SECTICKS); + fprintf(VarTerm, "%ld secs", IpcpInfo.fsm.StoppedTimer.load / SECTICKS); fprintf(VarTerm, ", CCP: "); - if (!CcpFsm.StoppedTimer.load) + if (!CcpInfo.fsm.StoppedTimer.load) fprintf(VarTerm, "Disabled"); else - fprintf(VarTerm, "%ld secs", CcpFsm.StoppedTimer.load / SECTICKS); + fprintf(VarTerm, "%ld secs", CcpInfo.fsm.StoppedTimer.load / SECTICKS); fprintf(VarTerm, "\n"); @@ -742,7 +742,7 @@ Prompt() pauth = " ON "; else pauth = " on "; - if (IpcpFsm.state == ST_OPENED && phase == PHASE_NETWORK) + if (IpcpInfo.fsm.state == ST_OPENED && phase == PHASE_NETWORK) pconnect = "PPP"; else pconnect = "ppp"; @@ -840,9 +840,9 @@ ShowCommand(struct cmdargs const *arg) static int TerminalCommand(struct cmdargs const *arg) { - if (LcpFsm.state > ST_CLOSED) { + if (LcpInfo.fsm.state > ST_CLOSED) { if (VarTerm) - fprintf(VarTerm, "LCP state is [%s]\n", StateNames[LcpFsm.state]); + fprintf(VarTerm, "LCP state is [%s]\n", StateNames[LcpInfo.fsm.state]); return 1; } if (!IsInteractive(1)) @@ -865,9 +865,9 @@ QuitCommand(struct cmdargs const *arg) { if (VarTerm) { DropClient(1); - if (mode & MODE_INTER) - Cleanup(EX_NORMAL); - else if (arg->argc > 0 && !strcasecmp(*arg->argv, "all") && VarLocalAuth&LOCAL_AUTH) + if ((mode & MODE_INTER) || + (arg->argc > 0 && !strcasecmp(*arg->argv, "all") && + VarLocalAuth&LOCAL_AUTH)) Cleanup(EX_NORMAL); } @@ -878,7 +878,7 @@ static int CloseCommand(struct cmdargs const *arg) { reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); return 0; } @@ -988,16 +988,16 @@ SetRedialTimeout(struct cmdargs const *arg) static int SetStoppedTimeout(struct cmdargs const *arg) { - LcpFsm.StoppedTimer.load = 0; - IpcpFsm.StoppedTimer.load = 0; - CcpFsm.StoppedTimer.load = 0; + LcpInfo.fsm.StoppedTimer.load = 0; + IpcpInfo.fsm.StoppedTimer.load = 0; + CcpInfo.fsm.StoppedTimer.load = 0; if (arg->argc <= 3) { if (arg->argc > 0) { - LcpFsm.StoppedTimer.load = atoi(arg->argv[0]) * SECTICKS; + LcpInfo.fsm.StoppedTimer.load = atoi(arg->argv[0]) * SECTICKS; if (arg->argc > 1) { - IpcpFsm.StoppedTimer.load = atoi(arg->argv[1]) * SECTICKS; + IpcpInfo.fsm.StoppedTimer.load = atoi(arg->argv[1]) * SECTICKS; if (arg->argc > 2) - CcpFsm.StoppedTimer.load = atoi(arg->argv[2]) * SECTICKS; + CcpInfo.fsm.StoppedTimer.load = atoi(arg->argv[2]) * SECTICKS; } } return 0; diff --git a/usr.sbin/ppp/deflate.c b/usr.sbin/ppp/deflate.c index dcc3f07e9aad..19cd462e457e 100644 --- a/usr.sbin/ppp/deflate.c +++ b/usr.sbin/ppp/deflate.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: deflate.c,v 1.6.4.2 1998/01/30 01:33:45 brian Exp $ + * $Id: deflate.c,v 1.6.4.3 1998/01/30 19:45:33 brian Exp $ */ #include @@ -41,10 +41,10 @@ #include "vars.h" #include "hdlc.h" #include "timer.h" +#include "fsm.h" #include "lcp.h" #include "ccp.h" #include "lcpproto.h" -#include "fsm.h" #include "throughput.h" #include "link.h" #include "deflate.h" @@ -227,7 +227,7 @@ DeflateInput(u_short *proto, struct mbuf *mi) LogPrintf(LogERROR, "DeflateInput: Seq error: Got %d, expected %d\n", seq, InputState.seqno); pfree(mi_head); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); return NULL; } } @@ -264,7 +264,7 @@ DeflateInput(u_short *proto, struct mbuf *mi) res, InputState.cx.msg ? InputState.cx.msg : ""); pfree(mo_head); pfree(mi); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); return NULL; } @@ -305,7 +305,7 @@ DeflateInput(u_short *proto, struct mbuf *mi) if (first) { LogPrintf(LogERROR, "DeflateInput: Length error\n"); pfree(mo_head); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); return NULL; } @@ -387,7 +387,7 @@ DeflateDictSetup(u_short proto, struct mbuf *mi) res, InputState.cx.msg ? InputState.cx.msg : ""); LogPrintf(LogERROR, "DeflateDictSetup: avail_in %d, avail_out %d\n", InputState.cx.avail_in, InputState.cx.avail_out); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); mbfree(mi_head); /* lose our allocated ``head'' buf */ return; } diff --git a/usr.sbin/ppp/filter.c b/usr.sbin/ppp/filter.c index a9ea18c0f3af..cdb5dd6d71b7 100644 --- a/usr.sbin/ppp/filter.c +++ b/usr.sbin/ppp/filter.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: filter.c,v 1.22.2.1 1998/01/29 23:11:33 brian Exp $ + * $Id: filter.c,v 1.22.2.2 1998/01/30 19:45:38 brian Exp $ * * TODO: Shoud send ICMP error message when we discard packets. */ @@ -42,6 +42,7 @@ #include "throughput.h" #include "hdlc.h" #include "link.h" +#include "fsm.h" #include "ipcp.h" #include "filter.h" diff --git a/usr.sbin/ppp/fsm.c b/usr.sbin/ppp/fsm.c index 095a4eb5b4f3..91990890bcfb 100644 --- a/usr.sbin/ppp/fsm.c +++ b/usr.sbin/ppp/fsm.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: fsm.c,v 1.27.2.1 1998/01/29 00:49:19 brian Exp $ + * $Id: fsm.c,v 1.27.2.2 1998/01/30 19:45:39 brian Exp $ * * TODO: * o Refer loglevel for log output @@ -77,9 +77,9 @@ StoppedTimeout(void *v) StopTimer(&fp->OpenTimer); } if (link_IsActive(fp->link)) - DownConnection(); - else FsmDown(fp); + else + DownConnection(); } void @@ -202,7 +202,7 @@ FsmUp(struct fsm * fp) } void -FsmDown(struct fsm * fp) +FsmDown(struct fsm *fp) { switch (fp->state) { case ST_CLOSED: @@ -226,7 +226,7 @@ FsmDown(struct fsm * fp) } void -FsmClose(struct fsm * fp) +FsmClose(struct fsm *fp) { switch (fp->state) { case ST_STARTING: @@ -674,7 +674,7 @@ FsmRecvProtoRej(struct fsm *fp, struct fsmheader *lhp, struct mbuf *bp) LogPrintf(LogERROR, "FsmRecvProtoRej: Not a physical link !\n"); break; case PROTO_CCP: - fp = &CcpFsm; + fp = &CcpInfo.fsm; (fp->LayerFinish) (fp); switch (fp->state) { case ST_CLOSED: @@ -779,7 +779,10 @@ FsmRecvResetAck(struct fsm * fp, struct fsmheader * lhp, struct mbuf * bp) pfree(bp); } -static const struct fsmcodedesc FsmCodes[] = { +static const struct fsmcodedesc { + void (*action) (struct fsm *, struct fsmheader *, struct mbuf *); + const char *name; +} FsmCodes[] = { {FsmRecvConfigReq, "Configure Request",}, {FsmRecvConfigAck, "Configure Ack",}, {FsmRecvConfigNak, "Configure Nak",}, @@ -794,7 +797,7 @@ static const struct fsmcodedesc FsmCodes[] = { {FsmRecvIdent, "Ident",}, {FsmRecvTimeRemain, "Time Remain",}, {FsmRecvResetReq, "Reset Request",}, - {FsmRecvResetAck, "Reset Ack",}, + {FsmRecvResetAck, "Reset Ack",} }; void @@ -810,7 +813,8 @@ FsmInput(struct fsm * fp, struct mbuf * bp) return; } lhp = (struct fsmheader *) MBUF_CTOP(bp); - if (lhp->code == 0 || lhp->code > fp->max_code) { + if (lhp->code == 0 || lhp->code > fp->max_code || + lhp->code > sizeof FsmCodes / sizeof *FsmCodes) { pfree(bp); /* XXX: Should send code reject */ return; } diff --git a/usr.sbin/ppp/fsm.h b/usr.sbin/ppp/fsm.h index 44b0a95745d3..96073d808da8 100644 --- a/usr.sbin/ppp/fsm.h +++ b/usr.sbin/ppp/fsm.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: fsm.h,v 1.16.2.1 1998/01/29 00:49:21 brian Exp $ + * $Id: fsm.h,v 1.16.2.2 1998/01/30 19:45:40 brian Exp $ * * TODO: */ @@ -49,11 +49,11 @@ struct fsm { const char *name; /* Name of protocol */ u_short proto; /* Protocol number */ u_short max_code; - int open_mode; + int open_mode; /* Delay before config REQ (-1 forever) */ int state; /* State of the machine */ u_char reqid; /* Next request id */ int restart; /* Restart counter value */ - int maxconfig; + int maxconfig; /* Max config REQ (overridden in Init funcs) */ struct pppTimer FsmTimer; /* Restart Timer */ struct pppTimer OpenTimer; /* Delay before opening */ @@ -73,15 +73,15 @@ struct fsm { /* The link layer active with this FSM. */ struct link *link; - void (*LayerUp) (struct fsm *); - void (*LayerDown) (struct fsm *); - void (*LayerStart) (struct fsm *); - void (*LayerFinish) (struct fsm *); - void (*InitRestartCounter) (struct fsm *); - void (*SendConfigReq) (struct fsm *); - void (*SendTerminateReq) (struct fsm *); - void (*SendTerminateAck) (struct fsm *); - void (*DecodeConfig) (u_char *, int, int); + void (*LayerUp) (struct fsm *); /* Layer is now up (tlu) */ + void (*LayerDown) (struct fsm *); /* About to come down (tld) */ + void (*LayerStart) (struct fsm *); /* Layer about to start up (tls) */ + void (*LayerFinish) (struct fsm *); /* Layer now down (tlf) */ + void (*InitRestartCounter) (struct fsm *); /* Set fsm timer load */ + void (*SendConfigReq) (struct fsm *); /* Send REQ please */ + void (*SendTerminateReq) (struct fsm *); /* Term REQ just sent */ + void (*SendTerminateAck) (struct fsm *); /* Send Term ACK please */ + void (*DecodeConfig) (u_char *, int, int); /* Deal with incoming data */ }; struct fsmheader { @@ -106,11 +106,7 @@ struct fsmheader { #define CODE_RESETREQ 14 /* Used in CCP */ #define CODE_RESETACK 15 /* Used in CCP */ -struct fsmcodedesc { - void (*action) (struct fsm *, struct fsmheader *, struct mbuf *); - const char *name; -}; - +/* Minimum config req size. This struct is *only* used for it's size */ struct fsmconfig { u_char type; u_char length; diff --git a/usr.sbin/ppp/hdlc.c b/usr.sbin/ppp/hdlc.c index 942ba316ca76..fae9822798cf 100644 --- a/usr.sbin/ppp/hdlc.c +++ b/usr.sbin/ppp/hdlc.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: hdlc.c,v 1.28.2.2 1998/01/29 23:11:34 brian Exp $ + * $Id: hdlc.c,v 1.28.2.3 1998/01/30 19:45:41 brian Exp $ * * TODO: */ @@ -151,7 +151,7 @@ HdlcOutput(struct link *l, int pri, u_short proto, struct mbuf *bp) u_short fcs; if ((proto & 0xfff1) == 0x21) /* Network Layer protocol */ - if (CcpFsm.state == ST_OPENED) + if (CcpInfo.fsm.state == ST_OPENED) if (CcpOutput(l, pri, proto, bp)) return; @@ -378,7 +378,7 @@ DecodePacket(u_short proto, struct mbuf * bp, struct link *l) * If proto isn't PROTO_COMPD, we still want to pass it to the * decompression routines so that the dictionary's updated */ - if (CcpFsm.state == ST_OPENED) + if (CcpInfo.fsm.state == ST_OPENED) if (proto == PROTO_COMPD) { if ((bp = CompdInput(&proto, bp)) == NULL) return; diff --git a/usr.sbin/ppp/ip.c b/usr.sbin/ppp/ip.c index ca63b84c22ab..7c056a269431 100644 --- a/usr.sbin/ppp/ip.c +++ b/usr.sbin/ppp/ip.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: ip.c,v 1.38.2.2 1998/01/29 23:11:35 brian Exp $ + * $Id: ip.c,v 1.38.2.3 1998/01/30 19:45:43 brian Exp $ * * TODO: * o Return ICMP message for filterd packet @@ -73,7 +73,7 @@ IdleTimeout(void *v) { LogPrintf(LogPHASE, "Idle timer expired.\n"); reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); } /* @@ -522,7 +522,7 @@ IpStartOutput(struct link *l) struct mbuf *bp; int cnt; - if (IpcpFsm.state != ST_OPENED) + if (IpcpInfo.fsm.state != ST_OPENED) return; for (queue = &IpOutputQueues[PRI_FAST]; queue >= IpOutputQueues; queue--) { if (queue->top) { diff --git a/usr.sbin/ppp/ipcp.c b/usr.sbin/ppp/ipcp.c index 77eea63a7021..fc493d82b863 100644 --- a/usr.sbin/ppp/ipcp.c +++ b/usr.sbin/ppp/ipcp.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: ipcp.c,v 1.50.2.2 1998/01/29 23:11:36 brian Exp $ + * $Id: ipcp.c,v 1.50.2.3 1998/01/30 19:45:44 brian Exp $ * * TODO: * o More RFC1772 backwoard compatibility @@ -65,42 +65,41 @@ struct compreq { u_char compcid; }; -struct ipcpstate IpcpInfo = { MAX_VJ_STATES, 1 }; - -static void IpcpSendConfigReq(struct fsm *); -static void IpcpSendTerminateAck(struct fsm *); -static void IpcpSendTerminateReq(struct fsm *); -static void IpcpDecodeConfig(u_char *, int, int); -static void IpcpLayerStart(struct fsm *); -static void IpcpLayerFinish(struct fsm *); static void IpcpLayerUp(struct fsm *); static void IpcpLayerDown(struct fsm *); +static void IpcpLayerStart(struct fsm *); +static void IpcpLayerFinish(struct fsm *); static void IpcpInitRestartCounter(struct fsm *); +static void IpcpSendConfigReq(struct fsm *); +static void IpcpSendTerminateReq(struct fsm *); +static void IpcpSendTerminateAck(struct fsm *); +static void IpcpDecodeConfig(u_char *, int, int); -struct fsm IpcpFsm = { - "IPCP", - PROTO_IPCP, - IPCP_MAXCODE, - 0, - ST_INITIAL, - 0, 0, 0, - - {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ - LogIPCP, - - NULL, - - IpcpLayerUp, - IpcpLayerDown, - IpcpLayerStart, - IpcpLayerFinish, - IpcpInitRestartCounter, - IpcpSendConfigReq, - IpcpSendTerminateReq, - IpcpSendTerminateAck, - IpcpDecodeConfig, +struct ipcpstate IpcpInfo = { + { + "IPCP", + PROTO_IPCP, + IPCP_MAXCODE, + 0, + ST_INITIAL, + 0, 0, 0, + {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ + LogIPCP, + NULL, + IpcpLayerUp, + IpcpLayerDown, + IpcpLayerStart, + IpcpLayerFinish, + IpcpInitRestartCounter, + IpcpSendConfigReq, + IpcpSendTerminateReq, + IpcpSendTerminateAck, + IpcpDecodeConfig, + }, + MAX_VJ_STATES, + 1 }; static const char *cftypes[] = { @@ -139,12 +138,11 @@ IpcpAddOutOctets(int n) int ReportIpcpStatus(struct cmdargs const *arg) { - struct fsm *fp = &IpcpFsm; - if (!VarTerm) return 1; - fprintf(VarTerm, "%s [%s]\n", fp->name, StateNames[fp->state]); - if (IpcpFsm.state == ST_OPENED) { + fprintf(VarTerm, "%s [%s]\n", IpcpInfo.fsm.name, + StateNames[IpcpInfo.fsm.state]); + if (IpcpInfo.fsm.state == ST_OPENED) { fprintf(VarTerm, " his side: %s, %s\n", inet_ntoa(IpcpInfo.his_ipaddr), vj2asc(IpcpInfo.his_compproto)); fprintf(VarTerm, " my side: %s, %s\n", @@ -175,6 +173,7 @@ ReportIpcpStatus(struct cmdargs const *arg) void IpcpDefAddress() { + /* Setup default IP addresses (`hostname` -> 0.0.0.0) */ struct hostent *hp; char name[200]; @@ -227,9 +226,10 @@ ShowInitVJ(struct cmdargs const *args) void IpcpInit(struct link *l) { + /* Initialise ourselves */ + FsmInit(&IpcpInfo.fsm, l); if (iplist_isvalid(&IpcpInfo.DefHisChoice)) iplist_setrandpos(&IpcpInfo.DefHisChoice); - FsmInit(&IpcpFsm, l); IpcpInfo.his_compproto = 0; IpcpInfo.his_reject = IpcpInfo.my_reject = 0; @@ -260,13 +260,14 @@ IpcpInit(struct link *l) IpcpInfo.want_compproto = 0; IpcpInfo.heis1172 = 0; - IpcpFsm.maxconfig = 10; + IpcpInfo.fsm.maxconfig = 10; throughput_init(&IpcpInfo.throughput); } static void IpcpInitRestartCounter(struct fsm * fp) { + /* Set fsm timer load */ fp->FsmTimer.load = VarRetryTimeout * SECTICKS; fp->restart = 5; } @@ -274,6 +275,7 @@ IpcpInitRestartCounter(struct fsm * fp) static void IpcpSendConfigReq(struct fsm *fp) { + /* Send config REQ please */ struct physical *p = link2physical(fp->link); u_char *cp; struct lcp_opt o; @@ -308,12 +310,13 @@ IpcpSendConfigReq(struct fsm *fp) static void IpcpSendTerminateReq(struct fsm * fp) { - /* Fsm has just send a terminate request */ + /* Term REQ just sent by FSM */ } static void IpcpSendTerminateAck(struct fsm * fp) { + /* Send Term ACK please */ LogPrintf(LogIPCP, "IpcpSendTerminateAck\n"); FsmOutput(fp, CODE_TERMACK, fp->reqid++, NULL, 0); } @@ -321,31 +324,33 @@ IpcpSendTerminateAck(struct fsm * fp) static void IpcpLayerStart(struct fsm * fp) { + /* We're about to start up ! */ LogPrintf(LogIPCP, "IpcpLayerStart.\n"); } static void IpcpLayerFinish(struct fsm * fp) { + /* We're now down */ LogPrintf(LogIPCP, "IpcpLayerFinish.\n"); + /* Better tell LCP that it's all over (we're the only NCP) */ reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); } static void IpcpLayerDown(struct fsm * fp) { + /* About to come down */ LogPrintf(LogIPCP, "IpcpLayerDown.\n"); throughput_stop(&IpcpInfo.throughput); throughput_log(&IpcpInfo.throughput, LogIPCP, NULL); } -/* - * Called when IPCP has reached to OPEN state - */ static void IpcpLayerUp(struct fsm * fp) { + /* We're now up */ char tbuff[100]; Prompt(); @@ -375,19 +380,33 @@ IpcpLayerUp(struct fsm * fp) void IpcpUp() { - FsmUp(&IpcpFsm); + /* Lower layers are ready.... go */ + FsmUp(&IpcpInfo.fsm); LogPrintf(LogIPCP, "IPCP Up event!!\n"); } +void +IpcpDown() +{ + /* Physical link is gone - sudden death */ + if (IpcpInfo.fsm.state >= ST_CLOSED) { + FsmDown(&IpcpInfo.fsm); + /* FsmDown() results in an IpcpLayerDown() if we're currently open. */ + IpcpLayerFinish(&IpcpInfo.fsm); + } +} + void IpcpOpen() { - FsmOpen(&IpcpFsm); + /* Start IPCP please */ + FsmOpen(&IpcpInfo.fsm); } static int -AcceptableAddr(struct in_range * prange, struct in_addr ipaddr) +AcceptableAddr(struct in_range *prange, struct in_addr ipaddr) { + /* Is the given IP in the given range ? */ LogPrintf(LogDEBUG, "requested = %x\n", htonl(ipaddr.s_addr)); LogPrintf(LogDEBUG, "range = %x\n", htonl(prange->ipaddr.s_addr)); LogPrintf(LogDEBUG, "/%x\n", htonl(prange->mask.s_addr)); @@ -400,6 +419,7 @@ AcceptableAddr(struct in_range * prange, struct in_addr ipaddr) static void IpcpDecodeConfig(u_char * cp, int plen, int mode_type) { + /* Deal with incoming PROTO_IPCP */ int type, length; u_long *lp, compproto; struct compreq *pcomp; @@ -469,7 +489,7 @@ IpcpDecodeConfig(u_char * cp, int plen, int mode_type) IpcpInfo.want_ipaddr = ipaddr; } else { LogPrintf(LogIPCP, "%s: Unacceptable address!\n", inet_ntoa(ipaddr)); - FsmClose(&IpcpFsm); + FsmClose(&IpcpInfo.fsm); } break; case MODE_REJ: @@ -676,12 +696,14 @@ IpcpDecodeConfig(u_char * cp, int plen, int mode_type) void IpcpInput(struct mbuf * bp) { - FsmInput(&IpcpFsm, bp); + /* Got PROTO_IPCP from link */ + FsmInput(&IpcpInfo.fsm, bp); } int UseHisaddr(const char *hisaddr, int setaddr) { + /* Use `hisaddr' for the peers address (set iface if `setaddr') */ memset(&IpcpInfo.DefHisAddress, '\0', sizeof IpcpInfo.DefHisAddress); iplist_reset(&IpcpInfo.DefHisChoice); if (strpbrk(hisaddr, ",-")) { diff --git a/usr.sbin/ppp/ipcp.h b/usr.sbin/ppp/ipcp.h index f55677331d17..5e77e4875395 100644 --- a/usr.sbin/ppp/ipcp.h +++ b/usr.sbin/ppp/ipcp.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: ipcp.h,v 1.18.2.4 1998/01/30 01:32:55 brian Exp $ + * $Id: ipcp.h,v 1.18.2.5 1998/01/30 19:45:45 brian Exp $ * * TODO: */ @@ -40,6 +40,8 @@ struct in_range { }; struct ipcpstate { + struct fsm fsm; /* The finite state machine */ + int VJInitSlots; /* Maximum VJ slots */ int VJInitComp : 1; /* Slot compression */ @@ -71,7 +73,6 @@ struct ipcpstate { }; extern struct ipcpstate IpcpInfo; -extern struct fsm IpcpFsm; extern void IpcpInit(struct link *l); extern void IpcpDefAddress(void); @@ -84,3 +85,4 @@ extern void IpcpAddOutOctets(int); extern int UseHisaddr(const char *, int); extern int SetInitVJ(struct cmdargs const *); extern int ShowInitVJ(struct cmdargs const *); +extern void IpcpDown(void); diff --git a/usr.sbin/ppp/lcp.c b/usr.sbin/ppp/lcp.c index feaf0b0bccc9..b01397ecba58 100644 --- a/usr.sbin/ppp/lcp.c +++ b/usr.sbin/ppp/lcp.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: lcp.c,v 1.55.2.3 1998/01/30 01:33:45 brian Exp $ + * $Id: lcp.c,v 1.55.2.4 1998/01/30 19:45:47 brian Exp $ * * TODO: * o Limit data field length by MRU @@ -76,17 +76,40 @@ struct lqrreq { u_long period; /* Reporting interval */ }; -struct lcpstate LcpInfo; - -static void LcpSendConfigReq(struct fsm *); -static void LcpSendTerminateReq(struct fsm *); -static void LcpSendTerminateAck(struct fsm *); -static void LcpDecodeConfig(u_char *, int, int); -static void LcpInitRestartCounter(struct fsm *); static void LcpLayerUp(struct fsm *); static void LcpLayerDown(struct fsm *); static void LcpLayerStart(struct fsm *); static void LcpLayerFinish(struct fsm *); +static void LcpInitRestartCounter(struct fsm *); +static void LcpSendConfigReq(struct fsm *); +static void LcpSendTerminateReq(struct fsm *); +static void LcpSendTerminateAck(struct fsm *); +static void LcpDecodeConfig(u_char *, int, int); + +struct lcpstate LcpInfo = { + { + "LCP", /* Name of protocol */ + PROTO_LCP, /* Protocol Number */ + LCP_MAXCODE, + 1, /* Open mode delay */ + ST_INITIAL, /* State of machine */ + 0, 0, 0, + {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ + {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ + LogLCP, + NULL, + LcpLayerUp, + LcpLayerDown, + LcpLayerStart, + LcpLayerFinish, + LcpInitRestartCounter, + LcpSendConfigReq, + LcpSendTerminateReq, + LcpSendTerminateAck, + LcpDecodeConfig, + } +}; static const char *cftypes[] = { /* Check out the latest ``Assigned numbers'' rfc (rfc1700.txt) */ @@ -118,58 +141,30 @@ static const char *cftypes[] = { #define NCFTYPES (sizeof cftypes/sizeof cftypes[0]) -struct fsm LcpFsm = { - "LCP", /* Name of protocol */ - PROTO_LCP, /* Protocol Number */ - LCP_MAXCODE, - 1, /* Open mode delay */ - ST_INITIAL, /* State of machine */ - 0, 0, 0, - {0, 0, 0, NULL, NULL, NULL}, /* FSM timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Open timer */ - {0, 0, 0, NULL, NULL, NULL}, /* Stopped timer */ - LogLCP, - - NULL, - - LcpLayerUp, - LcpLayerDown, - LcpLayerStart, - LcpLayerFinish, - LcpInitRestartCounter, - LcpSendConfigReq, - LcpSendTerminateReq, - LcpSendTerminateAck, - LcpDecodeConfig, -}; - static void -LcpReportTime(void *data) +LcpReportTime(void *v) { - struct pppTimer *me = (struct pppTimer *)data; + /* Moan about HDLC errors */ + struct pppTimer *ReportTimer = (struct pppTimer *)v; if (LogIsKept(LogDEBUG)) { - time_t t; - - time(&t); + time_t t = time(NULL); LogPrintf(LogDEBUG, "LcpReportTime: %s\n", ctime(&t)); } - - StopTimer(me); - me->state = TIMER_STOPPED; - StartTimer(me); + StopTimer(ReportTimer); + ReportTimer->state = TIMER_STOPPED; + StartTimer(ReportTimer); HdlcErrorCheck(); } int ReportLcpStatus(struct cmdargs const *arg) { - struct fsm *fp = &LcpFsm; - if (!VarTerm) return 1; - fprintf(VarTerm, "%s [%s]\n", fp->name, StateNames[fp->state]); + fprintf(VarTerm, "%s [%s]\n", LcpInfo.fsm.name, + StateNames[LcpInfo.fsm.state]); fprintf(VarTerm, " his side: MRU %d, ACCMAP %08lx, PROTOCOMP %d, ACFCOMP %d,\n" " MAGIC %08lx, REJECT %04x\n", @@ -190,12 +185,10 @@ ReportLcpStatus(struct cmdargs const *arg) return 0; } -/* - * Generate random number which will be used as magic number. - */ static u_int32_t GenerateMagic(void) { + /* Generate random number which will be used as magic number */ randinit(); return (random()); } @@ -203,31 +196,38 @@ GenerateMagic(void) void LcpInit(struct physical *physical) { - FsmInit(&LcpFsm, physical2link(physical)); + /* Initialise ourselves */ + FsmInit(&LcpInfo.fsm, physical2link(physical)); HdlcInit(); - memset(&LcpInfo, '\0', sizeof LcpInfo); - LcpInfo.want_mru = VarMRU; + LcpInfo.his_mru = DEF_MRU; LcpInfo.his_accmap = 0xffffffff; + LcpInfo.his_magic = 0; + LcpInfo.his_lqrperiod = 0; + LcpInfo.his_protocomp = 0; + LcpInfo.his_acfcomp = 0; + LcpInfo.his_auth = 0; + + LcpInfo.want_mru = VarMRU; LcpInfo.want_accmap = VarAccmap; LcpInfo.want_magic = GenerateMagic(); - LcpInfo.want_auth = LcpInfo.his_auth = 0; - if (Enabled(ConfChap)) - LcpInfo.want_auth = PROTO_CHAP; - else if (Enabled(ConfPap)) - LcpInfo.want_auth = PROTO_PAP; - if (Enabled(ConfLqr)) - LcpInfo.want_lqrperiod = VarLqrTimeout * 100; - if (Enabled(ConfAcfcomp)) - LcpInfo.want_acfcomp = 1; - if (Enabled(ConfProtocomp)) - LcpInfo.want_protocomp = 1; - LcpFsm.maxconfig = 10; + LcpInfo.want_auth = Enabled(ConfChap) ? PROTO_CHAP : + Enabled(ConfPap) ? PROTO_PAP : 0; + LcpInfo.want_lqrperiod = Enabled(ConfLqr) ? VarLqrTimeout * 100 : 0; + LcpInfo.want_protocomp = Enabled(ConfProtocomp) ? 1 : 0; + LcpInfo.want_acfcomp = Enabled(ConfAcfcomp) ? 1 : 0; + + LcpInfo.his_reject = LcpInfo.my_reject = 0; + LcpInfo.auth_iwait = LcpInfo.auth_ineed = 0; + LcpInfo.LcpFailedMagic = 0; + memset(&LcpInfo.ReportTimer, '\0', sizeof LcpInfo.ReportTimer); + LcpInfo.fsm.maxconfig = 10; } static void LcpInitRestartCounter(struct fsm * fp) { + /* Set fsm timer load */ fp->FsmTimer.load = VarRetryTimeout * SECTICKS; fp->restart = 5; } @@ -313,6 +313,7 @@ do { \ static void LcpSendConfigReq(struct fsm *fp) { + /* Send config REQ please */ struct physical *p = link2physical(fp->link); u_char *cp; struct lcp_opt o; @@ -362,23 +363,23 @@ LcpSendConfigReq(struct fsm *fp) } void -LcpSendProtoRej(u_char * option, int count) +LcpSendProtoRej(u_char *option, int count) { - struct fsm *fp = &LcpFsm; - + /* Don't understand `option' */ LogPrintf(LogLCP, "LcpSendProtoRej\n"); - FsmOutput(fp, CODE_PROTOREJ, fp->reqid, option, count); + FsmOutput(&LcpInfo.fsm, CODE_PROTOREJ, LcpInfo.fsm.reqid, option, count); } static void LcpSendTerminateReq(struct fsm * fp) { - /* Fsm has just send a terminate request */ + /* Term REQ just sent by FSM */ } static void LcpSendTerminateAck(struct fsm * fp) { + /* Send Term ACK please */ LogPrintf(LogLCP, "LcpSendTerminateAck.\n"); FsmOutput(fp, CODE_TERMACK, fp->reqid++, NULL, 0); } @@ -386,6 +387,7 @@ LcpSendTerminateAck(struct fsm * fp) static void LcpLayerStart(struct fsm *fp) { + /* We're about to start up ! */ struct physical *p = link2physical(fp->link); LogPrintf(LogLCP, "LcpLayerStart\n"); @@ -408,25 +410,28 @@ StopAllTimers(void) static void LcpLayerFinish(struct fsm *fp) { + /* We're now down */ struct physical *p = link2physical(fp->link); LogPrintf(LogLCP, "LcpLayerFinish\n"); + LcpInfo.LcpFailedMagic = 0; link_Close(fp->link, 0); StopAllTimers(); + CcpInfo.fsm.restart = 0; + IpcpInfo.fsm.restart = 0; + /* We're down at last. Lets tell background and direct mode to get out */ - if (p) { + if (p) NewPhase(p, PHASE_DEAD); - LcpInit(p); - } else + else LogPrintf(LogERROR, "LcpLayerFinish: Not a physical link !\n"); - IpcpInit(fp->link); - CcpInit(fp->link); Prompt(); } static void LcpLayerUp(struct fsm *fp) { + /* We're now up */ struct physical *p = link2physical(fp->link); LogPrintf(LogLCP, "LcpLayerUp\n"); @@ -449,8 +454,9 @@ LcpLayerUp(struct fsm *fp) static void LcpLayerDown(struct fsm * fp) { - StopAllTimers(); + /* About to come down */ OsLinkdown(); + LogPrintf(LogLCP, "LcpLayerDown\n"); /* * OsLinkdown() brings CCP & IPCP down, then waits 'till we go from @@ -461,30 +467,35 @@ LcpLayerDown(struct fsm * fp) void LcpUp() { - FsmUp(&LcpFsm); + /* Lower layers are ready.... go */ LcpInfo.LcpFailedMagic = 0; + FsmUp(&LcpInfo.fsm); } void LcpDown() -{ /* Sudden death */ - LcpInfo.LcpFailedMagic = 0; - FsmDown(&LcpFsm); +{ + /* Physical link is gone - sudden death */ + CcpDown(); /* CCP must come down */ + IpcpDown(); /* IPCP must come down */ + FsmDown(&LcpInfo.fsm); /* FsmDown() results in a LcpLayerDown() if we're currently open. */ - LcpLayerFinish(&LcpFsm); + LcpLayerFinish(&LcpInfo.fsm); } void LcpOpen(int open_mode) { - LcpFsm.open_mode = open_mode; + /* Start LCP please (with the given open_mode) */ + LcpInfo.fsm.open_mode = open_mode; LcpInfo.LcpFailedMagic = 0; - FsmOpen(&LcpFsm); + FsmOpen(&LcpInfo.fsm); } void LcpClose(struct fsm *fp) { + /* Stop LCP please */ struct physical *p = link2physical(fp->link); if (p) @@ -494,15 +505,12 @@ LcpClose(struct fsm *fp) OsInterfaceDown(0); FsmClose(fp); - LcpInfo.LcpFailedMagic = 0; } -/* - * XXX: Should validate option length - */ static void LcpDecodeConfig(u_char *cp, int plen, int mode_type) { + /* Deal with incoming PROTO_LCP */ int type, length, sz, pos; u_int32_t *lp, magic, accmap; u_short mtu, mru, *sp, proto; @@ -684,7 +692,7 @@ LcpDecodeConfig(u_char *cp, int plen, int mode_type) break; case TY_QUALPROTO: - req = (struct lqrreq *) cp; + req = (struct lqrreq *)cp; LogPrintf(LogLCP, "%s proto %x, interval %dms\n", request, ntohs(req->proto), ntohl(req->period) * 10); switch (mode_type) { @@ -858,5 +866,6 @@ reqreject: void LcpInput(struct mbuf * bp) { - FsmInput(&LcpFsm, bp); + /* Got PROTO_LCP from link */ + FsmInput(&LcpInfo.fsm, bp); } diff --git a/usr.sbin/ppp/lcp.h b/usr.sbin/ppp/lcp.h index b71a29b324b5..6c1a1ae1dcc0 100644 --- a/usr.sbin/ppp/lcp.h +++ b/usr.sbin/ppp/lcp.h @@ -15,7 +15,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: lcp.h,v 1.16.2.1 1998/01/29 00:49:25 brian Exp $ + * $Id: lcp.h,v 1.16.2.2 1998/01/30 01:33:45 brian Exp $ * * TODO: */ @@ -23,6 +23,7 @@ #define REJECTED(p, x) ((p)->his_reject & (1<<(x))) struct lcpstate { + struct fsm fsm; /* The finite state machine */ u_int16_t his_mru; /* Peers maximum packet size */ u_int32_t his_accmap; /* Peeers async char control map */ u_int32_t his_magic; /* Peers magic number */ @@ -72,7 +73,6 @@ struct lcp_opt { struct physical; extern struct lcpstate LcpInfo; -extern struct fsm LcpFsm; extern void LcpInit(struct physical *); extern void LcpUp(void); diff --git a/usr.sbin/ppp/lqr.c b/usr.sbin/ppp/lqr.c index b0fc9b5d116c..1c6988fcfe6b 100644 --- a/usr.sbin/ppp/lqr.c +++ b/usr.sbin/ppp/lqr.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: lqr.c,v 1.22.2.2 1998/01/30 01:33:46 brian Exp $ + * $Id: lqr.c,v 1.22.2.3 1998/01/30 19:45:51 brian Exp $ * * o LQR based on RFC1333 * @@ -67,16 +67,15 @@ struct echolqr { static void SendEchoReq(void) { - struct fsm *fp = &LcpFsm; struct echolqr *lqr, lqrdata; - if (fp->state == ST_OPENED) { + if (LcpInfo.fsm.state == ST_OPENED) { lqr = &lqrdata; lqr->magic = htonl(LcpInfo.want_magic); lqr->signature = htonl(SIGNATURE); LogPrintf(LogLQM, "Send echo LQR [%d]\n", echoseq); lqr->sequence = htonl(echoseq++); - FsmOutput(fp, CODE_ECHOREQ, fp->reqid++, + FsmOutput(&LcpInfo.fsm, CODE_ECHOREQ, LcpInfo.fsm.reqid++, (u_char *) lqr, sizeof(struct echolqr)); } } @@ -126,7 +125,7 @@ SendLqrReport(void *v) LogPrintf(LogPHASE, "** 1 Too many ECHO packets are lost. **\n"); lqmmethod = 0; /* Prevent rcursion via LcpClose() */ reconnect(RECON_TRUE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); } else { bp = mballoc(sizeof(struct lqrdata), MB_LQR); HdlcOutput(physical2link(physical), PRI_LINK, PROTO_LQR, bp); @@ -137,7 +136,7 @@ SendLqrReport(void *v) LogPrintf(LogPHASE, "** 2 Too many ECHO packets are lost. **\n"); lqmmethod = 0; /* Prevent rcursion via LcpClose() */ reconnect(RECON_TRUE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); } else SendEchoReq(); } diff --git a/usr.sbin/ppp/main.c b/usr.sbin/ppp/main.c index 4dd5992f09b0..6b1a733f07c5 100644 --- a/usr.sbin/ppp/main.c +++ b/usr.sbin/ppp/main.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: main.c,v 1.121.2.2 1998/01/29 23:11:39 brian Exp $ + * $Id: main.c,v 1.121.2.3 1998/01/30 19:45:53 brian Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. @@ -227,7 +227,7 @@ CloseSession(int signo) } LogPrintf(LogPHASE, "Signal %d, terminate.\n", signo); reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); Cleanup(EX_TERM); } @@ -655,7 +655,7 @@ ReadTty(void) /* * XXX: Should check carrier. */ - if (LcpFsm.state <= ST_CLOSED) + if (LcpInfo.fsm.state <= ST_CLOSED) PacketMode(0); break; case '.': @@ -798,14 +798,14 @@ DoLoop(void) /* * If the link is down and we're in DDIAL mode, bring it back up. */ - if (mode & MODE_DDIAL && LcpFsm.state <= ST_CLOSED) + if (mode & MODE_DDIAL && LcpInfo.fsm.state <= ST_CLOSED) dial_up = 1; /* * If we lost carrier and want to re-establish the connection due to the * "set reconnect" value, we'd better bring the line back up. */ - if (LcpFsm.state <= ST_CLOSED) { + if (LcpInfo.fsm.state <= ST_CLOSED) { if (!dial_up && reconnectState == RECON_TRUE) { if (++reconnectCount <= VarReconnectTries) { LogPrintf(LogPHASE, "Connection lost, re-establish (%d/%d)\n", @@ -1019,7 +1019,7 @@ DoLoop(void) } if (Physical_FD_ISSET(pppVars.physical, &rfds)) { /* something to read from modem */ - if (LcpFsm.state <= ST_CLOSED) + if (LcpInfo.fsm.state <= ST_CLOSED) nointr_usleep(10000); n = Physical_Read(pppVars.physical, rbuff, sizeof rbuff); if ((mode & MODE_DIRECT) && n <= 0) { @@ -1027,7 +1027,7 @@ DoLoop(void) } else LogDumpBuff(LogASYNC, "ReadFromModem", rbuff, n); - if (LcpFsm.state <= ST_CLOSED) { + if (LcpInfo.fsm.state <= ST_CLOSED) { /* * In dedicated mode, we just discard input until LCP is started. */ @@ -1092,7 +1092,7 @@ DoLoop(void) * Process on-demand dialup. Output packets are queued within tunnel * device until IPCP is opened. */ - if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO) && + if (LcpInfo.fsm.state <= ST_CLOSED && (mode & MODE_AUTO) && (pri = PacketCheck(rbuff, n, FL_DIAL)) >= 0) dial_up = 1; diff --git a/usr.sbin/ppp/os.c b/usr.sbin/ppp/os.c index e753270d8008..4e774005d7b5 100644 --- a/usr.sbin/ppp/os.c +++ b/usr.sbin/ppp/os.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: os.c,v 1.42 1998/01/21 02:15:23 brian Exp $ + * $Id: os.c,v 1.42.2.1 1998/01/29 23:11:40 brian Exp $ * */ #include @@ -238,6 +238,10 @@ OsLinkup() else LogPrintf(LogLCP, "OsLinkup: %s\n", s); + /* + * XXX this stuff should really live in the FSM. Our config should + * associate executable sections in files with events. + */ if (SelectSystem(inet_ntoa(IpcpInfo.want_ipaddr), LINKUPFILE) < 0) { if (GetLabel()) { if (SelectSystem(GetLabel(), LINKUPFILE) < 0) @@ -258,25 +262,30 @@ OsLinkIsUp() void OsLinkdown() { - char *s; + char *s = NULL; int Level; if (linkup) { - s = (char *) inet_ntoa(peer_addr); + s = inet_ntoa(peer_addr); Level = LogIsKept(LogLINK) ? LogLINK : LogIPCP; LogPrintf(Level, "OsLinkdown: %s\n", s); + } - FsmDown(&IpcpFsm); /* IPCP must come down */ - FsmDown(&CcpFsm); /* CCP must come down */ + FsmClose(&IpcpInfo.fsm); + FsmClose(&CcpInfo.fsm); + if (linkup) { + /* + * XXX this stuff should really live in the FSM. Our config should + * associate executable sections in files with events. + */ linkup = 0; - if (SelectSystem(s, LINKDOWNFILE) < 0) { + if (SelectSystem(s, LINKDOWNFILE) < 0) if (GetLabel()) { if (SelectSystem(GetLabel(), LINKDOWNFILE) < 0) SelectSystem("MYADDR", LINKDOWNFILE); } else SelectSystem("MYADDR", LINKDOWNFILE); - } } } @@ -287,7 +296,11 @@ OsInterfaceDown(int final) int s; OsLinkdown(); - if (!final && (mode & MODE_DAEMON)) /* We still want interface alive */ + if (!final && (mode & MODE_DAEMON)) + /* + * We still want the interface alive - in case we're closing because of + * an LQR problem (and are going to re-open) + */ return (0); s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { diff --git a/usr.sbin/ppp/pap.c b/usr.sbin/ppp/pap.c index 8655e9e54194..848c6e058497 100644 --- a/usr.sbin/ppp/pap.c +++ b/usr.sbin/ppp/pap.c @@ -18,7 +18,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: pap.c,v 1.20.2.2 1998/01/30 01:33:46 brian Exp $ + * $Id: pap.c,v 1.20.2.3 1998/01/30 19:46:00 brian Exp $ * * TODO: */ @@ -186,7 +186,7 @@ PapInput(struct mbuf * bp, struct physical *physical) } else { SendPapCode(php->id, PAP_NAK, "Login incorrect", physical); reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); } break; case PAP_ACK: @@ -208,7 +208,7 @@ PapInput(struct mbuf * bp, struct physical *physical) cp[len] = 0; LogPrintf(LogPHASE, "Received PAP_NAK (%s)\n", cp); reconnect(RECON_FALSE); - LcpClose(&LcpFsm); + LcpClose(&LcpInfo.fsm); break; } } diff --git a/usr.sbin/ppp/phase.c b/usr.sbin/ppp/phase.c index 23ed999c966b..615e42931719 100644 --- a/usr.sbin/ppp/phase.c +++ b/usr.sbin/ppp/phase.c @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: phase.c,v 1.6.4.3 1998/01/30 01:33:46 brian Exp $ + * $Id: phase.c,v 1.6.4.4 1998/01/30 19:46:01 brian Exp $ */ #include @@ -36,6 +36,7 @@ #include "mbuf.h" #include "log.h" #include "timer.h" +#include "fsm.h" #include "lcp.h" #include "lcpproto.h" #include "auth.h" diff --git a/usr.sbin/ppp/pred.c b/usr.sbin/ppp/pred.c index 3ab8dcbdf022..e1b724de6041 100644 --- a/usr.sbin/ppp/pred.c +++ b/usr.sbin/ppp/pred.c @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: pred.c,v 1.20.2.1 1998/01/29 00:49:30 brian Exp $ + * $Id: pred.c,v 1.20.2.2 1998/01/30 19:46:04 brian Exp $ */ #include @@ -248,7 +248,7 @@ Pred1Input(u_short *proto, struct mbuf *bp) len &= 0x7fff; if (len != len1) { /* Error is detected. Send reset request */ LogPrintf(LogCCP, "Pred1: Length error\n"); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); pfree(bp); pfree(wp); return NULL; @@ -285,7 +285,7 @@ Pred1Input(u_short *proto, struct mbuf *bp) return wp; } else { LogDumpBp(LogHDLC, "Bad FCS", wp); - CcpSendResetReq(&CcpFsm); + CcpSendResetReq(&CcpInfo.fsm); pfree(wp); } pfree(bp); diff --git a/usr.sbin/ppp/route.c b/usr.sbin/ppp/route.c index 052ce028a4ff..74c99a674f95 100644 --- a/usr.sbin/ppp/route.c +++ b/usr.sbin/ppp/route.c @@ -17,7 +17,7 @@ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id: route.c,v 1.42.2.1 1998/01/29 23:11:42 brian Exp $ + * $Id: route.c,v 1.42.2.2 1998/01/30 19:46:05 brian Exp $ * */ @@ -53,6 +53,7 @@ #include "throughput.h" #include "hdlc.h" #include "link.h" +#include "fsm.h" #include "ipcp.h" #include "route.h" -- cgit v1.3