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.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->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 field contains the PAE group address, or
290  //the specific MAC address of the PAE (refer to IEEE Std 802.1X-2004,
291  //section 7.5.7)
292  if(!macCompAddr(&msg.destMacAddr, &PAE_GROUP_ADDR) &&
293  !macCompAddr(&msg.destMacAddr, &context->interface->macAddr))
294  {
295  return;
296  }
297 
298  //The received MPDU must contain the PAE EtherType
299  if(msg.ethType != ETH_TYPE_EAPOL)
300  return;
301 
302  //Malformed EAPOL packet?
303  if(msg.length < sizeof(EapolPdu))
304  return;
305 
306  //Point to the EAPOL packet
307  pdu = (EapolPdu *) context->rxBuffer;
308 
309  //Debug message
310  TRACE_INFO("EAPOL packet received (%" PRIuSIZE " bytes)\r\n", msg.length);
311  //Dump EAPOL header contents for debugging purpose
313 
314  //Malformed EAPOL packet?
315  if(msg.length < ntohs(pdu->packetBodyLen))
316  return;
317 
318  //Any octets following the Packet Body field in the frame conveying the
319  //EAPOL PDU shall be ignored (refer to IEEE Std 802.1X-2004, section 11.4)
320  length = ntohs(pdu->packetBodyLen);
321 
322  //Check packet type
323  if(pdu->packetType == EAPOL_TYPE_EAP)
324  {
325  //Process incoming EAP packet
326  supplicantProcessEapPacket(context, (EapPacket *) pdu->packetBody,
327  length);
328  }
329 }
330 
331 
332 /**
333  * @brief Process incoming EAP packet
334  * @param[in] context Pointer to the 802.1X supplicant context
335  * @param[in] packet Pointer to the received EAP packet
336  * @param[in] length Length of the packet, in bytes
337  **/
338 
340  const EapPacket *packet, size_t length)
341 {
342  //Malformed EAP packet?
343  if(length < sizeof(EapPacket))
344  return;
345 
346  //Debug message
347  TRACE_DEBUG("EAP packet received (%" PRIuSIZE " bytes)\r\n", length);
348  //Dump EAP header contents for debugging purpose
349  eapDumpHeader(packet);
350 
351  //A message with the Length field set to a value larger than the number of
352  //received octets must be silently discarded (refer to RFC 3748, section 4.1)
353  if(ntohs(packet->length) > length)
354  return;
355 
356  //Octets outside the range of the Length field should be treated as data
357  //link layer padding and must be ignored upon reception
358  length = ntohs(packet->length);
359 
360  //Based on the Code field, the EAP layer demultiplexes incoming EAP packets
361  //to the EAP peer and authenticator layers
362  if(packet->code != EAP_CODE_RESPONSE)
363  {
364  //Point to the EAP request
365  context->eapReqData = (uint8_t *) packet;
366  context->eapReqDataLen = length;
367 
368  //The eapolEap variable is set TRUE by an external entity if an EAPOL
369  //PDU carrying a Packet Type of EAP-Packet is received
370  context->eapolEap = TRUE;
371 
372  //Invoke EAP to perform whatever processing is needed
373  supplicantFsm(context);
374  }
375 }
376 
377 #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:53
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:1634
#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:1894
error_t netGetMacAddr(NetInterface *interface, MacAddr *macAddr)
Retrieve MAC address.
Definition: net.c:516
#define TRACE_INFO(...)
Definition: debug.h:95
@ ETH_TYPE_EAPOL
Definition: ethernet.h:169
uint8_t length
Definition: tcp.h:368
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:107
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
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.