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 /** User Datagram Protocol (UDP) */
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_udp.h"
30 #include "nx_ipv4.h"
31 #ifdef FEATURE_NX_IPV6
32 #include "nx_ipv6.h"
33 #endif
34
35
36 /**************************************************************************/
37 /* */
38 /* FUNCTION RELEASE */
39 /* */
40 /* _nxd_udp_packet_info_extract PORTABLE C */
41 /* 6.1 */
42 /* AUTHOR */
43 /* */
44 /* Yuxin Zhou, Microsoft Corporation */
45 /* */
46 /* DESCRIPTION */
47 /* */
48 /* This function extracts the source IP address, protocol, (the */
49 /* protocol is always UDP), port number and the incoming interface */
50 /* from the incoming packet. */
51 /* */
52 /* INPUT */
53 /* */
54 /* packet_ptr Pointer to UDP packet */
55 /* ip_address Pointer to sender IP address */
56 /* protocol Pointer to packet protocol. */
57 /* Always 17 (UDP) */
58 /* port Pointer to sender source port */
59 /* interface_index Pointer to interface index */
60 /* packet received on */
61 /* */
62 /* OUTPUT */
63 /* */
64 /* status Completion status */
65 /* */
66 /* CALLS */
67 /* */
68 /* None */
69 /* */
70 /* CALLED BY */
71 /* */
72 /* Application Code */
73 /* */
74 /* RELEASE HISTORY */
75 /* */
76 /* DATE NAME DESCRIPTION */
77 /* */
78 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
79 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
80 /* resulting in version 6.1 */
81 /* */
82 /**************************************************************************/
_nxd_udp_packet_info_extract(NX_PACKET * packet_ptr,NXD_ADDRESS * ip_address,UINT * protocol,UINT * port,UINT * interface_index)83 UINT _nxd_udp_packet_info_extract(NX_PACKET *packet_ptr, NXD_ADDRESS *ip_address,
84 UINT *protocol, UINT *port, UINT *interface_index)
85 {
86
87 ULONG *temp_ptr;
88 UINT source_port;
89 NX_INTERFACE *nx_interface;
90 #ifndef NX_DISABLE_IPV4
91 NX_IPV4_HEADER *ipv4_header;
92 #endif /* !NX_DISABLE_IPV4 */
93 #ifdef TX_ENABLE_EVENT_TRACE
94 ULONG address = 0;
95 #endif /* TX_ENABLE_EVENT_TRACE */
96 #ifdef FEATURE_NX_IPV6
97 NX_IPV6_HEADER *ipv6_header;
98 #endif /* FEATURE_NX_IPV6 */
99
100
101 if (ip_address)
102 {
103
104 #ifndef NX_DISABLE_IPV4
105 if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
106 {
107
108 /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
109 ipv4_header = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;
110
111 ip_address -> nxd_ip_version = NX_IP_VERSION_V4;
112
113 /* At this point, the IP address in the IPv4 header is in host byte order. */
114 ip_address -> nxd_ip_address.v4 = ipv4_header -> nx_ip_header_source_ip;
115
116 #ifdef TX_ENABLE_EVENT_TRACE
117 address = ip_address -> nxd_ip_address.v4;
118 #endif /* TX_ENABLE_EVENT_TRACE */
119 }
120 else
121 #endif /* !NX_DISABLE_IPV4 */
122
123 #ifdef FEATURE_NX_IPV6
124 if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
125 {
126
127 /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
128 ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;
129
130 ip_address -> nxd_ip_version = NX_IP_VERSION_V6;
131
132 /* At this point, the IP address in the IPv6 header is in host byte order. */
133 COPY_IPV6_ADDRESS(ipv6_header -> nx_ip_header_source_ip, ip_address -> nxd_ip_address.v6);
134
135 #ifdef TX_ENABLE_EVENT_TRACE
136 address = ip_address -> nxd_ip_address.v6[3];
137 #endif /* TX_ENABLE_EVENT_TRACE*/
138 }
139 else
140 #endif /* FEATURE_NX_IPV6 */
141 {
142
143 /* Invalid IP version . */
144 return(NX_INVALID_PACKET);
145 }
146 }
147
148 /* Build an address to the current top of the packet. */
149 /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */
150 temp_ptr = (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
151
152 /* Pickup the source port. */
153 source_port = (UINT)(*(temp_ptr - 2) >> NX_SHIFT_BY_16);
154 if (port != NX_NULL)
155 {
156 *port = source_port;
157 }
158
159 if (protocol != NX_NULL)
160 {
161 *protocol = 0x11;
162 }
163
164 /* If trace is enabled, insert this event into the trace buffer. */
165 NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOURCE_EXTRACT, packet_ptr, address, source_port, 0, NX_TRACE_PACKET_EVENTS, 0, 0);
166
167 if (interface_index == NX_NULL)
168 {
169 return(NX_SUCCESS);
170 }
171
172 /* Search for interface index number. Initialize interface value as
173 invalid (0xFFFFFFFF). Once we find valid interface, we will update
174 the returned value. */
175 *interface_index = 0xFFFFFFFF;
176
177 if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
178 {
179 nx_interface = packet_ptr -> nx_packet_address.nx_packet_interface_ptr;
180 }
181 #ifdef FEATURE_NX_IPV6
182 else if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
183 {
184 if (packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr == NX_NULL)
185 {
186
187 /* No interface attached. Done here, and return success. */
188 return(NX_SUCCESS);
189 }
190 else
191 {
192 nx_interface = packet_ptr -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached;
193 }
194 }
195 #endif /* FEATURE_NX_IPV6 */
196 else
197 {
198 return(NX_SUCCESS);
199 }
200
201 if (nx_interface == NX_NULL)
202 {
203
204 /* No interface attached. Done here, and return success. */
205 return(NX_SUCCESS);
206 }
207
208 *interface_index = (UINT)nx_interface -> nx_interface_index;
209
210 return(NX_SUCCESS);
211 }
212
213