arp.h
Go to the documentation of this file.
1 /**
2  * @file arp.h
3  * @brief ARP (Address Resolution Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 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.4
29  **/
30 
31 #ifndef _ARP_H
32 #define _ARP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //ARP tick interval
38 #ifndef ARP_TICK_INTERVAL
39  #define ARP_TICK_INTERVAL 200
40 #elif (ARP_TICK_INTERVAL < 10)
41  #error ARP_TICK_INTERVAL parameter is not valid
42 #endif
43 
44 //Size of ARP cache
45 #ifndef ARP_CACHE_SIZE
46  #define ARP_CACHE_SIZE 8
47 #elif (ARP_CACHE_SIZE < 4)
48  #error ARP_CACHE_SIZE parameter is not valid
49 #endif
50 
51 //Maximum number of packets waiting for address resolution to complete
52 #ifndef ARP_MAX_PENDING_PACKETS
53  #define ARP_MAX_PENDING_PACKETS 2
54 #elif (ARP_MAX_PENDING_PACKETS < 1)
55  #error ARP_MAX_PENDING_PACKETS parameter is not valid
56 #endif
57 
58 //Maximum number of times that an ARP request will be retransmitted
59 #ifndef ARP_MAX_REQUESTS
60  #define ARP_MAX_REQUESTS 3
61 #elif (ARP_MAX_REQUESTS < 1)
62  #error ARP_MAX_REQUESTS parameter is not valid
63 #endif
64 
65 //Time interval between subsequent retransmissions of ARP requests
66 #ifndef ARP_REQUEST_TIMEOUT
67  #define ARP_REQUEST_TIMEOUT 1000
68 #elif (ARP_REQUEST_TIMEOUT < 100)
69  #error ARP_REQUEST_TIMEOUT parameter is not valid
70 #endif
71 
72 //Maximum number of times that a probe will be retransmitted
73 #ifndef ARP_MAX_PROBES
74  #define ARP_MAX_PROBES 2
75 #elif (ARP_MAX_PROBES < 1)
76  #error ARP_MAX_PROBES parameter is not valid
77 #endif
78 
79 //time interval between subsequent retransmissions of probes
80 #ifndef ARP_PROBE_TIMEOUT
81  #define ARP_PROBE_TIMEOUT 60000
82 #elif (ARP_PROBE_TIMEOUT < 1000)
83  #error ARP_PROBE_TIMEOUT parameter is not valid
84 #endif
85 
86 //The time a host is considered reachable after receiving a reachability confirmation
87 #ifndef ARP_REACHABLE_TIME
88  #define ARP_REACHABLE_TIME 60000
89 #elif (ARP_REACHABLE_TIME < 1000)
90  #error ARP_REACHABLE_TIME parameter is not valid
91 #endif
92 
93 //Delay before sending the first probe
94 #ifndef ARP_DELAY_FIRST_PROBE_TIME
95  #define ARP_DELAY_FIRST_PROBE_TIME 5000
96 #elif (ARP_DELAY_FIRST_PROBE_TIME < 1000)
97  #error ARP_DELAY_FIRST_PROBE_TIME parameter is not valid
98 #endif
99 
100 //Hardware type
101 #define ARP_HARDWARE_TYPE_ETH 0x0001
102 //Protocol type
103 #define ARP_PROTOCOL_TYPE_IPV4 0x0800
104 
105 //C++ guard
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109 
110 
111 /**
112  * @brief ARP opcodes
113  **/
114 
115 typedef enum
116 {
120 
121 
122 /**
123  * @brief ARP cache entry states
124  **/
125 
126 typedef enum
127 {
136 
137 
138 //CodeWarrior or Win32 compiler?
139 #if defined(__CWCC__) || defined(_WIN32)
140  #pragma pack(push, 1)
141 #endif
142 
143 
144 /**
145  * @brief ARP packet
146  **/
147 
148 typedef __start_packed struct
149 {
150  uint16_t hrd; //0-1
151  uint16_t pro; //2-3
152  uint8_t hln; //4
153  uint8_t pln; //5
154  uint16_t op; //6-7
155  MacAddr sha; //8-13
156  Ipv4Addr spa; //14-17
157  MacAddr tha; //18-23
158  Ipv4Addr tpa; //24-27
160 
161 
162 //CodeWarrior or Win32 compiler?
163 #if defined(__CWCC__) || defined(_WIN32)
164  #pragma pack(pop)
165 #endif
166 
167 
168 /**
169  * @brief ARP queue item
170  **/
171 
172 typedef struct
173 {
174  NetBuffer *buffer; ///<Packet waiting for address resolution
175  size_t offset; ///<Offset to the first byte of the packet
176  NetTxAncillary ancillary; ///<Additional options
177 } ArpQueueItem;
178 
179 
180 /**
181  * @brief ARP cache entry
182  **/
183 
184 typedef struct
185 {
186  ArpState state; ///<Reachability state
187  Ipv4Addr ipAddr; ///<Unicast IPv4 address
188  MacAddr macAddr; ///<Link layer address associated with the IPv4 address
189  systime_t timestamp; ///<Time stamp to manage entry lifetime
190  systime_t timeout; ///<Timeout value
191  uint_t retransmitCount; ///<Retransmission counter
192  ArpQueueItem queue[ARP_MAX_PENDING_PACKETS]; ///<Packets waiting for address resolution to complete
193  uint_t queueSize; ///<Number of queued packets
194 } ArpCacheEntry;
195 
196 
197 //Tick counter to handle periodic operations
199 
200 //ARP related functions
201 error_t arpInit(NetInterface *interface);
202 void arpFlushCache(NetInterface *interface);
203 
206 
207 void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
208 void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
209 
210 error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr);
211 
213  NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
214 
215 void arpTick(NetInterface *interface);
216 
217 void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket,
218  size_t length);
219 
220 void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest);
221 void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse);
222 
223 error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr);
224 
225 error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr,
226  const MacAddr *destMacAddr);
227 
228 error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr,
229  Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr);
230 
231 void arpDumpPacket(const ArpPacket *arpPacket);
232 
233 //C++ guard
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:874
uint16_t hrd
Definition: arp.h:150
Ipv4Addr tpa
Definition: arp.h:158
uint16_t pro
Definition: arp.h:151
void arpFlushCache(NetInterface *interface)
Flush ARP cache.
Definition: arp.c:75
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:814
error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr, Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr)
Send ARP reply.
Definition: arp.c:943
void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Send packets that are waiting for address resolution.
Definition: arp.c:183
void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse)
Incoming ARP reply processing.
Definition: arp.c:732
void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Flush packet queue.
Definition: arp.c:223
error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr)
Address resolution using ARP protocol.
Definition: arp.c:251
uint16_t op
Definition: arp.h:154
#define ARP_MAX_PENDING_PACKETS
Definition: arp.h:53
uint8_t hln
Definition: arp.h:152
MacAddr sha
Definition: arp.h:155
ArpState
ARP cache entry states.
Definition: arp.h:127
@ ARP_STATE_NONE
Definition: arp.h:128
@ ARP_STATE_STALE
Definition: arp.h:131
@ ARP_STATE_INCOMPLETE
Definition: arp.h:129
@ ARP_STATE_REACHABLE
Definition: arp.h:130
@ ARP_STATE_PROBE
Definition: arp.h:133
@ ARP_STATE_PERMANENT
Definition: arp.h:134
@ ARP_STATE_DELAY
Definition: arp.h:132
void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket, size_t length)
Incoming ARP packet processing.
Definition: arp.c:543
systime_t arpTickCounter
Definition: arp.c:51
ArpCacheEntry * arpCreateEntry(NetInterface *interface)
Create a new entry in the ARP cache.
Definition: arp.c:100
void arpDumpPacket(const ArpPacket *arpPacket)
Dump ARP packet for debugging purpose.
Definition: arp.c:1001
ArpCacheEntry * arpFindEntry(NetInterface *interface, Ipv4Addr ipAddr)
Search the ARP cache for a given IPv4 address.
Definition: arp.c:152
uint8_t pln
Definition: arp.h:153
Ipv4Addr spa
Definition: arp.h:156
__start_packed struct @61 ArpPacket
ARP packet.
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:60
MacAddr tha
Definition: arp.h:157
void arpTick(NetInterface *interface)
ARP timer handler.
Definition: arp.c:432
void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest)
Incoming ARP request processing.
Definition: arp.c:662
ArpOpcode
ARP opcodes.
Definition: arp.h:116
@ ARP_OPCODE_ARP_REQUEST
Definition: arp.h:117
@ ARP_OPCODE_ARP_REPLY
Definition: arp.h:118
error_t arpEnqueuePacket(NetInterface *interface, Ipv4Addr ipAddr, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Enqueue an IPv4 packet waiting for address resolution.
Definition: arp.c:344
uint8_t length
Definition: coap_common.h:191
unsigned int uint_t
Definition: compiler_port.h:45
error_t
Error codes.
Definition: error.h:43
__start_packed struct @5 MacAddr
MAC address.
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
__start_packed struct _Ipv4Header __end_packed
uint8_t ipAddr[4]
Definition: mib_common.h:187
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define NetTxAncillary
Definition: net_misc.h:36
uint32_t systime_t
System time.
ARP cache entry.
Definition: arp.h:185
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: arp.h:189
Ipv4Addr ipAddr
Unicast IPv4 address.
Definition: arp.h:187
MacAddr macAddr
Link layer address associated with the IPv4 address.
Definition: arp.h:188
uint_t retransmitCount
Retransmission counter.
Definition: arp.h:191
uint_t queueSize
Number of queued packets.
Definition: arp.h:193
systime_t timeout
Timeout value.
Definition: arp.h:190
ArpState state
Reachability state.
Definition: arp.h:186
ARP queue item.
Definition: arp.h:173
NetBuffer * buffer
Packet waiting for address resolution.
Definition: arp.h:174
size_t offset
Offset to the first byte of the packet.
Definition: arp.h:175
NetTxAncillary ancillary
Additional options.
Definition: arp.h:176
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89