fsm.h revision 36285
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 *
1836285Sbrian * $Id: fsm.h,v 1.16.2.16 1998/05/01 19:24:35 brian Exp $
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
4836285Sbrianstruct fsm;
4936285Sbrian
5036285Sbrianstruct fsm_decode {
5136285Sbrian  u_char ack[100], *ackend;
5236285Sbrian  u_char nak[100], *nakend;
5336285Sbrian  u_char rej[100], *rejend;
5436285Sbrian};
5536285Sbrian
5636285Sbrianstruct fsm_callbacks {
5736285Sbrian  int (*LayerUp) (struct fsm *);             /* Layer is now up (tlu) */
5836285Sbrian  void (*LayerDown) (struct fsm *);          /* About to come down (tld) */
5936285Sbrian  void (*LayerStart) (struct fsm *);         /* Layer about to start up (tls) */
6036285Sbrian  void (*LayerFinish) (struct fsm *);        /* Layer now down (tlf) */
6136285Sbrian  void (*InitRestartCounter) (struct fsm *); /* Set fsm timer load */
6236285Sbrian  void (*SendConfigReq) (struct fsm *);      /* Send REQ please */
6336285Sbrian  void (*SentTerminateReq) (struct fsm *);   /* Term REQ just sent */
6436285Sbrian  void (*SendTerminateAck) (struct fsm *, u_char); /* Send Term ACK please */
6536285Sbrian  void (*DecodeConfig) (struct fsm *, u_char *, int, int, struct fsm_decode *);
6636285Sbrian                                             /* Deal with incoming data */
6736285Sbrian  void (*RecvResetReq) (struct fsm *fp);         /* Reset output */
6836285Sbrian  void (*RecvResetAck) (struct fsm *fp, u_char); /* Reset input */
6936285Sbrian};
7036285Sbrian
7136285Sbrianstruct fsm_parent {
7236285Sbrian  void (*LayerStart) (void *, struct fsm *);         /* tls */
7336285Sbrian  void (*LayerUp) (void *, struct fsm *);            /* tlu */
7436285Sbrian  void (*LayerDown) (void *, struct fsm *);          /* tld */
7536285Sbrian  void (*LayerFinish) (void *, struct fsm *);        /* tlf */
7636285Sbrian  void *object;
7736285Sbrian};
7836285Sbrian
7936285Sbrianstruct link;
8036285Sbrianstruct bundle;
8136285Sbrian
826059Samuraistruct fsm {
8331343Sbrian  const char *name;		/* Name of protocol */
846059Samurai  u_short proto;		/* Protocol number */
8536285Sbrian  u_short min_code;
866059Samurai  u_short max_code;
8736285Sbrian  int open_mode;		/* Delay before config REQ (-1 forever) */
8828679Sbrian  int state;			/* State of the machine */
8932381Sbrian  u_char reqid;			/* Next request id */
9028679Sbrian  int restart;			/* Restart counter value */
9136285Sbrian  int maxconfig;		/* Max config REQ before a close() */
926059Samurai
936059Samurai  struct pppTimer FsmTimer;	/* Restart Timer */
9432658Sbrian  struct pppTimer OpenTimer;	/* Delay before opening */
956059Samurai
9628461Sbrian  /*
9728461Sbrian   * This timer times the ST_STOPPED state out after the given value
9828679Sbrian   * (specified via "set stopped ...").  Although this isn't specified in the
9928679Sbrian   * rfc, the rfc *does* say that "the application may use higher level
10028679Sbrian   * timers to avoid deadlock". The StoppedTimer takes effect when the other
10128679Sbrian   * side ABENDs rather than going into ST_ACKSENT (and sending the ACK),
10228679Sbrian   * causing ppp to time out and drop into ST_STOPPED.  At this point,
10328679Sbrian   * nothing will change this state :-(
10428461Sbrian   */
10528461Sbrian  struct pppTimer StoppedTimer;
10628461Sbrian  int LogLevel;
10728461Sbrian
10836285Sbrian  /* The link layer active with this FSM (may be our bundle below) */
10936285Sbrian  struct link *link;
11036285Sbrian
11136285Sbrian  /* Our high-level link */
11236285Sbrian  struct bundle *bundle;
11336285Sbrian
11436285Sbrian  const struct fsm_parent *parent;
11536285Sbrian  const struct fsm_callbacks *fn;
1166059Samurai};
1176059Samurai
1186059Samuraistruct fsmheader {
11928679Sbrian  u_char code;			/* Request code */
12028679Sbrian  u_char id;			/* Identification */
12128679Sbrian  u_short length;		/* Length of packet */
1226059Samurai};
1236059Samurai
1246059Samurai#define	CODE_CONFIGREQ	1
1256059Samurai#define	CODE_CONFIGACK	2
1266059Samurai#define	CODE_CONFIGNAK	3
1276059Samurai#define	CODE_CONFIGREJ	4
1286059Samurai#define	CODE_TERMREQ	5
1296059Samurai#define	CODE_TERMACK	6
1306059Samurai#define	CODE_CODEREJ	7
1316059Samurai#define	CODE_PROTOREJ	8
13228679Sbrian#define	CODE_ECHOREQ	9	/* Used in LCP */
13328679Sbrian#define	CODE_ECHOREP	10	/* Used in LCP */
1346059Samurai#define	CODE_DISCREQ	11
13528679Sbrian#define	CODE_IDENT	12	/* Used in LCP Extension */
13628679Sbrian#define	CODE_TIMEREM	13	/* Used in LCP Extension */
13728679Sbrian#define	CODE_RESETREQ	14	/* Used in CCP */
13828679Sbrian#define	CODE_RESETACK	15	/* Used in CCP */
1396059Samurai
14036285Sbrian/* Minimum config req size.  This struct is *only* used for it's size */
1416059Samuraistruct fsmconfig {
1426059Samurai  u_char type;
1436059Samurai  u_char length;
1446059Samurai};
1456059Samurai
14636285Sbrianextern void fsm_Init(struct fsm *, const char *, u_short, int, int, int, int,
14736285Sbrian                     struct bundle *, struct link *, const  struct fsm_parent *,
14836285Sbrian                     struct fsm_callbacks *, const char *[3]);
14936285Sbrianextern void fsm_Output(struct fsm *, u_int, u_int, u_char *, int);
15036285Sbrianextern void fsm_Open(struct fsm *);
15136285Sbrianextern void fsm_Up(struct fsm *);
15236285Sbrianextern void fsm_Down(struct fsm *);
15336285Sbrianextern void fsm_Input(struct fsm *, struct mbuf *);
15436285Sbrianextern void fsm_Close(struct fsm *);
15536285Sbrianextern void fsm_NullRecvResetReq(struct fsm *fp);
15636285Sbrianextern void fsm_NullRecvResetAck(struct fsm *fp, u_char);
15736285Sbrianextern const char *State2Nam(u_int);
158