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