ethernet.h
Go to the documentation of this file.
1 /**
2  * @file ethernet.h
3  * @brief Ethernet
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _ETHERNET_H
30 #define _ETHERNET_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //Ethernet support
36 #ifndef ETH_SUPPORT
37  #define ETH_SUPPORT ENABLED
38 #elif (ETH_SUPPORT != ENABLED && ETH_SUPPORT != DISABLED)
39  #error ETH_SUPPORT parameter is not valid
40 #endif
41 
42 //VLAN support (IEEE 802.1q)
43 #ifndef ETH_VLAN_SUPPORT
44  #define ETH_VLAN_SUPPORT DISABLED
45 #elif (ETH_VLAN_SUPPORT != ENABLED && ETH_VLAN_SUPPORT != DISABLED)
46  #error ETH_VLAN_SUPPORT parameter is not valid
47 #endif
48 
49 //VMAN support (IEEE 802.1ad)
50 #ifndef ETH_VMAN_SUPPORT
51  #define ETH_VMAN_SUPPORT DISABLED
52 #elif (ETH_VMAN_SUPPORT != ENABLED && ETH_VMAN_SUPPORT != DISABLED)
53  #error ETH_VMAN_SUPPORT parameter is not valid
54 #endif
55 
56 //Ethernet port multiplication using VLAN or tail tagging
57 #ifndef ETH_PORT_TAGGING_SUPPORT
58  #define ETH_PORT_TAGGING_SUPPORT DISABLED
59 #elif (ETH_PORT_TAGGING_SUPPORT != ENABLED && ETH_PORT_TAGGING_SUPPORT != DISABLED)
60  #error ETH_PORT_TAGGING_SUPPORT parameter is not valid
61 #endif
62 
63 //Size of the MAC address filter
64 #ifndef MAC_ADDR_FILTER_SIZE
65  #define MAC_ADDR_FILTER_SIZE 12
66 #elif (MAC_ADDR_FILTER_SIZE < 1)
67  #error MAC_ADDR_FILTER_SIZE parameter is not valid
68 #endif
69 
70 //CRC32 calculation using a pre-calculated lookup table
71 #ifndef ETH_FAST_CRC_SUPPORT
72  #define ETH_FAST_CRC_SUPPORT DISABLED
73 #elif (ETH_FAST_CRC_SUPPORT != ENABLED && ETH_FAST_CRC_SUPPORT != DISABLED)
74  #error ETH_FAST_CRC_SUPPORT parameter is not valid
75 #endif
76 
77 //Minimum Ethernet frame size
78 #define ETH_MIN_FRAME_SIZE 64
79 //Maximum Ethernet frame size
80 #define ETH_MAX_FRAME_SIZE 1518
81 //Ethernet maximum transmission unit
82 #define ETH_MTU 1500
83 //Ethernet CRC field size
84 #define ETH_CRC_SIZE 4
85 
86 //VLAN tag control information
87 #define VLAN_PCP_MASK 0xE000
88 #define VLAN_CFI_MASK 0x1000
89 #define VLAN_VID_MASK 0x0FFF
90 
91 //Copy MAC address
92 #define macCopyAddr(destMacAddr, srcMacAddr) memcpy(destMacAddr, srcMacAddr, sizeof(MacAddr))
93 
94 //Compare MAC addresses
95 #define macCompAddr(macAddr1, macAddr2) (!memcmp(macAddr1, macAddr2, sizeof(MacAddr)))
96 
97 //Determine whether a MAC address is a group address
98 #define macIsMulticastAddr(macAddr) ((macAddr)->b[0] & 0x01)
99 
100 //Copy EUI-64 address
101 #define eui64CopyAddr(destEui64Addr, srcEui64Addr) memcpy(destEui64Addr, srcEui64Addr, sizeof(Eui64))
102 
103 //Compare EUI-64 addresses
104 #define eui64CompAddr(eui64Addr1, eui64Addr2) (!memcmp(eui64Addr1, eui64Addr2, sizeof(Eui64)))
105 
106 //C++ guard
107 #ifdef __cplusplus
108  extern "C" {
109 #endif
110 
111 
112 /**
113  * @brief MAC address flags
114  **/
115 
116 typedef enum
117 {
120 } MacAddrFlags;
121 
122 
123 /**
124  * @brief Ethernet Type field
125  **/
126 
127 typedef enum
128 {
129  ETH_TYPE_IPV4 = 0x0800,
130  ETH_TYPE_ARP = 0x0806,
131  ETH_TYPE_RARP = 0x8035,
132  ETH_TYPE_VLAN = 0x8100,
133  ETH_TYPE_IPV6 = 0x86DD,
134  ETH_TYPE_EAPOL = 0x888E,
135  ETH_TYPE_VMAN = 0x88A8
136 } EthType;
137 
138 
139 //CodeWarrior or Win32 compiler?
140 #if defined(__CWCC__) || defined(_WIN32)
141  #pragma pack(push, 1)
142 #endif
143 
144 
145 /**
146  * @brief MAC address
147  **/
148 
149 typedef __start_packed struct
150 {
151  __start_packed union
152  {
153  uint8_t b[6];
154  uint16_t w[3];
155  };
157 
158 
159 /**
160  * @brief EUI-64 identifier
161  **/
162 
163 typedef __start_packed struct
164 {
165  __start_packed union
166  {
167  uint8_t b[8];
168  uint16_t w[4];
169  uint32_t dw[2];
170  };
172 
173 
174 /**
175  * @brief Ethernet frame header
176  **/
177 
178 typedef __start_packed struct
179 {
181  MacAddr srcAddr; //6-11
182  uint16_t type; //12-13
183  uint8_t data[]; //14
185 
186 
187 /**
188  * @brief VLAN tag
189  **/
190 
191 typedef __start_packed struct
192 {
193  uint16_t tci; //0-1
194  uint16_t type; //2-3
196 
197 
198 //CodeWarrior or Win32 compiler?
199 #if defined(__CWCC__) || defined(_WIN32)
200  #pragma pack(pop)
201 #endif
202 
203 
204 /**
205  * @brief MAC filter table entry
206  **/
207 
208 typedef struct
209 {
210  MacAddr addr; ///<MAC address
211  uint_t refCount; ///<Reference count for the current entry
215 
216 
217 //Ethernet related constants
218 extern const MacAddr MAC_UNSPECIFIED_ADDR;
219 extern const MacAddr MAC_BROADCAST_ADDR;
220 extern const Eui64 EUI64_UNSPECIFIED_ADDR;
221 extern const uint8_t ethPadding[64];
222 
223 //Ethernet related functions
224 error_t ethInit(NetInterface *interface);
225 
226 void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length);
227 
229  NetBuffer *buffer, size_t offset, uint16_t type);
230 
231 error_t ethPadFrame(NetBuffer *buffer, size_t *length);
232 
233 error_t ethCheckDestAddr(NetInterface *interface, const MacAddr *macAddr);
234 error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr);
235 error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr);
236 
237 void ethUpdateInStats(NetInterface *interface, const MacAddr *destMacAddr);
238 void ethUpdateOutStats(NetInterface *interface, const MacAddr *destMacAddr, size_t length);
239 void ethUpdateErrorStats(NetInterface *interface, error_t error);
240 
241 uint32_t ethCalcCrc(const void *data, size_t length);
242 uint32_t ethCalcCrcEx(const NetBuffer *buffer, size_t offset, size_t length);
243 
244 NetBuffer *ethAllocBuffer(size_t length, size_t *offset);
245 
246 error_t macStringToAddr(const char_t *str, MacAddr *macAddr);
247 char_t *macAddrToString(const MacAddr *macAddr, char_t *str);
248 void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId);
249 
250 void ethDumpHeader(const EthHeader *ethHeader);
251 
252 error_t eui64StringToAddr(const char_t *str, Eui64 *eui64);
253 char_t *eui64AddrToString(const Eui64 *eui64, char_t *str);
254 
255 //C++ guard
256 #ifdef __cplusplus
257  }
258 #endif
259 
260 #endif
MacAddr addr
MAC address.
Definition: ethernet.h:210
const uint8_t ethPadding[64]
Definition: ethernet.c:58
__start_packed struct @114 EthHeader
Ethernet frame header.
char char_t
Definition: compiler_port.h:41
uint8_t data[]
Definition: ethernet.h:183
bool_t addFlag
Definition: ethernet.h:212
uint8_t b[6]
Definition: ethernet.h:153
TCP/IP stack core.
uint32_t ethCalcCrcEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate CRC over a multi-part buffer.
Definition: ethernet.c:1034
void ethUpdateOutStats(NetInterface *interface, const MacAddr *destMacAddr, size_t length)
Update Ethernet output statistics.
Definition: ethernet.c:894
void ethProcessFrame(NetInterface *interface, uint8_t *frame, size_t length)
Process an incoming Ethernet frame.
Definition: ethernet.c:168
bool_t deleteFlag
Definition: ethernet.h:213
void ethUpdateErrorStats(NetInterface *interface, error_t error)
Update Ethernet error statistics.
Definition: ethernet.c:936
const MacAddr MAC_BROADCAST_ADDR
Definition: ethernet.c:55
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:1151
error_t ethPadFrame(NetBuffer *buffer, size_t *length)
Ethernet frame padding.
Definition: ethernet.c:634
error_t ethAcceptMacAddr(NetInterface *interface, const MacAddr *macAddr)
Add a unicast/multicast address to the MAC filter table.
Definition: ethernet.c:733
uint16_t w[3]
Definition: ethernet.h:154
const MacAddr MAC_UNSPECIFIED_ADDR
Definition: ethernet.c:53
uint16_t tci
Definition: ethernet.h:193
__start_packed struct @113 Eui64
EUI-64 identifier.
void ethDumpHeader(const EthHeader *ethHeader)
Dump Ethernet header for debugging purpose.
Definition: ethernet.c:1293
void macAddrToEui64(const MacAddr *macAddr, Eui64 *interfaceId)
Map a MAC address to the IPv6 modified EUI-64 identifier.
Definition: ethernet.c:1266
error_t ethInit(NetInterface *interface)
Ethernet related initialization.
Definition: ethernet.c:150
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
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:1315
EthType
Ethernet Type field.
Definition: ethernet.h:127
Eui64 interfaceId
Definition: ipv6cp.h:67
error_t ethCheckDestAddr(NetInterface *interface, const MacAddr *macAddr)
Destination MAC address filtering.
Definition: ethernet.c:673
void ethUpdateInStats(NetInterface *interface, const MacAddr *destMacAddr)
Update Ethernet input statistics.
Definition: ethernet.c:856
error_t ethDropMacAddr(NetInterface *interface, const MacAddr *macAddr)
Remove a unicast/multicast address from the MAC filter table.
Definition: ethernet.c:803
__start_packed struct _Ipv4Header __end_packed
error_t ethSendFrame(NetInterface *interface, const MacAddr *destAddr, NetBuffer *buffer, size_t offset, uint16_t type)
Send an Ethernet frame.
Definition: ethernet.c:476
uint_t refCount
Reference count for the current entry.
Definition: ethernet.h:211
error_t
Error codes.
Definition: error.h:40
uint32_t ethCalcCrc(const void *data, size_t length)
Ethernet CRC calculation.
Definition: ethernet.c:974
unsigned int uint_t
Definition: compiler_port.h:43
__start_packed struct @112 MacAddr
MAC address.
uint16_t type
Definition: ethernet.h:182
#define NetInterface
Definition: net.h:34
MacAddr srcAddr
Definition: ethernet.h:181
uint32_t dw[2]
Definition: ethernet.h:169
char_t * macAddrToString(const MacAddr *macAddr, char_t *str)
Convert a MAC address to a dash delimited string.
Definition: ethernet.c:1243
__start_packed struct @115 VlanTag
VLAN tag.
char_t * eui64AddrToString(const Eui64 *eui64, char_t *str)
Convert an EUI-64 address to a dash delimited string.
Definition: ethernet.c:1407
const Eui64 EUI64_UNSPECIFIED_ADDR
Definition: ethernet.c:1305
uint8_t length
Definition: dtls_misc.h:140
MacAddrFlags
MAC address flags.
Definition: ethernet.h:116
int bool_t
Definition: compiler_port.h:47
MacAddr destAddr
Definition: ethernet.h:180
MAC filter table entry.
Definition: ethernet.h:208
NetBuffer * ethAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an Ethernet frame.
Definition: ethernet.c:1107