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