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 /**   Packet Pool Management (Packet)                                     */
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_packet.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxe_packet_release                                 PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for errors in the packet release               */
44 /*    function call.                                                      */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    packet_ptr                            Pointer of packet to release  */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Completion status             */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _nx_packet_release                    Actual packet release         */
57 /*                                            function                    */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
68 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*                                                                        */
71 /**************************************************************************/
_nxe_packet_release(NX_PACKET ** packet_ptr_ptr)72 UINT  _nxe_packet_release(NX_PACKET **packet_ptr_ptr)
73 {
74 
75 UINT       status;
76 NX_PACKET *packet_ptr;
77 
78 
79     /* Setup packet pointer.  */
80     packet_ptr =  *packet_ptr_ptr;
81 
82     /* Simple integrity check on the packet.  */
83     if ((packet_ptr == NX_NULL) || (packet_ptr -> nx_packet_pool_owner == NX_NULL) ||
84         ((packet_ptr -> nx_packet_pool_owner) -> nx_packet_pool_id != NX_PACKET_POOL_ID))
85     {
86 
87         return(NX_PTR_ERROR);
88     }
89 
90     /* Check for an invalid packet prepend pointer.  */
91     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
92     if (packet_ptr -> nx_packet_prepend_ptr < packet_ptr -> nx_packet_data_start)
93     {
94         return(NX_UNDERFLOW);
95     }
96 
97     /* Check for an invalid packet append pointer.  */
98     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
99     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
100     {
101         return(NX_OVERFLOW);
102     }
103 
104     /* Call actual packet release function.  */
105     status =  _nx_packet_release(packet_ptr);
106 
107     /* Determine if the packet release was successful.  */
108     if (status == NX_SUCCESS)
109     {
110 
111         /* Yes, now clear the application's packet pointer so it can't be accidentally
112            used again by the application.  This is only done when error checking is
113            enabled.  */
114         *packet_ptr_ptr =  NX_NULL;
115     }
116 
117     /* Return completion status.  */
118     return(status);
119 }
120 
121