1 /*
2 * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3 * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * http://aws.amazon.com/freertos
25 * http://www.FreeRTOS.org
26 */
27
28 /**
29 * @file FreeRTOS_TCP_State_Handling_IPv4.c
30 * @brief Module which handles the TCP protocol state transition for FreeRTOS+TCP.
31 *
32 * Endianness: in this module all ports and IP addresses are stored in
33 * host byte-order, except fields in the IP-packets
34 */
35
36 /* Standard includes. */
37 #include <stdint.h>
38 #include <stdio.h>
39
40 /* FreeRTOS includes. */
41 #include "FreeRTOS.h"
42 #include "task.h"
43 #include "queue.h"
44 #include "semphr.h"
45
46 /* FreeRTOS+TCP includes. */
47 #include "FreeRTOS_IP.h"
48 #include "FreeRTOS_Sockets.h"
49 #include "FreeRTOS_IP_Private.h"
50 #include "FreeRTOS_UDP_IP.h"
51 #include "FreeRTOS_DHCP.h"
52 #include "NetworkInterface.h"
53 #include "NetworkBufferManagement.h"
54 #include "FreeRTOS_ARP.h"
55
56 #include "FreeRTOS_TCP_Reception.h"
57 #include "FreeRTOS_TCP_Transmission.h"
58 #include "FreeRTOS_TCP_State_Handling.h"
59 #include "FreeRTOS_TCP_Utils.h"
60
61 /* Just make sure the contents doesn't get compiled if TCP is not enabled. */
62 /* *INDENT-OFF* */
63 #if( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 )
64 /* *INDENT-ON* */
65
66 /**
67 * @brief Handle 'listen' event on the given socket.
68 *
69 * @param[in] pxSocket The socket on which the listen occurred.
70 * @param[in] pxNetworkBuffer The network buffer carrying the packet.
71 *
72 * @return If a new socket/duplicate socket is created, then the pointer to
73 * that socket is returned or else, a NULL pointer is returned.
74 */
prvHandleListen_IPV4(FreeRTOS_Socket_t * pxSocket,NetworkBufferDescriptor_t * pxNetworkBuffer)75 FreeRTOS_Socket_t * prvHandleListen_IPV4( FreeRTOS_Socket_t * pxSocket,
76 NetworkBufferDescriptor_t * pxNetworkBuffer )
77 {
78 /* Map the ethernet buffer onto a TCPPacket_t struct for easy access to the fields. */
79
80 const TCPPacket_t * pxTCPPacket = NULL;
81 FreeRTOS_Socket_t * pxReturn = NULL;
82 uint32_t ulInitialSequenceNumber = 0U;
83 const NetworkEndPoint_t * pxEndpoint = NULL;
84
85 if( ( pxSocket != NULL ) && ( pxNetworkBuffer != NULL ) )
86 {
87 /* Initialize pointers if inputs are valid. */
88 /* MISRA Ref 11.3.1 [Misaligned access] */
89 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
90 /* coverity[misra_c_2012_rule_11_3_violation] */
91 pxTCPPacket = ( ( const TCPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer );
92 pxEndpoint = pxNetworkBuffer->pxEndPoint;
93 }
94
95 /* Silently discard a SYN packet which was not specifically sent for this node. */
96 if( ( pxEndpoint != NULL ) && ( pxTCPPacket->xIPHeader.ulDestinationIPAddress == pxEndpoint->ipv4_settings.ulIPAddress ) )
97 {
98 /* Assume that a new Initial Sequence Number will be required. Request
99 * it now in order to fail out if necessary. */
100 ulInitialSequenceNumber = ulApplicationGetNextSequenceNumber( pxTCPPacket->xIPHeader.ulDestinationIPAddress,
101 pxSocket->usLocalPort,
102 pxTCPPacket->xIPHeader.ulSourceIPAddress,
103 pxTCPPacket->xTCPHeader.usSourcePort );
104 }
105 else
106 {
107 /* Set the sequence number to 0 to avoid further processing. */
108 ulInitialSequenceNumber = 0U;
109 }
110
111 /* A pure SYN (without ACK) has come in, create a new socket to answer
112 * it. */
113 if( ulInitialSequenceNumber != 0U )
114 {
115 if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )
116 {
117 /* The flag bReuseSocket indicates that the same instance of the
118 * listening socket should be used for the connection. */
119 pxReturn = pxSocket;
120 pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
121 pxSocket->u.xTCP.pxPeerSocket = pxSocket;
122 }
123 else
124 {
125 /* The socket does not have the bReuseSocket flag set meaning create a
126 * new socket when a connection comes in. */
127 pxReturn = NULL;
128
129 if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog )
130 {
131 FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n",
132 pxSocket->usLocalPort,
133 pxSocket->u.xTCP.usChildCount,
134 pxSocket->u.xTCP.usBacklog,
135 ( pxSocket->u.xTCP.usChildCount == 1U ) ? "" : "ren" ) );
136 ( void ) prvTCPSendReset( pxNetworkBuffer );
137 }
138 else
139 {
140 FreeRTOS_Socket_t * pxNewSocket = ( FreeRTOS_Socket_t * )
141 FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
142
143 /* MISRA Ref 11.4.1 [Socket error and integer to pointer conversion] */
144 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
145 /* coverity[misra_c_2012_rule_11_4_violation] */
146 if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) )
147 {
148 FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) );
149 ( void ) prvTCPSendReset( pxNetworkBuffer );
150 }
151 else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE )
152 {
153 /* The socket will be connected immediately, no time for the
154 * owner to setsockopt's, therefore copy properties of the server
155 * socket to the new socket. Only the binding might fail (due to
156 * lack of resources). */
157 pxReturn = pxNewSocket;
158 }
159 else
160 {
161 /* Copying failed somehow. */
162 }
163 }
164 }
165 }
166
167 if( ( ulInitialSequenceNumber != 0U ) && ( pxReturn != NULL ) )
168 {
169 size_t xCopyLength;
170
171 /* Map the byte stream onto the ProtocolHeaders_t for easy access to the fields. */
172
173 /* MISRA Ref 11.3.1 [Misaligned access] */
174 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
175 /* coverity[misra_c_2012_rule_11_3_violation] */
176 const ProtocolHeaders_t * pxProtocolHeaders = ( ( const ProtocolHeaders_t * )
177 &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) ] ) );
178
179 /* The endpoint in network buffer must be valid in this condition. */
180 pxReturn->pxEndPoint = pxNetworkBuffer->pxEndPoint;
181 pxReturn->bits.bIsIPv6 = pdFALSE_UNSIGNED;
182 pxReturn->u.xTCP.usRemotePort = FreeRTOS_htons( pxTCPPacket->xTCPHeader.usSourcePort );
183 pxReturn->u.xTCP.xRemoteIP.ulIP_IPv4 = FreeRTOS_htonl( pxTCPPacket->xIPHeader.ulSourceIPAddress );
184 pxReturn->u.xTCP.xTCPWindow.ulOurSequenceNumber = ulInitialSequenceNumber;
185
186 /* Here is the SYN action. */
187 pxReturn->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber = FreeRTOS_ntohl( pxProtocolHeaders->xTCPHeader.ulSequenceNumber );
188 prvSocketSetMSS( pxReturn );
189
190 prvTCPCreateWindow( pxReturn );
191
192 vTCPStateChange( pxReturn, eSYN_FIRST );
193
194 /* Make a copy of the header up to the TCP header. It is needed later
195 * on, whenever data must be sent to the peer. */
196 if( pxNetworkBuffer->xDataLength > sizeof( pxReturn->u.xTCP.xPacket.u.ucLastPacket ) )
197 {
198 xCopyLength = sizeof( pxReturn->u.xTCP.xPacket.u.ucLastPacket );
199 }
200 else
201 {
202 xCopyLength = pxNetworkBuffer->xDataLength;
203 }
204
205 ( void ) memcpy( ( void * ) pxReturn->u.xTCP.xPacket.u.ucLastPacket,
206 ( const void * ) pxNetworkBuffer->pucEthernetBuffer,
207 xCopyLength );
208 }
209
210 return pxReturn;
211 }
212 /*-----------------------------------------------------------*/
213
214 /* *INDENT-OFF* */
215 #endif /* ( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
216 /* *INDENT-ON* */
217