supplicant_misc.c
Go to the documentation of this file.
1 /**
2  * @file supplicant_misc.c
3  * @brief Helper functions for 802.1X supplicant
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2025 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.5.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SUPPLICANT_TRACE_LEVEL
33 
34 //Dependencies
35 #include "supplicant/supplicant.h"
39 #include "eap/eap_debug.h"
40 #include "debug.h"
41 
42 //Check EAP library configuration
43 #if (SUPPLICANT_SUPPORT == ENABLED)
44 
45 //PAE group address (refer to IEEE Std 802.1X-2010, section 11.1.1)
46 static const MacAddr PAE_GROUP_ADDR = {{{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}}};
47 
48 
49 /**
50  * @brief Handle periodic operations
51  * @param[in] context Pointer to the 802.1X supplicant context
52  **/
53 
55 {
56  //The portEnabled variable is externally controlled. Its value reflects
57  //the operational state of the MAC service supporting the port
58  context->portEnabled = supplicantGetLinkState(context);
59 
60  //Timers are decremented once per second
61  supplicantDecrementTimer(&context->startWhen);
62  supplicantDecrementTimer(&context->heldWhile);
63  supplicantDecrementTimer(&context->authWhile);
64  supplicantDecrementTimer(&context->idleWhile);
65 
66  //Update supplicant state machines
67  supplicantFsm(context);
68 
69  //Any registered callback?
70  if(context->tickCallback != NULL)
71  {
72  //Invoke user callback function
73  context->tickCallback(context);
74  }
75 }
76 
77 
78 /**
79  * @brief Get link state
80  * @param[in] context Pointer to the 802.1X supplicant context
81  * @return Error code
82  **/
83 
85 {
86  bool_t linkState;
87  NetInterface *interface;
88 
89  //Point to the underlying network interface
90  interface = context->interface;
91 
92  //Valid switch driver?
93  if(context->portIndex != 0 && interface != NULL &&
94  interface->switchDriver != NULL &&
95  interface->switchDriver->getLinkState != NULL)
96  {
97  //Get exclusive access
99 
100  //Retrieve the link state of the specified port
101  linkState = interface->switchDriver->getLinkState(interface,
102  context->portIndex);
103 
104  //Release exclusive access
106  }
107  else
108  {
109  //Retrieve the link state of the network interface
110  linkState = netGetLinkState(interface);
111  }
112 
113  //Return link state
114  return linkState;
115 }
116 
117 
118 /**
119  * @brief Add the PAE group address to the static MAC table
120  * @param[in] context Pointer to the 802.1X supplicant context
121  * @return Error code
122  **/
123 
125 {
126  error_t error;
127  SwitchFdbEntry entry;
128  NetInterface *interface;
129  NetInterface *physicalInterface;
130 
131  //Initialize status code
132  error = NO_ERROR;
133 
134  //Point to the underlying network interface
135  interface = context->interface;
136 
137  //Valid interface?
138  if(interface != NULL)
139  {
140  //Get exclusive access
142 
143  //Point to the physical interface
144  physicalInterface = nicGetPhysicalInterface(interface);
145 
146  //Valid switch driver?
147  if(physicalInterface->switchDriver != NULL &&
148  physicalInterface->switchDriver->addStaticFdbEntry != NULL)
149  {
150  //Format forwarding database entry
151  entry.macAddr = PAE_GROUP_ADDR;
152  entry.srcPort = 0;
154  entry.override = TRUE;
155 
156  //Update the static MAC table of the switch
157  error = physicalInterface->switchDriver->addStaticFdbEntry(
158  physicalInterface, &entry);
159  }
160 
161  //Check status code
162  if(!error)
163  {
164  //Add the PAE group address to the MAC filter table
165  error = ethAcceptMacAddr(interface, &PAE_GROUP_ADDR);
166  }
167 
168  //Check status code
169  if(!error)
170  {
171  //Virtual interface?
172  if(interface != physicalInterface)
173  {
174  //Configure the physical interface to accept the MAC address
175  error = ethAcceptMacAddr(physicalInterface, &PAE_GROUP_ADDR);
176 
177  //Any error to report?
178  if(error)
179  {
180  //Clean up side effects
181  ethDropMacAddr(interface, &PAE_GROUP_ADDR);
182  }
183  }
184  }
185 
186  //Release exclusive access
188  }
189 
190  //Return status code
191  return error;
192 }
193 
194 
195 /**
196  * @brief Remove the PAE group address from the static MAC table
197  * @param[in] context Pointer to the 802.1X supplicant context
198  * @return Error code
199  **/
200 
202 {
203  error_t error;
204  SwitchFdbEntry entry;
205  NetInterface *interface;
206  NetInterface *physicalInterface;
207 
208  //Initialize status code
209  error = NO_ERROR;
210 
211  //Point to the underlying network interface
212  interface = context->interface;
213 
214  //Valid interface?
215  if(interface != NULL)
216  {
217  //Get exclusive access
219 
220  //Point to the physical interface
221  physicalInterface = nicGetPhysicalInterface(interface);
222 
223  //Valid switch driver?
224  if(physicalInterface->switchDriver != NULL &&
225  physicalInterface->switchDriver->deleteStaticFdbEntry != NULL)
226  {
227  //Format forwarding database entry
228  entry.macAddr = PAE_GROUP_ADDR;
229  entry.srcPort = 0;
230  entry.destPorts = 0;
231  entry.override = FALSE;
232 
233  //Update the static MAC table of the switch
234  error = physicalInterface->switchDriver->deleteStaticFdbEntry(
235  physicalInterface, &entry);
236  }
237 
238  //Check status code
239  if(!error)
240  {
241  //Remove the PAE group address to the MAC filter table
242  ethDropMacAddr(interface, &PAE_GROUP_ADDR);
243 
244  //Virtual interface?
245  if(interface != physicalInterface)
246  {
247  //Drop the corresponding address from the MAC filter table of the
248  //physical interface
249  ethDropMacAddr(physicalInterface, &PAE_GROUP_ADDR);
250  }
251  }
252 
253  //Release exclusive access
255  }
256 
257  //Return status code
258  return error;
259 }
260 
261 
262 /**
263  * @brief Send EAPOL PDU
264  * @param[in] context Pointer to the 802.1X supplicant context
265  * @param[in] pdu Pointer to the PDU to be transmitted
266  * @param[in] length Length of the PDU, in bytes
267  * @return Error code
268  **/
269 
271  size_t length)
272 {
273  error_t error;
274  SocketMsg msg;
275 
276  //Point to the PDU to be transmitted
277  msg = SOCKET_DEFAULT_MSG;
278  msg.data = (uint8_t *) pdu;
279  msg.length = length;
280 
281  //All EAPOL MPDUs shall be identified using the PAE EtherType (refer to
282  //IEEE Std 802.1X-2010, section 11.1.4)
283  msg.ethType = ETH_TYPE_EAPOL;
284 
285 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
286  //Specify the egress port
287  msg.switchPort = context->portIndex;
288 #endif
289 
290  //The PAE group address is assigned specifically for use by EAPOL clients
291  //designed to maximize plug-and-play interoperability, and should be the
292  //default for those clients (refer to IEEE Std 802.1X-2010, section 11.1.1)
293  msg.destMacAddr = PAE_GROUP_ADDR;
294 
295  //The source address for each MAC service request used to transmit an EAPOL
296  //MPDU shall be an individual address associated with the service access
297  //point at which the request is made (refer to IEEE Std 802.1X-2010,
298  //section 11.1.2)
299  error = netGetMacAddr(context->interface, &msg.srcMacAddr);
300 
301  //Check status code
302  if(!error)
303  {
304  //Send EAPOL MPDU
305  error = socketSendMsg(context->socket, &msg, 0);
306  }
307 
308  //Return status code
309  return error;
310 }
311 
312 
313 /**
314  * @brief Process incoming EAPOL PDU
315  * @param[in] context Pointer to the 802.1X supplicant context
316  **/
317 
319 {
320  error_t error;
321  size_t length;
322  SocketMsg msg;
323  MacAddr macAddr;
324  EapolPdu *pdu;
325 
326  //Point to the receive buffer
327  msg = SOCKET_DEFAULT_MSG;
328  msg.data = context->rxBuffer;
330 
331  //Receive EAPOL MPDU
332  error = socketReceiveMsg(context->socket, &msg, 0);
333  //Any error to report?
334  if(error)
335  return;
336 
337 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
338  //Check the port number on which the EAPOL PDU was received
339  if(msg.switchPort != context->portIndex && context->portIndex != 0)
340  return;
341 #endif
342 
343  //Get the MAC address assigned to the interface
344  error = netGetMacAddr(context->interface, &macAddr);
345  //Any error to report?
346  if(error)
347  return;
348 
349  //The destination MAC address field contains the PAE group address, or
350  //the specific MAC address of the PAE (refer to IEEE Std 802.1X-2004,
351  //section 7.5.7)
352  if(!macCompAddr(&msg.destMacAddr, &PAE_GROUP_ADDR) &&
353  !macCompAddr(&msg.destMacAddr, &macAddr))
354  {
355  return;
356  }
357 
358  //The received MPDU must contain the PAE EtherType
359  if(msg.ethType != ETH_TYPE_EAPOL)
360  return;
361 
362  //Malformed EAPOL packet?
363  if(msg.length < sizeof(EapolPdu))
364  return;
365 
366  //Point to the EAPOL packet
367  pdu = (EapolPdu *) context->rxBuffer;
368 
369  //Debug message
370  TRACE_INFO("EAPOL packet received (%" PRIuSIZE " bytes)\r\n", msg.length);
371  //Dump EAPOL header contents for debugging purpose
373 
374  //Any octets following the Packet Body field in the frame conveying the
375  //EAPOL PDU shall be ignored (refer to IEEE Std 802.1X-2004, section 11.4)
376  length = ntohs(pdu->packetBodyLen);
377 
378  //Malformed EAPOL packet?
379  if(msg.length < (sizeof(EapolPdu) + length))
380  return;
381 
382  //Check packet type
383  if(pdu->packetType == EAPOL_TYPE_EAP)
384  {
385  //Process incoming EAP packet
386  supplicantProcessEapPacket(context, (EapPacket *) pdu->packetBody,
387  length);
388  }
389 }
390 
391 
392 /**
393  * @brief Process incoming EAP packet
394  * @param[in] context Pointer to the 802.1X supplicant context
395  * @param[in] packet Pointer to the received EAP packet
396  * @param[in] length Length of the packet, in bytes
397  **/
398 
400  const EapPacket *packet, size_t length)
401 {
402  //Malformed EAP packet?
403  if(length < sizeof(EapPacket))
404  return;
405 
406  //Debug message
407  TRACE_DEBUG("EAP packet received (%" PRIuSIZE " bytes)\r\n", length);
408  //Dump EAP header contents for debugging purpose
409  eapDumpHeader(packet);
410 
411  //A message with the Length field set to a value larger than the number of
412  //received octets must be silently discarded (refer to RFC 3748, section 4.1)
413  if(ntohs(packet->length) > length)
414  return;
415 
416  //Octets outside the range of the Length field should be treated as data
417  //link layer padding and must be ignored upon reception
418  length = ntohs(packet->length);
419 
420  //Based on the Code field, the EAP layer demultiplexes incoming EAP packets
421  //to the EAP peer and authenticator layers
422  if(packet->code != EAP_CODE_RESPONSE)
423  {
424  //Point to the EAP request
425  context->eapReqData = (uint8_t *) packet;
426  context->eapReqDataLen = length;
427 
428  //The eapolEap variable is set TRUE by an external entity if an EAPOL
429  //PDU carrying a Packet Type of EAP-Packet is received
430  context->eapolEap = TRUE;
431 
432  //Invoke EAP to perform whatever processing is needed
433  supplicantFsm(context);
434  }
435 }
436 
437 #endif
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:594
error_t supplicantAcceptPaeGroupAddr(SupplicantContext *context)
Add the PAE group address to the static MAC table.
int bool_t
Definition: compiler_port.h:61
void eapDumpHeader(const EapPacket *header)
Dump EAP header for debugging purpose.
Definition: eap_debug.c:105
Supplicant state machine.
uint32_t destPorts
Definition: nic.h:152
@ EAP_CODE_RESPONSE
Response.
Definition: eap.h:153
#define netMutex
Definition: net_legacy.h:195
Supplicant state machine procedures.
EapolPdu
Definition: eap.h:211
802.1X supplicant
#define TRUE
Definition: os_port.h:50
Message and ancillary data.
Definition: socket.h:241
#define SupplicantContext
Definition: supplicant.h:36
void * data
Pointer to the payload.
Definition: socket.h:242
bool_t supplicantGetLinkState(SupplicantContext *context)
Get link state.
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:666
uint16_t ethType
Ethernet type field.
Definition: socket.h:256
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1643
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
size_t length
Actual length of the payload, in bytes.
Definition: socket.h:244
void eapolDumpHeader(const EapolPdu *header)
Dump EAPOL header for debugging purpose.
Definition: eap_debug.c:85
#define SUPPLICANT_RX_BUFFER_SIZE
Definition: supplicant.h:79
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:85
error_t
Error codes.
Definition: error.h:43
void supplicantFsm(SupplicantContext *context)
Supplicant state machine implementation.
uint8_t pdu[]
@ EAPOL_TYPE_EAP
EAPOL-EAP.
Definition: eap.h:134
void supplicantTick(SupplicantContext *context)
Handle periodic operations.
#define NetInterface
Definition: net.h:36
uint8_t switchPort
Switch port identifier.
Definition: socket.h:259
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1903
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:520
#define TRACE_INFO(...)
Definition: debug.h:105
@ ETH_TYPE_EAPOL
Definition: ethernet.h:171
uint8_t length
Definition: tcp.h:375
error_t supplicantSendEapolPdu(SupplicantContext *context, const uint8_t *pdu, size_t length)
Send EAPOL PDU.
MacAddr
Definition: ethernet.h:197
MacAddr srcMacAddr
Source MAC address.
Definition: socket.h:254
void supplicantProcessEapolPdu(SupplicantContext *context)
Process incoming EAPOL PDU.
#define ntohs(value)
Definition: cpu_endian.h:421
#define TRACE_DEBUG(...)
Definition: debug.h:119
MacAddr destMacAddr
Destination MAC address.
Definition: socket.h:255
Data logging functions for debugging purpose (EAP)
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
EapPacket
Definition: eap.h:224
void supplicantProcessEapPacket(SupplicantContext *context, const EapPacket *packet, size_t length)
Process incoming EAP packet.
MacAddr macAddr
Definition: nic.h:150
uint8_t srcPort
Definition: nic.h:151
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
size_t size
Size of the payload, in bytes.
Definition: socket.h:243
#define SWITCH_CPU_PORT_MASK
Definition: nic.h:60
void supplicantDecrementTimer(uint_t *x)
Decrement timer value.
#define PRIuSIZE
bool_t netGetLinkState(NetInterface *interface)
Get link state.
Definition: net.c:1084
error_t supplicantDropPaeGroupAddr(SupplicantContext *context)
Remove the PAE group address from the static MAC table.
@ NO_ERROR
Success.
Definition: error.h:44
bool_t override
Definition: nic.h:153
Debugging facilities.
Forwarding database entry.
Definition: nic.h:149
Helper functions for 802.1X supplicant.