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 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _nxd_nd_cache_entry_ip_address_find                 PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Yuxin Zhou, Microsoft Corporation                                   */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds an IPv6 address in the neighbor discovery       */
46 /*    cache table based on user-speicified MAC address.                   */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    ip_ptr                                Pointer to IP instance        */
51 /*    ip_address                            Pointer to the IP address     */
52 /*                                            to search for               */
53 /*    physical_msw                          Physical address, most        */
54 /*                                            significant word            */
55 /*    physical_lsw                          Physical address, least       */
56 /*                                            significant word            */
57 /*    interface_index                       Index to the network          */
58 /*                                            interface through which the */
59 /*                                            node is reachable.          */
60 /*                                                                        */
61 /*  OUTPUT                                                                */
62 /*                                                                        */
63 /*    Status                                Completion status             */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    _nx_nd_cache_find_entry_by_mac_addr   Internal find IP address in   */
68 /*                                          ND cache table mapped to      */
69 /*                                            input by mac address.       */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Application Code                                                    */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
80 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_nxd_nd_cache_ip_address_find(NX_IP * ip_ptr,NXD_ADDRESS * ip_address,ULONG physical_msw,ULONG physical_lsw,UINT * interface_index)84 UINT  _nxd_nd_cache_ip_address_find(NX_IP *ip_ptr,
85                                     NXD_ADDRESS *ip_address,
86                                     ULONG physical_msw,
87                                     ULONG physical_lsw,
88                                     UINT *interface_index)
89 {
90 #ifdef FEATURE_NX_IPV6
91 
92 ND_CACHE_ENTRY *entry;
93 
94     /* Find ND entry according to the given MAC address. */
95     /* Obtain the protection. */
96     tx_mutex_get(&ip_ptr -> nx_ip_protection, TX_WAIT_FOREVER);
97 
98     if (_nx_nd_cache_find_entry_by_mac_addr(ip_ptr, physical_msw, physical_lsw, &entry) != NX_SUCCESS)
99     {
100 
101         /* Release the protection. */
102         tx_mutex_put(&ip_ptr -> nx_ip_protection);
103 
104         /* No such MAC address found in cache table. */
105         return(NX_ENTRY_NOT_FOUND);
106     }
107 
108     /* Copy the IP address and version from the cache entry into the address structure. */
109     ip_address -> nxd_ip_version = NX_IP_VERSION_V6;
110 
111     /*lint -e{644} suppress variable might not be initialized, since "entry" was initialized as long as previous call is NX_SUCCESS. */
112     COPY_IPV6_ADDRESS(entry -> nx_nd_cache_dest_ip, ip_address -> nxd_ip_address.v6);
113 
114     /* If trace is enabled, insert this event into the trace buffer. */
115     NX_TRACE_IN_LINE_INSERT(NX_TRACE_ND_CACHE_IP_ADDRESS_FIND, ip_ptr, ip_address -> nxd_ip_address.v6[3], physical_msw, physical_lsw, NX_TRACE_ARP_EVENTS, 0, 0);
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     /* Successful completion*/
124     return(NX_SUCCESS);
125 
126 #else /* !FEATURE_NX_IPV6 */
127     NX_PARAMETER_NOT_USED(ip_ptr);
128     NX_PARAMETER_NOT_USED(ip_address);
129     NX_PARAMETER_NOT_USED(physical_msw);
130     NX_PARAMETER_NOT_USED(physical_lsw);
131     NX_PARAMETER_NOT_USED(interface_index);
132 
133     return(NX_NOT_SUPPORTED);
134 
135 #endif /* FEATURE_NX_IPV6 */
136 }
137 
138