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_IP_IPv6.c
30 * @brief Module which handles the TCP connections for FreeRTOS+TCP.
31 * It depends on FreeRTOS_TCP_WIN.c, which handles the TCP windowing
32 * schemes.
33 *
34 * Endianness: in this module all ports and IP addresses are stored in
35 * host byte-order, except fields in the IP-packets
36 */
37
38 /* Standard includes. */
39 #include <stdint.h>
40 #include <stdio.h>
41
42 /* FreeRTOS includes. */
43 #include "FreeRTOS.h"
44 #include "task.h"
45 #include "queue.h"
46 #include "semphr.h"
47
48 /* FreeRTOS+TCP includes. */
49 #include "FreeRTOS_IP.h"
50 #include "FreeRTOS_Sockets.h"
51 #include "FreeRTOS_IP_Private.h"
52 #include "FreeRTOS_UDP_IP.h"
53 #include "FreeRTOS_DHCP.h"
54 #include "NetworkInterface.h"
55 #include "NetworkBufferManagement.h"
56 #include "FreeRTOS_ARP.h"
57
58 #include "FreeRTOS_TCP_Reception.h"
59 #include "FreeRTOS_TCP_Transmission.h"
60 #include "FreeRTOS_TCP_State_Handling.h"
61 #include "FreeRTOS_TCP_Utils.h"
62
63
64 /* Just make sure the contents doesn't get compiled if TCP is not enabled. */
65 /* *INDENT-OFF* */
66 #if( ipconfigUSE_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 )
67 /* *INDENT-ON* */
68
69 #if ( ipconfigHAS_DEBUG_PRINTF != 0 )
70
71 /*
72 * For logging and debugging: make a string showing the TCP flags.
73 */
74 const char * prvTCPFlagMeaning( UBaseType_t xFlags );
75 #endif /* ipconfigHAS_DEBUG_PRINTF != 0 */
76
77
78 /*-----------------------------------------------------------*/
79
80
81 /**
82 * @brief Process the received TCP packet.
83 *
84 * @param[in] pxDescriptor The descriptor in which the TCP packet is held.
85 *
86 * @return If the processing of the packet was successful, then pdPASS is returned
87 * or else pdFAIL.
88 *
89 * @note FreeRTOS_TCP_IP has only 2 public functions, this is the second one:
90 * xProcessReceivedTCPPacket()
91 * prvTCPHandleState()
92 * prvTCPPrepareSend()
93 * prvTCPReturnPacket()
94 * xNetworkInterfaceOutput() // Sends data to the NIC
95 * prvTCPSendRepeated()
96 * prvTCPReturnPacket() // Prepare for returning
97 * xNetworkInterfaceOutput() // Sends data to the NIC
98 */
xProcessReceivedTCPPacket_IPV6(NetworkBufferDescriptor_t * pxDescriptor)99 BaseType_t xProcessReceivedTCPPacket_IPV6( NetworkBufferDescriptor_t * pxDescriptor )
100 {
101 /* Function might modify the parameter. */
102 NetworkBufferDescriptor_t * pxNetworkBuffer = pxDescriptor;
103
104 configASSERT( pxNetworkBuffer != NULL );
105 configASSERT( pxNetworkBuffer->pucEthernetBuffer != NULL );
106
107 /* Map the buffer onto a ProtocolHeaders_t struct for easy access to the fields. */
108
109 /* MISRA Ref 11.3.1 [Misaligned access] */
110 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
111 /* coverity[misra_c_2012_rule_11_3_violation] */
112 const ProtocolHeaders_t * pxProtocolHeaders = ( ( const ProtocolHeaders_t * )
113 &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) ] ) );
114 FreeRTOS_Socket_t * pxSocket;
115 uint16_t ucTCPFlags = pxProtocolHeaders->xTCPHeader.ucTCPFlags;
116 uint16_t usLocalPort = FreeRTOS_htons( pxProtocolHeaders->xTCPHeader.usDestinationPort );
117 uint16_t usRemotePort = FreeRTOS_htons( pxProtocolHeaders->xTCPHeader.usSourcePort );
118 IPv46_Address_t xRemoteIP;
119 uint32_t ulSequenceNumber = FreeRTOS_ntohl( pxProtocolHeaders->xTCPHeader.ulSequenceNumber );
120 uint32_t ulAckNumber = FreeRTOS_ntohl( pxProtocolHeaders->xTCPHeader.ulAckNr );
121 BaseType_t xResult = pdPASS;
122
123 /* Check for a minimum packet size. */
124 if( pxNetworkBuffer->xDataLength < ( ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) + ipSIZE_OF_TCP_HEADER ) )
125 {
126 xResult = pdFAIL;
127 }
128 else
129 {
130 /* Map the ethernet buffer onto the IPHeader_t struct for easy access to the fields. */
131
132 /* MISRA Ref 11.3.1 [Misaligned access] */
133 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
134 /* coverity[misra_c_2012_rule_11_3_violation] */
135 const IPHeader_IPv6_t * pxIPHeader_IPv6 = ( ( IPHeader_IPv6_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] ) );
136 ( void ) memcpy( xRemoteIP.xIPAddress.xIP_IPv6.ucBytes, pxIPHeader_IPv6->xSourceAddress.ucBytes, sizeof( IPv6_Address_t ) );
137
138 /* Find the destination socket, and if not found: return a socket listing to
139 * the destination PORT. */
140 pxSocket = ( FreeRTOS_Socket_t * ) pxTCPSocketLookup( 0U, usLocalPort, xRemoteIP, usRemotePort );
141
142 if( ( pxSocket == NULL ) || ( prvTCPSocketIsActive( pxSocket->u.xTCP.eTCPState ) == pdFALSE ) )
143 {
144 /* A TCP messages is received but either there is no socket with the
145 * given port number or the there is a socket, but it is in one of these
146 * non-active states: eCLOSED, eCLOSE_WAIT, eFIN_WAIT_2, eCLOSING, or
147 * eTIME_WAIT. */
148
149 FreeRTOS_debug_printf( ( "TCP: No active socket on port %d (%xip:%d)\n", usLocalPort, ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort ) );
150
151 /* Send a RST to all packets that can not be handled. As a result
152 * the other party will get a ECONN error. There are two exceptions:
153 * 1) A packet that already has the RST flag set.
154 * 2) A packet that only has the ACK flag set.
155 * A packet with only the ACK flag set might be the last ACK in
156 * a three-way hand-shake that closes a connection. */
157 if( ( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) != tcpTCP_FLAG_ACK ) &&
158 ( ( ucTCPFlags & tcpTCP_FLAG_RST ) == 0U ) )
159 {
160 ( void ) prvTCPSendReset( pxNetworkBuffer );
161 }
162
163 /* The packet can't be handled. */
164 xResult = pdFAIL;
165 }
166 else
167 {
168 pxSocket->u.xTCP.ucRepCount = 0U;
169
170 if( pxSocket->u.xTCP.eTCPState == eTCP_LISTEN )
171 {
172 /* The matching socket is in a listening state. Test if the peer
173 * has set the SYN flag. */
174 if( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) != tcpTCP_FLAG_SYN )
175 {
176 /* What happens: maybe after a reboot, a client doesn't know the
177 * connection had gone. Send a RST in order to get a new connect
178 * request. */
179 #if ( ipconfigHAS_DEBUG_PRINTF == 1 )
180 {
181 FreeRTOS_debug_printf( ( "TCP: Server can't handle flags: %s from %xip:%u to port %u\n",
182 prvTCPFlagMeaning( ( UBaseType_t ) ucTCPFlags ), ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort, usLocalPort ) );
183 }
184 #endif /* ipconfigHAS_DEBUG_PRINTF */
185
186 if( ( ucTCPFlags & tcpTCP_FLAG_RST ) == 0U )
187 {
188 ( void ) prvTCPSendReset( pxNetworkBuffer );
189 }
190
191 xResult = pdFAIL;
192 }
193 else
194 {
195 /* prvHandleListen() will either return a newly created socket
196 * (if bReuseSocket is false), otherwise it returns the current
197 * socket which will later get connected. */
198 pxSocket = prvHandleListen( pxSocket, pxNetworkBuffer );
199
200 if( pxSocket == NULL )
201 {
202 xResult = pdFAIL;
203 }
204 }
205 } /* if( pxSocket->u.xTCP.eTCPState == eTCP_LISTEN ). */
206 else
207 {
208 /* This is not a socket in listening mode. Check for the RST
209 * flag. */
210 if( ( ucTCPFlags & tcpTCP_FLAG_RST ) != 0U )
211 {
212 FreeRTOS_debug_printf( ( "TCP: RST received from %xip:%u for %u\n", ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort, usLocalPort ) );
213
214 /* Implement https://tools.ietf.org/html/rfc5961#section-3.2. */
215 if( pxSocket->u.xTCP.eTCPState == eCONNECT_SYN )
216 {
217 /* Per the above RFC, "In the SYN-SENT state ... the RST is
218 * acceptable if the ACK field acknowledges the SYN." */
219 if( ulAckNumber == ( pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber + 1U ) )
220 {
221 vTCPStateChange( pxSocket, eCLOSED );
222 }
223 }
224 else
225 {
226 /* Check whether the packet matches the next expected sequence number. */
227 if( ulSequenceNumber == pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber )
228 {
229 vTCPStateChange( pxSocket, eCLOSED );
230 }
231 /* Otherwise, check whether the packet is within the receive window. */
232 else if( ( xSequenceGreaterThan( ulSequenceNumber, pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber ) != pdFALSE ) &&
233 ( xSequenceLessThan( ulSequenceNumber, pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber +
234 pxSocket->u.xTCP.xTCPWindow.xSize.ulRxWindowLength ) != pdFALSE ) )
235 {
236 /* Send a challenge ACK. */
237 ( void ) prvTCPSendChallengeAck( pxNetworkBuffer );
238 }
239 else
240 {
241 /* Nothing. */
242 }
243 }
244
245 /* Otherwise, do nothing. In any case, the packet cannot be handled. */
246 xResult = pdFAIL;
247 }
248 /* Check whether there is a pure SYN amongst the TCP flags while the connection is established. */
249 else if( ( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) == tcpTCP_FLAG_SYN ) && ( pxSocket->u.xTCP.eTCPState >= eESTABLISHED ) )
250 {
251 /* SYN flag while this socket is already connected. */
252 FreeRTOS_debug_printf( ( "TCP: SYN unexpected from %xip:%u\n", ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort ) );
253
254 /* The packet cannot be handled. */
255 xResult = pdFAIL;
256 }
257 else
258 {
259 /* Update the copy of the TCP header only (skipping eth and IP
260 * headers). It might be used later on, whenever data must be sent
261 * to the peer. */
262 const size_t uxOffset = ipSIZE_OF_ETH_HEADER + uxIPHeaderSizeSocket( pxSocket );
263 ( void ) memcpy( ( void * ) ( &( pxSocket->u.xTCP.xPacket.u.ucLastPacket[ uxOffset ] ) ),
264 ( const void * ) ( &( pxNetworkBuffer->pucEthernetBuffer[ uxOffset ] ) ),
265 ipSIZE_OF_TCP_HEADER );
266 /* Clear flags that are set by the peer, and set the ACK flag. */
267 pxSocket->u.xTCP.xPacket.u.ucLastPacket[ uxOffset + ipTCP_FLAGS_OFFSET ] = tcpTCP_FLAG_ACK;
268 }
269 }
270 }
271
272 if( xResult != pdFAIL )
273 {
274 uint16_t usWindow;
275
276 /* pxSocket is not NULL when xResult != pdFAIL. */
277 configASSERT( pxSocket != NULL ); /* LCOV_EXCL_LINE ,this branch will not be hit*/
278
279 /* Touch the alive timers because we received a message for this
280 * socket. */
281 prvTCPTouchSocket( pxSocket );
282
283 /* Parse the TCP option(s), if present. */
284
285 /* _HT_ : if we're in the SYN phase, and peer does not send a MSS option,
286 * then we MUST assume an MSS size of 536 bytes for backward compatibility. */
287
288 /* When there are no TCP options, the TCP offset equals 20 bytes, which is stored as
289 * the number 5 (words) in the higher nibble of the TCP-offset byte. */
290 if( ( pxProtocolHeaders->xTCPHeader.ucTCPOffset & tcpTCP_OFFSET_LENGTH_BITS ) > tcpTCP_OFFSET_STANDARD_LENGTH )
291 {
292 xResult = prvCheckOptions( pxSocket, pxNetworkBuffer );
293 }
294
295 if( xResult != pdFAIL )
296 {
297 usWindow = FreeRTOS_ntohs( pxProtocolHeaders->xTCPHeader.usWindow );
298 pxSocket->u.xTCP.ulWindowSize = ( uint32_t ) usWindow;
299 #if ( ipconfigUSE_TCP_WIN == 1 )
300 {
301 /* rfc1323 : The Window field in a SYN (i.e., a <SYN> or <SYN,ACK>)
302 * segment itself is never scaled. */
303 if( ( ucTCPFlags & ( uint8_t ) tcpTCP_FLAG_SYN ) == 0U )
304 {
305 pxSocket->u.xTCP.ulWindowSize =
306 ( pxSocket->u.xTCP.ulWindowSize << pxSocket->u.xTCP.ucPeerWinScaleFactor );
307 }
308 }
309 #endif /* ipconfigUSE_TCP_WIN */
310
311 /* In prvTCPHandleState() the incoming messages will be handled
312 * depending on the current state of the connection. */
313 if( prvTCPHandleState( pxSocket, &pxNetworkBuffer ) > 0 )
314 {
315 /* prvTCPHandleState() has sent a message, see if there are more to
316 * be transmitted. */
317 #if ( ipconfigUSE_TCP_WIN == 1 )
318 {
319 ( void ) prvTCPSendRepeated( pxSocket, &pxNetworkBuffer );
320 }
321 #endif /* ipconfigUSE_TCP_WIN */
322 }
323
324 if( pxNetworkBuffer != NULL )
325 {
326 /* We must check if the buffer is unequal to NULL, because the
327 * socket might keep a reference to it in case a delayed ACK must be
328 * sent. */
329 vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
330 #ifndef _lint
331 /* Clear pointers that are freed. */
332 pxNetworkBuffer = NULL;
333 #endif
334 }
335
336 /* And finally, calculate when this socket wants to be woken up. */
337 ( void ) prvTCPNextTimeout( pxSocket );
338 }
339 }
340 }
341
342 /* pdPASS being returned means the buffer has been consumed. */
343 return xResult;
344 }
345 /*-----------------------------------------------------------*/
346
347 /* Provide access to private members for testing. */
348 #ifdef FREERTOS_ENABLE_UNIT_TESTS
349 #include "freertos_tcp_test_access_tcp_define.h"
350 #endif
351
352 /* Provide access to private members for verification. */
353 #ifdef FREERTOS_TCP_ENABLE_VERIFICATION
354 #include "aws_freertos_tcp_verification_access_tcp_define.h"
355 #endif
356
357 /* *INDENT-OFF* */
358 #endif /* ( ipconfigUSE_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
359 /* *INDENT-ON* */
360