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_UDP_IP.h"
41 #include "FreeRTOS_ARP.h"
42 #include "FreeRTOS_DHCP.h"
43 #include "NetworkInterface.h"
44 #include "NetworkBufferManagement.h"
46 #if( ipconfigUSE_DNS == 1 )
47 #include "FreeRTOS_DNS.h"
50 /* The expected IP version and header length coded into the IP header itself. */
51 #define ipIP_VERSION_AND_HEADER_LENGTH_BYTE ( ( uint8_t ) 0x45 )
53 /* Part of the Ethernet and IP headers are always constant when sending an IPv4
54 UDP packet. This array defines the constant parts, allowing this part of the
55 packet to be filled in using a simple memcpy() instead of individual writes. */
56 UDPPacketHeader_t xDefaultPartUDPPacketHeader =
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Ethernet source MAC address. */
61 0x08, 0x00, /* Ethernet frame type. */
62 ipIP_VERSION_AND_HEADER_LENGTH_BYTE, /* ucVersionHeaderLength. */
63 0x00, /* ucDifferentiatedServicesCode. */
64 0x00, 0x00, /* usLength. */
65 0x00, 0x00, /* usIdentification. */
66 0x00, 0x00, /* usFragmentOffset. */
67 ipconfigUDP_TIME_TO_LIVE, /* ucTimeToLive */
68 ipPROTOCOL_UDP, /* ucProtocol. */
69 0x00, 0x00, /* usHeaderChecksum. */
70 0x00, 0x00, 0x00, 0x00 /* Source IP address. */
73 /*-----------------------------------------------------------*/
75 void vProcessGeneratedUDPPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer )
77 UDPPacket_t *pxUDPPacket;
78 IPHeader_t *pxIPHeader;
79 eARPLookupResult_t eReturned;
80 uint32_t ulIPAddress = pxNetworkBuffer->ulIPAddress;
83 /* Map the UDP packet onto the start of the frame. */
84 pxUDPPacket = ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer;
86 #if ipconfigSUPPORT_OUTGOING_PINGS == 1
87 if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )
89 uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( ICMPPacket_t );
94 uxPayloadSize = pxNetworkBuffer->xDataLength - sizeof( UDPPacket_t );
97 /* Determine the ARP cache status for the requested IP address. */
98 eReturned = eARPGetCacheEntry( &( ulIPAddress ), &( pxUDPPacket->xEthernetHeader.xDestinationAddress ) );
100 if( eReturned != eCantSendPacket )
102 if( eReturned == eARPCacheHit )
104 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
105 uint8_t ucSocketOptions;
107 iptraceSENDING_UDP_PACKET( pxNetworkBuffer->ulIPAddress );
109 /* Create short cuts to the data within the packet. */
110 pxIPHeader = &( pxUDPPacket->xIPHeader );
112 #if ( ipconfigSUPPORT_OUTGOING_PINGS == 1 )
113 /* Is it possible that the packet is not actually a UDP packet
114 after all, but an ICMP packet. */
115 if( pxNetworkBuffer->usPort != ipPACKET_CONTAINS_ICMP_DATA )
116 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
118 UDPHeader_t *pxUDPHeader;
120 pxUDPHeader = &( pxUDPPacket->xUDPHeader );
122 pxUDPHeader->usDestinationPort = pxNetworkBuffer->usPort;
123 pxUDPHeader->usSourcePort = pxNetworkBuffer->usBoundPort;
124 pxUDPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( UDPHeader_t ) );
125 pxUDPHeader->usLength = FreeRTOS_htons( pxUDPHeader->usLength );
126 pxUDPHeader->usChecksum = 0u;
129 /* memcpy() the constant parts of the header information into
130 the correct location within the packet. This fills in:
131 xEthernetHeader.xSourceAddress
132 xEthernetHeader.usFrameType
133 xIPHeader.ucVersionHeaderLength
134 xIPHeader.ucDifferentiatedServicesCode
136 xIPHeader.usIdentification
137 xIPHeader.usFragmentOffset
138 xIPHeader.ucTimeToLive
141 xIPHeader.usHeaderChecksum
143 /* Save options now, as they will be overwritten by memcpy */
144 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
145 ucSocketOptions = pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ];
148 * Offset the memcpy by the size of a MAC address to start at the packet's
149 * Ethernet header 'source' MAC address; the preceding 'destination' should not be altered.
151 char *pxUdpSrcAddrOffset = ( char *) pxUDPPacket + sizeof( MACAddress_t );
152 memcpy( pxUdpSrcAddrOffset, xDefaultPartUDPPacketHeader.ucBytes, sizeof( xDefaultPartUDPPacketHeader ) );
154 #if ipconfigSUPPORT_OUTGOING_PINGS == 1
155 if( pxNetworkBuffer->usPort == ipPACKET_CONTAINS_ICMP_DATA )
157 pxIPHeader->ucProtocol = ipPROTOCOL_ICMP;
158 pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( ICMPHeader_t ) );
161 #endif /* ipconfigSUPPORT_OUTGOING_PINGS */
163 pxIPHeader->usLength = ( uint16_t ) ( uxPayloadSize + sizeof( IPHeader_t ) + sizeof( UDPHeader_t ) );
166 pxIPHeader->usLength = FreeRTOS_htons( pxIPHeader->usLength );
167 /* HT:endian: changed back to network endian */
168 pxIPHeader->ulDestinationIPAddress = pxNetworkBuffer->ulIPAddress;
170 #if( ipconfigUSE_LLMNR == 1 )
172 /* LLMNR messages are typically used on a LAN and they're
173 * not supposed to cross routers */
174 if( pxNetworkBuffer->ulIPAddress == ipLLMNR_IP_ADDR )
176 pxIPHeader->ucTimeToLive = 0x01;
181 #if( ipconfigDRIVER_INCLUDED_TX_IP_CHECKSUM == 0 )
183 pxIPHeader->usHeaderChecksum = 0u;
184 pxIPHeader->usHeaderChecksum = usGenerateChecksum( 0UL, ( uint8_t * ) &( pxIPHeader->ucVersionHeaderLength ), ipSIZE_OF_IPv4_HEADER );
185 pxIPHeader->usHeaderChecksum = ~FreeRTOS_htons( pxIPHeader->usHeaderChecksum );
187 if( ( ucSocketOptions & ( uint8_t ) FREERTOS_SO_UDPCKSUM_OUT ) != 0u )
189 usGenerateProtocolChecksum( (uint8_t*)pxUDPPacket, pxNetworkBuffer->xDataLength, pdTRUE );
193 pxUDPPacket->xUDPHeader.usChecksum = 0u;
198 else if( eReturned == eARPCacheMiss )
200 /* Add an entry to the ARP table with a null hardware address.
201 This allows the ARP timer to know that an ARP reply is
202 outstanding, and perform retransmissions if necessary. */
203 vARPRefreshCacheEntry( NULL, ulIPAddress );
205 /* Generate an ARP for the required IP address. */
206 iptracePACKET_DROPPED_TO_GENERATE_ARP( pxNetworkBuffer->ulIPAddress );
207 pxNetworkBuffer->ulIPAddress = ulIPAddress;
208 vARPGenerateRequestPacket( pxNetworkBuffer );
212 /* The lookup indicated that an ARP request has already been
213 sent out for the queried IP address. */
214 eReturned = eCantSendPacket;
218 if( eReturned != eCantSendPacket )
220 /* The network driver is responsible for freeing the network buffer
221 after the packet has been sent. */
223 #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
225 if( pxNetworkBuffer->xDataLength < ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES )
229 for( xIndex = ( BaseType_t ) pxNetworkBuffer->xDataLength; xIndex < ( BaseType_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES; xIndex++ )
231 pxNetworkBuffer->pucEthernetBuffer[ xIndex ] = 0u;
233 pxNetworkBuffer->xDataLength = ( size_t ) ipconfigETHERNET_MINIMUM_PACKET_BYTES;
238 xNetworkInterfaceOutput( pxNetworkBuffer, pdTRUE );
242 /* The packet can't be sent (DHCP not completed?). Just drop the
244 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
247 /*-----------------------------------------------------------*/
249 BaseType_t xProcessReceivedUDPPacket( NetworkBufferDescriptor_t *pxNetworkBuffer, uint16_t usPort )
251 BaseType_t xReturn = pdPASS;
252 FreeRTOS_Socket_t *pxSocket;
253 configASSERT(pxNetworkBuffer);
254 configASSERT(pxNetworkBuffer->pucEthernetBuffer);
257 UDPPacket_t *pxUDPPacket = (UDPPacket_t *) pxNetworkBuffer->pucEthernetBuffer;
259 /* Caller must check for minimum packet size. */
260 pxSocket = pxUDPSocketLookup( usPort );
265 /* When refreshing the ARP cache with received UDP packets we must be
266 careful; hundreds of broadcast messages may pass and if we're not
267 handling them, no use to fill the ARP cache with those IP addresses. */
268 vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
270 #if( ipconfigUSE_CALLBACKS == 1 )
272 /* Did the owner of this socket register a reception handler ? */
273 if( ipconfigIS_VALID_PROG_ADDRESS( pxSocket->u.xUDP.pxHandleReceive ) )
275 struct freertos_sockaddr xSourceAddress, destinationAddress;
276 void *pcData = ( void * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipUDP_PAYLOAD_OFFSET_IPv4 ] );
277 FOnUDPReceive_t xHandler = ( FOnUDPReceive_t ) pxSocket->u.xUDP.pxHandleReceive;
278 xSourceAddress.sin_port = pxNetworkBuffer->usPort;
279 xSourceAddress.sin_addr = pxNetworkBuffer->ulIPAddress;
280 destinationAddress.sin_port = usPort;
281 destinationAddress.sin_addr = pxUDPPacket->xIPHeader.ulDestinationIPAddress;
283 if( xHandler( ( Socket_t ) pxSocket, ( void* ) pcData, ( size_t ) pxNetworkBuffer->xDataLength,
284 &xSourceAddress, &destinationAddress ) )
286 xReturn = pdFAIL; /* FAIL means that we did not consume or release the buffer */
290 #endif /* ipconfigUSE_CALLBACKS */
292 #if( ipconfigUDP_MAX_RX_PACKETS > 0 )
294 if( xReturn == pdPASS )
296 if ( listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ) >= pxSocket->u.xUDP.uxMaxPackets )
298 FreeRTOS_debug_printf( ( "xProcessReceivedUDPPacket: buffer full %ld >= %ld port %u\n",
299 listCURRENT_LIST_LENGTH( &( pxSocket->u.xUDP.xWaitingPacketsList ) ),
300 pxSocket->u.xUDP.uxMaxPackets, pxSocket->usLocalPort ) );
301 xReturn = pdFAIL; /* we did not consume or release the buffer */
307 if( xReturn == pdPASS )
311 if( xReturn == pdPASS )
313 taskENTER_CRITICAL();
315 /* Add the network packet to the list of packets to be
316 processed by the socket. */
317 vListInsertEnd( &( pxSocket->u.xUDP.xWaitingPacketsList ), &( pxNetworkBuffer->xBufferListItem ) );
324 /* Set the socket's receive event */
325 if( pxSocket->xEventGroup != NULL )
327 xEventGroupSetBits( pxSocket->xEventGroup, eSOCKET_RECEIVE );
330 #if( ipconfigSUPPORT_SELECT_FUNCTION == 1 )
332 if( ( pxSocket->pxSocketSet != NULL ) && ( ( pxSocket->xSelectBits & eSELECT_READ ) != 0 ) )
334 xEventGroupSetBits( pxSocket->pxSocketSet->xSelectGroup, eSELECT_READ );
339 #if( ipconfigSOCKET_HAS_USER_SEMAPHORE == 1 )
341 if( pxSocket->pxUserSemaphore != NULL )
343 xSemaphoreGive( pxSocket->pxUserSemaphore );
348 #if( ipconfigUSE_DHCP == 1 )
350 if( xIsDHCPSocket( pxSocket ) )
352 xSendEventToIPTask( eDHCPEvent );
360 /* There is no socket listening to the target port, but still it might
363 #if( ipconfigUSE_DNS == 1 ) && ( ipconfigDNS_USE_CALLBACKS == 1 )
364 /* A DNS reply, check for the source port. Although the DNS client
365 does open a UDP socket to send a messages, this socket will be
366 closed after a short timeout. Messages that come late (after the
367 socket is closed) will be treated here. */
368 if( FreeRTOS_ntohs( pxUDPPacket->xUDPHeader.usSourcePort ) == ipDNS_PORT )
370 vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
371 xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );
376 #if( ipconfigUSE_LLMNR == 1 )
377 /* a LLMNR request, check for the destination port. */
378 if( ( usPort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) ||
379 ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipLLMNR_PORT ) ) )
381 vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
382 xReturn = ( BaseType_t )ulDNSHandlePacket( pxNetworkBuffer );
385 #endif /* ipconfigUSE_LLMNR */
387 #if( ipconfigUSE_NBNS == 1 )
388 /* a NetBIOS request, check for the destination port */
389 if( ( usPort == FreeRTOS_ntohs( ipNBNS_PORT ) ) ||
390 ( pxUDPPacket->xUDPHeader.usSourcePort == FreeRTOS_ntohs( ipNBNS_PORT ) ) )
392 vARPRefreshCacheEntry( &( pxUDPPacket->xEthernetHeader.xSourceAddress ), pxUDPPacket->xIPHeader.ulSourceIPAddress );
393 xReturn = ( BaseType_t )ulNBNSHandlePacket( pxNetworkBuffer );
396 #endif /* ipconfigUSE_NBNS */
404 /*-----------------------------------------------------------*/