fsm.h revision 55146
16059Samurai/*
26059Samurai *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
36059Samurai *
46059Samurai *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
56059Samurai *
66059Samurai * Redistribution and use in source and binary forms are permitted
76059Samurai * provided that the above copyright notice and this paragraph are
86059Samurai * duplicated in all such forms and that any documentation,
96059Samurai * advertising materials, and other materials related to such
106059Samurai * distribution and use acknowledge that the software was developed
116059Samurai * by the Internet Initiative Japan.  The name of the
126059Samurai * IIJ may not be used to endorse or promote products derived
136059Samurai * from this software without specific prior written permission.
146059Samurai * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
156059Samurai * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
166059Samurai * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
176059Samurai *
1850479Speter * $FreeBSD: head/usr.sbin/ppp/fsm.h 55146 1999-12-27 11:54:57Z brian $
196059Samurai *
206059Samurai *	TODO:
216059Samurai */
226735Samurai
236059Samurai/*
246059Samurai *  State of machine
256059Samurai */
266059Samurai#define	ST_INITIAL	0
276059Samurai#define	ST_STARTING	1
286059Samurai#define	ST_CLOSED	2
296059Samurai#define	ST_STOPPED	3
306059Samurai#define	ST_CLOSING	4
316059Samurai#define	ST_STOPPING	5
326059Samurai#define	ST_REQSENT	6
336059Samurai#define	ST_ACKRCVD	7
346059Samurai#define	ST_ACKSENT	8
356059Samurai#define	ST_OPENED	9
366059Samurai
376059Samurai#define	ST_MAX		10
386059Samurai#define	ST_UNDEF	-1
396059Samurai
406059Samurai#define	MODE_REQ	0
416059Samurai#define	MODE_NAK	1
426059Samurai#define	MODE_REJ	2
436735Samurai#define	MODE_NOP	3
4431514Sbrian#define	MODE_ACK	4	/* pseudo mode for ccp negotiations */
456059Samurai
4632658Sbrian#define	OPEN_PASSIVE	-1
476059Samurai
4844305Sbrian#define FSM_REQ_TIMER	1
4944305Sbrian#define FSM_TRM_TIMER	2
5044305Sbrian
5136285Sbrianstruct fsm;
5236285Sbrian
5344305Sbrianstruct fsm_retry {
5444305Sbrian  u_int timeout;                             /* FSM retry frequency */
5544305Sbrian  u_int maxreq;                              /* Max Config REQ retries */
5644305Sbrian  u_int maxtrm;                              /* Max Term REQ retries */
5744305Sbrian};
5844305Sbrian
5936285Sbrianstruct fsm_decode {
6036285Sbrian  u_char ack[100], *ackend;
6136285Sbrian  u_char nak[100], *nakend;
6236285Sbrian  u_char rej[100], *rejend;
6336285Sbrian};
6436285Sbrian
6536285Sbrianstruct fsm_callbacks {
6636285Sbrian  int (*LayerUp) (struct fsm *);             /* Layer is now up (tlu) */
6736285Sbrian  void (*LayerDown) (struct fsm *);          /* About to come down (tld) */
6836285Sbrian  void (*LayerStart) (struct fsm *);         /* Layer about to start up (tls) */
6936285Sbrian  void (*LayerFinish) (struct fsm *);        /* Layer now down (tlf) */
7044305Sbrian  void (*InitRestartCounter) (struct fsm *, int); /* Set fsm timer load */
7136285Sbrian  void (*SendConfigReq) (struct fsm *);      /* Send REQ please */
7236285Sbrian  void (*SentTerminateReq) (struct fsm *);   /* Term REQ just sent */
7336285Sbrian  void (*SendTerminateAck) (struct fsm *, u_char); /* Send Term ACK please */
7436285Sbrian  void (*DecodeConfig) (struct fsm *, u_char *, int, int, struct fsm_decode *);
7536285Sbrian                                             /* Deal with incoming data */
7636285Sbrian  void (*RecvResetReq) (struct fsm *fp);         /* Reset output */
7736285Sbrian  void (*RecvResetAck) (struct fsm *fp, u_char); /* Reset input */
7836285Sbrian};
7936285Sbrian
8036285Sbrianstruct fsm_parent {
8136285Sbrian  void (*LayerStart) (void *, struct fsm *);         /* tls */
8236285Sbrian  void (*LayerUp) (void *, struct fsm *);            /* tlu */
8336285Sbrian  void (*LayerDown) (void *, struct fsm *);          /* tld */
8436285Sbrian  void (*LayerFinish) (void *, struct fsm *);        /* tlf */
8536285Sbrian  void *object;
8636285Sbrian};
8736285Sbrian
8836285Sbrianstruct link;
8936285Sbrianstruct bundle;
9036285Sbrian
916059Samuraistruct fsm {
9231343Sbrian  const char *name;		/* Name of protocol */
936059Samurai  u_short proto;		/* Protocol number */
9436285Sbrian  u_short min_code;
956059Samurai  u_short max_code;
9636285Sbrian  int open_mode;		/* Delay before config REQ (-1 forever) */
9728679Sbrian  int state;			/* State of the machine */
9832381Sbrian  u_char reqid;			/* Next request id */
9928679Sbrian  int restart;			/* Restart counter value */
1006059Samurai
10144305Sbrian  struct {
10244305Sbrian    int reqs;			/* Max config REQs before a close() */
10344305Sbrian    int naks;			/* Max config NAKs before a close() */
10444305Sbrian    int rejs;			/* Max config REJs before a close() */
10544305Sbrian  } more;
10644305Sbrian
1076059Samurai  struct pppTimer FsmTimer;	/* Restart Timer */
10832658Sbrian  struct pppTimer OpenTimer;	/* Delay before opening */
1096059Samurai
11028461Sbrian  /*
11128461Sbrian   * This timer times the ST_STOPPED state out after the given value
11228679Sbrian   * (specified via "set stopped ...").  Although this isn't specified in the
11328679Sbrian   * rfc, the rfc *does* say that "the application may use higher level
11428679Sbrian   * timers to avoid deadlock". The StoppedTimer takes effect when the other
11528679Sbrian   * side ABENDs rather than going into ST_ACKSENT (and sending the ACK),
11628679Sbrian   * causing ppp to time out and drop into ST_STOPPED.  At this point,
11728679Sbrian   * nothing will change this state :-(
11828461Sbrian   */
11928461Sbrian  struct pppTimer StoppedTimer;
12028461Sbrian  int LogLevel;
12128461Sbrian
12236285Sbrian  /* The link layer active with this FSM (may be our bundle below) */
12336285Sbrian  struct link *link;
12436285Sbrian
12536285Sbrian  /* Our high-level link */
12636285Sbrian  struct bundle *bundle;
12736285Sbrian
12836285Sbrian  const struct fsm_parent *parent;
12936285Sbrian  const struct fsm_callbacks *fn;
1306059Samurai};
1316059Samurai
1326059Samuraistruct fsmheader {
13328679Sbrian  u_char code;			/* Request code */
13428679Sbrian  u_char id;			/* Identification */
13528679Sbrian  u_short length;		/* Length of packet */
1366059Samurai};
1376059Samurai
1386059Samurai#define	CODE_CONFIGREQ	1
1396059Samurai#define	CODE_CONFIGACK	2
1406059Samurai#define	CODE_CONFIGNAK	3
1416059Samurai#define	CODE_CONFIGREJ	4
1426059Samurai#define	CODE_TERMREQ	5
1436059Samurai#define	CODE_TERMACK	6
1446059Samurai#define	CODE_CODEREJ	7
1456059Samurai#define	CODE_PROTOREJ	8
14628679Sbrian#define	CODE_ECHOREQ	9	/* Used in LCP */
14728679Sbrian#define	CODE_ECHOREP	10	/* Used in LCP */
1486059Samurai#define	CODE_DISCREQ	11
14928679Sbrian#define	CODE_IDENT	12	/* Used in LCP Extension */
15028679Sbrian#define	CODE_TIMEREM	13	/* Used in LCP Extension */
15128679Sbrian#define	CODE_RESETREQ	14	/* Used in CCP */
15228679Sbrian#define	CODE_RESETACK	15	/* Used in CCP */
1536059Samurai
15436285Sbrian/* Minimum config req size.  This struct is *only* used for it's size */
1556059Samuraistruct fsmconfig {
1566059Samurai  u_char type;
1576059Samurai  u_char length;
1586059Samurai};
1596059Samurai
16044305Sbrianextern void fsm_Init(struct fsm *, const char *, u_short, int, int, int,
16136285Sbrian                     struct bundle *, struct link *, const  struct fsm_parent *,
16255146Sbrian                     struct fsm_callbacks *, const char * const [3]);
16347695Sbrianextern void fsm_Output(struct fsm *, u_int, u_int, u_char *, int, int);
16436285Sbrianextern void fsm_Open(struct fsm *);
16536285Sbrianextern void fsm_Up(struct fsm *);
16636285Sbrianextern void fsm_Down(struct fsm *);
16736285Sbrianextern void fsm_Input(struct fsm *, struct mbuf *);
16836285Sbrianextern void fsm_Close(struct fsm *);
16937060Sbrianextern void fsm_NullRecvResetReq(struct fsm *);
17037060Sbrianextern void fsm_NullRecvResetAck(struct fsm *, u_char);
17137160Sbrianextern void fsm_Reopen(struct fsm *);
17237060Sbrianextern void fsm2initial(struct fsm *);
17336285Sbrianextern const char *State2Nam(u_int);
174