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 /**   Neighbor Discovery Cache                                            */
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_nd_cache.h"
29 #ifdef FEATURE_NX_IPV6
30 #include "nx_ipv6.h"
31 #endif /* FEATURE_NX_IPV6 */
32 
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nxd_nd_cache_hardware_address_find                 PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function finds the hardware address in the neighbor discovery  */
48 /*    (ND) cache table mapped to the input IP address.                    */
49 /*    address.                                                            */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    ip_ptr                                IP instance pointer           */
54 /*    ip_address                            Pointer to the IP address to  */
55 /*                                            search for                  */
56 /*    physical_msw                          Physical address, most        */
57 /*                                            signifcant word             */
58 /*    physical_lsw                          Physical address, least       */
59 /*                                            signifcant word             */
60 /*    interface_index                       Pointer to interface through  */
61 /*                                            which the neighbor can be   */
62 /*                                            reached.                    */
63 /*                                                                        */
64 /*  OUTPUT                                                                */
65 /*                                                                        */
66 /*    status                                Completion status             */
67 /*                                                                        */
68 /*  CALLS                                                                 */
69 /*                                                                        */
70 /*    tx_mutex_get                          Obtain protection mutex       */
71 /*    tx_mutex_put                          Release protection mutex      */
72 /*    _nx_nd_cache_find_entry               Find entry by IP address.     */
73 /*                                                                        */
74 /*  CALLED BY                                                             */
75 /*                                                                        */
76 /*    Application Code                                                    */
77 /*                                                                        */
78 /*  RELEASE HISTORY                                                       */
79 /*                                                                        */
80 /*    DATE              NAME                      DESCRIPTION             */
81 /*                                                                        */
82 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
83 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
84 /*                                            resulting in version 6.1    */
85 /*                                                                        */
86 /**************************************************************************/
_nxd_nd_cache_hardware_address_find(NX_IP * ip_ptr,NXD_ADDRESS * ip_address,ULONG * physical_msw,ULONG * physical_lsw,UINT * interface_index)87 UINT  _nxd_nd_cache_hardware_address_find(NX_IP *ip_ptr,
88                                           NXD_ADDRESS *ip_address,
89                                           ULONG *physical_msw,
90                                           ULONG *physical_lsw,
91                                           UINT *interface_index)
92 {
93 #ifdef FEATURE_NX_IPV6
94 
95 ND_CACHE_ENTRY *entry;
96 
97     /* Obtain the protection. */
98     tx_mutex_get(&ip_ptr -> nx_ip_protection, TX_WAIT_FOREVER);
99 
100     /* Find ND cache entry for a given IP address. */
101     if (_nx_nd_cache_find_entry(ip_ptr, ip_address -> nxd_ip_address.v6, &entry))
102     {
103         /* Release the protection. */
104         tx_mutex_put(&ip_ptr -> nx_ip_protection);
105 
106         /* Not found */
107         return(NX_ENTRY_NOT_FOUND);
108     }
109 
110     /* Construct the MAC address. */
111     /*lint -e{644} suppress variable might not be initialized, since "entry" was initialized when the return value of _nx_nd_cache_find_entry is NX_SUCCESS. */
112     *physical_msw = ((ULONG)entry -> nx_nd_cache_mac_addr[0]) << 8 | (ULONG)entry -> nx_nd_cache_mac_addr[1];
113     *physical_lsw = ((ULONG)entry -> nx_nd_cache_mac_addr[2]) << 24 | ((ULONG)entry -> nx_nd_cache_mac_addr[3]) << 16 |
114         ((ULONG)entry -> nx_nd_cache_mac_addr[4]) << 8 | (ULONG)entry -> nx_nd_cache_mac_addr[5];
115 
116     /* Get the interface_index.  */
117     *interface_index = (entry -> nx_nd_cache_interface_ptr -> nx_interface_index);
118 
119     /* Release the protection. */
120     tx_mutex_put(&ip_ptr -> nx_ip_protection);
121 
122     return(NX_SUCCESS);
123 
124 #else /* !FEATURE_NX_IPV6 */
125     NX_PARAMETER_NOT_USED(ip_ptr);
126     NX_PARAMETER_NOT_USED(ip_address);
127     NX_PARAMETER_NOT_USED(physical_msw);
128     NX_PARAMETER_NOT_USED(physical_lsw);
129     NX_PARAMETER_NOT_USED(interface_index);
130 
131     return(NX_NOT_SUPPORTED);
132 
133 #endif /* FEATURE_NX_IPV6 */
134 }
135 
136