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