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