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_tcp.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_tcp_packet_send_rst                             PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function sends a RST from the specified socket.                */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    socket_ptr                            Pointer to socket             */
48 /*    header_ptr                            Pointer to received header    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _nx_tcp_packet_send_control           Send TCP control packet       */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    _nx_tcp_socket_disconnect             Disconnect processing         */
61 /*    _nx_tcp_socket_state_syn_received     Socket SYN received processing*/
62 /*    _nx_tcp_socket_state_syn_sent         Socket SYN sent processing    */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
69 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_nx_tcp_packet_send_rst(NX_TCP_SOCKET * socket_ptr,NX_TCP_HEADER * header_ptr)73 VOID  _nx_tcp_packet_send_rst(NX_TCP_SOCKET *socket_ptr, NX_TCP_HEADER *header_ptr)
74 {
75 
76     /* Reset Generation, RFC793, Section3.4, Page37, the RST packet is set up based on if the incoming packet has the ACK bit set. */
77     /* If the incoming segment has an ACK field, the reset takes its sequence number from the ACK field of the segment,
78        otherwise the reset has sequence number zero and the ACK field is set to the sum of the sequence number and segment length of the incoming segment.  */
79 
80     /* Check for the ACK bit in the incoming TCP header.  */
81     if (header_ptr -> nx_tcp_header_word_3 & NX_TCP_ACK_BIT)
82     {
83         _nx_tcp_packet_send_control(socket_ptr, NX_TCP_RST_BIT, header_ptr -> nx_tcp_acknowledgment_number,
84                                     0, 0, 0, NX_NULL);
85     }
86     else
87     {
88         _nx_tcp_packet_send_control(socket_ptr, (NX_TCP_RST_BIT | NX_TCP_ACK_BIT), 0,
89                                     header_ptr -> nx_tcp_sequence_number, 0, 0, NX_NULL);
90     }
91 
92 #ifndef NX_DISABLE_TCP_INFO
93     /* Increment the resets sent count.  */
94     socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_tcp_resets_sent++;
95 #endif /* NX_DISABLE_TCP_INFO */
96 }
97 
98