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-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"
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->switchDriver != NULL &&
94  interface->switchDriver->getLinkState != NULL)
95  {
96  //Get exclusive access
98 
99  //Retrieve the link state of the specified port
100  linkState = interface->switchDriver->getLinkState(interface,
101  context->portIndex);
102 
103  //Release exclusive access
105  }
106  else
107  {
108  //Retrieve the link state of the network interface
109  linkState = interface->linkState;
110  }
111 
112  //Return link state
113  return linkState;
114 }
115 
116 
117 /**
118  * @brief Add the PAE group address to the static MAC table
119  * @param[in] context Pointer to the 802.1X supplicant context
120  * @return Error code
121  **/
122 
124 {
125  error_t error;
126  SwitchFdbEntry entry;
127  NetInterface *interface;
128 
129  //Initialize status code
130  error = NO_ERROR;
131 
132  //Point to the underlying network interface
133  interface = context->interface;
134 
135  //Get exclusive access
137 
138  //Valid switch driver?
139  if(context->portIndex != 0 && interface->switchDriver != NULL &&
140  interface->switchDriver->addStaticFdbEntry != NULL)
141  {
142  //Format forwarding database entry
143  entry.macAddr = PAE_GROUP_ADDR;
144  entry.srcPort = 0;
146  entry.override = TRUE;
147 
148  //Update the static MAC table of the switch
149  error = interface->switchDriver->addStaticFdbEntry(interface, &entry);
150  }
151 
152  //Check status code
153  if(!error)
154  {
155  //Add the PAE group address to the MAC filter table
156  error = ethAcceptMacAddr(interface, &PAE_GROUP_ADDR);
157  }
158 
159  //Release exclusive access
161 
162  //Return status code
163  return error;
164 }
165 
166 
167 /**
168  * @brief Remove the PAE group address from the static MAC table
169  * @param[in] context Pointer to the 802.1X supplicant context
170  * @return Error code
171  **/
172 
174 {
175  error_t error;
176  SwitchFdbEntry entry;
177  NetInterface *interface;
178 
179  //Initialize status code
180  error = NO_ERROR;
181 
182  //Point to the underlying network interface
183  interface = context->interface;
184 
185  //Get exclusive access
187 
188  //Valid switch driver?
189  if(context->portIndex != 0 && interface->switchDriver != NULL &&
190  interface->switchDriver->deleteStaticFdbEntry != NULL)
191  {
192  //Format forwarding database entry
193  entry.macAddr = PAE_GROUP_ADDR;
194  entry.srcPort = 0;
195  entry.destPorts = 0;
196  entry.override = FALSE;
197 
198  //Update the static MAC table of the switch
199  error = interface->switchDriver->deleteStaticFdbEntry(interface, &entry);
200  }
201 
202  //Check status code
203  if(!error)
204  {
205  //Remove the PAE group address to the MAC filter table
206  ethDropMacAddr(interface, &PAE_GROUP_ADDR);
207  }
208 
209  //Release exclusive access
211 
212  //Return status code
213  return error;
214 }
215 
216 
217 /**
218  * @brief Send EAPOL PDU
219  * @param[in] context Pointer to the 802.1X supplicant context
220  * @param[in] pdu Pointer to the PDU to be transmitted
221  * @param[in] length Length of the PDU, in bytes
222  * @return Error code
223  **/
224 
226  size_t length)
227 {
228  SocketMsg msg;
229 
230  //Point to the PDU to be transmitted
231  msg = SOCKET_DEFAULT_MSG;
232  msg.data = (uint8_t *) pdu;
233  msg.length = length;
234 
235  //The PAE group address is assigned specifically for use by EAPOL clients
236  //designed to maximize plug-and-play interoperability, and should be the
237  //default for those clients (refer to IEEE Std 802.1X-2010, section 11.1.1)
238  msg.destMacAddr = PAE_GROUP_ADDR;
239 
240  //The source address for each MAC service request used to transmit an EAPOL
241  //MPDU shall be an individual address associated with the service access
242  //point at which the request is made (refer to IEEE Std 802.1X-2010,
243  //section 11.1.2)
244  netGetMacAddr(context->interface, &msg.srcMacAddr);
245 
246  //All EAPOL MPDUs shall be identified using the PAE EtherType (refer to
247  //IEEE Std 802.1X-2010, section 11.1.4)
248  msg.ethType = ETH_TYPE_EAPOL;
249 
250 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
251  //Specify the egress port
252  msg.switchPort = context->portIndex;
253 #endif
254 
255  //Send EAPOL MPDU
256  return socketSendMsg(context->socket, &msg, 0);
257 }
258 
259 
260 /**
261  * @brief Process incoming EAPOL PDU
262  * @param[in] context Pointer to the 802.1X supplicant context
263  **/
264 
266 {
267  error_t error;
268  size_t length;
269  SocketMsg msg;
270  EapolPdu *pdu;
271 
272  //Point to the receive buffer
273  msg = SOCKET_DEFAULT_MSG;
274  msg.data = context->rxBuffer;
276 
277  //Receive EAPOL MPDU
278  error = socketReceiveMsg(context->socket, &msg, 0);
279  //Failed to receive packet
280  if(error)
281  return;
282 
283 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
284  //Check the port number on which the EAPOL PDU was received
285  if(msg.switchPort != context->portIndex && context->portIndex != 0)
286  return;
287 #endif
288 
289  //The destination MAC address must the group address recognized by the
290  //receiving MSAP for the application scenario
291  if(!macCompAddr(&msg.destMacAddr, &PAE_GROUP_ADDR))
292  return;
293 
294  //The received MPDU must contain the PAE EtherType
295  if(msg.ethType != ETH_TYPE_EAPOL)
296  return;
297 
298  //Malformed EAPOL packet?
299  if(msg.length < sizeof(EapolPdu))
300  return;
301 
302  //Point to the EAPOL packet
303  pdu = (EapolPdu *) context->rxBuffer;
304 
305  //Debug message
306  TRACE_INFO("EAPOL packet received (%" PRIuSIZE " bytes)\r\n", msg.length);
307  //Dump EAPOL header contents for debugging purpose
309 
310  //Malformed EAPOL packet?
311  if(msg.length < ntohs(pdu->packetBodyLen))
312  return;
313 
314  //Any octets following the Packet Body field in the frame conveying the
315  //EAPOL PDU shall be ignored (refer to IEEE Std 802.1X-2004, section 11.4)
316  length = ntohs(pdu->packetBodyLen);
317 
318  //Check packet type
319  if(pdu->packetType == EAPOL_TYPE_EAP)
320  {
321  //Process incoming EAP packet
322  supplicantProcessEapPacket(context, (EapPacket *) pdu->packetBody,
323  length);
324  }
325 }
326 
327 
328 /**
329  * @brief Process incoming EAP packet
330  * @param[in] context Pointer to the 802.1X supplicant context
331  * @param[in] packet Pointer to the received EAP packet
332  * @param[in] length Length of the packet, in bytes
333  **/
334 
336  const EapPacket *packet, size_t length)
337 {
338  //Malformed EAP packet?
339  if(length < sizeof(EapPacket))
340  return;
341 
342  //Debug message
343  TRACE_DEBUG("EAP packet received (%" PRIuSIZE " bytes)\r\n", length);
344  //Dump EAP header contents for debugging purpose
345  eapDumpHeader(packet);
346 
347  //A message with the Length field set to a value larger than the number of
348  //received octets must be silently discarded (refer to RFC 3748, section 4.1)
349  if(ntohs(packet->length) > length)
350  return;
351 
352  //Octets outside the range of the Length field should be treated as data
353  //link layer padding and must be ignored upon reception
354  length = ntohs(packet->length);
355 
356  //Based on the Code field, the EAP layer demultiplexes incoming EAP packets
357  //to the EAP peer and authenticator layers
358  if(packet->code != EAP_CODE_RESPONSE)
359  {
360  //Point to the EAP request
361  context->eapReqData = (uint8_t *) packet;
362  context->eapReqDataLen = length;
363 
364  //The eapolEap variable is set TRUE by an external entity if an EAPOL
365  //PDU carrying a Packet Type of EAP-Packet is received
366  context->eapolEap = TRUE;
367 
368  //Invoke EAP to perform whatever processing is needed
369  supplicantFsm(context);
370  }
371 }
372 
373 #endif
#define PRIuSIZE
int bool_t
Definition: compiler_port.h:53
#define ntohs(value)
Definition: cpu_endian.h:421
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
@ EAP_CODE_RESPONSE
Response.
Definition: eap.h:153
EapolPdu
Definition: eap.h:211
EapPacket
Definition: eap.h:224
@ EAPOL_TYPE_EAP
EAPOL-EAP.
Definition: eap.h:134
void eapolDumpHeader(const EapolPdu *header)
Dump EAPOL header for debugging purpose.
Definition: eap_debug.c:85
void eapDumpHeader(const EapPacket *header)
Dump EAP header for debugging purpose.
Definition: eap_debug.c:105
Data logging functions for debugging purpose (EAP)
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:664
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:594
@ ETH_TYPE_EAPOL
Definition: ethernet.h:169
#define macCompAddr(macAddr1, macAddr2)
Definition: ethernet.h:130
MacAddr
Definition: ethernet.h:195
uint8_t pdu[]
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:519
#define NetInterface
Definition: net.h:36
#define netMutex
Definition: net_legacy.h:195
#define SWITCH_CPU_PORT_MASK
Definition: nic.h:60
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1354
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1094
Message and ancillary data.
Definition: socket.h:230
MacAddr srcMacAddr
Source MAC address.
Definition: socket.h:243
void * data
Pointer to the payload.
Definition: socket.h:231
MacAddr destMacAddr
Destination MAC address.
Definition: socket.h:244
size_t size
Size of the payload, in bytes.
Definition: socket.h:232
size_t length
Actual length of the payload, in bytes.
Definition: socket.h:233
uint16_t ethType
Ethernet type field.
Definition: socket.h:245
uint8_t switchPort
Switch port identifier.
Definition: socket.h:248
Forwarding database entry.
Definition: nic.h:149
MacAddr macAddr
Definition: nic.h:150
uint32_t destPorts
Definition: nic.h:152
bool_t override
Definition: nic.h:153
uint8_t srcPort
Definition: nic.h:151
802.1X supplicant
#define SupplicantContext
Definition: supplicant.h:36
#define SUPPLICANT_RX_BUFFER_SIZE
Definition: supplicant.h:79
void supplicantFsm(SupplicantContext *context)
Supplicant state machine implementation.
Supplicant state machine.
void supplicantProcessEapPacket(SupplicantContext *context, const EapPacket *packet, size_t length)
Process incoming EAP packet.
error_t supplicantSendEapolPdu(SupplicantContext *context, const uint8_t *pdu, size_t length)
Send EAPOL PDU.
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.
bool_t supplicantGetLinkState(SupplicantContext *context)
Get link state.
Helper functions for 802.1X supplicant.
void supplicantDecrementTimer(uint_t *x)
Decrement timer value.
Supplicant state machine procedures.
uint8_t length
Definition: tcp.h:368