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.4
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 *physicalInterface;
129 
130  //Initialize status code
131  error = NO_ERROR;
132 
133  //Valid interface?
134  if(context->interface != NULL)
135  {
136  //Get exclusive access
138 
139  //Point to the physical interface
140  physicalInterface = nicGetPhysicalInterface(context->interface);
141 
142  //Valid switch driver?
143  if(context->portIndex != 0 &&
144  physicalInterface->switchDriver != NULL &&
145  physicalInterface->switchDriver->addStaticFdbEntry != NULL)
146  {
147  //Format forwarding database entry
148  entry.macAddr = PAE_GROUP_ADDR;
149  entry.srcPort = 0;
151  entry.override = TRUE;
152 
153  //Update the static MAC table of the switch
154  error = physicalInterface->switchDriver->addStaticFdbEntry(
155  physicalInterface, &entry);
156  }
157 
158  //Check status code
159  if(!error)
160  {
161  //Add the PAE group address to the MAC filter table
162  error = ethAcceptMacAddr(context->interface, &PAE_GROUP_ADDR);
163  }
164 
165  //Check status code
166  if(!error)
167  {
168  //Virtual interface?
169  if(context->interface != physicalInterface)
170  {
171  //Configure the physical interface to accept the MAC address
172  error = ethAcceptMacAddr(physicalInterface, &PAE_GROUP_ADDR);
173 
174  //Any error to report?
175  if(error)
176  {
177  //Clean up side effects
178  ethDropMacAddr(context->interface, &PAE_GROUP_ADDR);
179  }
180  }
181  }
182 
183  //Release exclusive access
185  }
186 
187  //Return status code
188  return error;
189 }
190 
191 
192 /**
193  * @brief Remove the PAE group address from the static MAC table
194  * @param[in] context Pointer to the 802.1X supplicant context
195  * @return Error code
196  **/
197 
199 {
200  error_t error;
201  SwitchFdbEntry entry;
202  NetInterface *physicalInterface;
203 
204  //Initialize status code
205  error = NO_ERROR;
206 
207  //Valid interface?
208  if(context->interface != NULL)
209  {
210  //Get exclusive access
212 
213  //Point to the physical interface
214  physicalInterface = nicGetPhysicalInterface(context->interface);
215 
216  //Valid switch driver?
217  if(context->portIndex != 0 &&
218  physicalInterface->switchDriver != NULL &&
219  physicalInterface->switchDriver->deleteStaticFdbEntry != NULL)
220  {
221  //Format forwarding database entry
222  entry.macAddr = PAE_GROUP_ADDR;
223  entry.srcPort = 0;
224  entry.destPorts = 0;
225  entry.override = FALSE;
226 
227  //Update the static MAC table of the switch
228  error = physicalInterface->switchDriver->deleteStaticFdbEntry(
229  physicalInterface, &entry);
230  }
231 
232  //Check status code
233  if(!error)
234  {
235  //Remove the PAE group address to the MAC filter table
236  ethDropMacAddr(context->interface, &PAE_GROUP_ADDR);
237 
238  //Virtual interface?
239  if(context->interface != physicalInterface)
240  {
241  //Drop the corresponding address from the MAC filter table of the
242  //physical interface
243  ethDropMacAddr(physicalInterface, &PAE_GROUP_ADDR);
244  }
245  }
246 
247  //Release exclusive access
249  }
250 
251  //Return status code
252  return error;
253 }
254 
255 
256 /**
257  * @brief Send EAPOL PDU
258  * @param[in] context Pointer to the 802.1X supplicant context
259  * @param[in] pdu Pointer to the PDU to be transmitted
260  * @param[in] length Length of the PDU, in bytes
261  * @return Error code
262  **/
263 
265  size_t length)
266 {
267  error_t error;
268  SocketMsg msg;
269 
270  //Point to the PDU to be transmitted
271  msg = SOCKET_DEFAULT_MSG;
272  msg.data = (uint8_t *) pdu;
273  msg.length = length;
274 
275  //All EAPOL MPDUs shall be identified using the PAE EtherType (refer to
276  //IEEE Std 802.1X-2010, section 11.1.4)
277  msg.ethType = ETH_TYPE_EAPOL;
278 
279 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
280  //Specify the egress port
281  msg.switchPort = context->portIndex;
282 #endif
283 
284  //The PAE group address is assigned specifically for use by EAPOL clients
285  //designed to maximize plug-and-play interoperability, and should be the
286  //default for those clients (refer to IEEE Std 802.1X-2010, section 11.1.1)
287  msg.destMacAddr = PAE_GROUP_ADDR;
288 
289  //The source address for each MAC service request used to transmit an EAPOL
290  //MPDU shall be an individual address associated with the service access
291  //point at which the request is made (refer to IEEE Std 802.1X-2010,
292  //section 11.1.2)
293  error = netGetMacAddr(context->interface, &msg.srcMacAddr);
294 
295  //Check status code
296  if(!error)
297  {
298  //Send EAPOL MPDU
299  error = socketSendMsg(context->socket, &msg, 0);
300  }
301 
302  //Return status code
303  return error;
304 }
305 
306 
307 /**
308  * @brief Process incoming EAPOL PDU
309  * @param[in] context Pointer to the 802.1X supplicant context
310  **/
311 
313 {
314  error_t error;
315  size_t length;
316  SocketMsg msg;
317  MacAddr macAddr;
318  EapolPdu *pdu;
319 
320  //Point to the receive buffer
321  msg = SOCKET_DEFAULT_MSG;
322  msg.data = context->rxBuffer;
324 
325  //Receive EAPOL MPDU
326  error = socketReceiveMsg(context->socket, &msg, 0);
327  //Any error to report?
328  if(error)
329  return;
330 
331 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
332  //Check the port number on which the EAPOL PDU was received
333  if(msg.switchPort != context->portIndex && context->portIndex != 0)
334  return;
335 #endif
336 
337  //Get the MAC address assigned to the interface
338  error = netGetMacAddr(context->interface, &macAddr);
339  //Any error to report?
340  if(error)
341  return;
342 
343  //The destination MAC address field contains the PAE group address, or
344  //the specific MAC address of the PAE (refer to IEEE Std 802.1X-2004,
345  //section 7.5.7)
346  if(!macCompAddr(&msg.destMacAddr, &PAE_GROUP_ADDR) &&
347  !macCompAddr(&msg.destMacAddr, &macAddr))
348  {
349  return;
350  }
351 
352  //The received MPDU must contain the PAE EtherType
353  if(msg.ethType != ETH_TYPE_EAPOL)
354  return;
355 
356  //Malformed EAPOL packet?
357  if(msg.length < sizeof(EapolPdu))
358  return;
359 
360  //Point to the EAPOL packet
361  pdu = (EapolPdu *) context->rxBuffer;
362 
363  //Debug message
364  TRACE_INFO("EAPOL packet received (%" PRIuSIZE " bytes)\r\n", msg.length);
365  //Dump EAPOL header contents for debugging purpose
367 
368  //Any octets following the Packet Body field in the frame conveying the
369  //EAPOL PDU shall be ignored (refer to IEEE Std 802.1X-2004, section 11.4)
370  length = ntohs(pdu->packetBodyLen);
371 
372  //Malformed EAPOL packet?
373  if(msg.length < (sizeof(EapolPdu) + length))
374  return;
375 
376  //Check packet type
377  if(pdu->packetType == EAPOL_TYPE_EAP)
378  {
379  //Process incoming EAP packet
380  supplicantProcessEapPacket(context, (EapPacket *) pdu->packetBody,
381  length);
382  }
383 }
384 
385 
386 /**
387  * @brief Process incoming EAP packet
388  * @param[in] context Pointer to the 802.1X supplicant context
389  * @param[in] packet Pointer to the received EAP packet
390  * @param[in] length Length of the packet, in bytes
391  **/
392 
394  const EapPacket *packet, size_t length)
395 {
396  //Malformed EAP packet?
397  if(length < sizeof(EapPacket))
398  return;
399 
400  //Debug message
401  TRACE_DEBUG("EAP packet received (%" PRIuSIZE " bytes)\r\n", length);
402  //Dump EAP header contents for debugging purpose
403  eapDumpHeader(packet);
404 
405  //A message with the Length field set to a value larger than the number of
406  //received octets must be silently discarded (refer to RFC 3748, section 4.1)
407  if(ntohs(packet->length) > length)
408  return;
409 
410  //Octets outside the range of the Length field should be treated as data
411  //link layer padding and must be ignored upon reception
412  length = ntohs(packet->length);
413 
414  //Based on the Code field, the EAP layer demultiplexes incoming EAP packets
415  //to the EAP peer and authenticator layers
416  if(packet->code != EAP_CODE_RESPONSE)
417  {
418  //Point to the EAP request
419  context->eapReqData = (uint8_t *) packet;
420  context->eapReqDataLen = length;
421 
422  //The eapolEap variable is set TRUE by an external entity if an EAPOL
423  //PDU carrying a Packet Type of EAP-Packet is received
424  context->eapolEap = TRUE;
425 
426  //Invoke EAP to perform whatever processing is needed
427  supplicantFsm(context);
428  }
429 }
430 
431 #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.