xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_TCP_State_Handling_IPv6.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_State_Handling_IPv6.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_IPv6 != 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_IPV6(FreeRTOS_Socket_t * pxSocket,NetworkBufferDescriptor_t * pxNetworkBuffer)75 FreeRTOS_Socket_t * prvHandleListen_IPV6( FreeRTOS_Socket_t * pxSocket,
76                                           NetworkBufferDescriptor_t * pxNetworkBuffer )
77 {
78     const TCPPacket_IPv6_t * pxTCPPacket = NULL;
79     FreeRTOS_Socket_t * pxReturn = NULL;
80     uint32_t ulInitialSequenceNumber = 0;
81     BaseType_t xHasSequence = pdFALSE;
82 
83     if( ( pxSocket != NULL ) && ( pxNetworkBuffer != NULL ) )
84     {
85         /* Map the ethernet buffer onto a TCPPacket_IPv6_t struct for easy access to the fields. */
86 
87         /* MISRA Ref 11.3.1 [Misaligned access] */
88         /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
89         /* coverity[misra_c_2012_rule_11_3_violation] */
90         pxTCPPacket = ( ( const TCPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer );
91 
92         configASSERT( pxNetworkBuffer->pxEndPoint != NULL );
93 
94         /* Silently discard a SYN packet which was not specifically sent for this node. */
95         if( memcmp( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, pxNetworkBuffer->pxEndPoint->ipv6_settings.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS ) == 0 )
96         {
97             /* Assume that a new Initial Sequence Number will be required. Request
98              * it now in order to fail out if necessary. */
99             if( xApplicationGetRandomNumber( &ulInitialSequenceNumber ) == pdPASS )
100             {
101                 xHasSequence = pdTRUE;
102             }
103         }
104     }
105 
106     /* A pure SYN (without ACK) has come in, create a new socket to answer
107      * it. */
108     if( xHasSequence != pdFALSE )
109     {
110         if( pxSocket->u.xTCP.bits.bReuseSocket != pdFALSE_UNSIGNED )
111         {
112             /* The flag bReuseSocket indicates that the same instance of the
113              * listening socket should be used for the connection. */
114             pxReturn = pxSocket;
115             pxSocket->u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
116             pxSocket->u.xTCP.pxPeerSocket = pxSocket;
117         }
118         else
119         {
120             /* The socket does not have the bReuseSocket flag set meaning create a
121              * new socket when a connection comes in. */
122             pxReturn = NULL;
123 
124             if( pxSocket->u.xTCP.usChildCount >= pxSocket->u.xTCP.usBacklog )
125             {
126                 FreeRTOS_printf( ( "Check: Socket %u already has %u / %u child%s\n",
127                                    pxSocket->usLocalPort,
128                                    pxSocket->u.xTCP.usChildCount,
129                                    pxSocket->u.xTCP.usBacklog,
130                                    ( pxSocket->u.xTCP.usChildCount == 1U ) ? "" : "ren" ) );
131                 ( void ) prvTCPSendReset( pxNetworkBuffer );
132             }
133             else
134             {
135                 FreeRTOS_Socket_t * pxNewSocket = ( FreeRTOS_Socket_t * )
136                                                   FreeRTOS_socket( FREERTOS_AF_INET6, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
137 
138                 /* MISRA Ref 11.4.1 [Socket error and integer to pointer conversion] */
139                 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-114 */
140                 /* coverity[misra_c_2012_rule_11_4_violation] */
141                 if( ( pxNewSocket == NULL ) || ( pxNewSocket == FREERTOS_INVALID_SOCKET ) )
142                 {
143                     FreeRTOS_debug_printf( ( "TCP: Listen: new socket failed\n" ) );
144                     ( void ) prvTCPSendReset( pxNetworkBuffer );
145                 }
146                 else if( prvTCPSocketCopy( pxNewSocket, pxSocket ) != pdFALSE )
147                 {
148                     /* The socket will be connected immediately, no time for the
149                      * owner to setsockopt's, therefore copy properties of the server
150                      * socket to the new socket.  Only the binding might fail (due to
151                      * lack of resources). */
152                     pxReturn = pxNewSocket;
153                 }
154                 else
155                 {
156                     /* Copying failed somehow. */
157                 }
158             }
159         }
160     }
161 
162     if( ( xHasSequence != pdFALSE ) && ( pxReturn != NULL ) )
163     {
164         size_t xCopyLength;
165 
166         /* Map the byte stream onto the ProtocolHeaders_t for easy access to the fields. */
167 
168         /* MISRA Ref 11.3.1 [Misaligned access] */
169         /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
170         /* coverity[misra_c_2012_rule_11_3_violation] */
171         const ProtocolHeaders_t * pxProtocolHeaders = ( ( const ProtocolHeaders_t * )
172                                                         &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + uxIPHeaderSizePacket( pxNetworkBuffer ) ] ) );
173 
174         pxReturn->pxEndPoint = pxNetworkBuffer->pxEndPoint;
175         pxReturn->bits.bIsIPv6 = pdTRUE_UNSIGNED;
176 
177         const IPHeader_IPv6_t * pxIPHeader_IPv6;
178         /* MISRA Ref 11.3.1 [Misaligned access] */
179         /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
180         /* coverity[misra_c_2012_rule_11_3_violation] */
181         pxIPHeader_IPv6 = ( ( const IPHeader_IPv6_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] ) );
182         pxReturn->u.xTCP.usRemotePort = FreeRTOS_ntohs( pxTCPPacket->xTCPHeader.usSourcePort );
183         ( void ) memcpy( pxReturn->u.xTCP.xRemoteIP.xIP_IPv6.ucBytes, pxIPHeader_IPv6->xSourceAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
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_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
216 /* *INDENT-ON* */
217