ethernet.h
Go to the documentation of this file.
1/**
2 * @file ethernet.h
3 * @brief Ethernet
4 *
5 * @section License
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * Copyright (C) 2010-2021 Oryx Embedded SARL. All rights reserved.
10 *
11 * This file is part of CycloneTCP 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.1.2
29 **/
30
31#ifndef _ETHERNET_H
32#define _ETHERNET_H
33
34//Dependencies
35#include "core/net.h"
36
37//Ethernet support
38#ifndef ETH_SUPPORT
39 #define ETH_SUPPORT ENABLED
40#elif (ETH_SUPPORT != ENABLED && ETH_SUPPORT != DISABLED)
41 #error ETH_SUPPORT parameter is not valid
42#endif
43
44//Virtual interface support
45#ifndef ETH_VIRTUAL_IF_SUPPORT
46 #define ETH_VIRTUAL_IF_SUPPORT DISABLED
47#elif (ETH_VIRTUAL_IF_SUPPORT != ENABLED && ETH_VIRTUAL_IF_SUPPORT != DISABLED)
48 #error ETH_VIRTUAL_IF_SUPPORT parameter is not valid
49#endif
50
51//VLAN support (IEEE 802.1Q)
52#ifndef ETH_VLAN_SUPPORT
53 #define ETH_VLAN_SUPPORT DISABLED
54#elif (ETH_VLAN_SUPPORT != ENABLED && ETH_VLAN_SUPPORT != DISABLED)
55 #error ETH_VLAN_SUPPORT parameter is not valid
56#endif
57
58//VMAN support (IEEE 802.1ad)
59#ifndef ETH_VMAN_SUPPORT
60 #define ETH_VMAN_SUPPORT DISABLED
61#elif (ETH_VMAN_SUPPORT != ENABLED && ETH_VMAN_SUPPORT != DISABLED)
62 #error ETH_VMAN_SUPPORT parameter is not valid
63#endif
64
65//LLC support (IEEE 802.2)
66#ifndef ETH_LLC_SUPPORT
67 #define ETH_LLC_SUPPORT DISABLED
68#elif (ETH_LLC_SUPPORT != ENABLED && ETH_LLC_SUPPORT != DISABLED)
69 #error ETH_LLC_SUPPORT parameter is not valid
70#endif
71
72//Switch port tagging
73#ifndef ETH_PORT_TAGGING_SUPPORT
74 #define ETH_PORT_TAGGING_SUPPORT DISABLED
75#elif (ETH_PORT_TAGGING_SUPPORT != ENABLED && ETH_PORT_TAGGING_SUPPORT != DISABLED)
76 #error ETH_PORT_TAGGING_SUPPORT parameter is not valid
77#endif
78
79//Size of tags used for switch port tagging
80#ifndef ETH_PORT_TAG_SIZE
81 #define ETH_PORT_TAG_SIZE 4
82#elif (ETH_PORT_TAG_SIZE < 0)
83 #error ETH_PORT_TAG_SIZE parameter is not valid
84#endif
85
86//Hardware time stamping support
87#ifndef ETH_TIMESTAMP_SUPPORT
88 #define ETH_TIMESTAMP_SUPPORT DISABLED
89#elif (ETH_TIMESTAMP_SUPPORT != ENABLED && ETH_TIMESTAMP_SUPPORT != DISABLED)
90 #error ETH_TIMESTAMP_SUPPORT parameter is not valid
91#endif
92
93//Size of the MAC address filter
94#ifndef MAC_ADDR_FILTER_SIZE
95 #define MAC_ADDR_FILTER_SIZE 12
96#elif (MAC_ADDR_FILTER_SIZE < 1)
97 #error MAC_ADDR_FILTER_SIZE parameter is not valid
98#endif
99
100//CRC32 calculation using a pre-calculated lookup table
101#ifndef ETH_FAST_CRC_SUPPORT
102 #define ETH_FAST_CRC_SUPPORT DISABLED
103#elif (ETH_FAST_CRC_SUPPORT != ENABLED && ETH_FAST_CRC_SUPPORT != DISABLED)
104 #error ETH_FAST_CRC_SUPPORT parameter is not valid
105#endif
106
107//Minimum Ethernet frame size
108#define ETH_MIN_FRAME_SIZE 64
109//Maximum Ethernet frame size
110#define ETH_MAX_FRAME_SIZE 1518
111//Ethernet maximum transmission unit
112#define ETH_MTU 1500
113//Ethernet CRC field size
114#define ETH_CRC_SIZE 4
115
116//VLAN tag control information
117#define VLAN_PCP_POS 13
118#define VLAN_DEI_POS 12
119#define VLAN_VID_POS 0
120#define VLAN_PCP_MASK 0xE000
121#define VLAN_DEI_MASK 0x1000
122#define VLAN_VID_MASK 0x0FFF
123
124//Copy MAC address
125#define macCopyAddr(destMacAddr, srcMacAddr) osMemcpy(destMacAddr, srcMacAddr, sizeof(MacAddr))
126
127//Compare MAC addresses
128#define macCompAddr(macAddr1, macAddr2) (!osMemcmp(macAddr1, macAddr2, sizeof(MacAddr)))
129
130//Determine whether a MAC address is a group address
131#define macIsMulticastAddr(macAddr) (((macAddr)->b[0] & 0x01) != 0)
132
133//Copy EUI-64 address
134#define eui64CopyAddr(destEui64Addr, srcEui64Addr) osMemcpy(destEui64Addr, srcEui64Addr, sizeof(Eui64))
135
136//Compare EUI-64 addresses
137#define eui64CompAddr(eui64Addr1, eui64Addr2) (!osMemcmp(eui64Addr1, eui64Addr2, sizeof(Eui64)))
138
139//C++ guard
140#ifdef __cplusplus
141extern "C" {
142#endif
143
144
145/**
146 * @brief MAC address flags
147 **/
148
149typedef enum
150{
154
155
156/**
157 * @brief Ethernet Type field
158 **/
159
160typedef enum
161{
163 ETH_TYPE_ARP = 0x0806,
169 ETH_TYPE_LLDP = 0x88CC
171
172
173//CodeWarrior or Win32 compiler?
174#if defined(__CWCC__) || defined(_WIN32)
175 #pragma pack(push, 1)
176#endif
177
178
179/**
180 * @brief MAC address
181 **/
182
183typedef __start_packed struct
184{
185 __start_packed union
186 {
187 uint8_t b[6];
188 uint16_t w[3];
189 };
191
192
193/**
194 * @brief EUI-64 identifier
195 **/
196
197typedef __start_packed struct
198{
199 __start_packed union
200 {
201 uint8_t b[8];
202 uint16_t w[4];
203 uint32_t dw[2];
204 };
206
207
208/**
209 * @brief Ethernet frame header
210 **/
211
212typedef __start_packed struct
213{
216 uint16_t type; //12-13
217 uint8_t data[]; //14
219
220
221/**
222 * @brief LLC header
223 **/
224
225typedef __start_packed struct
226{
227 uint8_t dsap; //0
228 uint8_t ssap; //1
229 uint8_t control; //2
231
232
233/**
234 * @brief VLAN tag
235 **/
236
237typedef __start_packed struct
238{
239 uint16_t tci; //0-1
240 uint16_t type; //2-3
242
243
244//CodeWarrior or Win32 compiler?
245#if defined(__CWCC__) || defined(_WIN32)
246 #pragma pack(pop)
247#endif
248
249
250/**
251 * @brief MAC filter table entry
252 **/
253
254typedef struct
255{
256 MacAddr addr; ///<MAC address
257 uint_t refCount; ///<Reference count for the current entry
261
262
263/**
264 * @brief LLC frame received callback
265 **/
266
267typedef void (*LlcRxCallback)(NetInterface *interface, EthHeader *header,
268 const uint8_t *data, size_t length, NetRxAncillary *ancillary, void *param);
269
270
271//Ethernet related constants
272extern const MacAddr MAC_UNSPECIFIED_ADDR;
273extern const MacAddr MAC_BROADCAST_ADDR;
274extern const Eui64 EUI64_UNSPECIFIED_ADDR;
275
276//Ethernet related functions
277error_t ethInit(NetInterface *interface);
278
279void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length,
280 NetRxAncillary *ancillary);
281
283 uint16_t type, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
284
285error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr);
286error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr);
287
289 void *param);
290
292
293NetBuffer *ethAllocBuffer(size_t length, size_t *offset);
294
295error_t macStringToAddr(const char_t *str, MacAddr *macAddr);
296char_t *macAddrToString(const MacAddr *macAddr, char_t *str);
297void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId);
298
299void ethDumpHeader(const EthHeader *ethHeader);
300
301error_t eui64StringToAddr(const char_t *str, Eui64 *eui64);
302char_t *eui64AddrToString(const Eui64 *eui64, char_t *str);
303
304//C++ guard
305#ifdef __cplusplus
306}
307#endif
308
309#endif
unsigned int uint_t
Definition: compiler_port.h:45
char char_t
Definition: compiler_port.h:43
int bool_t
Definition: compiler_port.h:49
error_t
Error codes.
Definition: error.h:43
MacAddr destAddr
Definition: ethernet.h:214
error_t ethDetachLlcRxCalback(NetInterface *interface)
Unregister LLC frame received callback.
Definition: ethernet.c:748
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:665
__start_packed struct @118 EthHeader
Ethernet frame header.
error_t ethSendFrame(NetInterface *interface, const MacAddr *destAddr, uint16_t type, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an Ethernet frame.
Definition: ethernet.c:400
__start_packed struct @117 Eui64
EUI-64 identifier.
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:56
error_t eui64StringToAddr(const char_t *str, Eui64 *eui64)
Convert a string representation of an EUI-64 address to a binary EUI-64 address.
Definition: ethernet.c:990
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:67
__start_packed struct @120 VlanTag
VLAN tag.
error_t ethAttachLlcRxCalback(NetInterface *interface, LlcRxCallback callback, void *param)
Register LLC frame received callback.
Definition: ethernet.c:720
MacAddr srcAddr
Definition: ethernet.h:215
EthType
Ethernet Type field.
Definition: ethernet.h:161
@ ETH_TYPE_RARP
Definition: ethernet.h:164
@ ETH_TYPE_VLAN
Definition: ethernet.h:165
@ ETH_TYPE_VMAN
Definition: ethernet.h:168
@ ETH_TYPE_IPV6
Definition: ethernet.h:166
@ ETH_TYPE_ARP
Definition: ethernet.h:163
@ ETH_TYPE_LLDP
Definition: ethernet.h:169
@ ETH_TYPE_IPV4
Definition: ethernet.h:162
@ ETH_TYPE_EAPOL
Definition: ethernet.h:167
uint8_t data[]
Definition: ethernet.h:217
uint8_t b[6]
Definition: ethernet.h:187
uint8_t dsap
Definition: ethernet.h:227
uint8_t ssap
Definition: ethernet.h:228
const Eui64 EUI64_UNSPECIFIED_ADDR
Definition: ethernet.c:980
__start_packed struct @119 LlcHeader
LLC header.
error_t macStringToAddr(const char_t *str, MacAddr *macAddr)
Convert a string representation of a MAC address to a binary MAC address.
Definition: ethernet.c:820
uint32_t dw[2]
Definition: ethernet.h:203
uint16_t w[3]
Definition: ethernet.h:188
void ethDumpHeader(const EthHeader *ethHeader)
Dump Ethernet header for debugging purpose.
Definition: ethernet.c:968
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:941
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:595
NetBuffer * ethAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an Ethernet frame.
Definition: ethernet.c:776
void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length, NetRxAncillary *ancillary)
Process an incoming Ethernet frame.
Definition: ethernet.c:87
__start_packed struct @116 MacAddr
MAC address.
uint16_t type
Definition: ethernet.h:216
MacAddrFlags
MAC address flags.
Definition: ethernet.h:150
@ MAC_ADDR_FLAG_MULTICAST
Definition: ethernet.h:151
@ MAC_ADDR_FLAG_LOCAL
Definition: ethernet.h:152
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:1088
void(* LlcRxCallback)(NetInterface *interface, EthHeader *header, const uint8_t *data, size_t length, NetRxAncillary *ancillary, void *param)
LLC frame received callback.
Definition: ethernet.h:267
uint8_t control
Definition: ethernet.h:229
const MacAddr MAC_BROADCAST_ADDR
Definition: ethernet.c:58
uint16_t tci
Definition: ethernet.h:239
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:918
__start_packed struct _Ipv4Header __end_packed
Eui64 interfaceId
Definition: ipv6cp.h:69
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define NetRxAncillary
Definition: net_misc.h:40
#define NetTxAncillary
Definition: net_misc.h:36
uint32_t length
Definition: sftp_common.h:214
MAC filter table entry.
Definition: ethernet.h:255
bool_t addFlag
Definition: ethernet.h:258
MacAddr addr
MAC address.
Definition: ethernet.h:256
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:257
bool_t deleteFlag
Definition: ethernet.h:259
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89