Move EAPOL authenticator state machine into src/eapol_auth
[libeap.git] / src / eapol_auth / eapol_auth_sm.h
1 /*
2  * IEEE 802.1X-2004 Authenticator - EAPOL state machine
3  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #ifndef EAPOL_AUTH_SM_H
16 #define EAPOL_AUTH_SM_H
17
18 #include "common/defs.h"
19 #include "radius/radius.h"
20
21 /* IEEE Std 802.1X-2004, Ch. 8.2 */
22
23 typedef enum { ForceUnauthorized = 1, ForceAuthorized = 3, Auto = 2 }
24         PortTypes;
25 typedef enum { Unauthorized = 2, Authorized = 1 } PortState;
26 typedef enum { Both = 0, In = 1 } ControlledDirection;
27 typedef unsigned int Counter;
28
29 struct eap_sm;
30
31
32 struct eapol_auth_config {
33         int eap_reauth_period;
34         int wpa;
35         int individual_wep_key_len;
36         int eap_server;
37         void *ssl_ctx;
38         void *eap_sim_db_priv;
39         char *eap_req_id_text; /* a copy of this will be allocated */
40         size_t eap_req_id_text_len;
41         u8 *pac_opaque_encr_key;
42         u8 *eap_fast_a_id;
43         size_t eap_fast_a_id_len;
44         char *eap_fast_a_id_info;
45         int eap_fast_prov;
46         int pac_key_lifetime;
47         int pac_key_refresh_time;
48         int eap_sim_aka_result_ind;
49         int tnc;
50         struct wps_context *wps;
51
52         /* Opaque context pointer to owner data for callback functions */
53         void *ctx;
54 };
55
56 struct eap_user;
57
58 typedef enum {
59         EAPOL_LOGGER_DEBUG, EAPOL_LOGGER_INFO, EAPOL_LOGGER_WARNING
60 } eapol_logger_level;
61
62 enum eapol_event {
63         EAPOL_AUTH_SM_CHANGE,
64         EAPOL_AUTH_REAUTHENTICATE
65 };
66
67 struct eapol_auth_cb {
68         void (*eapol_send)(void *ctx, void *sta_ctx, u8 type, const u8 *data,
69                            size_t datalen);
70         void (*aaa_send)(void *ctx, void *sta_ctx, const u8 *data,
71                          size_t datalen);
72         void (*finished)(void *ctx, void *sta_ctx, int success, int preauth);
73         int (*get_eap_user)(void *ctx, const u8 *identity, size_t identity_len,
74                             int phase2, struct eap_user *user);
75         int (*sta_entry_alive)(void *ctx, const u8 *addr);
76         void (*logger)(void *ctx, const u8 *addr, eapol_logger_level level,
77                        const char *txt);
78         void (*set_port_authorized)(void *ctx, void *sta_ctx, int authorized);
79         void (*abort_auth)(void *ctx, void *sta_ctx);
80         void (*tx_key)(void *ctx, void *sta_ctx);
81         void (*eapol_event)(void *ctx, void *sta_ctx, enum eapol_event type);
82 };
83
84 /**
85  * struct eapol_authenticator - Global EAPOL authenticator data
86  */
87 struct eapol_authenticator {
88         struct eapol_auth_config conf;
89         struct eapol_auth_cb cb;
90
91         u8 *default_wep_key;
92         u8 default_wep_key_idx;
93 };
94
95
96 /**
97  * struct eapol_state_machine - Per-Supplicant Authenticator state machines
98  */
99 struct eapol_state_machine {
100         /* timers */
101         int aWhile;
102         int quietWhile;
103         int reAuthWhen;
104
105         /* global variables */
106         Boolean authAbort;
107         Boolean authFail;
108         PortState authPortStatus;
109         Boolean authStart;
110         Boolean authTimeout;
111         Boolean authSuccess;
112         Boolean eapolEap;
113         Boolean initialize;
114         Boolean keyDone;
115         Boolean keyRun;
116         Boolean keyTxEnabled;
117         PortTypes portControl;
118         Boolean portValid;
119         Boolean reAuthenticate;
120
121         /* Port Timers state machine */
122         /* 'Boolean tick' implicitly handled as registered timeout */
123
124         /* Authenticator PAE state machine */
125         enum { AUTH_PAE_INITIALIZE, AUTH_PAE_DISCONNECTED, AUTH_PAE_CONNECTING,
126                AUTH_PAE_AUTHENTICATING, AUTH_PAE_AUTHENTICATED,
127                AUTH_PAE_ABORTING, AUTH_PAE_HELD, AUTH_PAE_FORCE_AUTH,
128                AUTH_PAE_FORCE_UNAUTH, AUTH_PAE_RESTART } auth_pae_state;
129         /* variables */
130         Boolean eapolLogoff;
131         Boolean eapolStart;
132         PortTypes portMode;
133         unsigned int reAuthCount;
134         /* constants */
135         unsigned int quietPeriod; /* default 60; 0..65535 */
136 #define AUTH_PAE_DEFAULT_quietPeriod 60
137         unsigned int reAuthMax; /* default 2 */
138 #define AUTH_PAE_DEFAULT_reAuthMax 2
139         /* counters */
140         Counter authEntersConnecting;
141         Counter authEapLogoffsWhileConnecting;
142         Counter authEntersAuthenticating;
143         Counter authAuthSuccessesWhileAuthenticating;
144         Counter authAuthTimeoutsWhileAuthenticating;
145         Counter authAuthFailWhileAuthenticating;
146         Counter authAuthEapStartsWhileAuthenticating;
147         Counter authAuthEapLogoffWhileAuthenticating;
148         Counter authAuthReauthsWhileAuthenticated;
149         Counter authAuthEapStartsWhileAuthenticated;
150         Counter authAuthEapLogoffWhileAuthenticated;
151
152         /* Backend Authentication state machine */
153         enum { BE_AUTH_REQUEST, BE_AUTH_RESPONSE, BE_AUTH_SUCCESS,
154                BE_AUTH_FAIL, BE_AUTH_TIMEOUT, BE_AUTH_IDLE, BE_AUTH_INITIALIZE,
155                BE_AUTH_IGNORE
156         } be_auth_state;
157         /* constants */
158         unsigned int serverTimeout; /* default 30; 1..X */
159 #define BE_AUTH_DEFAULT_serverTimeout 30
160         /* counters */
161         Counter backendResponses;
162         Counter backendAccessChallenges;
163         Counter backendOtherRequestsToSupplicant;
164         Counter backendAuthSuccesses;
165         Counter backendAuthFails;
166
167         /* Reauthentication Timer state machine */
168         enum { REAUTH_TIMER_INITIALIZE, REAUTH_TIMER_REAUTHENTICATE
169         } reauth_timer_state;
170         /* constants */
171         unsigned int reAuthPeriod; /* default 3600 s */
172         Boolean reAuthEnabled;
173
174         /* Authenticator Key Transmit state machine */
175         enum { AUTH_KEY_TX_NO_KEY_TRANSMIT, AUTH_KEY_TX_KEY_TRANSMIT
176         } auth_key_tx_state;
177
178         /* Key Receive state machine */
179         enum { KEY_RX_NO_KEY_RECEIVE, KEY_RX_KEY_RECEIVE } key_rx_state;
180         /* variables */
181         Boolean rxKey;
182
183         /* Controlled Directions state machine */
184         enum { CTRL_DIR_FORCE_BOTH, CTRL_DIR_IN_OR_BOTH } ctrl_dir_state;
185         /* variables */
186         ControlledDirection adminControlledDirections;
187         ControlledDirection operControlledDirections;
188         Boolean operEdge;
189
190         /* Authenticator Statistics Table */
191         Counter dot1xAuthEapolFramesRx;
192         Counter dot1xAuthEapolFramesTx;
193         Counter dot1xAuthEapolStartFramesRx;
194         Counter dot1xAuthEapolLogoffFramesRx;
195         Counter dot1xAuthEapolRespIdFramesRx;
196         Counter dot1xAuthEapolRespFramesRx;
197         Counter dot1xAuthEapolReqIdFramesTx;
198         Counter dot1xAuthEapolReqFramesTx;
199         Counter dot1xAuthInvalidEapolFramesRx;
200         Counter dot1xAuthEapLengthErrorFramesRx;
201         Counter dot1xAuthLastEapolFrameVersion;
202
203         /* Other variables - not defined in IEEE 802.1X */
204         u8 addr[ETH_ALEN]; /* Supplicant address */
205 #define EAPOL_SM_PREAUTH BIT(0)
206 #define EAPOL_SM_WAIT_START BIT(1)
207 #define EAPOL_SM_USES_WPA BIT(2)
208 #define EAPOL_SM_FROM_PMKSA_CACHE BIT(3)
209         int flags; /* EAPOL_SM_* */
210
211         /* EAPOL/AAA <-> EAP full authenticator interface */
212         struct eap_eapol_interface *eap_if;
213
214         int radius_identifier;
215         /* TODO: check when the last messages can be released */
216         struct radius_msg *last_recv_radius;
217         u8 last_eap_id; /* last used EAP Identifier */
218         u8 *identity;
219         size_t identity_len;
220         u8 eap_type_authsrv; /* EAP type of the last EAP packet from
221                               * Authentication server */
222         u8 eap_type_supp; /* EAP type of the last EAP packet from Supplicant */
223         struct radius_class_data radius_class;
224
225         /* Keys for encrypting and signing EAPOL-Key frames */
226         u8 *eapol_key_sign;
227         size_t eapol_key_sign_len;
228         u8 *eapol_key_crypt;
229         size_t eapol_key_crypt_len;
230
231         struct eap_sm *eap;
232
233         Boolean initializing; /* in process of initializing state machines */
234         Boolean changed;
235
236         struct eapol_authenticator *eapol;
237
238         void *sta; /* station context pointer to use in callbacks */
239 };
240
241
242 struct eapol_authenticator * eapol_auth_init(struct eapol_auth_config *conf,
243                                              struct eapol_auth_cb *cb);
244 void eapol_auth_deinit(struct eapol_authenticator *eapol);
245 struct eapol_state_machine *
246 eapol_auth_alloc(struct eapol_authenticator *eapol, const u8 *addr,
247                  int flags, const struct wpabuf *assoc_wps_ie, void *sta_ctx);
248 void eapol_auth_free(struct eapol_state_machine *sm);
249 void eapol_auth_step(struct eapol_state_machine *sm);
250 void eapol_auth_dump_state(FILE *f, const char *prefix,
251                            struct eapol_state_machine *sm);
252 int eapol_auth_eap_pending_cb(struct eapol_state_machine *sm, void *ctx);
253
254 #endif /* EAPOL_AUTH_SM_H */