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-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 _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 {
119 } ArpOpcode;
120 
121 
122 /**
123  * @brief ARP cache entry states
124  **/
125 
126 typedef enum
127 {
135 } ArpState;
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 } ArpQueueItem;
177 
178 
179 /**
180  * @brief ARP cache entry
181  **/
182 
183 typedef struct
184 {
185  ArpState state; //Reachability state
186  Ipv4Addr ipAddr; //Unicast IPv4 address
187  MacAddr macAddr; //Link layer address associated with the IPv4 address
188  systime_t timestamp; //Time stamp to manage entry lifetime
189  systime_t timeout; //Timeout value
190  uint_t retransmitCount; //Retransmission counter
191  ArpQueueItem queue[ARP_MAX_PENDING_PACKETS]; //Packets waiting for address resolution to complete
192  uint_t queueSize; //Number of queued packets
193 } ArpCacheEntry;
194 
195 
196 //Tick counter to handle periodic operations
198 
199 //ARP related functions
200 error_t arpInit(NetInterface *interface);
201 void arpFlushCache(NetInterface *interface);
202 
205 
206 void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
207 void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
208 
209 error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr);
210 
212  Ipv4Addr ipAddr, NetBuffer *buffer, size_t offset);
213 
214 void arpTick(NetInterface *interface);
215 
216 void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket,
217  size_t length);
218 
219 void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest);
220 void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse);
221 
222 error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr);
223 
224 error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr,
225  const MacAddr *destMacAddr);
226 
227 error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr,
228  Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr);
229 
230 void arpDumpPacket(const ArpPacket *arpPacket);
231 
232 //C++ guard
233 #ifdef __cplusplus
234 }
235 #endif
236 
237 #endif
void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest)
Incoming ARP request processing.
Definition: arp.c:654
uint8_t length
Definition: dtls_misc.h:149
@ ARP_STATE_STALE
Definition: arp.h:131
void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Flush packet queue.
Definition: arp.c:223
#define ARP_MAX_PENDING_PACKETS
Definition: arp.h:53
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:806
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
@ 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:251
size_t offset
Definition: arp.h:175
@ ARP_STATE_DELAY
Definition: arp.h:132
uint16_t pro
Definition: arp.h:151
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
NetBuffer * buffer
Definition: arp.h:174
void arpDumpPacket(const ArpPacket *arpPacket)
Dump ARP packet for debugging purpose.
Definition: arp.c:976
ArpOpcode
ARP opcodes.
Definition: arp.h:115
uint_t queueSize
Definition: arp.h:192
error_t arpEnqueuePacket(NetInterface *interface, Ipv4Addr ipAddr, NetBuffer *buffer, size_t offset)
Enqueue an IPv4 packet waiting for address resolution.
Definition: arp.c:342
@ ARP_OPCODE_ARP_REQUEST
Definition: arp.h:117
ArpCacheEntry * arpCreateEntry(NetInterface *interface)
Create a new entry in the ARP cache.
Definition: arp.c:100
error_t
Error codes.
Definition: error.h:42
MacAddr macAddr
Definition: arp.h:187
uint16_t hrd
Definition: arp.h:150
Ipv4Addr spa
Definition: arp.h:156
void arpTick(NetInterface *interface)
ARP timer handler.
Definition: arp.c:426
void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse)
Incoming ARP reply processing.
Definition: arp.c:724
@ ARP_STATE_INCOMPLETE
Definition: arp.h:129
#define NetInterface
Definition: net.h:36
uint_t retransmitCount
Definition: arp.h:190
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:861
ArpState
ARP cache entry states.
Definition: arp.h:126
__start_packed struct _Ipv4Header __end_packed
Ipv4Addr tpa
Definition: arp.h:158
uint8_t hln
Definition: arp.h:152
error_t arpSendReply(NetInterface *interface, Ipv4Addr senderIpAddr, Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr)
Send ARP reply.
Definition: arp.c:924
ArpState state
Definition: arp.h:185
__start_packed struct @162 ArpPacket
ARP packet.
systime_t arpTickCounter
Definition: arp.c:51
uint8_t pln
Definition: arp.h:153
MacAddr tha
Definition: arp.h:157
@ ARP_STATE_PERMANENT
Definition: arp.h:134
void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Send packets that are waiting for address resolution.
Definition: arp.c:183
systime_t timestamp
Definition: arp.h:188
ARP cache entry.
Definition: arp.h:183
systime_t timeout
Definition: arp.h:189
ArpCacheEntry * arpFindEntry(NetInterface *interface, Ipv4Addr ipAddr)
Search the ARP cache for a given IPv4 address.
Definition: arp.c:152
@ ARP_STATE_PROBE
Definition: arp.h:133
MacAddr sha
Definition: arp.h:155
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:60
ARP queue item.
Definition: arp.h:172
@ ARP_OPCODE_ARP_REPLY
Definition: arp.h:118
void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket, size_t length)
Incoming ARP packet processing.
Definition: arp.c:537
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
uint32_t systime_t
Definition: compiler_port.h:46
uint8_t ipAddr[4]
Definition: mib_common.h:187
Ipv4Addr ipAddr
Definition: arp.h:186
void arpFlushCache(NetInterface *interface)
Flush ARP cache.
Definition: arp.c:75
uint16_t op
Definition: arp.h:154
__start_packed struct @108 MacAddr
MAC address.