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