1 /*
2  * FreeRTOS+TCP V3.1.0
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 #ifndef FREERTOS_TCP_IP_H
29 #define FREERTOS_TCP_IP_H
30 
31 /* *INDENT-OFF* */
32 #ifdef __cplusplus
33     extern "C" {
34 #endif
35 /* *INDENT-ON* */
36 
37 BaseType_t xProcessReceivedTCPPacket( NetworkBufferDescriptor_t * pxDescriptor );
38 
39 typedef enum eTCP_STATE
40 {
41     /* Comments about the TCP states are borrowed from the very useful
42      * Wiki page:
43      * http://en.wikipedia.org/wiki/Transmission_Control_Protocol */
44     eCLOSED = 0U,  /* 0 (server + client) no connection state at all. */
45     eTCP_LISTEN,   /* 1 (server) waiting for a connection request
46                     * from any remote TCP and port. */
47     eCONNECT_SYN,  /* 2 (client) internal state: socket wants to send
48                     * a connect */
49     eSYN_FIRST,    /* 3 (server) Just created, must ACK the SYN request. */
50     eSYN_RECEIVED, /* 4 (server) waiting for a confirming connection request
51                     * acknowledgement after having both received and sent a connection request. */
52     eESTABLISHED,  /* 5 (server + client) an open connection, data received can be
53                     * delivered to the user. The normal state for the data transfer phase of the connection. */
54     eFIN_WAIT_1,   /* 6 (server + client) waiting for a connection termination request from the remote TCP,
55                     * or an acknowledgement of the connection termination request previously sent. */
56     eFIN_WAIT_2,   /* 7 (server + client) waiting for a connection termination request from the remote TCP. */
57     eCLOSE_WAIT,   /* 8 (server + client) waiting for a connection termination request from the local user. */
58     eCLOSING,      /* 9 (server + client) waiting for a connection termination request acknowledgement from the remote TCP. */
59     eLAST_ACK,     /*10 (server + client) waiting for an acknowledgement of the connection termination request
60                     * previously sent to the remote TCP
61                     * (which includes an acknowledgement of its connection termination request). */
62     eTIME_WAIT,    /*11 (either server or client) waiting for enough time to pass to be sure the remote TCP received the
63                     * acknowledgement of its connection termination request. [According to RFC 793 a connection can
64                     * stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime).] */
65 } eIPTCPState_t;
66 
67 /*
68  * The meaning of the TCP flags:
69  */
70 #define tcpTCP_FLAG_FIN     ( ( uint8_t ) 0x01U )                           /**< No more data from sender. */
71 #define tcpTCP_FLAG_SYN     ( ( uint8_t ) 0x02U )                           /**< Synchronize sequence numbers. */
72 #define tcpTCP_FLAG_RST     ( ( uint8_t ) 0x04U )                           /**< Reset the connection. */
73 #define tcpTCP_FLAG_PSH     ( ( uint8_t ) 0x08U )                           /**< Push function: please push buffered data to the recv application. */
74 #define tcpTCP_FLAG_ACK     ( ( uint8_t ) 0x10U )                           /**< Acknowledgment field is significant. */
75 #define tcpTCP_FLAG_URG     ( ( uint8_t ) 0x20U )                           /**< Urgent pointer field is significant. */
76 #define tcpTCP_FLAG_ECN     ( ( uint8_t ) 0x40U )                           /**< ECN-Echo. */
77 #define tcpTCP_FLAG_CWR     ( ( uint8_t ) 0x80U )                           /**< Congestion Window Reduced. */
78 
79 #define tcpTCP_FLAG_CTRL    ( ( uint8_t ) 0x1FU )                           /**< A mask to filter all protocol flags. */
80 
81 
82 /*
83  * A few values of the TCP options:
84  */
85 #define tcpTCP_OPT_END               0U                  /**< End of TCP options list. */
86 #define tcpTCP_OPT_NOOP              1U                  /**< "No-operation" TCP option. */
87 #define tcpTCP_OPT_MSS               2U                  /**< Maximum segment size TCP option. */
88 #define tcpTCP_OPT_WSOPT             3U                  /**< TCP Window Scale Option (3-byte long). */
89 #define tcpTCP_OPT_SACK_P            4U                  /**< Advertise that SACK is permitted. */
90 #define tcpTCP_OPT_SACK_A            5U                  /**< SACK option with first/last. */
91 #define tcpTCP_OPT_TIMESTAMP         8U                  /**< Time-stamp option. */
92 
93 
94 #define tcpTCP_OPT_MSS_LEN           4U                  /**< Length of TCP MSS option. */
95 #define tcpTCP_OPT_WSOPT_LEN         3U                  /**< Length of TCP WSOPT option. */
96 
97 #define tcpTCP_OPT_TIMESTAMP_LEN     10                  /**< fixed length of the time-stamp option. */
98 
99 /** @brief
100  * Minimum segment length as outlined by RFC 791 section 3.1.
101  * Minimum segment length ( 536 ) = Minimum MTU ( 576 ) - IP Header ( 20 ) - TCP Header ( 20 ).
102  */
103 #define tcpMINIMUM_SEGMENT_LENGTH    536U
104 
105 /** @brief
106  * The macro tcpNOW_CONNECTED() is use to determine if the connection makes a
107  * transition from connected to non-connected and vice versa.
108  * tcpNOW_CONNECTED() returns true when the status has one of these values:
109  * eESTABLISHED, eFIN_WAIT_1, eFIN_WAIT_2, eCLOSING, eLAST_ACK, eTIME_WAIT
110  * Technically the connection status is closed earlier, but the library wants
111  * to prevent that the socket will be deleted before the last ACK has been
112  * and thus causing a 'RST' packet on either side.
113  */
114 #define tcpNOW_CONNECTED( status ) \
115     ( ( ( ( status ) >= ( BaseType_t ) eESTABLISHED ) && ( ( status ) != ( BaseType_t ) eCLOSE_WAIT ) ) ? 1 : 0 )
116 
117 /** @brief
118  * The highest 4 bits in the TCP offset byte indicate the total length of the
119  * TCP header, divided by 4.
120  */
121 #define tcpVALID_BITS_IN_TCP_OFFSET_BYTE    ( 0xF0U )
122 
123 /*
124  * Acknowledgements to TCP data packets may be delayed as long as more is being expected.
125  * A normal delay would be 200ms. Here a much shorter delay of 20 ms is being used to
126  * gain performance.
127  */
128 #define tcpDELAYED_ACK_SHORT_DELAY_MS       ( 2 )           /**< Should not become smaller than 1. */
129 #define tcpDELAYED_ACK_LONGER_DELAY_MS      ( 20 )          /**< Longer delay for ACK. */
130 
131 
132 /** @brief
133  * The MSS (Maximum Segment Size) will be taken as large as possible. However, packets with
134  * an MSS of 1460 bytes won't be transported through the internet.  The MSS will be reduced
135  * to 1400 bytes.
136  */
137 #define tcpREDUCED_MSS_THROUGH_INTERNET    ( 1400 )
138 
139 /** @brief
140  * When there are no TCP options, the TCP offset equals 20 bytes, which is stored as
141  * the number 5 (words) in the higher nibble of the TCP-offset byte.
142  */
143 #define tcpTCP_OFFSET_LENGTH_BITS          ( 0xf0U )
144 #define tcpTCP_OFFSET_STANDARD_LENGTH      ( 0x50U )          /**< Standard TCP packet offset. */
145 
146 
147 /** @brief
148  * Each TCP socket is checked regularly to see if it can send data packets.
149  * By default, the maximum number of packets sent during one check is limited to 8.
150  * This amount may be further limited by setting the socket's TX window size.
151  */
152 #if ( !defined( SEND_REPEATED_COUNT ) )
153     #define SEND_REPEATED_COUNT    ( 8 )
154 #endif /* !defined( SEND_REPEATED_COUNT ) */
155 
156 /** @brief
157  * Define a maximum period of time (ms) to leave a TCP-socket unattended.
158  * When a TCP timer expires, retries and keep-alive messages will be checked.
159  */
160 #ifndef tcpMAXIMUM_TCP_WAKEUP_TIME_MS
161     #define tcpMAXIMUM_TCP_WAKEUP_TIME_MS    20000U
162 #endif
163 
164 /* Two macro's that were introduced to work with both IPv4 and IPv6. */
165 #define xIPHeaderSize( pxNetworkBuffer )    ( ipSIZE_OF_IPv4_HEADER )          /**< Size of IP Header. */
166 #define uxIPHeaderSizeSocket( pxSocket )    ( ipSIZE_OF_IPv4_HEADER )          /**< Size of IP Header socket. */
167 
168 
169 /* *INDENT-OFF* */
170 #ifdef __cplusplus
171     } /* extern "C" */
172 #endif
173 /* *INDENT-ON* */
174 
175 #endif /* FREERTOS_TCP_IP_H */
176