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 /**   Internet Protocol (IP)                                              */
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_ip.h"
29 #include "nx_ipv6.h"
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxd_ipv6_address_get                               PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*   This function retrieves the IP address at the specified address list */
44 /*  index of the physical host interface specified by the interface index.*/
45 /*  Returns an IPv6 address and prefix length into the specified pointers.*/
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP control block pointer      */
50 /*    address_index                         Index to the IPv6 address     */
51 /*                                            table                       */
52 /*    ip_address                            Pointer to IP address         */
53 /*                                            structure to be filled      */
54 /*    prefix_length                         Pointer to prefix length      */
55 /*                                            to return                   */
56 /*    interface_index                       The index to the physical     */
57 /*                                            interface this address      */
58 /*                                            belongs to                  */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    status                                Completion status             */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    tx_mutex_get                          Get protection mutex          */
67 /*    tx_mutex_put                          Put protection mutex          */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application Code                                                    */
72 /*                                                                        */
73 /*  NOTE                                                                  */
74 /*    Application needs to fill in the ip_address.nxd_ip_version field.   */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
81 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
82 /*                                            resulting in version 6.1    */
83 /*                                                                        */
84 /**************************************************************************/
_nxd_ipv6_address_get(NX_IP * ip_ptr,UINT address_index,NXD_ADDRESS * ip_address,ULONG * prefix_length,UINT * interface_index)85 UINT  _nxd_ipv6_address_get(NX_IP *ip_ptr, UINT address_index, NXD_ADDRESS *ip_address,
86                             ULONG *prefix_length, UINT *interface_index)
87 {
88 #ifdef FEATURE_NX_IPV6
89 
90 TX_INTERRUPT_SAVE_AREA
91 
92 UINT              status;
93 NXD_IPV6_ADDRESS *interface_ipv6_address_next;
94 #ifdef TX_ENABLE_EVENT_TRACE
95 ULONG             ip_address_lsw;
96 #endif /* TX_ENABLE_EVENT_TRACE */
97 
98 
99     status = NX_SUCCESS;
100 
101     /* Get mutex protection.  */
102     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
103     /* Disable interrupts.  */
104     TX_DISABLE
105 
106     /* Get the ip address.  */
107     interface_ipv6_address_next = &ip_ptr -> nx_ipv6_address[address_index];
108 
109     /* Check if this is a valid IP address. */
110     if (interface_ipv6_address_next -> nxd_ipv6_address_state != NX_IPV6_ADDR_STATE_VALID)
111     {
112 
113         /* No, the address is not validated yet. */
114 
115         /* Zero out the return values. */
116         *prefix_length = 0;
117         SET_UNSPECIFIED_ADDRESS(ip_address -> nxd_ip_address.v6);
118 
119         /* Return the error status. */
120         status = NX_NO_INTERFACE_ADDRESS;
121     }
122     else
123     {
124 
125         /* Record the interface index.  */
126         *interface_index = (UINT)ip_ptr -> nx_ipv6_address[address_index].nxd_ipv6_address_attached -> nx_interface_index;
127 
128         /* We have a valid address. Mark with the IPv6 stamp. */
129         ip_address -> nxd_ip_version = NX_IP_VERSION_V6;
130 
131         /* Copy interface IP address from the address entry in the IP address table into the return address structure. */
132         COPY_IPV6_ADDRESS(interface_ipv6_address_next -> nxd_ipv6_address,
133                           ip_address -> nxd_ip_address.v6);
134 
135         /* Copy interface IP address prefix length from the address entry in the IP address table into the return prefix length. */
136         *prefix_length = interface_ipv6_address_next -> nxd_ipv6_address_prefix_length;
137     }
138 
139     /* Restore interrupts.  */
140     TX_RESTORE
141 
142     /* Release mutex protection.  */
143     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
144 
145 #ifdef TX_ENABLE_EVENT_TRACE
146     ip_address_lsw = ip_address -> nxd_ip_address.v6[3];
147 
148     /* If trace is enabled, insert this info into the trace buffer.  */
149     NX_TRACE_IN_LINE_INSERT(NXD_TRACE_IPV6_INTERFACE_ADDRESS_GET, ip_ptr, ip_address_lsw, *prefix_length, address_index, NX_TRACE_IP_EVENTS, 0, 0);
150 #endif /* TX_ENABLE_EVENT_TRACE */
151 
152     /* Return completion status.  */
153     return(status);
154 
155 #else /* !FEATURE_NX_IPV6 */
156     NX_PARAMETER_NOT_USED(ip_ptr);
157     NX_PARAMETER_NOT_USED(address_index);
158     NX_PARAMETER_NOT_USED(ip_address);
159     NX_PARAMETER_NOT_USED(prefix_length);
160     NX_PARAMETER_NOT_USED(interface_index);
161 
162     return(NX_NOT_SUPPORTED);
163 
164 #endif /* FEATURE_NX_IPV6 */
165 }
166 
167