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