fsm.h revision 44305
1/*
2 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
3 *
4 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
5 *
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
11 * by the Internet Initiative Japan.  The name of the
12 * IIJ may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * $Id: fsm.h,v 1.19 1998/06/25 22:33:24 brian Exp $
19 *
20 *	TODO:
21 */
22
23/*
24 *  State of machine
25 */
26#define	ST_INITIAL	0
27#define	ST_STARTING	1
28#define	ST_CLOSED	2
29#define	ST_STOPPED	3
30#define	ST_CLOSING	4
31#define	ST_STOPPING	5
32#define	ST_REQSENT	6
33#define	ST_ACKRCVD	7
34#define	ST_ACKSENT	8
35#define	ST_OPENED	9
36
37#define	ST_MAX		10
38#define	ST_UNDEF	-1
39
40#define	MODE_REQ	0
41#define	MODE_NAK	1
42#define	MODE_REJ	2
43#define	MODE_NOP	3
44#define	MODE_ACK	4	/* pseudo mode for ccp negotiations */
45
46#define	OPEN_PASSIVE	-1
47
48#define FSM_REQ_TIMER	1
49#define FSM_TRM_TIMER	2
50
51struct fsm;
52
53struct fsm_retry {
54  u_int timeout;                             /* FSM retry frequency */
55  u_int maxreq;                              /* Max Config REQ retries */
56  u_int maxtrm;                              /* Max Term REQ retries */
57};
58
59struct fsm_decode {
60  u_char ack[100], *ackend;
61  u_char nak[100], *nakend;
62  u_char rej[100], *rejend;
63};
64
65struct fsm_callbacks {
66  int (*LayerUp) (struct fsm *);             /* Layer is now up (tlu) */
67  void (*LayerDown) (struct fsm *);          /* About to come down (tld) */
68  void (*LayerStart) (struct fsm *);         /* Layer about to start up (tls) */
69  void (*LayerFinish) (struct fsm *);        /* Layer now down (tlf) */
70  void (*InitRestartCounter) (struct fsm *, int); /* Set fsm timer load */
71  void (*SendConfigReq) (struct fsm *);      /* Send REQ please */
72  void (*SentTerminateReq) (struct fsm *);   /* Term REQ just sent */
73  void (*SendTerminateAck) (struct fsm *, u_char); /* Send Term ACK please */
74  void (*DecodeConfig) (struct fsm *, u_char *, int, int, struct fsm_decode *);
75                                             /* Deal with incoming data */
76  void (*RecvResetReq) (struct fsm *fp);         /* Reset output */
77  void (*RecvResetAck) (struct fsm *fp, u_char); /* Reset input */
78};
79
80struct fsm_parent {
81  void (*LayerStart) (void *, struct fsm *);         /* tls */
82  void (*LayerUp) (void *, struct fsm *);            /* tlu */
83  void (*LayerDown) (void *, struct fsm *);          /* tld */
84  void (*LayerFinish) (void *, struct fsm *);        /* tlf */
85  void *object;
86};
87
88struct link;
89struct bundle;
90
91struct fsm {
92  const char *name;		/* Name of protocol */
93  u_short proto;		/* Protocol number */
94  u_short min_code;
95  u_short max_code;
96  int open_mode;		/* Delay before config REQ (-1 forever) */
97  int state;			/* State of the machine */
98  u_char reqid;			/* Next request id */
99  int restart;			/* Restart counter value */
100
101  struct {
102    int reqs;			/* Max config REQs before a close() */
103    int naks;			/* Max config NAKs before a close() */
104    int rejs;			/* Max config REJs before a close() */
105  } more;
106
107  struct pppTimer FsmTimer;	/* Restart Timer */
108  struct pppTimer OpenTimer;	/* Delay before opening */
109
110  /*
111   * This timer times the ST_STOPPED state out after the given value
112   * (specified via "set stopped ...").  Although this isn't specified in the
113   * rfc, the rfc *does* say that "the application may use higher level
114   * timers to avoid deadlock". The StoppedTimer takes effect when the other
115   * side ABENDs rather than going into ST_ACKSENT (and sending the ACK),
116   * causing ppp to time out and drop into ST_STOPPED.  At this point,
117   * nothing will change this state :-(
118   */
119  struct pppTimer StoppedTimer;
120  int LogLevel;
121
122  /* The link layer active with this FSM (may be our bundle below) */
123  struct link *link;
124
125  /* Our high-level link */
126  struct bundle *bundle;
127
128  const struct fsm_parent *parent;
129  const struct fsm_callbacks *fn;
130};
131
132struct fsmheader {
133  u_char code;			/* Request code */
134  u_char id;			/* Identification */
135  u_short length;		/* Length of packet */
136};
137
138#define	CODE_CONFIGREQ	1
139#define	CODE_CONFIGACK	2
140#define	CODE_CONFIGNAK	3
141#define	CODE_CONFIGREJ	4
142#define	CODE_TERMREQ	5
143#define	CODE_TERMACK	6
144#define	CODE_CODEREJ	7
145#define	CODE_PROTOREJ	8
146#define	CODE_ECHOREQ	9	/* Used in LCP */
147#define	CODE_ECHOREP	10	/* Used in LCP */
148#define	CODE_DISCREQ	11
149#define	CODE_IDENT	12	/* Used in LCP Extension */
150#define	CODE_TIMEREM	13	/* Used in LCP Extension */
151#define	CODE_RESETREQ	14	/* Used in CCP */
152#define	CODE_RESETACK	15	/* Used in CCP */
153
154/* Minimum config req size.  This struct is *only* used for it's size */
155struct fsmconfig {
156  u_char type;
157  u_char length;
158};
159
160extern void fsm_Init(struct fsm *, const char *, u_short, int, int, int,
161                     struct bundle *, struct link *, const  struct fsm_parent *,
162                     struct fsm_callbacks *, const char *[3]);
163extern void fsm_Output(struct fsm *, u_int, u_int, u_char *, int);
164extern void fsm_Open(struct fsm *);
165extern void fsm_Up(struct fsm *);
166extern void fsm_Down(struct fsm *);
167extern void fsm_Input(struct fsm *, struct mbuf *);
168extern void fsm_Close(struct fsm *);
169extern void fsm_NullRecvResetReq(struct fsm *);
170extern void fsm_NullRecvResetAck(struct fsm *, u_char);
171extern void fsm_Reopen(struct fsm *);
172extern void fsm2initial(struct fsm *);
173extern const char *State2Nam(u_int);
174