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 /**   Internet Control Message Protocol (ICMP)                            */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "tx_thread.h"
29 #include "nx_icmp.h"
30 #include "nx_ip.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nx_icmp_cleanup                                    PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function processes ICMP ping timeout and thread terminate      */
46 /*    actions that require the IP/ICMP data structures to be cleaned      */
47 /*    up.                                                                 */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread_ptr                            Pointer to suspended thread's */
52 /*                                            control block               */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_thread_system_resume              Resume thread service         */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _nx_ip_delete                         Delete IP instance            */
65 /*    _tx_thread_timeout                    Thread timeout processing     */
66 /*    _tx_thread_terminate                  Thread terminate processing   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nx_icmp_cleanup(TX_THREAD * thread_ptr NX_CLEANUP_PARAMETER)77 VOID  _nx_icmp_cleanup(TX_THREAD *thread_ptr NX_CLEANUP_PARAMETER)
78 {
79 
80 TX_INTERRUPT_SAVE_AREA
81 
82 NX_IP *ip_ptr;
83 
84     NX_CLEANUP_EXTENSION
85 
86     /* Setup pointer to IP control block.  */
87     ip_ptr =  (NX_IP *)thread_ptr -> tx_thread_suspend_control_block;
88 
89     /* Disable interrupts to remove the suspended thread from the packet pool.  */
90     TX_DISABLE
91 
92     /* Determine if the cleanup is still required.  */
93     if ((thread_ptr -> tx_thread_suspend_cleanup) && (ip_ptr) &&
94         (ip_ptr -> nx_ip_id == NX_IP_ID))
95     {
96 
97         /* Yes, we still have thread suspension!  */
98 
99         /* Clear the suspension cleanup flag.  */
100         thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
101 
102         /* Remove the suspended thread from the list.  */
103 
104         /* See if this is the only suspended thread on the list.  */
105         if (thread_ptr == thread_ptr -> tx_thread_suspended_next)
106         {
107 
108             /* Yes, the only suspended thread.  */
109 
110             /* Update the head pointer.  */
111             ip_ptr -> nx_ip_icmp_ping_suspension_list =  TX_NULL;
112         }
113         else
114         {
115 
116             /* At least one more thread is on the same suspension list.  */
117 
118             /* Update the list head pointer.  */
119             if (ip_ptr -> nx_ip_icmp_ping_suspension_list == thread_ptr)
120             {
121                 ip_ptr -> nx_ip_icmp_ping_suspension_list =  thread_ptr -> tx_thread_suspended_next;
122             }
123 
124             /* Update the links of the adjacent threads.  */
125             (thread_ptr -> tx_thread_suspended_next) -> tx_thread_suspended_previous =
126                 thread_ptr -> tx_thread_suspended_previous;
127             (thread_ptr -> tx_thread_suspended_previous) -> tx_thread_suspended_next =
128                 thread_ptr -> tx_thread_suspended_next;
129         }
130 
131         /* Decrement the suspension count.  */
132         ip_ptr -> nx_ip_icmp_ping_suspended_count--;
133 
134         /* Now we need to determine if this cleanup is from a terminate, timeout,
135            or from a wait abort.  */
136         if (thread_ptr -> tx_thread_state == TX_TCP_IP)
137         {
138 
139             /* Thread still suspended on the IP ping message.  Setup return error status and
140                resume the thread.  */
141 
142             /* Setup return status.  */
143             thread_ptr -> tx_thread_suspend_status =  NX_NO_RESPONSE;
144 
145 #ifndef NX_DISABLE_ICMP_INFO
146             /* Increment the ICMP timeout count.  */
147             ip_ptr -> nx_ip_ping_timeouts++;
148 #endif
149 
150             /* Temporarily disable preemption.  */
151             _tx_thread_preempt_disable++;
152 
153             /* Restore interrupts.  */
154             TX_RESTORE
155 
156             /* Resume the thread!  Check for preemption even though we are executing
157                from the system timer thread right now which normally executes at the
158                highest priority.  */
159             _tx_thread_system_resume(thread_ptr);
160 
161             /* Finished, just return.  */
162             return;
163         }
164     }
165 
166     /* Restore interrupts.  */
167     TX_RESTORE
168 }
169 
170