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