xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_TCP_IP_IPv4.c (revision 67b9e1c2813792625500f9e8c091822b325f3fe7)
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_IPv4.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_IPv4 != 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_IPV4(NetworkBufferDescriptor_t * pxDescriptor)99 BaseType_t xProcessReceivedTCPPacket_IPV4( NetworkBufferDescriptor_t * pxDescriptor )
100 {
101     /* Function might modify the parameter. */
102     NetworkBufferDescriptor_t * pxNetworkBuffer = pxDescriptor;
103     const ProtocolHeaders_t * pxProtocolHeaders;
104     FreeRTOS_Socket_t * pxSocket;
105     uint16_t ucTCPFlags;
106     uint32_t ulLocalIP;
107     uint16_t usLocalPort;
108     uint16_t usRemotePort;
109     IPv46_Address_t xRemoteIP;
110     uint32_t ulSequenceNumber;
111     uint32_t ulAckNumber;
112     BaseType_t xResult = pdPASS;
113 
114     const IPHeader_t * pxIPHeader;
115 
116     /* MISRA Ref 11.3.1 [Misaligned access] */
117     /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
118     /* coverity[misra_c_2012_rule_11_3_violation] */
119     pxProtocolHeaders = ( ( ProtocolHeaders_t * )
120                           &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) ] ) );
121 
122     ucTCPFlags = pxProtocolHeaders->xTCPHeader.ucTCPFlags;
123     usLocalPort = FreeRTOS_htons( pxProtocolHeaders->xTCPHeader.usDestinationPort );
124     usRemotePort = FreeRTOS_htons( pxProtocolHeaders->xTCPHeader.usSourcePort );
125     ulSequenceNumber = FreeRTOS_ntohl( pxProtocolHeaders->xTCPHeader.ulSequenceNumber );
126     ulAckNumber = FreeRTOS_ntohl( pxProtocolHeaders->xTCPHeader.ulAckNr );
127 
128     /* Check for a minimum packet size. */
129     if( pxNetworkBuffer->xDataLength < ( ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) + ipSIZE_OF_TCP_HEADER ) )
130     {
131         xResult = pdFAIL;
132     }
133     else
134     {
135         /* Map the ethernet buffer onto the IPHeader_t struct for easy access to the fields. */
136 
137         /* MISRA Ref 11.3.1 [Misaligned access] */
138         /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
139         /* coverity[misra_c_2012_rule_11_3_violation] */
140         pxIPHeader = ( ( const IPHeader_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] ) );
141         ulLocalIP = FreeRTOS_htonl( pxIPHeader->ulDestinationIPAddress );
142         xRemoteIP.xIs_IPv6 = pdFALSE;
143         xRemoteIP.xIPAddress.ulIP_IPv4 = FreeRTOS_htonl( pxIPHeader->ulSourceIPAddress );
144 
145         /* Find the destination socket, and if not found: return a socket listening to
146          * the destination PORT. */
147         pxSocket = ( FreeRTOS_Socket_t * ) pxTCPSocketLookup( ulLocalIP, usLocalPort, xRemoteIP, usRemotePort );
148 
149         if( ( pxSocket == NULL ) || ( prvTCPSocketIsActive( pxSocket->u.xTCP.eTCPState ) == pdFALSE ) )
150         {
151             /* A TCP messages is received but either there is no socket with the
152              * given port number or the there is a socket, but it is in one of these
153              * non-active states:  eCLOSED, eCLOSE_WAIT, eFIN_WAIT_2, eCLOSING, or
154              * eTIME_WAIT. */
155 
156             FreeRTOS_debug_printf( ( "TCP: No active socket on port %d (%xip:%d)\n", usLocalPort, ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort ) );
157 
158             /* Send a RST to all packets that can not be handled.  As a result
159              * the other party will get a ECONN error.  There are two exceptions:
160              * 1) A packet that already has the RST flag set.
161              * 2) A packet that only has the ACK flag set.
162              * A packet with only the ACK flag set might be the last ACK in
163              * a three-way hand-shake that closes a connection. */
164             if( ( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) != tcpTCP_FLAG_ACK ) &&
165                 ( ( ucTCPFlags & tcpTCP_FLAG_RST ) == 0U ) )
166             {
167                 ( void ) prvTCPSendReset( pxNetworkBuffer );
168             }
169 
170             /* The packet can't be handled. */
171             xResult = pdFAIL;
172         }
173         else
174         {
175             pxSocket->u.xTCP.ucRepCount = 0U;
176 
177             if( pxSocket->u.xTCP.eTCPState == eTCP_LISTEN )
178             {
179                 /* The matching socket is in a listening state.  Test if the peer
180                  * has set the SYN flag. */
181                 if( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) != tcpTCP_FLAG_SYN )
182                 {
183                     /* What happens: maybe after a reboot, a client doesn't know the
184                      * connection had gone.  Send a RST in order to get a new connect
185                      * request. */
186                     #if ( ipconfigHAS_DEBUG_PRINTF == 1 )
187                         {
188                             FreeRTOS_debug_printf( ( "TCP: Server can't handle flags: %s from %xip:%u to port %u\n",
189                                                      prvTCPFlagMeaning( ( UBaseType_t ) ucTCPFlags ), ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort, usLocalPort ) );
190                         }
191                     #endif /* ipconfigHAS_DEBUG_PRINTF */
192 
193                     if( ( ucTCPFlags & tcpTCP_FLAG_RST ) == 0U )
194                     {
195                         ( void ) prvTCPSendReset( pxNetworkBuffer );
196                     }
197 
198                     xResult = pdFAIL;
199                 }
200                 else
201                 {
202                     /* prvHandleListen() will either return a newly created socket
203                      * (if bReuseSocket is false), otherwise it returns the current
204                      * socket which will later get connected. */
205                     pxSocket = prvHandleListen( pxSocket, pxNetworkBuffer );
206 
207                     if( pxSocket == NULL )
208                     {
209                         xResult = pdFAIL;
210                     }
211                 }
212             } /* if( pxSocket->u.xTCP.eTCPState == eTCP_LISTEN ). */
213             else
214             {
215                 /* This is not a socket in listening mode. Check for the RST
216                  * flag. */
217                 if( ( ucTCPFlags & tcpTCP_FLAG_RST ) != 0U )
218                 {
219                     FreeRTOS_debug_printf( ( "TCP: RST received from %xip:%u for %u\n", ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort, usLocalPort ) );
220 
221                     /* Implement https://tools.ietf.org/html/rfc5961#section-3.2. */
222                     if( pxSocket->u.xTCP.eTCPState == eCONNECT_SYN )
223                     {
224                         /* Per the above RFC, "In the SYN-SENT state ... the RST is
225                          * acceptable if the ACK field acknowledges the SYN." */
226                         if( ulAckNumber == ( pxSocket->u.xTCP.xTCPWindow.ulOurSequenceNumber + 1U ) )
227                         {
228                             vTCPStateChange( pxSocket, eCLOSED );
229                         }
230                     }
231                     else
232                     {
233                         /* Check whether the packet matches the next expected sequence number. */
234                         if( ulSequenceNumber == pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber )
235                         {
236                             vTCPStateChange( pxSocket, eCLOSED );
237                         }
238                         /* Otherwise, check whether the packet is within the receive window. */
239                         else if( ( xSequenceGreaterThan( ulSequenceNumber, pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber ) != pdFALSE ) &&
240                                  ( xSequenceLessThan( ulSequenceNumber, pxSocket->u.xTCP.xTCPWindow.rx.ulCurrentSequenceNumber +
241                                                       pxSocket->u.xTCP.xTCPWindow.xSize.ulRxWindowLength ) != pdFALSE ) )
242                         {
243                             /* Send a challenge ACK. */
244                             ( void ) prvTCPSendChallengeAck( pxNetworkBuffer );
245                         }
246                         else
247                         {
248                             /* Nothing. */
249                         }
250                     }
251 
252                     /* Otherwise, do nothing. In any case, the packet cannot be handled. */
253                     xResult = pdFAIL;
254                 }
255                 /* Check whether there is a pure SYN amongst the TCP flags while the connection is established. */
256                 else if( ( ( ucTCPFlags & tcpTCP_FLAG_CTRL ) == tcpTCP_FLAG_SYN ) && ( pxSocket->u.xTCP.eTCPState >= eESTABLISHED ) )
257                 {
258                     /* SYN flag while this socket is already connected. */
259                     FreeRTOS_debug_printf( ( "TCP: SYN unexpected from %xip:%u\n", ( unsigned ) xRemoteIP.xIPAddress.ulIP_IPv4, usRemotePort ) );
260 
261                     /* The packet cannot be handled. */
262                     xResult = pdFAIL;
263                 }
264                 else
265                 {
266                     /* Update the copy of the TCP header only (skipping eth and IP
267                      * headers).  It might be used later on, whenever data must be sent
268                      * to the peer. */
269                     const size_t uxOffset = ipSIZE_OF_ETH_HEADER + uxIPHeaderSizeSocket( pxSocket );
270                     ( void ) memcpy( ( void * ) ( &( pxSocket->u.xTCP.xPacket.u.ucLastPacket[ uxOffset ] ) ),
271                                      ( const void * ) ( &( pxNetworkBuffer->pucEthernetBuffer[ uxOffset ] ) ),
272                                      ipSIZE_OF_TCP_HEADER );
273                     /* Clear flags that are set by the peer, and set the ACK flag. */
274                     pxSocket->u.xTCP.xPacket.u.ucLastPacket[ uxOffset + ipTCP_FLAGS_OFFSET ] = tcpTCP_FLAG_ACK;
275                 }
276             }
277         }
278 
279         if( xResult != pdFAIL )
280         {
281             uint16_t usWindow;
282 
283             /* pxSocket is not NULL when xResult != pdFAIL. */
284             configASSERT( pxSocket != NULL ); /* LCOV_EXCL_LINE ,this branch will not be hit*/
285 
286             /* Touch the alive timers because we received a message for this
287              * socket. */
288             prvTCPTouchSocket( pxSocket );
289 
290             /* Parse the TCP option(s), if present. */
291 
292             /* _HT_ : if we're in the SYN phase, and peer does not send a MSS option,
293              * then we MUST assume an MSS size of 536 bytes for backward compatibility. */
294 
295             /* When there are no TCP options, the TCP offset equals 20 bytes, which is stored as
296              * the number 5 (words) in the higher nibble of the TCP-offset byte. */
297             if( ( pxProtocolHeaders->xTCPHeader.ucTCPOffset & tcpTCP_OFFSET_LENGTH_BITS ) > tcpTCP_OFFSET_STANDARD_LENGTH )
298             {
299                 xResult = prvCheckOptions( pxSocket, pxNetworkBuffer );
300             }
301 
302             if( xResult != pdFAIL )
303             {
304                 usWindow = FreeRTOS_ntohs( pxProtocolHeaders->xTCPHeader.usWindow );
305                 pxSocket->u.xTCP.ulWindowSize = ( uint32_t ) usWindow;
306                 #if ( ipconfigUSE_TCP_WIN == 1 )
307                     {
308                         /* rfc1323 : The Window field in a SYN (i.e., a <SYN> or <SYN,ACK>)
309                          * segment itself is never scaled. */
310                         if( ( ucTCPFlags & ( uint8_t ) tcpTCP_FLAG_SYN ) == 0U )
311                         {
312                             pxSocket->u.xTCP.ulWindowSize =
313                                 ( pxSocket->u.xTCP.ulWindowSize << pxSocket->u.xTCP.ucPeerWinScaleFactor );
314                         }
315                     }
316                 #endif /* ipconfigUSE_TCP_WIN */
317 
318                 /* In prvTCPHandleState() the incoming messages will be handled
319                  * depending on the current state of the connection. */
320                 if( prvTCPHandleState( pxSocket, &pxNetworkBuffer ) > 0 )
321                 {
322                     /* prvTCPHandleState() has sent a message, see if there are more to
323                      * be transmitted. */
324                     #if ( ipconfigUSE_TCP_WIN == 1 )
325                         {
326                             ( void ) prvTCPSendRepeated( pxSocket, &pxNetworkBuffer );
327                         }
328                     #endif /* ipconfigUSE_TCP_WIN */
329                 }
330 
331                 if( pxNetworkBuffer != NULL )
332                 {
333                     /* We must check if the buffer is unequal to NULL, because the
334                      * socket might keep a reference to it in case a delayed ACK must be
335                      * sent. */
336                     vReleaseNetworkBufferAndDescriptor( pxNetworkBuffer );
337                     #ifndef _lint
338                         /* Clear pointers that are freed. */
339                         pxNetworkBuffer = NULL;
340                     #endif
341                 }
342 
343                 /* And finally, calculate when this socket wants to be woken up. */
344                 ( void ) prvTCPNextTimeout( pxSocket );
345             }
346         }
347     }
348 
349     /* pdPASS being returned means the buffer has been consumed. */
350     return xResult;
351 }
352 /*-----------------------------------------------------------*/
353 
354 /* *INDENT-OFF* */
355 #endif /* ( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
356 /* *INDENT-ON* */
357 
358 /* Provide access to private members for testing. */
359 #ifdef FREERTOS_ENABLE_UNIT_TESTS
360     #include "freertos_tcp_test_access_tcp_define.h"
361 #endif
362 
363 /* Provide access to private members for verification. */
364 #ifdef FREERTOS_TCP_ENABLE_VERIFICATION
365     #include "aws_freertos_tcp_verification_access_tcp_define.h"
366 #endif
367