xref: /freebsd/usr.sbin/ppp/fsm.h (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 /*
32  *  State of machine
33  */
34 #define	ST_INITIAL	0
35 #define	ST_STARTING	1
36 #define	ST_CLOSED	2
37 #define	ST_STOPPED	3
38 #define	ST_CLOSING	4
39 #define	ST_STOPPING	5
40 #define	ST_REQSENT	6
41 #define	ST_ACKRCVD	7
42 #define	ST_ACKSENT	8
43 #define	ST_OPENED	9
44 
45 #define	ST_MAX		10
46 #define	ST_UNDEF	-1
47 
48 #define	MODE_REQ	0
49 #define	MODE_NAK	1
50 #define	MODE_REJ	2
51 #define	MODE_NOP	3
52 #define	MODE_ACK	4	/* pseudo mode for ccp negotiations */
53 
54 #define	OPEN_PASSIVE	-1
55 
56 #define FSM_REQ_TIMER	1
57 #define FSM_TRM_TIMER	2
58 
59 struct fsm;
60 
61 struct fsm_retry {
62   u_int timeout;                             /* FSM retry frequency */
63   u_int maxreq;                              /* Max Config REQ retries */
64   u_int maxtrm;                              /* Max Term REQ retries */
65 };
66 
67 struct fsm_decode {
68   u_char ack[100], *ackend;
69   u_char nak[100], *nakend;
70   u_char rej[100], *rejend;
71 };
72 
73 struct fsm_callbacks {
74   int (*LayerUp) (struct fsm *);             /* Layer is now up (tlu) */
75   void (*LayerDown) (struct fsm *);          /* About to come down (tld) */
76   void (*LayerStart) (struct fsm *);         /* Layer about to start up (tls) */
77   void (*LayerFinish) (struct fsm *);        /* Layer now down (tlf) */
78   void (*InitRestartCounter) (struct fsm *, int); /* Set fsm timer load */
79   void (*SendConfigReq) (struct fsm *);      /* Send REQ please */
80   void (*SentTerminateReq) (struct fsm *);   /* Term REQ just sent */
81   void (*SendTerminateAck) (struct fsm *, u_char); /* Send Term ACK please */
82   void (*DecodeConfig) (struct fsm *, u_char *, int, int, struct fsm_decode *);
83                                              /* Deal with incoming data */
84   int (*RecvResetReq) (struct fsm *fp);         /* Reset output */
85   void (*RecvResetAck) (struct fsm *fp, u_char); /* Reset input */
86 };
87 
88 struct fsm_parent {
89   void (*LayerStart) (void *, struct fsm *);         /* tls */
90   void (*LayerUp) (void *, struct fsm *);            /* tlu */
91   void (*LayerDown) (void *, struct fsm *);          /* tld */
92   void (*LayerFinish) (void *, struct fsm *);        /* tlf */
93   void *object;
94 };
95 
96 struct link;
97 struct bundle;
98 
99 struct fsm {
100   const char *name;		/* Name of protocol */
101   u_short proto;		/* Protocol number */
102   u_short min_code;
103   u_short max_code;
104   int open_mode;		/* Delay before config REQ (-1 forever) */
105   int state;			/* State of the machine */
106   u_char reqid;			/* Next request id */
107   int restart;			/* Restart counter value */
108 
109   struct {
110     int reqs;			/* Max config REQs before a close() */
111     int naks;			/* Max config NAKs before a close() */
112     int rejs;			/* Max config REJs before a close() */
113   } more;
114 
115   struct pppTimer FsmTimer;	/* Restart Timer */
116   struct pppTimer OpenTimer;	/* Delay before opening */
117 
118   /*
119    * This timer times the ST_STOPPED state out after the given value
120    * (specified via "set stopped ...").  Although this isn't specified in the
121    * rfc, the rfc *does* say that "the application may use higher level
122    * timers to avoid deadlock". The StoppedTimer takes effect when the other
123    * side ABENDs rather than going into ST_ACKSENT (and sending the ACK),
124    * causing ppp to time out and drop into ST_STOPPED.  At this point,
125    * nothing will change this state :-(
126    */
127   struct pppTimer StoppedTimer;
128   int LogLevel;
129 
130   /* The link layer active with this FSM (may be our bundle below) */
131   struct link *link;
132 
133   /* Our high-level link */
134   struct bundle *bundle;
135 
136   const struct fsm_parent *parent;
137   const struct fsm_callbacks *fn;
138 };
139 
140 struct fsmheader {
141   u_char code;			/* Request code */
142   u_char id;			/* Identification */
143   u_short length;		/* Length of packet */
144 };
145 
146 #define	CODE_CONFIGREQ	1
147 #define	CODE_CONFIGACK	2
148 #define	CODE_CONFIGNAK	3
149 #define	CODE_CONFIGREJ	4
150 #define	CODE_TERMREQ	5
151 #define	CODE_TERMACK	6
152 #define	CODE_CODEREJ	7
153 #define	CODE_PROTOREJ	8
154 #define	CODE_ECHOREQ	9	/* Used in LCP */
155 #define	CODE_ECHOREP	10	/* Used in LCP */
156 #define	CODE_DISCREQ	11
157 #define	CODE_IDENT	12	/* Used in LCP Extension */
158 #define	CODE_TIMEREM	13	/* Used in LCP Extension */
159 #define	CODE_RESETREQ	14	/* Used in CCP */
160 #define	CODE_RESETACK	15	/* Used in CCP */
161 
162 /* Minimum config req size.  This struct is *only* used for it's size */
163 struct fsmconfig {
164   u_char type;
165   u_char length;
166 };
167 
168 extern void fsm_Init(struct fsm *, const char *, u_short, int, int, int,
169                      struct bundle *, struct link *, const  struct fsm_parent *,
170                      struct fsm_callbacks *, const char * const [3]);
171 extern void fsm_Output(struct fsm *, u_int, u_int, u_char *, int, int);
172 extern void fsm_Open(struct fsm *);
173 extern void fsm_Up(struct fsm *);
174 extern void fsm_Down(struct fsm *);
175 extern void fsm_Input(struct fsm *, struct mbuf *);
176 extern void fsm_Close(struct fsm *);
177 extern int fsm_NullRecvResetReq(struct fsm *);
178 extern void fsm_NullRecvResetAck(struct fsm *, u_char);
179 extern void fsm_Reopen(struct fsm *);
180 extern void fsm2initial(struct fsm *);
181 extern const char *State2Nam(u_int);
182