3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * http://aws.amazon.com/freertos
23 * http://www.FreeRTOS.org
26 /* Standard includes. */
30 /* FreeRTOS includes. */
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"
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 )
55 /* The time between gratuitous ARPs. */
56 #ifndef arpGRATUITOUS_ARP_PERIOD
57 #define arpGRATUITOUS_ARP_PERIOD ( pdMS_TO_TICKS( 20000 ) )
60 /*-----------------------------------------------------------*/
63 * Lookup an MAC address in the ARP cache from the IP address.
65 static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress );
67 /*-----------------------------------------------------------*/
70 static ARPCacheRow_t xARPCache[ ipconfigARP_CACHE_ENTRIES ];
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;
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:
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 */
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[] =
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. */
106 /*-----------------------------------------------------------*/
108 eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame )
110 eFrameProcessingResult_t eReturn = eReleaseBuffer;
111 ARPHeader_t *pxARPHeader;
112 uint32_t ulTargetProtocolAddress, ulSenderProtocolAddress;
114 pxARPHeader = &( pxARPFrame->xARPHeader );
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;
121 traceARP_PACKET_RECEIVED();
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 )
127 switch( pxARPHeader->usOperation )
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 )
134 iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress );
136 /* The request is for the address of this node. Add the
137 entry into the ARP cache, or refresh the entry if it
139 vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress );
141 /* Generate a reply payload in the same buffer. */
142 pxARPHeader->usOperation = ( uint16_t ) ipARP_REPLY;
143 if( ulTargetProtocolAddress == ulSenderProtocolAddress )
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;
153 memcpy( pxARPHeader->xTargetHardwareAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( MACAddress_t ) );
154 pxARPHeader->ulTargetProtocolAddress = ulSenderProtocolAddress;
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 ) );
159 eReturn = eReturnEthernetFrame;
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 )
169 if( ulSenderProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER )
171 xARPHadIPClash = pdTRUE;
172 memcpy( xARPClashMacAddress.ucBytes, pxARPHeader->xSenderHardwareAddress.ucBytes, sizeof( xARPClashMacAddress.ucBytes ) );
175 #endif /* ipconfigARP_USE_CLASH_DETECTION */
186 /*-----------------------------------------------------------*/
188 #if( ipconfigUSE_ARP_REMOVE_ENTRY != 0 )
190 uint32_t ulARPRemoveCacheEntryByMac( const MACAddress_t * pxMACAddress )
193 uint32_t lResult = 0;
195 /* For each entry in the ARP cache table. */
196 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
198 if( ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
200 lResult = xARPCache[ x ].ulIPAddress;
201 memset( &xARPCache[ x ], '\0', sizeof( xARPCache[ x ] ) );
209 #endif /* ipconfigUSE_ARP_REMOVE_ENTRY != 0 */
210 /*-----------------------------------------------------------*/
212 void vARPRefreshCacheEntry( const MACAddress_t * pxMACAddress, const uint32_t ulIPAddress )
215 BaseType_t xIpEntry = -1;
216 BaseType_t xMacEntry = -1;
217 BaseType_t xUseEntry = 0;
218 uint8_t ucMinAgeFound = 0U;
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 ) )
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. */
236 /* Start with the maximum possible number. */
239 /* For each entry in the ARP cache table. */
240 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
242 /* Does this line in the cache table hold an entry for the IP
243 address being queried? */
244 if( xARPCache[ x ].ulIPAddress == ulIPAddress )
246 if( pxMACAddress == NULL )
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". */
255 /* See if the MAC-address also matches. */
256 if( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 )
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
262 xARPCache[ x ].ucAge = ( uint8_t ) ipconfigMAX_ARP_AGE;
263 xARPCache[ x ].ucValid = ( uint8_t ) pdTRUE;
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
274 else if( ( pxMACAddress != NULL ) && ( memcmp( xARPCache[ x ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) ) == 0 ) )
276 /* Found an entry with the given MAC-address, but the IP-address
277 is different. Continue looping to find a possible match with
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
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 ] )
295 Shouldn't we test for xARPCache[ x ].ucValid == pdFALSE here ? */
296 else if( xARPCache[ x ].ucAge < ucMinAgeFound )
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;
309 xUseEntry = xMacEntry;
313 /* Both the MAC address as well as the IP address were found in
314 different locations: clear the entry which matches the
316 memset( &xARPCache[ xIpEntry ], '\0', sizeof( xARPCache[ xIpEntry ] ) );
319 else if( xIpEntry >= 0 )
321 /* An entry containing the IP-address was found, but it had a different MAC address */
322 xUseEntry = xIpEntry;
325 /* If the entry was not found, we use the oldest entry and set the IPaddress */
326 xARPCache[ xUseEntry ].ulIPAddress = ulIPAddress;
328 if( pxMACAddress != NULL )
330 memcpy( xARPCache[ xUseEntry ].xMACAddress.ucBytes, pxMACAddress->ucBytes, sizeof( pxMACAddress->ucBytes ) );
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;
337 else if( xIpEntry < 0 )
339 xARPCache[ xUseEntry ].ucAge = ( uint8_t ) ipconfigMAX_ARP_RETRANSMISSIONS;
340 xARPCache[ xUseEntry ].ucValid = ( uint8_t ) pdFALSE;
344 /*-----------------------------------------------------------*/
346 #if( ipconfigUSE_ARP_REVERSED_LOOKUP == 1 )
347 eARPLookupResult_t eARPGetCacheEntryByMac( MACAddress_t * const pxMACAddress, uint32_t *pulIPAddress )
350 eARPLookupResult_t eReturn = eARPCacheMiss;
352 /* Loop through each entry in the ARP cache. */
353 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
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 )
359 *pulIPAddress = xARPCache[ x ].ulIPAddress;
360 eReturn = eARPCacheHit;
367 #endif /* ipconfigUSE_ARP_REVERSED_LOOKUP */
369 /*-----------------------------------------------------------*/
371 eARPLookupResult_t eARPGetCacheEntry( uint32_t *pulIPAddress, MACAddress_t * const pxMACAddress )
373 eARPLookupResult_t eReturn;
374 uint32_t ulAddressToLookup;
376 #if( ipconfigUSE_LLMNR == 1 )
377 if( *pulIPAddress == ipLLMNR_IP_ADDR ) /* Is in network byte order. */
379 /* The LLMNR IP-address has a fixed virtual MAC address. */
380 memcpy( pxMACAddress->ucBytes, xLLMNR_MacAdress.ucBytes, sizeof( MACAddress_t ) );
381 eReturn = eARPCacheHit;
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? */
388 /* This is a broadcast so uses the broadcast MAC address. */
389 memcpy( pxMACAddress->ucBytes, xBroadcastMACAddress.ucBytes, sizeof( MACAddress_t ) );
390 eReturn = eARPCacheHit;
392 else if( *ipLOCAL_IP_ADDRESS_POINTER == 0UL )
394 /* The IP address has not yet been assigned, so there is nothing that
396 eReturn = eCantSendPacket;
400 eReturn = eARPCacheMiss;
402 if( ( *pulIPAddress & xNetworkAddressing.ulNetMask ) != ( ( *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) )
404 #if( ipconfigARP_STORES_REMOTE_ADDRESSES == 1 )
405 eReturn = prvCacheLookup( *pulIPAddress, pxMACAddress );
407 if( eReturn == eARPCacheHit )
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 */
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 )
420 ulAddressToLookup = xNetworkAddressing.ulGatewayAddress;
424 ulAddressToLookup = *pulIPAddress;
430 /* The IP address is on the local network, so lookup the requested
431 IP address directly. */
432 ulAddressToLookup = *pulIPAddress;
435 if( eReturn == eARPCacheMiss )
437 if( ulAddressToLookup == 0UL )
439 /* The address is not on the local network, and there is not a
441 eReturn = eCantSendPacket;
445 eReturn = prvCacheLookup( ulAddressToLookup, pxMACAddress );
447 if( eReturn == eARPCacheMiss )
449 /* It might be that the ARP has to go to the gateway. */
450 *pulIPAddress = ulAddressToLookup;
459 /*-----------------------------------------------------------*/
461 static eARPLookupResult_t prvCacheLookup( uint32_t ulAddressToLookup, MACAddress_t * const pxMACAddress )
464 eARPLookupResult_t eReturn = eARPCacheMiss;
466 /* Loop through each entry in the ARP cache. */
467 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
469 /* Does this row in the ARP cache table hold an entry for the IP address
471 if( xARPCache[ x ].ulIPAddress == ulAddressToLookup )
473 /* A matching valid entry was found. */
474 if( xARPCache[ x ].ucValid == ( uint8_t ) pdFALSE )
476 /* This entry is waiting an ARP reply, so is not valid. */
477 eReturn = eCantSendPacket;
481 /* A valid entry was found. */
482 memcpy( pxMACAddress->ucBytes, xARPCache[ x ].xMACAddress.ucBytes, sizeof( MACAddress_t ) );
483 eReturn = eARPCacheHit;
491 /*-----------------------------------------------------------*/
493 void vARPAgeCache( void )
498 /* Loop through each entry in the ARP cache. */
499 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
501 /* If the entry is valid (its age is greater than zero). */
502 if( xARPCache[ x ].ucAge > 0U )
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 )--;
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 )
512 FreeRTOS_OutputARPRequest( xARPCache[ x ].ulIPAddress );
514 else if( xARPCache[ x ].ucAge <= ( uint8_t ) arpMAX_ARP_AGE_BEFORE_NEW_ARP_REQUEST )
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 );
523 /* The age has just ticked down, with nothing to do. */
526 if( xARPCache[ x ].ucAge == 0u )
528 /* The entry is no longer valid. Wipe it out. */
529 iptraceARP_TABLE_ENTRY_EXPIRED( xARPCache[ x ].ulIPAddress );
530 xARPCache[ x ].ulIPAddress = 0UL;
535 xTimeNow = xTaskGetTickCount ();
537 if( ( xLastGratuitousARPTime == ( TickType_t ) 0 ) || ( ( xTimeNow - xLastGratuitousARPTime ) > ( TickType_t ) arpGRATUITOUS_ARP_PERIOD ) )
539 FreeRTOS_OutputARPRequest( *ipLOCAL_IP_ADDRESS_POINTER );
540 xLastGratuitousARPTime = xTimeNow;
543 /*-----------------------------------------------------------*/
545 void vARPSendGratuitous( void )
547 /* Setting xLastGratuitousARPTime to 0 will force a gratuitous ARP the next
548 time vARPAgeCache() is called. */
549 xLastGratuitousARPTime = ( TickType_t ) 0;
551 /* Let the IP-task call vARPAgeCache(). */
552 xSendEventToIPTask( eARPTimerEvent );
555 /*-----------------------------------------------------------*/
556 void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress )
558 NetworkBufferDescriptor_t *pxNetworkBuffer;
560 /* This is called from the context of the IP event task, so a block time
562 pxNetworkBuffer = pxGetNetworkBufferWithDescriptor( sizeof( ARPPacket_t ), ( TickType_t ) 0 );
564 if( pxNetworkBuffer != NULL )
566 pxNetworkBuffer->ulIPAddress = ulIPAddress;
567 vARPGenerateRequestPacket( pxNetworkBuffer );
569 #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
571 if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
575 for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
577 pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
579 pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
583 if( xIsCallingFromIPTask() != 0 )
585 /* Only the IP-task is allowed to call this function directly. */
586 xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );
590 IPStackEvent_t xSendEvent;
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 )
597 /* Failed to send the message, so release the network buffer. */
598 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
604 void vARPGenerateRequestPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
606 ARPPacket_t *pxARPPacket;
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) );
614 pxARPPacket = ( ARPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
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;
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 );
631 memcpy( ( void* )pxARPPacket->xARPHeader.ucSenderProtocolAddress, ( void* )ipLOCAL_IP_ADDRESS_POINTER, sizeof( pxARPPacket->xARPHeader.ucSenderProtocolAddress ) );
632 pxARPPacket->xARPHeader.ulTargetProtocolAddress = pxNetworkBuffer->ulIPAddress;
634 pxNetworkBuffer->xDataLength = sizeof( ARPPacket_t );
636 iptraceCREATING_ARP_REQUEST( pxNetworkBuffer->ulIPAddress );
638 /*-----------------------------------------------------------*/
640 void FreeRTOS_ClearARP( void )
642 memset( xARPCache, '\0', sizeof( xARPCache ) );
644 /*-----------------------------------------------------------*/
646 #if( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 )
648 void FreeRTOS_PrintARPCache( void )
650 BaseType_t x, xCount = 0;
652 /* Loop through each entry in the ARP cache. */
653 for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ )
655 if( ( xARPCache[ x ].ulIPAddress != 0ul ) && ( xARPCache[ x ].ucAge > 0U ) )
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",
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] ) );
672 FreeRTOS_printf( ( "Arp has %ld entries\n", xCount ) );
675 #endif /* ( ipconfigHAS_PRINTF != 0 ) || ( ipconfigHAS_DEBUG_PRINTF != 0 ) */