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 /*    _nx_packet_data_adjust                              PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function adjusts the packet data to fill the specified header. */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    packet_ptr                            Pointer to the source packet  */
48 /*    header_size                           The size of header            */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    status                                Completion status             */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _nx_packet_allocate                   Allocate data packet          */
57 /*    _nx_packet_data_append                Packet data append service    */
58 /*    _nx_packet_release                    Release data packet           */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    _nx_ip_forward_packet_process         Forward IP packet             */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
69 /*  09-30-2020     Yuxin Zhou               Modified comment(s), and      */
70 /*                                            verified memcpy use cases,  */
71 /*                                            verified memmove use cases, */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_packet_data_adjust(NX_PACKET * packet_ptr,ULONG header_size)75 UINT  _nx_packet_data_adjust(NX_PACKET *packet_ptr, ULONG header_size)
76 {
77 
78 ULONG           available_size;
79 ULONG           shift_size;
80 #ifndef NX_DISABLE_PACKET_CHAIN
81 UINT            status;
82 ULONG           append_size;
83 UCHAR          *data_start;
84 NX_PACKET      *work_ptr;
85 #endif /* !NX_DISABLE_PACKET_CHAIN  */
86 
87 
88     /* Add debug information. */
89     NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);
90 
91     /* The header must be filled in one packet. */
92     if (((ALIGN_TYPE)packet_ptr -> nx_packet_data_end - (ALIGN_TYPE)packet_ptr -> nx_packet_data_start) < header_size)
93     {
94         return(NX_UNDERFLOW);
95     }
96 
97     /* 1. Check if there is enough space to add header.  */
98     if (((ALIGN_TYPE)packet_ptr -> nx_packet_prepend_ptr - (ALIGN_TYPE)packet_ptr -> nx_packet_data_start) >= header_size)
99     {
100 
101         /* Yes. Just return success. */
102         return(NX_SUCCESS);
103     }
104 
105     /* Compute the total avilable size in this packet.  */
106     available_size = (ULONG)(((ALIGN_TYPE)packet_ptr -> nx_packet_prepend_ptr - (ALIGN_TYPE)packet_ptr -> nx_packet_data_start) +
107                              ((ALIGN_TYPE)packet_ptr -> nx_packet_data_end - (ALIGN_TYPE)packet_ptr -> nx_packet_append_ptr));
108 
109     /* 2. Would the header fit into the available space?  */
110     if (available_size >= header_size)
111     {
112 
113         /* Yes, adjust the data.  */
114 
115         /* Calculate the shift data size. */
116         shift_size = (ULONG)((ALIGN_TYPE)packet_ptr -> nx_packet_append_ptr - (ALIGN_TYPE)packet_ptr -> nx_packet_prepend_ptr);
117 
118         /* Move the data, */
119         memmove(packet_ptr -> nx_packet_data_start + header_size, packet_ptr -> nx_packet_prepend_ptr, shift_size); /* Use case of memmove is verified.  */
120 
121         /* Update the prepend and append pointer.  */
122         packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_data_start + header_size;
123         packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + shift_size;
124     }
125     else
126     {
127 
128 #ifndef NX_DISABLE_PACKET_CHAIN
129 
130         /* 3. Current packet does not have enough space to fill the header.
131               Allocate a new packet to fill the overflowing data and chain the packet.  */
132 
133         /* Odd value of header_size is not supported now.
134          * In _nx_ip_checksum_compute(), nx_packet_append_ptr must not be odd value. */
135         if (header_size & 1)
136         {
137             return(NX_NOT_SUPPORTED);
138         }
139 
140         /* Calculate the append data size. */
141         append_size = header_size - available_size;
142 
143         /* Set the append data pointer.  */
144         data_start = packet_ptr -> nx_packet_append_ptr - append_size;
145 
146         /* Allocate a packet.  */
147         status = _nx_packet_allocate(packet_ptr -> nx_packet_pool_owner, &work_ptr, 0, NX_NO_WAIT);
148 
149         /* Check status.  */
150         if (status)
151         {
152             return(status);
153         }
154 
155         /* Firstly, append the overflowing data to the new packet.. */
156         memcpy(work_ptr -> nx_packet_prepend_ptr, data_start, append_size); /* Use case of memcpy is verified.  lgtm[cpp/banned-api-usage-required-any] */
157         work_ptr -> nx_packet_append_ptr = (UCHAR *)((ALIGN_TYPE)work_ptr -> nx_packet_prepend_ptr + append_size);
158 
159         /* Secondly, calculate the shift data size.  */
160         shift_size = (ULONG)(((ALIGN_TYPE)packet_ptr -> nx_packet_append_ptr - (ALIGN_TYPE)packet_ptr -> nx_packet_prepend_ptr) - append_size);
161 
162         /* Move the data.  */
163         memmove(packet_ptr -> nx_packet_data_start + header_size, packet_ptr -> nx_packet_prepend_ptr, shift_size); /* Use case of memmove is verified.  */
164 
165         /* Update the prepend and append pointer.  */
166         packet_ptr -> nx_packet_prepend_ptr = packet_ptr -> nx_packet_data_start + header_size;
167         packet_ptr -> nx_packet_append_ptr = packet_ptr -> nx_packet_prepend_ptr + shift_size;
168 
169         /* At this point, all the necessary packets have been allocated.
170            We need to link this new packet to the next of the supplied packet.  */
171         work_ptr -> nx_packet_next = packet_ptr -> nx_packet_next;
172         packet_ptr -> nx_packet_next = work_ptr;
173 
174         /* Update the last packet pointer.  */
175         if (packet_ptr -> nx_packet_last == NX_NULL)
176         {
177             packet_ptr -> nx_packet_last = work_ptr;
178         }
179 #else /* NX_DISABLE_PACKET_CHAIN  */
180 
181         /* Return error code.  */
182         return(NX_UNDERFLOW);
183 #endif /* !NX_DISABLE_PACKET_CHAIN  */
184     }
185 
186     /* Return successful completion.  */
187     return(NX_SUCCESS);
188 }
189 
190