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-2024 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.4.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 < 1)
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 //CC-RX, CodeWarrior or Win32 compiler?
139 #if defined(__CCRX__)
140  #pragma pack
141 #elif defined(__CWCC__) || defined(_WIN32)
142  #pragma pack(push, 1)
143 #endif
144 
145 
146 /**
147  * @brief ARP packet
148  **/
149 
151 {
152  uint16_t hrd; //0-1
153  uint16_t pro; //2-3
154  uint8_t hln; //4
155  uint8_t pln; //5
156  uint16_t op; //6-7
157  MacAddr sha; //8-13
158  Ipv4Addr spa; //14-17
159  MacAddr tha; //18-23
160  Ipv4Addr tpa; //24-27
162 
163 
164 //CC-RX, CodeWarrior or Win32 compiler?
165 #if defined(__CCRX__)
166  #pragma unpack
167 #elif defined(__CWCC__) || defined(_WIN32)
168  #pragma pack(pop)
169 #endif
170 
171 
172 /**
173  * @brief ARP queue item
174  **/
175 
176 typedef struct
177 {
178  NetBuffer *buffer; ///<Packet waiting for address resolution
179  size_t offset; ///<Offset to the first byte of the packet
180  NetTxAncillary ancillary; ///<Additional options
181 } ArpQueueItem;
182 
183 
184 /**
185  * @brief ARP cache entry
186  **/
187 
188 typedef struct
189 {
190  ArpState state; ///<Reachability state
191  Ipv4Addr ipAddr; ///<Unicast IPv4 address
192  MacAddr macAddr; ///<Link layer address associated with the IPv4 address
193  systime_t timestamp; ///<Time stamp to manage entry lifetime
194  systime_t timeout; ///<Timeout value
195  uint_t retransmitCount; ///<Retransmission counter
196  ArpQueueItem queue[ARP_MAX_PENDING_PACKETS]; ///<Packets waiting for address resolution to complete
197  uint_t queueSize; ///<Number of queued packets
198 } ArpCacheEntry;
199 
200 
201 //Tick counter to handle periodic operations
203 
204 //ARP related functions
205 error_t arpInit(NetInterface *interface);
206 error_t arpEnable(NetInterface *interface, bool_t enable);
207 
209  const MacAddr *macAddr);
210 
212 
213 error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr);
214 
216  NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
217 
218 void arpTick(NetInterface *interface);
219 
220 void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket,
221  size_t length);
222 
223 void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest);
224 void arpProcessReply(NetInterface *interface, ArpPacket *arpReply);
225 
226 error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr);
227 
228 error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr,
229  const MacAddr *destMacAddr);
230 
231 error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr,
232  Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr);
233 
234 void arpDumpPacket(const ArpPacket *arpPacket);
235 
236 //C++ guard
237 #ifdef __cplusplus
238 }
239 #endif
240 
241 #endif
void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest)
Incoming ARP request processing.
Definition: arp.c:665
@ ARP_STATE_STALE
Definition: arp.h:131
int bool_t
Definition: compiler_port.h:53
#define ARP_MAX_PENDING_PACKETS
Definition: arp.h:53
typedef __packed_struct
ARP packet.
Definition: arp.h:151
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:817
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
@ ARP_STATE_REACHABLE
Definition: arp.h:130
@ ARP_STATE_NONE
Definition: arp.h:128
error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr)
Address resolution using ARP protocol.
Definition: arp.c:235
size_t offset
Offset to the first byte of the packet.
Definition: arp.h:179
error_t arpRemoveStaticEntry(NetInterface *interface, Ipv4Addr ipAddr)
Remove a static entry from the ARP cache.
Definition: arp.c:190
@ ARP_STATE_DELAY
Definition: arp.h:132
uint16_t pro
Definition: arp.h:153
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:297
NetBuffer * buffer
Packet waiting for address resolution.
Definition: arp.h:178
void arpDumpPacket(const ArpPacket *arpPacket)
Dump ARP packet for debugging purpose.
Definition: arp.c:1004
error_t arpEnable(NetInterface *interface, bool_t enable)
Enable address resolution using ARP.
Definition: arp.c:83
ArpOpcode
ARP opcodes.
Definition: arp.h:116
error_t arpAddStaticEntry(NetInterface *interface, Ipv4Addr ipAddr, const MacAddr *macAddr)
Add a static entry in the ARP cache.
Definition: arp.c:117
uint_t queueSize
Number of queued packets.
Definition: arp.h:197
ArpPacket
Definition: arp.h:161
@ ARP_OPCODE_ARP_REQUEST
Definition: arp.h:117
error_t
Error codes.
Definition: error.h:43
MacAddr macAddr
Link layer address associated with the IPv4 address.
Definition: arp.h:192
Ipv4Addr spa
Definition: arp.h:158
void arpTick(NetInterface *interface)
ARP timer handler.
Definition: arp.c:421
@ ARP_STATE_INCOMPLETE
Definition: arp.h:129
#define NetInterface
Definition: net.h:36
uint_t retransmitCount
Retransmission counter.
Definition: arp.h:195
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:877
ArpState
ARP cache entry states.
Definition: arp.h:127
#define NetTxAncillary
Definition: net_misc.h:36
Ipv4Addr tpa
Definition: arp.h:160
uint8_t hln
Definition: arp.h:154
uint8_t length
Definition: tcp.h:368
error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr, Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr)
Send ARP reply.
Definition: arp.c:946
ArpState state
Reachability state.
Definition: arp.h:190
MacAddr
Definition: ethernet.h:195
systime_t arpTickCounter
Definition: arp.c:51
uint8_t pln
Definition: arp.h:155
uint32_t systime_t
System time.
MacAddr tha
Definition: arp.h:159
@ ARP_STATE_PERMANENT
Definition: arp.h:134
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: arp.h:193
ARP cache entry.
Definition: arp.h:189
systime_t timeout
Timeout value.
Definition: arp.h:194
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:333
@ ARP_STATE_PROBE
Definition: arp.h:133
MacAddr sha
Definition: arp.h:157
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:60
NetTxAncillary ancillary
Additional options.
Definition: arp.h:180
void arpProcessReply(NetInterface *interface, ArpPacket *arpReply)
Incoming ARP reply processing.
Definition: arp.c:735
Ipv4Addr ipAddr
Definition: ipcp.h:105
ARP queue item.
Definition: arp.h:177
@ ARP_OPCODE_ARP_REPLY
Definition: arp.h:118
void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket, size_t length)
Incoming ARP packet processing.
Definition: arp.c:546
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
Ipv4Addr ipAddr
Unicast IPv4 address.
Definition: arp.h:191
uint16_t op
Definition: arp.h:156