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 #include "nx_ipv6.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _nx_packet_copy PORTABLE C */
37 /* 6.1 */
38 /* AUTHOR */
39 /* */
40 /* Yuxin Zhou, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function copies the specified packet into one or more packets */
45 /* allocated from the specified packet pool. */
46 /* */
47 /* INPUT */
48 /* */
49 /* packet_ptr Pointer to source packet */
50 /* new_packet_ptr Pointer for return packet */
51 /* pool_ptr Pointer to packet pool to use */
52 /* for new packet(s) */
53 /* wait_option Timeout option on packet */
54 /* allocate and data append */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* status Actual completion status */
59 /* NX_SUCCESS Successful completion status */
60 /* NX_INVALID_PACKET If zero packet payload or data*/
61 /* to copy */
62 /* CALLS */
63 /* */
64 /* _nx_packet_allocate Allocate data packet */
65 /* _nx_packet_data_append Packet data append service */
66 /* _nx_packet_release Release data packet */
67 /* */
68 /* CALLED BY */
69 /* */
70 /* Application Code */
71 /* */
72 /* RELEASE HISTORY */
73 /* */
74 /* DATE NAME DESCRIPTION */
75 /* */
76 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
77 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
78 /* resulting in version 6.1 */
79 /* */
80 /**************************************************************************/
_nx_packet_copy(NX_PACKET * packet_ptr,NX_PACKET ** new_packet_ptr,NX_PACKET_POOL * pool_ptr,ULONG wait_option)81 UINT _nx_packet_copy(NX_PACKET *packet_ptr, NX_PACKET **new_packet_ptr,
82 NX_PACKET_POOL *pool_ptr, ULONG wait_option)
83 {
84
85 NX_PACKET *work_ptr; /* Working packet pointer */
86 NX_PACKET *source_ptr; /* Source packet pointer */
87 ULONG size; /* Packet data size */
88 UINT status; /* Return status */
89 UINT first_packet; /* First packet flag */
90 UINT data_prepend_offset; /* Data prepend offset */
91 UINT ip_header_offset; /* IP header offset */
92
93 #ifdef TX_ENABLE_EVENT_TRACE
94 TX_TRACE_BUFFER_ENTRY *trace_event;
95 ULONG trace_timestamp;
96 #endif
97
98
99 /* Default the return packet pointer to NULL. */
100 *new_packet_ptr = NX_NULL;
101
102 /* Default the first packet to TRUE. */
103 first_packet = NX_TRUE;
104
105 /* If trace is enabled, insert this event into the trace buffer. */
106 NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_COPY, packet_ptr, 0, pool_ptr, wait_option, NX_TRACE_PACKET_EVENTS, &trace_event, &trace_timestamp);
107
108 /* Determine if there is anything to copy. */
109 if (packet_ptr -> nx_packet_length == 0)
110 {
111
112 /* Empty source packet, return an error. */
113 return(NX_INVALID_PACKET);
114 }
115
116 /* Allocate a new packet from the default packet pool supplied. */
117 /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
118 status = _nx_packet_allocate(pool_ptr, &work_ptr, 0, wait_option);
119
120 /* Determine if the packet was not allocated. */
121 if (status != NX_SUCCESS)
122 {
123
124 /* Return the error code from the packet allocate routine. */
125 return(status);
126 }
127
128 /* Copy the packet interface information. */
129 /*lint -e{644} suppress variable might not be initialized, since "work_ptr" was initialized by _nx_packet_allocate. */
130 work_ptr -> nx_packet_address.nx_packet_interface_ptr = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;
131
132 #ifdef FEATURE_NX_IPV6
133
134 /* Copy the IP version information. */
135 work_ptr -> nx_packet_ip_version = packet_ptr -> nx_packet_ip_version;
136 #endif /* FEATURE_NX_IPV6 */
137
138 #ifdef NX_ENABLE_INTERFACE_CAPABILITY
139
140 /* Copy the packet interface capability. */
141 work_ptr -> nx_packet_interface_capability_flag = packet_ptr -> nx_packet_interface_capability_flag;
142 #endif /* NX_ENABLE_INTERFACE_CAPABILITY */
143
144 #ifdef NX_IPSEC_ENABLE
145 work_ptr -> nx_packet_ipsec_sa_ptr = packet_ptr -> nx_packet_ipsec_sa_ptr;
146 #endif /* NX_IPSEC_ENABLE */
147
148 /* Save the source packet pointer. */
149 source_ptr = packet_ptr;
150
151 #ifndef NX_DISABLE_PACKET_CHAIN
152 /* Loop to copy the original packet's data. */
153 do
154 {
155 #endif /* NX_DISABLE_PACKET_CHAIN */
156
157 /* Check if it is the first packet. */
158 if (first_packet == NX_TRUE)
159 {
160
161 /* Yes, it is, copied the data beginning at data starting position. */
162
163 /* Calculate this packet's data size. */
164 /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
165 size = (ULONG)(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_data_start);
166
167 /* Copy the data from the source packet into the new packet using
168 the data append feature. */
169 status = _nx_packet_data_append(work_ptr, packet_ptr -> nx_packet_data_start, size, pool_ptr, wait_option);
170 }
171 else
172 {
173
174 /* Calculate this packet's data size. */
175 /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
176 size = (ULONG)(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr);
177
178 /* Copy the data from the source packet into the new packet using
179 the data append feature. */
180 status = _nx_packet_data_append(work_ptr, packet_ptr -> nx_packet_prepend_ptr, size, pool_ptr, wait_option);
181 }
182
183 /* Determine if there was an error in the data append. */
184 if (status != NX_SUCCESS)
185 {
186
187 /* An error is present, release the new packet. */
188 _nx_packet_release(work_ptr);
189
190 /* Return the error code from the packet data append service. */
191 return(status);
192 }
193
194 #ifndef NX_DISABLE_PACKET_CHAIN
195 /* Move to the next packet in the packet chain. */
196 packet_ptr = packet_ptr -> nx_packet_next;
197
198 /* Set the first packet to FALSE. */
199 first_packet = NX_FALSE;
200 } while (packet_ptr);
201 #endif /* NX_DISABLE_PACKET_CHAIN */
202
203 /* Adjust the prepend pointer and data length. */
204 /*lint --e{946} --e{947} --e{732} suppress pointer subtraction, since it is necessary. */
205 data_prepend_offset = (UINT)(source_ptr -> nx_packet_prepend_ptr - source_ptr -> nx_packet_data_start);
206 work_ptr -> nx_packet_prepend_ptr = work_ptr -> nx_packet_data_start + data_prepend_offset;
207 work_ptr -> nx_packet_length = work_ptr -> nx_packet_length - data_prepend_offset;
208
209 /* Set the ip_header information. */
210 ip_header_offset = (UINT)(source_ptr -> nx_packet_ip_header - source_ptr -> nx_packet_data_start);
211 work_ptr -> nx_packet_ip_header = work_ptr -> nx_packet_data_start + ip_header_offset;
212
213 /* Determine if the packet copy was successful. */
214 if (source_ptr -> nx_packet_length != work_ptr -> nx_packet_length)
215 {
216
217 /* An error is present, release the new packet. */
218 _nx_packet_release(work_ptr);
219
220 /* Return an error code. */
221 return(NX_INVALID_PACKET);
222 }
223 else
224 {
225
226 /* Everything is okay, return the new packet pointer. */
227 *new_packet_ptr = work_ptr;
228
229 /* Add debug information. */
230 NX_PACKET_DEBUG(__FILE__, __LINE__, work_ptr);
231
232 /* Update the trace event with the status. */
233 NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_PACKET_COPY, 0, work_ptr, 0, 0);
234
235 /* Return success status. */
236 return(NX_SUCCESS);
237 }
238 }
239
240