xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_TCP_Utils_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_Utils_IPv6.c
30  * @brief Module contains utility functions used by FreeRTOS+TCP module.
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 /* Standard includes. */
36 #include <stdint.h>
37 #include <stdio.h>
38 
39 /* FreeRTOS includes. */
40 #include "FreeRTOS.h"
41 
42 /* FreeRTOS+TCP includes. */
43 #include "FreeRTOS_IP.h"
44 #include "FreeRTOS_IP_Private.h"
45 
46 #include "FreeRTOS_TCP_Utils.h"
47 
48 /* Just make sure the contents doesn't get compiled if TCP is not enabled. */
49 /* *INDENT-OFF* */
50 #if( ipconfigUSE_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 )
51 /* *INDENT-ON* */
52 
53 /**
54  * @brief Set the MSS (Maximum segment size) associated with the given socket.
55  *
56  * @param[in] pxSocket The socket whose MSS is to be set.
57  */
prvSocketSetMSS_IPV6(FreeRTOS_Socket_t * pxSocket)58 void prvSocketSetMSS_IPV6( FreeRTOS_Socket_t * pxSocket )
59 {
60     uint32_t ulMSS = ipconfigTCP_MSS;
61 
62     #if ( ipconfigHAS_DEBUG_PRINTF == 1 )
63         char cIPv6Address[ 40 ];
64     #endif
65 
66     const NetworkEndPoint_t * pxEndPoint = NULL;
67 
68     do
69     {
70         if( pxSocket == NULL )
71         {
72             /* If NULL socket handler, skip all following steps. */
73             FreeRTOS_debug_printf( ( "prvSocketSetMSS_IPV6: NULL socket handler\n" ) );
74 
75             break;
76         }
77 
78         pxEndPoint = pxSocket->pxEndPoint;
79 
80         if( pxEndPoint != NULL )
81         {
82             /* Compared to IPv4, an IPv6 header is 20 bytes longer.
83              * It must be subtracted from the MSS. */
84             size_t uxDifference = ipSIZE_OF_IPv6_HEADER - ipSIZE_OF_IPv4_HEADER;
85             /* Do not allow MSS smaller than tcpMINIMUM_SEGMENT_LENGTH. */
86             #if ( ipconfigTCP_MSS >= tcpMINIMUM_SEGMENT_LENGTH )
87                 {
88                     ulMSS = ipconfigTCP_MSS;
89                 }
90             #else
91                 {
92                     ulMSS = tcpMINIMUM_SEGMENT_LENGTH;
93                 }
94             #endif
95 
96             ulMSS = ( uint32_t ) ( ulMSS - uxDifference );
97             IPv6_Type_t eType = xIPv6_GetIPType( &( pxSocket->u.xTCP.xRemoteIP.xIP_IPv6 ) );
98 
99             if( eType == eIPv6_Global )
100             {
101                 /* The packet will travel through Internet, make the MSS
102                  * smaller. */
103                 ulMSS = FreeRTOS_min_uint32( ( uint32_t ) tcpREDUCED_MSS_THROUGH_INTERNET, ulMSS );
104             }
105         }
106 
107         #if ( ipconfigHAS_DEBUG_PRINTF == 1 )
108             {
109                 ( void ) FreeRTOS_inet_ntop( FREERTOS_AF_INET6, ( const void * ) pxSocket->u.xTCP.xRemoteIP.xIP_IPv6.ucBytes, cIPv6Address, sizeof( cIPv6Address ) );
110                 FreeRTOS_debug_printf( ( "prvSocketSetMSS: %u bytes for %s ip port %u\n", ( unsigned ) ulMSS, cIPv6Address, pxSocket->u.xTCP.usRemotePort ) );
111             }
112         #endif
113 
114         pxSocket->u.xTCP.usMSS = ( uint16_t ) ulMSS;
115     } while( ipFALSE_BOOL );
116 }
117 /*-----------------------------------------------------------*/
118 
119 /* *INDENT-OFF* */
120 #endif /* ( ipconfigUSE_IPv6 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
121 /* *INDENT-ON* */
122