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-2019 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 1.9.6
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 //Ethernet port multiplication using VLAN or tail tagging
66 #ifndef ETH_PORT_TAGGING_SUPPORT
67  #define ETH_PORT_TAGGING_SUPPORT DISABLED
68 #elif (ETH_PORT_TAGGING_SUPPORT != ENABLED && ETH_PORT_TAGGING_SUPPORT != DISABLED)
69  #error ETH_PORT_TAGGING_SUPPORT parameter is not valid
70 #endif
71 
72 //Size of the MAC address filter
73 #ifndef MAC_ADDR_FILTER_SIZE
74  #define MAC_ADDR_FILTER_SIZE 12
75 #elif (MAC_ADDR_FILTER_SIZE < 1)
76  #error MAC_ADDR_FILTER_SIZE parameter is not valid
77 #endif
78 
79 //CRC32 calculation using a pre-calculated lookup table
80 #ifndef ETH_FAST_CRC_SUPPORT
81  #define ETH_FAST_CRC_SUPPORT DISABLED
82 #elif (ETH_FAST_CRC_SUPPORT != ENABLED && ETH_FAST_CRC_SUPPORT != DISABLED)
83  #error ETH_FAST_CRC_SUPPORT parameter is not valid
84 #endif
85 
86 //Minimum Ethernet frame size
87 #define ETH_MIN_FRAME_SIZE 64
88 //Maximum Ethernet frame size
89 #define ETH_MAX_FRAME_SIZE 1518
90 //Ethernet maximum transmission unit
91 #define ETH_MTU 1500
92 //Ethernet CRC field size
93 #define ETH_CRC_SIZE 4
94 
95 //VLAN tag control information
96 #define VLAN_PCP_POS 13
97 #define VLAN_DEI_POS 12
98 #define VLAN_VID_POS 0
99 #define VLAN_PCP_MASK 0xE000
100 #define VLAN_DEI_MASK 0x1000
101 #define VLAN_VID_MASK 0x0FFF
102 
103 //Copy MAC address
104 #define macCopyAddr(destMacAddr, srcMacAddr) memcpy(destMacAddr, srcMacAddr, sizeof(MacAddr))
105 
106 //Compare MAC addresses
107 #define macCompAddr(macAddr1, macAddr2) (!memcmp(macAddr1, macAddr2, sizeof(MacAddr)))
108 
109 //Determine whether a MAC address is a group address
110 #define macIsMulticastAddr(macAddr) ((macAddr)->b[0] & 0x01)
111 
112 //Copy EUI-64 address
113 #define eui64CopyAddr(destEui64Addr, srcEui64Addr) memcpy(destEui64Addr, srcEui64Addr, sizeof(Eui64))
114 
115 //Compare EUI-64 addresses
116 #define eui64CompAddr(eui64Addr1, eui64Addr2) (!memcmp(eui64Addr1, eui64Addr2, sizeof(Eui64)))
117 
118 //C++ guard
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
122 
123 
124 /**
125  * @brief MAC address flags
126  **/
127 
128 typedef enum
129 {
132 } MacAddrFlags;
133 
134 
135 /**
136  * @brief Ethernet Type field
137  **/
138 
139 typedef enum
140 {
141  ETH_TYPE_IPV4 = 0x0800,
142  ETH_TYPE_ARP = 0x0806,
143  ETH_TYPE_RARP = 0x8035,
144  ETH_TYPE_VLAN = 0x8100,
145  ETH_TYPE_IPV6 = 0x86DD,
146  ETH_TYPE_EAPOL = 0x888E,
147  ETH_TYPE_VMAN = 0x88A8
148 } EthType;
149 
150 
151 //CodeWarrior or Win32 compiler?
152 #if defined(__CWCC__) || defined(_WIN32)
153  #pragma pack(push, 1)
154 #endif
155 
156 
157 /**
158  * @brief MAC address
159  **/
160 
161 typedef __start_packed struct
162 {
163  __start_packed union
164  {
165  uint8_t b[6];
166  uint16_t w[3];
167  };
169 
170 
171 /**
172  * @brief EUI-64 identifier
173  **/
174 
175 typedef __start_packed struct
176 {
177  __start_packed union
178  {
179  uint8_t b[8];
180  uint16_t w[4];
181  uint32_t dw[2];
182  };
184 
185 
186 /**
187  * @brief Ethernet frame header
188  **/
189 
190 typedef __start_packed struct
191 {
193  MacAddr srcAddr; //6-11
194  uint16_t type; //12-13
195  uint8_t data[]; //14
197 
198 
199 /**
200  * @brief VLAN tag
201  **/
202 
203 typedef __start_packed struct
204 {
205  uint16_t tci; //0-1
206  uint16_t type; //2-3
208 
209 
210 //CodeWarrior or Win32 compiler?
211 #if defined(__CWCC__) || defined(_WIN32)
212  #pragma pack(pop)
213 #endif
214 
215 
216 /**
217  * @brief MAC filter table entry
218  **/
219 
220 typedef struct
221 {
222  MacAddr addr; ///<MAC address
223  uint_t refCount; ///<Reference count for the current entry
227 
228 
229 //Ethernet related constants
230 extern const MacAddr MAC_UNSPECIFIED_ADDR;
231 extern const MacAddr MAC_BROADCAST_ADDR;
232 extern const Eui64 EUI64_UNSPECIFIED_ADDR;
233 
234 //Ethernet related functions
235 error_t ethInit(NetInterface *interface);
236 
237 void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length);
238 
240  NetBuffer *buffer, size_t offset, uint16_t type);
241 
242 error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr);
243 error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr);
244 
245 NetBuffer *ethAllocBuffer(size_t length, size_t *offset);
246 
247 error_t macStringToAddr(const char_t *str, MacAddr *macAddr);
248 char_t *macAddrToString(const MacAddr *macAddr, char_t *str);
249 void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId);
250 
251 void ethDumpHeader(const EthHeader *ethHeader);
252 
253 error_t eui64StringToAddr(const char_t *str, Eui64 *eui64);
254 char_t *eui64AddrToString(const Eui64 *eui64, char_t *str);
255 
256 //C++ guard
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif
@ ETH_TYPE_RARP
Definition: ethernet.h:143
uint8_t length
Definition: dtls_misc.h:149
int bool_t
Definition: compiler_port.h:49
MacAddr srcAddr
Definition: ethernet.h:193
__start_packed struct @109 Eui64
EUI-64 identifier.
uint8_t data[]
Definition: ethernet.h:195
uint16_t type
Definition: ethernet.h:194
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:67
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
error_t ethSendFrame(NetInterface *interface, const MacAddr *destAddr, NetBuffer *buffer, size_t offset, uint16_t type)
Send an Ethernet frame.
Definition: ethernet.c:345
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:800
@ MAC_ADDR_FLAG_LOCAL
Definition: ethernet.h:131
bool_t addFlag
Definition: ethernet.h:224
@ MAC_ADDR_FLAG_MULTICAST
Definition: ethernet.h:130
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:223
void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length)
Process an incoming Ethernet frame.
Definition: ethernet.c:85
@ ETH_TYPE_IPV4
Definition: ethernet.h:141
uint16_t w[3]
Definition: ethernet.h:166
MacAddr destAddr
Definition: ethernet.h:192
error_t
Error codes.
Definition: error.h:42
const Eui64 EUI64_UNSPECIFIED_ADDR
Definition: ethernet.c:839
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:586
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:56
#define NetInterface
Definition: net.h:36
MacAddr addr
MAC address.
Definition: ethernet.h:222
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:777
__start_packed struct _Ipv4Header __end_packed
@ ETH_TYPE_ARP
Definition: ethernet.h:142
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:941
Eui64 interfaceId
Definition: ipv6cp.h:69
@ ETH_TYPE_EAPOL
Definition: ethernet.h:146
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:685
EthType
Ethernet Type field.
Definition: ethernet.h:139
uint8_t b[6]
Definition: ethernet.h:165
@ ETH_TYPE_VLAN
Definition: ethernet.h:144
char char_t
Definition: compiler_port.h:43
MacAddrFlags
MAC address flags.
Definition: ethernet.h:128
__start_packed struct @111 VlanTag
VLAN tag.
MAC filter table entry.
Definition: ethernet.h:220
@ ETH_TYPE_IPV6
Definition: ethernet.h:145
uint16_t tci
Definition: ethernet.h:205
@ ETH_TYPE_VMAN
Definition: ethernet.h:147
NetBuffer * ethAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an Ethernet frame.
Definition: ethernet.c:641
const MacAddr MAC_BROADCAST_ADDR
Definition: ethernet.c:58
bool_t deleteFlag
Definition: ethernet.h:225
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:516
unsigned int uint_t
Definition: compiler_port.h:45
__start_packed struct @110 EthHeader
Ethernet frame header.
TCP/IP stack core.
void ethDumpHeader(const EthHeader *ethHeader)
Dump Ethernet header for debugging purpose.
Definition: ethernet.c:827
uint32_t dw[2]
Definition: ethernet.h:181
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:849
__start_packed struct @108 MacAddr
MAC address.