supplicant.c
Go to the documentation of this file.
1 /**
2  * @file supplicant.c
3  * @brief 802.1X supplicant
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneEAP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SUPPLICANT_TRACE_LEVEL
33 
34 //Dependencies
35 #include "supplicant/supplicant.h"
38 #include "debug.h"
39 
40 //Check EAP library configuration
41 #if (SUPPLICANT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Initialize settings with default values
46  * @param[out] settings Structure that contains 802.1X supplicant settings
47  **/
48 
50 {
51  //Default task parameters
52  settings->task = OS_TASK_DEFAULT_PARAMS;
54  settings->task.priority = SUPPLICANT_PRIORITY;
55 
56  //The supplicant is not bound to any interface
57  settings->interface = NULL;
58  //Port index
59  settings->portIndex = 0;
60 
61 #if (EAP_TLS_SUPPORT == ENABLED)
62  //TLS initialization callback function
63  settings->tlsInitCallback = NULL;
64 #endif
65 
66  //Supplicant PAE state change callback function
67  settings->paeStateChangeCallback = NULL;
68  //Supplicant backend state change callback function
69  settings->backendStateChangeCallback = NULL;
70  //EAP peer state change callback function
71  settings->eapPeerStateChangeCallback = NULL;
72  //Tick callback function
73  settings->tickCallback = NULL;
74 }
75 
76 
77 /**
78  * @brief Initialize 802.1X supplicant context
79  * @param[in] context Pointer to the 802.1X supplicant context
80  * @param[in] settings 802.1X supplicant specific settings
81  * @return Error code
82  **/
83 
85  const SupplicantSettings *settings)
86 {
87  error_t error;
88 
89  //Debug message
90  TRACE_INFO("Initializing 802.1X supplicant...\r\n");
91 
92  //Ensure the parameters are valid
93  if(context == NULL || settings == NULL)
95 
96  //Clear supplicant context
97  osMemset(context, 0, sizeof(SupplicantContext));
98 
99  //Initialize task parameters
100  context->taskParams = settings->task;
101  context->taskId = OS_INVALID_TASK_ID;
102 
103  //Initialize supplicant context
104  context->interface = settings->interface;
105  context->portIndex = settings->portIndex;
106  context->paeStateChangeCallback = settings->paeStateChangeCallback;
107  context->backendStateChangeCallback = settings->backendStateChangeCallback;
108  context->eapPeerStateChangeCallback = settings->eapPeerStateChangeCallback;
109  context->tickCallback = settings->tickCallback;
110 
111 #if (EAP_TLS_SUPPORT == ENABLED)
112  //TLS initialization callback function
113  context->tlsInitCallback = settings->tlsInitCallback;
114 #endif
115 
116  //Initialize supplicant state machine
117  supplicantInitFsm(context);
118 
119  //Initialize status code
120  error = NO_ERROR;
121 
122  //Create an event object to poll the state of sockets
123  if(!osCreateEvent(&context->event))
124  {
125  //Failed to create event
126  error = ERROR_OUT_OF_RESOURCES;
127  }
128 
129  //Any error to report?
130  if(error)
131  {
132  //Clean up side effects
133  supplicantDeinit(context);
134  }
135 
136  //Return status code
137  return error;
138 }
139 
140 
141 /**
142  * @brief Set user name
143  * @param[in] context Pointer to the 802.1X supplicant context
144  * @param[in] username NULL-terminated string containing the user name
145  * @return Error code
146  **/
147 
149  const char_t *username)
150 {
151  //Check parameters
152  if(context == NULL || username == NULL)
154 
155  //Make sure the length of the user name is acceptable
156  if(osStrlen(username) > SUPPLICANT_MAX_USERNAME_LEN)
157  return ERROR_INVALID_LENGTH;
158 
159  //Save user name
160  osStrcpy(context->username, username);
161 
162  //Successful processing
163  return NO_ERROR;
164 }
165 
166 
167 /**
168  * @brief Set password
169  * @param[in] context Pointer to the 802.1X supplicant context
170  * @param[in] password NULL-terminated string containing the password
171  * @return Error code
172  **/
173 
175  const char_t *password)
176 {
177 #if (EAP_MD5_SUPPORT == ENABLED)
178  //Check parameters
179  if(context == NULL || password == NULL)
181 
182  //Make sure the length of the password is acceptable
183  if(osStrlen(password) > SUPPLICANT_MAX_PASSWORD_LEN)
184  return ERROR_INVALID_LENGTH;
185 
186  //Save password
187  osStrcpy(context->password, password);
188 
189  //Successful processing
190  return NO_ERROR;
191 #else
192  //EAP-MD5 challenge is not implemented
193  return ERROR_NOT_IMPLEMENTED;
194 #endif
195 }
196 
197 
198 /**
199  * @brief Start 802.1X supplicant
200  * @param[in] context Pointer to the 802.1X supplicant context
201  * @return Error code
202  **/
203 
205 {
206  error_t error;
207 
208  //Make sure the supplicant context is valid
209  if(context == NULL)
211 
212  //Debug message
213  TRACE_INFO("Starting 802.1X supplicant...\r\n");
214 
215  //Make sure the supplicant is not already running
216  if(context->running)
217  return ERROR_ALREADY_RUNNING;
218 
219  //Start of exception handling block
220  do
221  {
222  //Open a raw socket
223  context->socket = socketOpen(SOCKET_TYPE_RAW_ETH, ETH_TYPE_EAPOL);
224  //Failed to open socket?
225  if(context->socket == NULL)
226  {
227  //Report an error
228  error = ERROR_OPEN_FAILED;
229  break;
230  }
231 
232  //Force the socket to operate in non-blocking mode
233  error = socketSetTimeout(context->socket, 0);
234  //Any error to report?
235  if(error)
236  break;
237 
238  //Associate the socket with the relevant interface
239  error = socketBindToInterface(context->socket, context->interface);
240  //Any error to report?
241  if(error)
242  break;
243 
244  //The PAE group address is one of the reserved set of group MAC addresses
245  //that are not forwarded by MAC Bridges (refer to IEEE Std 802.1X-2010,
246  //section 7.8)
247  error = supplicantAcceptPaeGroupAddr(context);
248  //Any error to report?
249  if(error)
250  return error;
251 
252  //Start the supplicant
253  context->stop = FALSE;
254  context->running = TRUE;
255 
256  //Save current time
257  context->timestamp = osGetSystemTime();
258 
259  //Reinitialize supplicant state machine
260  supplicantInitFsm(context);
261 
262  //Create a task
263  context->taskId = osCreateTask("Supplicant", (OsTaskCode) supplicantTask,
264  context, &context->taskParams);
265 
266  //Failed to create task?
267  if(context->taskId == OS_INVALID_TASK_ID)
268  {
269  //Report an error
270  error = ERROR_OUT_OF_RESOURCES;
271  break;
272  }
273 
274  //End of exception handling block
275  } while(0);
276 
277  //Any error to report?
278  if(error)
279  {
280  //Clean up side effects
281  context->running = FALSE;
282 
283  //Remove the PAE group address from the static MAC table
285 
286  //Close the raw socket
287  socketClose(context->socket);
288  context->socket = NULL;
289  }
290 
291  //Return status code
292  return error;
293 }
294 
295 
296 /**
297  * @brief Stop 802.1X supplicant
298  * @param[in] context Pointer to the 802.1X supplicant context
299  * @return Error code
300  **/
301 
303 {
304  //Make sure the supplicant context is valid
305  if(context == NULL)
307 
308  //Debug message
309  TRACE_INFO("Stopping 802.1X supplicant...\r\n");
310 
311  //Check whether the supplicant is running
312  if(context->running)
313  {
314  //Stop the supplicant
315  context->stop = TRUE;
316  //Send a signal to the task to abort any blocking operation
317  osSetEvent(&context->event);
318 
319  //Wait for the task to terminate
320  while(context->running)
321  {
322  osDelayTask(1);
323  }
324 
325  //Remove the PAE group address from the static MAC table
327 
328  //Close the raw socket
329  socketClose(context->socket);
330  context->socket = NULL;
331  }
332 
333  //Successful processing
334  return NO_ERROR;
335 }
336 
337 
338 /**
339  * @brief 802.1X supplicant task
340  * @param[in] context Pointer to the 802.1X supplicant context
341  **/
342 
344 {
345  systime_t time;
346  systime_t timeout;
347  SocketEventDesc eventDesc;
348 
349 #if (NET_RTOS_SUPPORT == ENABLED)
350  //Task prologue
351  osEnterTask();
352 
353  //Process events
354  while(1)
355  {
356 #endif
357  //Get current time
358  time = osGetSystemTime();
359 
360  //Maximum time to wait for an incoming datagram
361  if((time - context->timestamp) < SUPPLICANT_TICK_INTERVAL)
362  {
363  timeout = context->timestamp + SUPPLICANT_TICK_INTERVAL - time;
364  }
365  else
366  {
367  timeout = 0;
368  }
369 
370  //Specify the events the application is interested in
371  eventDesc.socket = context->socket;
372  eventDesc.eventMask = SOCKET_EVENT_RX_READY;
373  eventDesc.eventFlags = 0;
374 
375  //Wait for an event
376  socketPoll(&eventDesc, 1, &context->event, timeout);
377 
378  //Stop request?
379  if(context->stop)
380  {
381  //The supplicant considers that its user is logged off
382  context->userLogoff = TRUE;
383  //Update supplicant state machines
384  supplicantFsm(context);
385 
386  //Stop supplicant operation
387  context->running = FALSE;
388  //Task epilogue
389  osExitTask();
390  //Kill ourselves
392  }
393 
394  //Any EAPOL packet received?
395  if(eventDesc.eventFlags != 0)
396  {
397  //Process incoming EAPOL packet
398  supplicantProcessEapolPdu(context);
399  }
400 
401  //Get current time
402  time = osGetSystemTime();
403 
404  //Timers have a resolution of one second
405  if((time - context->timestamp) >= SUPPLICANT_TICK_INTERVAL)
406  {
407  //Handle periodic operations
408  supplicantTick(context);
409  //Save current time
410  context->timestamp = time;
411  }
412 
413 #if (NET_RTOS_SUPPORT == ENABLED)
414  }
415 #endif
416 }
417 
418 
419 /**
420  * @brief Release 802.1X supplicant context
421  * @param[in] context Pointer to the 802.1X supplicant context
422  **/
423 
425 {
426  //Make sure the 802.1X supplicant context is valid
427  if(context != NULL)
428  {
429  //Free previously allocated resources
430  osDeleteEvent(&context->event);
431 
432  //Clear supplicant context
433  osMemset(context, 0, sizeof(SupplicantContext));
434  }
435 }
436 
437 #endif
char char_t
Definition: compiler_port.h:48
Debugging facilities.
#define TRACE_INFO(...)
Definition: debug.h:95
uint32_t time
error_t
Error codes.
Definition: error.h:43
@ ERROR_ALREADY_RUNNING
Definition: error.h:292
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
@ ETH_TYPE_EAPOL
Definition: ethernet.h:169
#define socketBindToInterface
Definition: net_legacy.h:193
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osStrlen(s)
Definition: os_port.h:165
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define osStrcpy(s1, s2)
Definition: os_port.h:207
void osDeleteEvent(OsEvent *event)
Delete an event object.
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
void osDelayTask(systime_t delay)
Delay routine.
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
void osDeleteTask(OsTaskId taskId)
Delete a task.
systime_t osGetSystemTime(void)
Retrieve system time.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void(* OsTaskCode)(void *arg)
Task routine.
#define osEnterTask()
#define OS_SELF_TASK_ID
#define OS_INVALID_TASK_ID
uint32_t systime_t
System time.
#define osExitTask()
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:1592
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1517
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:88
@ SOCKET_EVENT_RX_READY
Definition: socket.h:169
Structure describing socket events.
Definition: socket.h:398
uint_t eventMask
Requested events.
Definition: socket.h:400
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:399
uint_t eventFlags
Returned events.
Definition: socket.h:401
802.1X supplicant settings
Definition: supplicant.h:186
OsTaskParameters task
Task parameters.
Definition: supplicant.h:187
SupplicantTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: supplicant.h:191
SupplicantTickCallback tickCallback
Tick callback function.
Definition: supplicant.h:196
SupplicantPaeStateChangeCallback paeStateChangeCallback
Supplicant PAE state change callback function.
Definition: supplicant.h:193
uint_t portIndex
Port index.
Definition: supplicant.h:189
NetInterface * interface
Underlying network interface.
Definition: supplicant.h:188
EapPeerStateChangeCallback eapPeerStateChangeCallback
EAP peer state change callback function.
Definition: supplicant.h:195
SupplicantBackendStateChangeCallback backendStateChangeCallback
Supplicant backend state change callback function.
Definition: supplicant.h:194
error_t supplicantSetPassword(SupplicantContext *context, const char_t *password)
Set password.
Definition: supplicant.c:174
error_t supplicantSetUsername(SupplicantContext *context, const char_t *username)
Set user name.
Definition: supplicant.c:148
error_t supplicantStop(SupplicantContext *context)
Stop 802.1X supplicant.
Definition: supplicant.c:302
void supplicantGetDefaultSettings(SupplicantSettings *settings)
Initialize settings with default values.
Definition: supplicant.c:49
void supplicantTask(SupplicantContext *context)
802.1X supplicant task
Definition: supplicant.c:343
error_t supplicantInit(SupplicantContext *context, const SupplicantSettings *settings)
Initialize 802.1X supplicant context.
Definition: supplicant.c:84
error_t supplicantStart(SupplicantContext *context)
Start 802.1X supplicant.
Definition: supplicant.c:204
void supplicantDeinit(SupplicantContext *context)
Release 802.1X supplicant context.
Definition: supplicant.c:424
802.1X supplicant
#define SUPPLICANT_MAX_USERNAME_LEN
Definition: supplicant.h:86
#define SUPPLICANT_PRIORITY
Definition: supplicant.h:60
#define SUPPLICANT_STACK_SIZE
Definition: supplicant.h:53
#define SupplicantContext
Definition: supplicant.h:36
#define SUPPLICANT_MAX_PASSWORD_LEN
Definition: supplicant.h:93
#define SUPPLICANT_TICK_INTERVAL
Definition: supplicant.h:65
void supplicantInitFsm(SupplicantContext *context)
Supplicant state machine initialization.
void supplicantFsm(SupplicantContext *context)
Supplicant state machine implementation.
Supplicant state machine.
void supplicantProcessEapolPdu(SupplicantContext *context)
Process incoming EAPOL PDU.
void supplicantTick(SupplicantContext *context)
Handle periodic operations.
error_t supplicantAcceptPaeGroupAddr(SupplicantContext *context)
Add the PAE group address to the static MAC table.
error_t supplicantDropPaeGroupAddr(SupplicantContext *context)
Remove the PAE group address from the static MAC table.
Helper functions for 802.1X supplicant.