1 /*
2  * FreeRTOS+TCP V3.1.0
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 #ifndef FREERTOS_ARP_H
29 #define FREERTOS_ARP_H
30 
31 /* *INDENT-OFF* */
32 #ifdef __cplusplus
33     extern "C" {
34 #endif
35 /* *INDENT-ON* */
36 
37 /* Application level configuration options. */
38 #include "FreeRTOSIPConfig.h"
39 #include "FreeRTOSIPConfigDefaults.h"
40 #include "IPTraceMacroDefaults.h"
41 
42 /*-----------------------------------------------------------*/
43 /* Miscellaneous structure and definitions. */
44 /*-----------------------------------------------------------*/
45 
46 /**
47  * Structure for one row in the ARP cache table.
48  */
49 typedef struct xARP_CACHE_TABLE_ROW
50 {
51     uint32_t ulIPAddress;     /**< The IP address of an ARP cache entry. */
52     MACAddress_t xMACAddress; /**< The MAC address of an ARP cache entry. */
53     uint8_t ucAge;            /**< A value that is periodically decremented but can also be refreshed by active communication.  The ARP cache entry is removed if the value reaches zero. */
54     uint8_t ucValid;          /**< pdTRUE: xMACAddress is valid, pdFALSE: waiting for ARP reply */
55 } ARPCacheRow_t;
56 
57 typedef enum
58 {
59     eARPCacheMiss = 0, /* 0 An ARP table lookup did not find a valid entry. */
60     eARPCacheHit,      /* 1 An ARP table lookup found a valid entry. */
61     eCantSendPacket    /* 2 There is no IP address, or an ARP is still in progress, so the packet cannot be sent. */
62 } eARPLookupResult_t;
63 
64 /*
65  * If ulIPAddress is already in the ARP cache table then reset the age of the
66  * entry back to its maximum value.  If ulIPAddress is not already in the ARP
67  * cache table then add it - replacing the oldest current entry if there is not
68  * a free space available.
69  */
70 void vARPRefreshCacheEntry( const MACAddress_t * pxMACAddress,
71                             const uint32_t ulIPAddress );
72 
73 #if ( ipconfigARP_USE_CLASH_DETECTION != 0 )
74     /* Becomes non-zero if another device responded to a gratuitous ARP message. */
75     extern BaseType_t xARPHadIPClash;
76     /* MAC-address of the other device containing the same IP-address. */
77     extern MACAddress_t xARPClashMacAddress;
78 #endif /* ipconfigARP_USE_CLASH_DETECTION */
79 
80 #if ( ipconfigUSE_ARP_REMOVE_ENTRY != 0 )
81 
82 /*
83  * In some rare cases, it might be useful to remove a ARP cache entry of a
84  * known MAC address to make sure it gets refreshed.
85  */
86     uint32_t ulARPRemoveCacheEntryByMac( const MACAddress_t * pxMACAddress );
87 
88 #endif /* ipconfigUSE_ARP_REMOVE_ENTRY != 0 */
89 
90 
91 BaseType_t xIsIPInARPCache( uint32_t ulAddressToLookup );
92 
93 BaseType_t xCheckRequiresARPResolution( const NetworkBufferDescriptor_t * pxNetworkBuffer );
94 
95 /*
96  * Look for ulIPAddress in the ARP cache.  If the IP address exists, copy the
97  * associated MAC address into pxMACAddress, refresh the ARP cache entry's
98  * age, and return eARPCacheHit.  If the IP address does not exist in the ARP
99  * cache return eARPCacheMiss.  If the packet cannot be sent for any reason
100  * (maybe DHCP is still in process, or the addressing needs a gateway but there
101  * isn't a gateway defined) then return eCantSendPacket.
102  */
103 eARPLookupResult_t eARPGetCacheEntry( uint32_t * pulIPAddress,
104                                       MACAddress_t * const pxMACAddress );
105 
106 #if ( ipconfigUSE_ARP_REVERSED_LOOKUP != 0 )
107 
108 /* Lookup an IP-address if only the MAC-address is known */
109     eARPLookupResult_t eARPGetCacheEntryByMac( const MACAddress_t * const pxMACAddress,
110                                                uint32_t * pulIPAddress );
111 
112 #endif
113 
114 /*
115  * Reduce the age count in each entry within the ARP cache.  An entry is no
116  * longer considered valid and is deleted if its age reaches zero.
117  */
118 void vARPAgeCache( void );
119 
120 /*
121  * Send out an ARP request for the IP address contained in pxNetworkBuffer, and
122  * add an entry into the ARP table that indicates that an ARP reply is
123  * outstanding so re-transmissions can be generated.
124  */
125 void vARPGenerateRequestPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );
126 
127 /*
128  * After DHCP is ready and when changing IP address, force a quick send of our new IP
129  * address
130  */
131 void vARPSendGratuitous( void );
132 
133 /* This function will check if the target IP-address belongs to this device.
134  * If so, the packet will be passed to the IP-stack, who will answer it.
135  * The function is to be called within the function xNetworkInterfaceOutput()
136  * in NetworkInterface.c as follows:
137  *
138  *   if( xCheckLoopback( pxDescriptor, bReleaseAfterSend ) != 0 )
139  *   {
140  *      / * The packet has been sent back to the IP-task.
141  *        * The IP-task will further handle it.
142  *        * Do not release the descriptor.
143  *        * /
144  *       return pdTRUE;
145  *   }
146  *   / * Send the packet as usual. * /
147  */
148 BaseType_t xCheckLoopback( NetworkBufferDescriptor_t * const pxDescriptor,
149                            BaseType_t bReleaseAfterSend );
150 
151 void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress );
152 
153 /* *INDENT-OFF* */
154 #ifdef __cplusplus
155     } /* extern "C" */
156 #endif
157 /* *INDENT-ON* */
158 
159 #endif /* FREERTOS_ARP_H */
160