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 /**   User Datagram Protocol (UDP)                                        */
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_udp.h"
29 #include "nx_ipv6.h"
30 #include "nx_ipv4.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nxd_udp_socket_extract                             PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function extracts the source IP address and UDP port number    */
46 /*    from the packet received on a host UDP socket. It is up to the      */
47 /*    caller to verify the source port and ip address are valid (non null)*/
48 /*    data.                                                               */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    packet_ptr                            Pointer to UDP packet pointer */
53 /*    ip_address                            Pointer to source IP address  */
54 /*    port                                  Pointer to source UDP port    */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    status                                Actual completion status      */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_nxd_udp_source_extract(NX_PACKET * packet_ptr,NXD_ADDRESS * ip_address,UINT * port)77 UINT  _nxd_udp_source_extract(NX_PACKET *packet_ptr, NXD_ADDRESS *ip_address, UINT *port)
78 {
79 
80 #ifdef TX_ENABLE_EVENT_TRACE
81 ULONG  ip_address_word3 = 0;
82 ULONG  ip_version;
83 #endif
84 ULONG *temp_ptr;
85 
86 
87     /* Build an address to the current top of the packet.  */
88     /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
89     temp_ptr =  (ULONG *)packet_ptr -> nx_packet_prepend_ptr;
90 
91     /* Pickup the source port from the UDP header.  */
92     *port =  (UINT)(*(temp_ptr - 2) >> NX_SHIFT_BY_16);
93 
94     /* Determine IPv4 or IPv6 connectivity. */
95     ip_address -> nxd_ip_version = packet_ptr -> nx_packet_ip_version;
96 
97 #ifndef NX_DISABLE_IPV4
98     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
99     {
100 
101     NX_IPV4_HEADER *ipv4_header;
102 
103         /* Obtain the IPv4 header. */
104         /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
105         ipv4_header = (NX_IPV4_HEADER *)packet_ptr -> nx_packet_ip_header;
106 
107         /* Pickup the source IP address.  */
108         ip_address -> nxd_ip_address.v4 =  ipv4_header -> nx_ip_header_source_ip;
109 
110 #ifdef TX_ENABLE_EVENT_TRACE
111         ip_version = NX_IP_VERSION_V4;
112         ip_address_word3 = ip_address -> nxd_ip_address.v4;
113 #endif /* TX_ENABLE_EVENT_TRACE */
114     }
115 #endif /* NX_DISABLE_IPV4 */
116 #ifdef FEATURE_NX_IPV6
117     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
118     {
119     NX_IPV6_HEADER *ipv6_header;
120 
121 
122         /* Obtain the IPv6 header. */
123         /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
124         ipv6_header = (NX_IPV6_HEADER *)packet_ptr -> nx_packet_ip_header;
125 
126         COPY_IPV6_ADDRESS(ipv6_header -> nx_ip_header_source_ip,
127                           ip_address -> nxd_ip_address.v6);
128 
129 #ifdef TX_ENABLE_EVENT_TRACE
130         ip_version = NX_IP_VERSION_V6;
131         ip_address_word3 = ip_address -> nxd_ip_address.v6[3];
132 #endif /* TX_ENABLE_EVENT_TRACE */
133     }
134 #endif /* FEATURE_NX_IPV6 */
135 
136 #ifdef TX_ENABLE_EVENT_TRACE
137 
138     /* If trace is enabled, insert this event into the trace buffer.  */
139     NX_TRACE_IN_LINE_INSERT(NXD_TRACE_UDP_SOURCE_EXTRACT, packet_ptr, ip_version, ip_address_word3, *port, NX_TRACE_UDP_EVENTS, 0, 0);
140 #endif /* TX_ENABLE_EVENT_TRACE */
141 
142     return(NX_SUCCESS);
143 }
144 
145