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_fin_wait1                      PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function processes packets during the FIN WAIT 1 state,        */
45 /*    which is the state after the initial FIN was issued in an active    */
46 /*    disconnect issued by the application.                               */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    socket_ptr                            Pointer to owning socket      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _nx_tcp_packet_send_ack               Send ACK message              */
59 /*    _nx_tcp_socket_thread_resume          Resume suspended thread       */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_tcp_socket_packet_process         Process TCP packet for socket */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
70 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_nx_tcp_socket_state_fin_wait1(NX_TCP_SOCKET * socket_ptr)74 VOID  _nx_tcp_socket_state_fin_wait1(NX_TCP_SOCKET *socket_ptr)
75 {
76 
77 
78     /* Determine if the peer has proper ACK number but FIN is not sent,
79        move into the FIN WAIT 2 state and do nothing else.  */
80     if ((socket_ptr -> nx_tcp_socket_fin_acked) &&
81         (socket_ptr -> nx_tcp_socket_fin_received == NX_FALSE))
82     {
83 
84         /* If trace is enabled, insert this event into the trace buffer.  */
85         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_FIN_WAIT_2, NX_TRACE_INTERNAL_EVENTS, 0, 0);
86 
87         /* We have a legitimate ACK message.  Simply move into the WAIT FIN 2 state
88            for the other side to finish its processing and disconnect.  */
89         socket_ptr -> nx_tcp_socket_state =  NX_TCP_FIN_WAIT_2;
90 
91         /* Otherwise, simply clear the FIN timeout.  */
92         socket_ptr -> nx_tcp_socket_timeout =  0;
93     }
94     else if ((socket_ptr -> nx_tcp_socket_fin_acked) &&
95              (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
96     {
97 
98         /* Return to the proper socket state.  */
99 
100         /* Yes, return the socket to the TIMED WAIT state. */
101 
102         /* If trace is enabled, insert this event into the trace buffer.  */
103         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_TIMED_WAIT, NX_TRACE_INTERNAL_EVENTS, 0, 0);
104 
105         /* Set the socket state to TIMED WAIT now.  */
106         socket_ptr -> nx_tcp_socket_state = NX_TCP_TIMED_WAIT;
107 
108         /* Set the timeout as 2MSL (Maximum Segment Lifetime). */
109         socket_ptr -> nx_tcp_socket_timeout = _nx_tcp_2MSL_timer_rate;
110 
111         /* Send ACK back to the other side of the connection.  */
112 
113         /* Increment the received sequence number.  */
114         socket_ptr -> nx_tcp_socket_rx_sequence++;
115 
116         /* Send ACK message.  */
117         _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
118 
119         /* Determine if we need to wake a thread suspended on the connection.  */
120         if (socket_ptr -> nx_tcp_socket_disconnect_suspended_thread)
121         {
122 
123             /* Resume the thread suspended for the disconnect.  */
124             _nx_tcp_socket_thread_resume(&(socket_ptr -> nx_tcp_socket_disconnect_suspended_thread), NX_SUCCESS);
125         }
126 
127         /* If given, call the application's disconnect callback function
128            for disconnect.  */
129         if (socket_ptr -> nx_tcp_disconnect_callback)
130         {
131 
132             /* Call the application's disconnect handling function.  It is
133                responsible for calling the socket disconnect function.  */
134             (socket_ptr -> nx_tcp_disconnect_callback)(socket_ptr);
135         }
136 
137 #ifndef NX_DISABLE_EXTENDED_NOTIFY_SUPPORT
138 
139         /* Is a timed wait callback registered for this socket?  */
140         if (socket_ptr -> nx_tcp_timed_wait_callback)
141         {
142 
143             /* Call the timed wait callback for this socket to let the host
144                know the socket can now be put in the timed wait state (if
145                the RE-USE ADDRESS socket option is not enabled). */
146             (socket_ptr -> nx_tcp_timed_wait_callback)(socket_ptr);
147         }
148 
149         /* Is a disconnect complete callback registered with the TCP socket? */
150         if (socket_ptr -> nx_tcp_disconnect_complete_notify)
151         {
152 
153             /* Call the application's disconnect_complete callback function.    */
154             (socket_ptr -> nx_tcp_disconnect_complete_notify)(socket_ptr);
155         }
156 #endif
157     }
158     else if ((socket_ptr -> nx_tcp_socket_fin_received) &&
159              (socket_ptr -> nx_tcp_socket_fin_sequence == socket_ptr -> nx_tcp_socket_rx_sequence))
160     {
161 
162         /* If trace is enabled, insert this event into the trace buffer.  */
163         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_CLOSING, NX_TRACE_INTERNAL_EVENTS, 0, 0);
164 
165         /* Move to the CLOSING state for simultaneous close situation.  */
166         socket_ptr -> nx_tcp_socket_state =  NX_TCP_CLOSING;
167 
168         /* Send ACK back to the other side of the connection.  */
169 
170         /* Increment the received sequence number.  */
171         socket_ptr -> nx_tcp_socket_rx_sequence++;
172 
173         /* Send ACK message.  */
174         _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
175     }
176 }
177 
178