]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-TCP/FreeRTOS_ARP.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-TCP / FreeRTOS_ARP.c
1 /*
2  * FreeRTOS+TCP V2.2.1
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25
26 /* Standard includes. */
27 #include <stdint.h>
28 #include <stdio.h>
29
30 /* FreeRTOS includes. */
31 #include "FreeRTOS.h"
32 #include "task.h"
33 #include "queue.h"
34 #include "semphr.h"
35
36 /* FreeRTOS+TCP includes. */
37 #include "FreeRTOS_IP.h"
38 #include "FreeRTOS_Sockets.h"
39 #include "FreeRTOS_IP_Private.h"
40 #include "FreeRTOS_ARP.h"
41 #include "FreeRTOS_UDP_IP.h"
42 #include "FreeRTOS_DHCP.h"
43 #if( ipconfigUSE_LLMNR == 1 )
44         #include "FreeRTOS_DNS.h"
45 #endif /* ipconfigUSE_LLMNR */
46 #include "NetworkInterface.h"
47 #include "NetworkBufferManagement.h"
48
49
50 /* When the age of an entry in the ARP table reaches this value (it counts down
51 to zero, so this is an old entry) an ARP request will be sent to see if the
52 entry is still valid and can therefore be refreshed. */
53 #define arpMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST           ( 3 )
54
55 /* The time between gratuitous ARPs. */
56 #ifndef arpGRATUITOUS_ARP_PERIOD
57         #define arpGRATUITOUS_ARP_PERIOD                                        ( pdMS_TO_TICKS( 20000 ) )
58 #endif
59
60 /*-----------------------------------------------------------*/
61
62 /*
63  * Lookup an MAC address in the ARP cache from the IP address.
64  */
65 static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress );
66
67 /*-----------------------------------------------------------*/
68
69 /* The ARP cache. */
70 static ARPCacheRow_t xARPCache[ ipconfigARP_CACHE_ENTRIES ];
71
72 /* The time at which the last gratuitous ARP was sent.  Gratuitous ARPs are used
73 to ensure ARP tables are up to date and to detect IP address conflicts. */
74 static TickType_t xLastGratuitousARPTime = ( TickType_t ) 0;
75
76 /*
77  * IP-clash detection is currently only used internally. When DHCP doesn't respond, the
78  * driver can try out a random LinkLayer IP address (169.254.x.x).  It will send out a
79  * gratuitos ARP message and, after a period of time, check the variables here below:
80  */
81 #if( ipconfigARP_USE_CLASH_DETECTION != 0 )
82         /* Becomes non-zero if another device responded to a gratuitos ARP message. */
83         BaseType_t xARPHadIPClash;
84         /* MAC-address of the other device containing the same IP-address. */
85         MACAddress_t xARPClashMacAddress;
86 #endif /* ipconfigARP_USE_CLASH_DETECTION */
87
88 /* Part of the Ethernet and ARP headers are always constant when sending an IPv4
89 ARP packet.  This array defines the constant parts, allowing this part of the
90 packet to be filled in using a simple memcpy() instead of individual writes. */
91 static const uint8_t xDefaultPartARPPacketHeader[] =
92 {
93         0xff, 0xff, 0xff, 0xff, 0xff, 0xff,     /* Ethernet destination address. */
94         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     /* Ethernet source address. */
95         0x08, 0x06,                                                     /* Ethernet frame type (ipARP_FRAME_TYPE). */
96         0x00, 0x01,                                                     /* usHardwareType (ipARP_HARDWARE_TYPE_ETHERNET). */
97         0x08, 0x00,                                                             /* usProtocolType. */
98         ipMAC_ADDRESS_LENGTH_BYTES,                     /* ucHardwareAddressLength. */
99         ipIP_ADDRESS_LENGTH_BYTES,                              /* ucProtocolAddressLength. */
100         0x00, 0x01,                                                     /* usOperation (ipARP_REQUEST). */
101         0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     /* xSenderHardwareAddress. */
102         0x00, 0x00, 0x00, 0x00,                                 /* ulSenderProtocolAddress. */
103         0x00, 0x00, 0x00, 0x00, 0x00, 0x00              /* xTargetHardwareAddress. */
104 };
105
106 /*-----------------------------------------------------------*/
107
108 eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame )
109 {
110 eFrameProcessingResult_t eReturn = eReleaseBuffer;
111 ARPHeader_t *pxARPHeader;
112 uint32_t ulTargetProtocolAddress, ulSenderProtocolAddress;
113
114         pxARPHeader = &( pxARPFrame->xARPHeader );
115
116         /* The field ulSenderProtocolAddress is badly aligned, copy byte-by-byte. */
117         memcpy( ( void *)&( ulSenderProtocolAddress ), ( void * )pxARPHeader->ucSenderProtocolAddress, sizeof( ulSenderProtocolAddress ) );
118         /* The field ulTargetProtocolAddress is well-aligned, a 32-bits copy. */
119         ulTargetProtocolAddress = pxARPHeader->ulTargetProtocolAddress;
120
121         traceARP_PACKET_RECEIVED();
122
123         /* Don't do anything if the local IP address is zero because
124         that means a DHCP request has not completed. */
125         if( *ipLOCAL_IP_ADDRESS_POINTER != 0UL )
126         {
127                 switch( pxARPHeader->usOperation )
128                 {
129                         case ipARP_REQUEST      :
130                                 /* The packet contained an ARP request.  Was it for the IP
131                                 address of the node running this code? */
132                                 if( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
133                                 {
134                                         iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress );
135
136                                         /* The request is for the address of this node.  Add the
137                                         entry into the ARP cache, or refresh the entry if it
138                                         already exists. */
139                                         vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress );
140
141                                         /* Generate a reply payload in the same buffer. */
142                                         pxARPHeader->usOperation = ( uint16_t ) ipARP_REPLY;
143                                         if( ulTargetProtocolAddress == ulSenderProtocolAddress )
144                                         {
145                                                 /* A double IP address is detected! */
146                                                 /* Give the sources MAC address the value of the broadcast address, will be swapped later */
147                                                 memcpy( pxARPFrame->xEthernetHeader.xSourceAddress.ucBytes, xBroadcastMACAddress.ucBytes, sizeof( xBroadcastMACAddress ) );
148                                                 memset( pxARPHeader->xTargetHardwareAddress.ucBytes, '\0', sizeof( MACAddress_t ) );
149                                                 pxARPHeader->ulTargetProtocolAddress = 0UL;
150                                         }
151                                         else
152                                         {
153                                                 memcpy( pxARPHeader->xTargetHardwareAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( MACAddress_t ) );
154                                                 pxARPHeader->ulTargetProtocolAddress = ulSenderProtocolAddress;
155                                         }
156                                         memcpy( pxARPHeader->xSenderHardwareAddress.ucBytes, ( void * ) ipLOCAL_MAC_ADDRESS, sizeof( MACAddress_t ) );
157                                         memcpy( ( void* )pxARPHeader->ucSenderProtocolAddress, ( void* )ipLOCAL_IP_ADDRESS_POINTER, sizeof( pxARPHeader->ucSenderProtocolAddress ) );
158
159                                         eReturn = eReturnEthernetFrame;
160                                 }
161                                 break;
162
163                         case ipARP_REPLY :
164                                 iptracePROCESSING_RECEIVED_ARP_REPLY( ulTargetProtocolAddress );
165                                 vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress );
166                                 /* Process received ARP frame to see if there is a clash. */
167                                 #if( ipconfigARP_USE_CLASH_DETECTION != 0 )
168                                 {
169                                         if( ulSenderProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
170                                         {
171                                                 xARPHadIPClash = pdTRUE;
172                                                 memcpy( xARPClashMacAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( xARPClashMacAddress.ucBytes ) );
173                                         }
174                                 }
175                                 #endif /* ipconfigARP_USE_CLASH_DETECTION */
176                                 break;
177
178                         default :
179                                 /* Invalid. */
180                                 break;
181                 }
182         }
183
184         return eReturn;
185 }
186 /*-----------------------------------------------------------*/
187
188 #if( ipconfigUSE_ARP_REMOVE_ENTRY != 0 )
189
190         uint32_t ulARPRemoveCacheEntryByMac( const MACAddress_t * pxMACAddress )
191         {
192         BaseType_t x;
193         uint32_t lResult = 0;
194
195                 /* For each entry in the ARP cache table. */
196                 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
197                 {
198                         if( ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
199                         {
200                                 lResult = xARPCache[ x ].ulIPAddress;
201                                 memset( &xARPCache[ x ], '\0', sizeof( xARPCache[ x ] ) );
202                                 break;
203                         }
204                 }
205
206                 return lResult;
207         }
208
209 #endif  /* ipconfigUSE_ARP_REMOVE_ENTRY != 0 */
210 /*-----------------------------------------------------------*/
211
212 void vARPRefreshCacheEntry( const MACAddress_t * pxMACAddress, const uint32_t ulIPAddress )
213 {
214 BaseType_t x = 0;
215 BaseType_t xIpEntry = -1;
216 BaseType_t xMacEntry = -1;
217 BaseType_t xUseEntry = 0;
218 uint8_t ucMinAgeFound = 0U;
219
220         #if( ipconfigARP_STORES_REMOTE_ADDRESSES == 0 )
221                 /* Only process the IP address if it is on the local network.
222                 Unless: when '*ipLOCAL_IP_ADDRESS_POINTER' equals zero, the IP-address
223                 and netmask are still unknown. */
224                 if( ( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) ) ||
225                         ( *ipLOCAL_IP_ADDRESS_POINTER == 0ul ) )
226         #else
227                 /* If ipconfigARP_STORES_REMOTE_ADDRESSES is non-zero, IP addresses with
228                 a different netmask will also be stored.  After when replying to a UDP
229                 message from a different netmask, the IP address can be looped up and a
230                 reply sent.  This option is useful for systems with multiple gateways,
231                 the reply will surely arrive.  If ipconfigARP_STORES_REMOTE_ADDRESSES is
232                 zero the the gateway address is the only option. */
233                 if( pdTRUE )
234         #endif
235         {
236                 /* Start with the maximum possible number. */
237                 ucMinAgeFound--;
238
239                 /* For each entry in the ARP cache table. */
240                 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
241                 {
242                         /* Does this line in the cache table hold an entry for the IP
243                         address being queried? */
244                         if( xARPCache[ x ].ulIPAddress == ulIPAddress )
245                         {
246                                 if( pxMACAddress == NULL )
247                                 {
248                                         /* In case the parameter pxMACAddress is NULL, an entry will be reserved to
249                                         indicate that there is an outstanding ARP request, This entry will have
250                                         "ucValid == pdFALSE". */
251                                         xIpEntry = x;
252                                         break;
253                                 }
254
255                                 /* See if the MAC-address also matches. */
256                                 if( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 )
257                                 {
258                                         /* This function will be called for each received packet
259                                         As this is by far the most common path the coding standard
260                                         is relaxed in this case and a return is permitted as an
261                                         optimisation. */
262                                         xARPCache[ x ].ucAge = ( uint8_t ) ipconfigMAX_ARP_AGE;
263                                         xARPCache[ x ].ucValid = ( uint8_t ) pdTRUE;
264                                         return;
265                                 }
266
267                                 /* Found an entry containing ulIPAddress, but the MAC address
268                                 doesn't match.  Might be an entry with ucValid=pdFALSE, waiting
269                                 for an ARP reply.  Still want to see if there is match with the
270                                 given MAC address.ucBytes.  If found, either of the two entries
271                                 must be cleared. */
272                                 xIpEntry = x;
273                         }
274                         else if( ( pxMACAddress != NULL ) && ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
275                         {
276                                 /* Found an entry with the given MAC-address, but the IP-address
277                                 is different.  Continue looping to find a possible match with
278                                 ulIPAddress. */
279         #if( ipconfigARP_STORES_REMOTE_ADDRESSES != 0 )
280                                 /* If ARP stores the MAC address of IP addresses outside the
281                                 network, than the MAC address of the gateway should not be
282                                 overwritten. */
283                                 BaseType_t bIsLocal[ 2 ];
284                                 bIsLocal[ 0 ] = ( ( xARPCache[ x ].ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) );
285                                 bIsLocal[ 1 ] = ( ( ulIPAddress & xNetworkAddressing.ulNetMask ) == ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) );
286                                 if( bIsLocal[ 0 ] == bIsLocal[ 1 ] )
287                                 {
288                                         xMacEntry = x;
289                                 }
290         #else
291                                 xMacEntry = x;
292         #endif
293                         }
294                         /* _HT_
295                         Shouldn't we test for xARPCache[ x ].ucValid == pdFALSE here ? */
296                         else if( xARPCache[ x ].ucAge < ucMinAgeFound )
297                         {
298                                 /* As the table is traversed, remember the table row that
299                                 contains the oldest entry (the lowest age count, as ages are
300                                 decremented to zero) so the row can be re-used if this function
301                                 needs to add an entry that does not already exist. */
302                                 ucMinAgeFound = xARPCache[ x ].ucAge;
303                                 xUseEntry = x;
304                         }
305                 }
306
307                 if( xMacEntry >= 0 )
308                 {
309                         xUseEntry = xMacEntry;
310
311                         if( xIpEntry >= 0 )
312                         {
313                                 /* Both the MAC address as well as the IP address were found in
314                                 different locations: clear the entry which matches the
315                                 IP-address */
316                                 memset( &xARPCache[ xIpEntry ], '\0', sizeof( xARPCache[ xIpEntry ] ) );
317                         }
318                 }
319                 else if( xIpEntry >= 0 )
320                 {
321                         /* An entry containing the IP-address was found, but it had a different MAC address */
322                         xUseEntry = xIpEntry;
323                 }
324
325                 /* If the entry was not found, we use the oldest entry and set the IPaddress */
326                 xARPCache[ xUseEntry ].ulIPAddress = ulIPAddress;
327
328                 if( pxMACAddress != NULL )
329                 {
330                         memcpy( xARPCache[ xUseEntry ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) );
331
332                         iptraceARP_TABLE_ENTRY_CREATED( ulIPAddress, (*pxMACAddress) );
333                         /* And this entry does not need immediate attention */
334                         xARPCache[ xUseEntry ].ucAge = ( uint8_t ) ipconfigMAX_ARP_AGE;
335                         xARPCache[ xUseEntry ].ucValid = ( uint8_t ) pdTRUE;
336                 }
337                 else if( xIpEntry < 0 )
338                 {
339                         xARPCache[ xUseEntry ].ucAge = ( uint8_t ) ipconfigMAX_ARP_RETRANSMISSIONS;
340                         xARPCache[ xUseEntry ].ucValid = ( uint8_t ) pdFALSE;
341                 }
342         }
343 }
344 /*-----------------------------------------------------------*/
345
346 #if( ipconfigUSE_ARP_REVERSED_LOOKUP == 1 )
347         eARPLookupResult_t eARPGetCacheEntryByMac( MACAddress_t * const pxMACAddress, uint32_t *pulIPAddress )
348         {
349         BaseType_t x;
350         eARPLookupResult_t eReturn = eARPCacheMiss;
351
352                 /* Loop through each entry in the ARP cache. */
353                 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
354                 {
355                         /* Does this row in the ARP cache table hold an entry for the MAC
356                         address being searched? */
357                         if( memcmp( pxMACAddress->ucBytes, xARPCache[ x ].xMACAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 )
358                         {
359                                 *pulIPAddress = xARPCache[ x ].ulIPAddress;
360                                 eReturn = eARPCacheHit;
361                                 break;
362                         }
363                 }
364
365                 return eReturn;
366         }
367 #endif /* ipconfigUSE_ARP_REVERSED_LOOKUP */
368
369 /*-----------------------------------------------------------*/
370
371 eARPLookupResult_t eARPGetCacheEntry( uint32_t *pulIPAddress, MACAddress_t * const pxMACAddress )
372 {
373 eARPLookupResult_t eReturn;
374 uint32_t ulAddressToLookup;
375
376 #if( ipconfigUSE_LLMNR == 1 )
377         if( *pulIPAddress == ipLLMNR_IP_ADDR )  /* Is in network byte order. */
378         {
379                 /* The LLMNR IP-address has a fixed virtual MAC address. */
380                 memcpy( pxMACAddress->ucBytes, xLLMNR_MacAdress.ucBytes, sizeof( MACAddress_t ) );
381                 eReturn = eARPCacheHit;
382         }
383         else
384 #endif
385         if( ( *pulIPAddress == ipBROADCAST_IP_ADDRESS ) ||      /* Is it the general broadcast address 255.255.255.255? */
386                 ( *pulIPAddress == xNetworkAddressing.ulBroadcastAddress ) )/* Or a local broadcast address, eg 192.168.1.255? */
387         {
388                 /* This is a broadcast so uses the broadcast MAC address. */
389                 memcpy( pxMACAddress->ucBytes, xBroadcastMACAddress.ucBytes, sizeof( MACAddress_t ) );
390                 eReturn = eARPCacheHit;
391         }
392         else if( *ipLOCAL_IP_ADDRESS_POINTER == 0UL )
393         {
394                 /* The IP address has not yet been assigned, so there is nothing that
395                 can be done. */
396                 eReturn = eCantSendPacket;
397         }
398         else
399         {
400                 eReturn = eARPCacheMiss;
401
402                 if( ( *pulIPAddress & xNetworkAddressing.ulNetMask ) != ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
403                 {
404 #if( ipconfigARP_STORES_REMOTE_ADDRESSES == 1 )
405                         eReturn = prvCacheLookup( *pulIPAddress, pxMACAddress );
406
407                         if( eReturn == eARPCacheHit )
408                         {
409                                 /* The stack is configured to store 'remote IP addresses', i.e. addresses
410                                 belonging to a different the netmask.  prvCacheLookup() returned a hit, so
411                                 the MAC address is known */
412                         }
413                         else
414 #endif
415                         {
416                                 /* The IP address is off the local network, so look up the
417                                 hardware address of the router, if any. */
418                                 if( xNetworkAddressing.ulGatewayAddress != ( uint32_t )0u )
419                                 {
420                                         ulAddressToLookup = xNetworkAddressing.ulGatewayAddress;
421                                 }
422                                 else
423                                 {
424                                         ulAddressToLookup = *pulIPAddress;
425                                 }
426                         }
427                 }
428                 else
429                 {
430                         /* The IP address is on the local network, so lookup the requested
431                         IP address directly. */
432                         ulAddressToLookup = *pulIPAddress;
433                 }
434
435                 if( eReturn == eARPCacheMiss )
436                 {
437                         if( ulAddressToLookup == 0UL )
438                         {
439                                 /* The address is not on the local network, and there is not a
440                                 router. */
441                                 eReturn = eCantSendPacket;
442                         }
443                         else
444                         {
445                                 eReturn = prvCacheLookup( ulAddressToLookup, pxMACAddress );
446
447                                 if( eReturn == eARPCacheMiss )
448                                 {
449                                         /* It might be that the ARP has to go to the gateway. */
450                                         *pulIPAddress = ulAddressToLookup;
451                                 }
452                         }
453                 }
454         }
455
456         return eReturn;
457 }
458
459 /*-----------------------------------------------------------*/
460
461 static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress )
462 {
463 BaseType_t x;
464 eARPLookupResult_t eReturn = eARPCacheMiss;
465
466         /* Loop through each entry in the ARP cache. */
467         for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
468         {
469                 /* Does this row in the ARP cache table hold an entry for the IP address
470                 being queried? */
471                 if( xARPCache[ x ].ulIPAddress == ulAddressToLookup )
472                 {
473                         /* A matching valid entry was found. */
474                         if( xARPCache[ x ].ucValid == ( uint8_t ) pdFALSE )
475                         {
476                                 /* This entry is waiting an ARP reply, so is not valid. */
477                                 eReturn = eCantSendPacket;
478                         }
479                         else
480                         {
481                                 /* A valid entry was found. */
482                                 memcpy( pxMACAddress->ucBytes, xARPCache[ x ].xMACAddress.ucBytes, sizeof( MACAddress_t ) );
483                                 eReturn = eARPCacheHit;
484                         }
485                         break;
486                 }
487         }
488
489         return eReturn;
490 }
491 /*-----------------------------------------------------------*/
492
493 void vARPAgeCache( void )
494 {
495 BaseType_t x;
496 TickType_t xTimeNow;
497
498         /* Loop through each entry in the ARP cache. */
499         for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
500         {
501                 /* If the entry is valid (its age is greater than zero). */
502                 if( xARPCache[ x ].ucAge > 0U )
503                 {
504                         /* Decrement the age value of the entry in this ARP cache table row.
505                         When the age reaches zero it is no longer considered valid. */
506                         ( xARPCache[ x ].ucAge )--;
507
508                         /* If the entry is not yet valid, then it is waiting an ARP
509                         reply, and the ARP request should be retransmitted. */
510                         if( xARPCache[ x ].ucValid == ( uint8_t ) pdFALSE )
511                         {
512                                 FreeRTOS_OutputARPRequest( xARPCache[ x ].ulIPAddress );
513                         }
514                         else if( xARPCache[ x ].ucAge <= ( uint8_t ) arpMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST )
515                         {
516                                 /* This entry will get removed soon.  See if the MAC address is
517                                 still valid to prevent this happening. */
518                                 iptraceARP_TABLE_ENTRY_WILL_EXPIRE( xARPCache[ x ].ulIPAddress );
519                                 FreeRTOS_OutputARPRequest( xARPCache[ x ].ulIPAddress );
520                         }
521                         else
522                         {
523                                 /* The age has just ticked down, with nothing to do. */
524                         }
525
526                         if( xARPCache[ x ].ucAge == 0u )
527                         {
528                                 /* The entry is no longer valid.  Wipe it out. */
529                                 iptraceARP_TABLE_ENTRY_EXPIRED( xARPCache[ x ].ulIPAddress );
530                                 xARPCache[ x ].ulIPAddress = 0UL;
531                         }
532                 }
533         }
534
535         xTimeNow = xTaskGetTickCount ();
536
537         if( ( xLastGratuitousARPTime == ( TickType_t ) 0 ) || ( ( xTimeNow - xLastGratuitousARPTime ) > ( TickType_t ) arpGRATUITOUS_ARP_PERIOD ) )
538         {
539                 FreeRTOS_OutputARPRequest( *ipLOCAL_IP_ADDRESS_POINTER );
540                 xLastGratuitousARPTime = xTimeNow;
541         }
542 }
543 /*-----------------------------------------------------------*/
544
545 void vARPSendGratuitous( void )
546 {
547         /* Setting xLastGratuitousARPTime to 0 will force a gratuitous ARP the next
548         time vARPAgeCache() is called. */
549         xLastGratuitousARPTime = ( TickType_t ) 0;
550
551         /* Let the IP-task call vARPAgeCache(). */
552         xSendEventToIPTask( eARPTimerEvent );
553 }
554
555 /*-----------------------------------------------------------*/
556 void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress )
557 {
558 NetworkBufferDescriptor_t *pxNetworkBuffer;
559
560         /* This is called from the context of the IP event task, so a block time
561         must not be used. */
562         pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( sizeof( ARPPacket_t ), ( TickType_t ) 0 );
563
564         if( pxNetworkBuffer != NULL )
565         {
566                 pxNetworkBuffer->ulIPAddress = ulIPAddress;
567                 vARPGenerateRequestPacket( pxNetworkBuffer );
568
569                 #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
570                 {
571                         if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
572                         {
573                         BaseType_t xIndex;
574
575                                 for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
576                                 {
577                                         pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
578                                 }
579                                 pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
580                         }
581                 }
582                 #endif
583                 if( xIsCallingFromIPTask() != 0 )
584                 {
585                         /* Only the IP-task is allowed to call this function directly. */
586                         xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );
587                 }
588                 else
589                 {
590                 IPStackEvent_t xSendEvent;
591
592                         /* Send a message to the IP-task to send this ARP packet. */
593                         xSendEvent.eEventType = eNetworkTxEvent;
594                         xSendEvent.pvData = ( void * ) pxNetworkBuffer;
595                         if( xSendEventStructToIPTask( &xSendEvent, ( TickType_t ) portMAX_DELAY ) == pdFAIL )
596                         {
597                                 /* Failed to send the message, so release the network buffer. */
598                                 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
599                         }
600                 }
601         }
602 }
603
604 void vARPGenerateRequestPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
605 {
606 ARPPacket_t *pxARPPacket;
607
608         /* Buffer allocation ensures that buffers always have space
609         for an ARP packet. See buffer allocation implementations 1
610         and 2 under portable/BufferManagement. */
611         configASSERT( pxNetworkBuffer );
612         configASSERT( pxNetworkBuffer->xDataLength >= sizeof(ARPPacket_t) );
613
614         pxARPPacket = ( ARPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
615
616         /* memcpy the const part of the header information into the correct
617         location in the packet.  This copies:
618                 xEthernetHeader.ulDestinationAddress
619                 xEthernetHeader.usFrameType;
620                 xARPHeader.usHardwareType;
621                 xARPHeader.usProtocolType;
622                 xARPHeader.ucHardwareAddressLength;
623                 xARPHeader.ucProtocolAddressLength;
624                 xARPHeader.usOperation;
625                 xARPHeader.xTargetHardwareAddress;
626         */
627         memcpy( ( void * ) pxARPPacket, ( void * ) xDefaultPartARPPacketHeader, sizeof( xDefaultPartARPPacketHeader ) );
628         memcpy( ( void * ) pxARPPacket->xEthernetHeader.xSourceAddress.ucBytes , ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
629         memcpy( ( void * ) pxARPPacket->xARPHeader.xSenderHardwareAddress.ucBytes, ( void * ) ipLOCAL_MAC_ADDRESS, ( size_t ) ipMAC_ADDRESS_LENGTH_BYTES );
630
631         memcpy( ( void* )pxARPPacket->xARPHeader.ucSenderProtocolAddress, ( void* )ipLOCAL_IP_ADDRESS_POINTER, sizeof( pxARPPacket->xARPHeader.ucSenderProtocolAddress ) );
632         pxARPPacket->xARPHeader.ulTargetProtocolAddress = pxNetworkBuffer->ulIPAddress;
633
634         pxNetworkBuffer->xDataLength = sizeof( ARPPacket_t );
635
636         iptraceCREATING_ARP_REQUEST( pxNetworkBuffer->ulIPAddress );
637 }
638 /*-----------------------------------------------------------*/
639
640 void FreeRTOS_ClearARP( void )
641 {
642         memset( xARPCache, '\0', sizeof( xARPCache ) );
643 }
644 /*-----------------------------------------------------------*/
645
646 #if( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 )
647
648         void FreeRTOS_PrintARPCache( void )
649         {
650         BaseType_t x, xCount = 0;
651
652                 /* Loop through each entry in the ARP cache. */
653                 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
654                 {
655                         if( ( xARPCache[ x ].ulIPAddress != 0ul ) && ( xARPCache[ x ].ucAge > 0U ) )
656                         {
657                                 /* See if the MAC-address also matches, and we're all happy */
658                                 FreeRTOS_printf( ( "Arp %2ld: %3u - %16lxip : %02x:%02x:%02x : %02x:%02x:%02x\n",
659                                         x,
660                                         xARPCache[ x ].ucAge,
661                                         xARPCache[ x ].ulIPAddress,
662                                         xARPCache[ x ].xMACAddress.ucBytes[0],
663                                         xARPCache[ x ].xMACAddress.ucBytes[1],
664                                         xARPCache[ x ].xMACAddress.ucBytes[2],
665                                         xARPCache[ x ].xMACAddress.ucBytes[3],
666                                         xARPCache[ x ].xMACAddress.ucBytes[4],
667                                         xARPCache[ x ].xMACAddress.ucBytes[5] ) );
668                                 xCount++;
669                         }
670                 }
671
672                 FreeRTOS_printf( ( "Arp has %ld entries\n", xCount ) );
673         }
674
675 #endif /* ( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 ) */