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 Secure Component                                                 */
16 /**                                                                       */
17 /**    Datagram Transport Layer Security (DTLS)                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SECURE_SOURCE_CODE
23 
24 #include "nx_secure_dtls.h"
25 
26 #ifdef NX_SECURE_ENABLE_DTLS
27 #include "nx_packet.h"
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    _nx_secure_dtls_retransmit_queue_flush              PORTABLE C      */
34 /*                                                           6.1.10       */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    Timothy Stapko, Microsoft Corporation                               */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This function flushes the DTLS transmit queue when the appropriate  */
42 /*    response is received from the remote host, clearing out the         */
43 /*    previously-sent flight, resetting the queue for the next flight.    */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    dtls_session                          DTLS control block            */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    None                                                                */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    nx_secure_tls_packet_release          Release packet                */
56 /*    tx_mutex_get                          Get protection mutex          */
57 /*    tx_mutex_put                          Put protection mutex          */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    _nx_secure_dtls_client_handshake      DTLS client state machine     */
62 /*    _nx_secure_dtls_server_handshake      DTLS server state machine     */
63 /*    _nx_secure_dtls_session_start         Actual DTLS session start call*/
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
70 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
71 /*                                            released packet securely,   */
72 /*                                            resulting in version 6.1    */
73 /*  01-31-2022     Timothy Stapko           Modified comment(s),          */
74 /*                                            fixed packet leak,          */
75 /*                                            resulting in version 6.1.10 */
76 /*                                                                        */
77 /**************************************************************************/
_nx_secure_dtls_retransmit_queue_flush(NX_SECURE_DTLS_SESSION * dtls_session)78 VOID  _nx_secure_dtls_retransmit_queue_flush(NX_SECURE_DTLS_SESSION *dtls_session)
79 {
80 TX_INTERRUPT_SAVE_AREA
81 NX_PACKET *packet_ptr;
82 NX_PACKET *next_packet_ptr;
83 
84 
85     /* Get the protection. */
86     tx_mutex_get(&_nx_secure_tls_protection, TX_WAIT_FOREVER);
87 
88     /* Setup packet pointer.  */
89     packet_ptr =  dtls_session -> nx_secure_dtls_transmit_sent_head;
90 
91     /* Clear the head and the tail pointers.  */
92     dtls_session -> nx_secure_dtls_transmit_sent_head =  NX_NULL;
93     dtls_session -> nx_secure_dtls_transmit_sent_tail =  NX_NULL;
94 
95     /* Loop to clear all the packets out.  */
96     while (packet_ptr &&
97            (packet_ptr != (NX_PACKET *)NX_PACKET_ENQUEUED))
98     {
99 
100         /* Disable interrupts.  */
101         TX_DISABLE
102 
103         /* Pickup the next queued packet.  */
104         next_packet_ptr =  packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next;
105 
106         /* Mark the packet as no longer being in a TCP queue.  */
107         /*lint -e{923} suppress cast of ULONG to pointer.  */
108         packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next =  (NX_PACKET *)NX_PACKET_ALLOCATED;
109 
110         /* Has the packet been transmitted?  */
111         if (packet_ptr -> nx_packet_queue_next ==  ((NX_PACKET *)NX_DRIVER_TX_DONE))
112         {
113 
114             /* Yes, the driver has already released the packet.  */
115 
116             /* Restore interrupts.  */
117             TX_RESTORE
118 
119             /* Release the packet.  */
120             nx_secure_tls_packet_release(packet_ptr);
121         }
122         else
123         {
124 
125             /* Restore interrupts.  */
126             TX_RESTORE
127         }
128 
129         /* Move to the next packet.  */
130         packet_ptr =  next_packet_ptr;
131 
132         /* Decrease the queued packet count.  */
133         dtls_session -> nx_secure_dtls_transmit_sent_count--;
134     }
135 
136     /* Release the protection before suspending on event. */
137     tx_mutex_put(&_nx_secure_tls_protection);
138 }
139 #endif /* NX_SECURE_ENABLE_DTLS */
140 
141