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 /**   Internet Protocol (IP)                                              */
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_ip.h"
30 #include "nx_packet.h"
31 
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nx_ip_fragment_disable                             PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function disables IP fragment assembly processing and releases */
47 /*    all partial fragments being assembled.                              */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    ip_ptr                                Pointer to IP instance        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _nx_packet_release                    Release packet                */
60 /*    tx_mutex_get                          Get protection mutex          */
61 /*    tx_mutex_put                          Put protection mutex          */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_nx_ip_fragment_disable(NX_IP * ip_ptr)76 UINT  _nx_ip_fragment_disable(NX_IP *ip_ptr)
77 {
78 #ifndef NX_DISABLE_FRAGMENTATION
79 TX_INTERRUPT_SAVE_AREA
80 
81 #ifndef NX_FRAGMENT_IMMEDIATE_ASSEMBLY
82 NX_PACKET *new_fragment_head;
83 #endif /* NX_FRAGMENT_IMMEDIATE_ASSEMBLY */
84 NX_PACKET *assemble_head;
85 NX_PACKET *next_packet;
86 NX_PACKET *release_packet;
87 
88 
89     /* If trace is enabled, insert this event into the trace buffer.  */
90     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_FRAGMENT_DISABLE, ip_ptr, 0, 0, 0, NX_TRACE_IP_EVENTS, 0, 0);
91 
92     /* Get mutex protection.  */
93     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
94 
95     /* Disable interrupts temporarily.  */
96     TX_DISABLE
97 
98     /* Clear the IP fragment processing routine pointer.  */
99     ip_ptr -> nx_ip_fragment_processing =  NX_NULL;
100 
101     /* Clear the IP fragment assembly routine pointer.  */
102     ip_ptr -> nx_ip_fragment_assembly =  NX_NULL;
103 
104     /* Clear the IP fragment timeout routine pointer.  */
105     ip_ptr -> nx_ip_fragment_timeout_check =  NX_NULL;
106 
107     /* Pickup the fragment list pointer.  */
108 #ifndef NX_FRAGMENT_IMMEDIATE_ASSEMBLY
109     new_fragment_head = ip_ptr -> nx_ip_received_fragment_head;
110 #else
111     NX_ASSERT(ip_ptr -> nx_ip_received_fragment_head == NX_NULL);
112 #endif /* NX_FRAGMENT_IMMEDIATE_ASSEMBLY */
113     assemble_head =     ip_ptr -> nx_ip_fragment_assembly_head;
114 
115     /* Clear the IP structure lists.  */
116     ip_ptr -> nx_ip_received_fragment_head =  NX_NULL;
117     ip_ptr -> nx_ip_received_fragment_tail =  NX_NULL;
118     ip_ptr -> nx_ip_fragment_assembly_head =  NX_NULL;
119     ip_ptr -> nx_ip_fragment_assembly_tail =  NX_NULL;
120 
121     /* Restore interrupts.  */
122     TX_RESTORE
123 
124     /* Release mutex protection.  */
125     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
126 
127 #ifndef NX_FRAGMENT_IMMEDIATE_ASSEMBLY
128     /* Now walk through the receive and assembly lists to free the packets.  */
129     next_packet =  new_fragment_head;
130     while (next_packet)
131     {
132 
133         /* Set the release packet to this packet.  */
134         release_packet =  next_packet;
135 
136         /* Move next packet to the next in the list.  */
137         next_packet =  next_packet -> nx_packet_queue_next;
138 
139         /* Release the current packet.  */
140         _nx_packet_release(release_packet);
141     }
142 #endif /* NX_FRAGMENT_IMMEDIATE_ASSEMBLY */
143 
144     /* Now walk through the assemble list and release all packets.  */
145     while (assemble_head)
146     {
147 
148         /* Walk through the list of packets being assembled for this packet and release them.  */
149         next_packet =  assemble_head;
150         assemble_head =  next_packet -> nx_packet_queue_next;
151         while (next_packet)
152         {
153 
154             /* Set the release packet to this packet.  */
155             release_packet =  next_packet;
156 
157             /* Move next packet to the next in the list.  */
158             next_packet =  next_packet -> nx_packet_union_next.nx_packet_fragment_next;
159 
160             /* Reset tcp_queue_next before releasing. */
161             /*lint -e{923} suppress cast of ULONG to pointer.  */
162             release_packet -> nx_packet_union_next.nx_packet_tcp_queue_next = (NX_PACKET *)NX_PACKET_ALLOCATED;
163 
164             /* Release the current packet.  */
165             _nx_packet_release(release_packet);
166         }
167     }
168 
169     /* Return success to the caller.  */
170     return(NX_SUCCESS);
171 #else /* NX_DISABLE_FRAGMENTATION */
172     NX_PARAMETER_NOT_USED(ip_ptr);
173 
174     return(NX_NOT_ENABLED);
175 
176 #endif /* NX_DISABLE_FRAGMENTATION */
177 }
178 
179