xref: /openbsd-src/usr.sbin/npppd/npppd/fsm.h (revision 64eba9a26c6e9236e866b97e2b18e0edb144b187)
1 /*	$OpenBSD: fsm.h,v 1.7 2024/02/26 08:25:51 yasuoka Exp $ */
2 /*	$NetBSD: fsm.h,v 1.10 2000/09/23 22:39:35 christos Exp $	*/
3 
4 /*
5  * fsm.h - {Link, IP} Control Protocol Finite State Machine definitions.
6  *
7  * Copyright (c) 1989 Carnegie Mellon University.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that the above copyright notice and this paragraph are
12  * duplicated in all such forms and that any documentation,
13  * advertising materials, and other materials related to such
14  * distribution and use acknowledge that the software was developed
15  * by Carnegie Mellon University.  The name of the
16  * University may not be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  * Id: fsm.h,v 1.8 1999/11/15 01:51:50 paulus Exp
23  */
24 #ifndef	FSM_H
25 #define	FSM_H 1
26 
27 /*
28  * Packet header = Code, id, length.
29  */
30 #define HEADERLEN	4
31 
32 
33 /*
34  *  CP (LCP, IPCP, etc.) codes.
35  */
36 #define CONFREQ		1	/* Configuration Request */
37 #define CONFACK		2	/* Configuration Ack */
38 #define CONFNAK		3	/* Configuration Nak */
39 #define CONFREJ		4	/* Configuration Reject */
40 #define TERMREQ		5	/* Termination Request */
41 #define TERMACK		6	/* Termination Ack */
42 #define CODEREJ		7	/* Code Reject */
43 #define RESETREQ	14	/* Reset Request */
44 #define RESETACK	15	/* Reset Ack */
45 
46 struct evtimer_wrap {
47 	void *ctx;
48 	void (*func)(void *);
49 	struct event ev;
50 };
51 /*
52  * Each FSM is described by an fsm structure and fsm callbacks.
53  */
54 typedef struct fsm {
55     //int unit;			/* Interface unit number */
56     npppd_ppp *ppp;		/* npppd's ppp */
57     struct evtimer_wrap timerctx;/* context for event(3) */
58     int protocol;		/* Data Link Layer Protocol field value */
59     int state;			/* State */
60     int flags;			/* Contains option bits */
61     u_char id;			/* Current id */
62     u_char reqid;		/* Current request id */
63     u_char seen_ack;		/* Have received valid Ack/Nak/Rej to Req */
64     int timeouttime;		/* Timeout time in milliseconds */
65     int maxconfreqtransmits;	/* Maximum Configure-Request transmissions */
66     int retransmits;		/* Number of retransmissions left */
67     int maxtermtransmits;	/* Maximum Terminate-Request transmissions */
68     int nakloops;		/* Number of nak loops since last ack */
69     int maxnakloops;		/* Maximum number of nak loops tolerated */
70     struct fsm_callbacks *callbacks;	/* Callback routines */
71     const char *term_reason;	/* Reason for closing protocol */
72     int term_reason_len;	/* Length of term_reason */
73 } fsm;
74 
75 
76 typedef struct fsm_callbacks {
77     void (*resetci)(fsm *);	/* Reset our Configuration Information */
78     int  (*cilen)(fsm *);	/* Length of our Configuration Information */
79     void (*addci) 		/* Add our Configuration Information */
80 		(fsm *, u_char *, int *);
81     int  (*ackci)		/* ACK our Configuration Information */
82 		(fsm *, u_char *, int);
83     int  (*nakci)		/* NAK our Configuration Information */
84 		(fsm *, u_char *, int);
85     int  (*rejci)		/* Reject our Configuration Information */
86 		(fsm *, u_char *, int);
87     int  (*reqci)		/* Request peer's Configuration Information */
88 		(fsm *, u_char *, int *, int);
89     void (*up)(fsm *);		/* Called when fsm reaches OPENED state */
90     void (*down)(fsm *);	/* Called when fsm leaves OPENED state */
91 
92     void (*starting)(fsm *);	/* Called when we want the lower layer */
93     void (*finished)(fsm *);	/* Called when we don't want the lower layer */
94     void (*protreject)(int);	/* Called when Protocol-Reject received */
95     void (*retransmit)(fsm *);	/* Retransmission is necessary */
96     int  (*extcode)		/* Called when unknown code received */
97 		(fsm *, int, int, u_char *, int);
98     char *proto_name;		/* String name for protocol (for messages) */
99 } fsm_callbacks;
100 
101 
102 /*
103  * Link states.
104  */
105 #define INITIAL		0	/* Down, hasn't been opened */
106 #define STARTING	1	/* Down, been opened */
107 #define CLOSED		2	/* Up, hasn't been opened */
108 #define STOPPED		3	/* Open, waiting for down event */
109 #define CLOSING		4	/* Terminating the connection, not open */
110 #define STOPPING	5	/* Terminating, but open */
111 #define REQSENT		6	/* We've sent a Config Request */
112 #define ACKRCVD		7	/* We've received a Config Ack */
113 #define ACKSENT		8	/* We've sent a Config Ack */
114 #define OPENED		9	/* Connection available */
115 
116 
117 /*
118  * Flags - indicate options controlling FSM operation
119  */
120 #define OPT_PASSIVE	1	/* Don't die if we don't get a response */
121 #define OPT_RESTART	2	/* Treat 2nd OPEN as DOWN, UP */
122 #define OPT_SILENT	4	/* Wait for peer to speak first */
123 
124 
125 /*
126  * Timeouts.
127  */
128 #define DEFTIMEOUT	3	/* Timeout time in seconds */
129 #define DEFMAXTERMREQS	2	/* Maximum Terminate-Request transmissions */
130 #define DEFMAXCONFREQS	10	/* Maximum Configure-Request transmissions */
131 #define DEFMAXNAKLOOPS	5	/* Maximum number of nak loops */
132 
133 /** define TIMEOUT to use event(3)'s timer. */
134 #define	TIMEOUT(fn, f, t)						\
135 	{								\
136 		struct timeval tv0;					\
137 									\
138 		tv0.tv_usec = 0;					\
139 		tv0.tv_sec = (t);					\
140 		if (!evtimer_initialized(&(f)->timerctx.ev))		\
141 			evtimer_set(&(f)->timerctx.ev, fsm_evtimer_timeout,\
142 			    &(f)->timerctx);				\
143 		(f)->timerctx.func = fn;				\
144 		evtimer_del(&(f)->timerctx.ev);				\
145 		evtimer_add(&(f)->timerctx.ev, &tv0);			\
146 	}
147 
148 #define	UNTIMEOUT(fn, f)	evtimer_del(&(f)->timerctx.ev)
149 
150 /*
151  * Prototypes
152  */
153 void fsm_evtimer_timeout(int, short, void *);
154 void fsm_init(fsm *);
155 void fsm_lowerup(fsm *);
156 void fsm_lowerdown(fsm *);
157 void fsm_open(fsm *);
158 void fsm_close(fsm *, const char *);
159 void fsm_input(fsm *, u_char *, int);
160 void fsm_protreject(fsm *);
161 void fsm_sdata(fsm *, u_char, u_char, u_char *, int);
162 void fsm_log(fsm *, uint32_t, const char *, ...) __attribute__((__format__ (__printf__, 3, 4)));
163 
164 
165 #endif
166