1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Transmission Control Protocol (TCP)                                 */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_ip.h"
29 #include "nx_tcp.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_socket_state_syn_received                   PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes packets during the SYN RECEIVED state,      */
45 /*    which is the state after the initial SYN message was responded to   */
46 /*    with an SYN/ACK message.  The expected value here is an ACK, which  */
47 /*    will move us into an ESTABLISHED state ready for sending and        */
48 /*    receiving of TCP data.                                              */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    socket_ptr                            Pointer to owning socket      */
53 /*    tcp_header_ptr                        Pointer to packet header      */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _nx_tcp_socket_thread_resume          Resume suspended thread       */
62 /*    _nx_tcp_packet_send_rst               Send RST packet               */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    _nx_tcp_socket_packet_process         Process TCP packet for socket */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_tcp_socket_state_syn_received(NX_TCP_SOCKET * socket_ptr,NX_TCP_HEADER * tcp_header_ptr)77 VOID  _nx_tcp_socket_state_syn_received(NX_TCP_SOCKET *socket_ptr, NX_TCP_HEADER *tcp_header_ptr)
78 {
79 
80 
81     /* Determine if the incoming message is an ACK message.  If it is and
82        if it is proper, move into the ESTABLISHED state.  */
83     if (tcp_header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT)
84     {
85 
86         if (tcp_header_ptr -> nx_tcp_acknowledgment_number == socket_ptr -> nx_tcp_socket_tx_sequence)
87         {
88 
89             /* If trace is enabled, insert this event into the trace buffer.  */
90             NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_TCP_STATE_CHANGE, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_state, NX_TCP_ESTABLISHED, NX_TRACE_INTERNAL_EVENTS, 0, 0);
91 
92             /* Save the window size.  */
93             socket_ptr -> nx_tcp_socket_tx_window_advertised =
94                 tcp_header_ptr -> nx_tcp_header_word_3 & NX_LOWER_16_MASK;
95 
96 #ifdef NX_ENABLE_TCP_WINDOW_SCALING
97 
98             /* The window size advertised in the SYN packet is NEVER scaled. Therefore there is no
99                need to apply the scale shift.  However validate snd_win_scale  and rcv_win_scale. */
100             if (socket_ptr -> nx_tcp_snd_win_scale_value == 0xFF)
101             {
102 
103                 /* Peer does not support window scale option. */
104                 socket_ptr -> nx_tcp_snd_win_scale_value = 0;
105                 socket_ptr -> nx_tcp_rcv_win_scale_value = 0;
106 
107                 /* Since peer does not offer window scaling feature, make sure
108                    our default window size for this connection does not exceed 65535 bytes. */
109                 if (socket_ptr -> nx_tcp_socket_rx_window_maximum > 65535)
110                 {
111                     socket_ptr -> nx_tcp_socket_rx_window_default = 65535;
112                     socket_ptr -> nx_tcp_socket_rx_window_current = 65535;
113                 }
114             }
115 
116             /* Updated the window size.  */
117             socket_ptr -> nx_tcp_socket_tx_window_advertised <<= socket_ptr -> nx_tcp_snd_win_scale_value;
118 
119 #endif /* NX_ENABLE_TCP_WINDOW_SCALING  */
120 
121             /* Set the initial slow start threshold to be the advertised window size. */
122             socket_ptr -> nx_tcp_socket_tx_slow_start_threshold = socket_ptr -> nx_tcp_socket_tx_window_advertised;
123 
124             /* Set the initial congestion control window size. */
125             /* Section 3.1, Page 5, RFC5681. */
126             if (socket_ptr -> nx_tcp_socket_timeout_retries > 0)
127             {
128 
129                 /* Set the initial congestion control window size to be the mss. */
130                 socket_ptr -> nx_tcp_socket_tx_window_congestion = socket_ptr -> nx_tcp_socket_connect_mss;
131             }
132             else
133             {
134                 socket_ptr -> nx_tcp_socket_tx_window_congestion = (socket_ptr -> nx_tcp_socket_connect_mss << 2);
135                 if (socket_ptr -> nx_tcp_socket_connect_mss > 1095)
136                 {
137                     socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss;
138                 }
139                 if (socket_ptr -> nx_tcp_socket_connect_mss > 2190)
140                 {
141                     socket_ptr -> nx_tcp_socket_tx_window_congestion -= socket_ptr -> nx_tcp_socket_connect_mss;
142                 }
143             }
144 
145             /* Move into the ESTABLISHED state.  */
146             socket_ptr -> nx_tcp_socket_state =  NX_TCP_ESTABLISHED;
147 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
148 
149             /* If registered with the TCP socket, call the application's connection completion callback function.  */
150             if (socket_ptr -> nx_tcp_establish_notify)
151             {
152 
153                 /* Call the application's establish callback function.    */
154                 (socket_ptr -> nx_tcp_establish_notify)(socket_ptr);
155             }
156 #endif
157 
158 
159 #ifdef NX_ENABLE_TCP_KEEPALIVE
160             /* Is the keepalive feature enabled on this socket? */
161             if (socket_ptr -> nx_tcp_socket_keepalive_enabled)
162             {
163                 /* Setup the TCP Keepalive timer to initial values.  */
164                 socket_ptr -> nx_tcp_socket_keepalive_timeout =  NX_TCP_KEEPALIVE_INITIAL;
165                 socket_ptr -> nx_tcp_socket_keepalive_retries =  0;
166             }
167 #endif
168             /* Update the value of nx_tcp_socket_rx_sequence_acked */
169             socket_ptr -> nx_tcp_socket_rx_sequence_acked =    socket_ptr -> nx_tcp_socket_rx_sequence;
170 
171             /* Determine if we need to wake a thread suspended on the connection.  */
172             if (socket_ptr -> nx_tcp_socket_connect_suspended_thread)
173             {
174 
175                 /* Resume the suspended thread.  */
176                 _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_connect_suspended_thread), NX_SUCCESS);
177             }
178         }
179         /* Check for an invalid ACK message that signals an error on the other side.  */
180         else
181         {
182 
183             /* Invalid response was received, it is likely that the other side still
184                thinks a previous connection is active.  Send a reset (RST) message to
185                the other side to clear any previous connection.  */
186 
187             /* Send the RST packet.  */
188             _nx_tcp_packet_send_rst(socket_ptr, tcp_header_ptr);
189         }
190     }
191 }
192 
193