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 /* Bring in externs for caller checking code.  */
31 
32 NX_CALLER_CHECKING_EXTERNS
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nxe_packet_copy                                    PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function checks for errors in the packet copy                  */
48 /*    function call.                                                      */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    packet_ptr                            Pointer to source packet      */
53 /*    new_packet_ptr                        Pointer to return packet      */
54 /*    pool_ptr                              Pointer to packet pool to use */
55 /*                                            for new packet(s)           */
56 /*    wait_option                           Suspension option             */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    status                                Completion status             */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _nx_packet_copy                       Actual packet copy function   */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_nxe_packet_copy(NX_PACKET * packet_ptr,NX_PACKET ** new_packet_ptr,NX_PACKET_POOL * pool_ptr,ULONG wait_option)79 UINT  _nxe_packet_copy(NX_PACKET *packet_ptr, NX_PACKET **new_packet_ptr,
80                        NX_PACKET_POOL *pool_ptr, ULONG wait_option)
81 {
82 
83 UINT status;
84 
85 
86     /* Check for invalid input pointers.  */
87     if ((pool_ptr == NX_NULL) || (pool_ptr -> nx_packet_pool_id != NX_PACKET_POOL_ID) ||
88         (packet_ptr == NX_NULL) || (new_packet_ptr == NX_NULL))
89     {
90         return(NX_PTR_ERROR);
91     }
92 
93     /* Check for an invalid packet prepend pointer.  */
94     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
95     if (packet_ptr -> nx_packet_prepend_ptr < packet_ptr -> nx_packet_data_start)
96     {
97         return(NX_UNDERFLOW);
98     }
99 
100     /* Check for an invalid packet append pointer.  */
101     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
102     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
103     {
104         return(NX_OVERFLOW);
105     }
106 
107     /* Check for appropriate caller.  */
108     NX_THREAD_WAIT_CALLER_CHECKING
109 
110     /* Call actual packet copy function.  */
111     status =  _nx_packet_copy(packet_ptr, new_packet_ptr, pool_ptr, wait_option);
112 
113     /* Return completion status.  */
114     return(status);
115 }
116 
117