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_ipv6.h"
30 #include "nx_nd_cache.h"
31 
32 #ifdef FEATURE_NX_IPV6
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    nx_nd_cache_find_entry                              PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This internal function finds an entry in the ND cache that is       */
48 /*    mapped to the specified IPv6 address.                               */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    ip_ptr                   IP instance pointer                        */
53 /*    dest_ip                  The IP address to match                    */
54 /*    nd_cache_entry           User specified storage space of pointer to */
55 /*                                the corresponding ND cache.             */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    status                   NX_SUCCESS: The ND cache entry is located. */
60 /*                                nd_cache_entry contains valid value.    */
61 /*                             NX_NOT_SUCCESSFUL:  The ND cache entry     */
62 /*                                cannot be found, or nd_cache_entry is   */
63 /*                                NULL.                                   */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    None                                                                */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application Code                                                    */
72 /*                                                                        */
73 /*  RELEASE HISTORY                                                       */
74 /*                                                                        */
75 /*    DATE              NAME                      DESCRIPTION             */
76 /*                                                                        */
77 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
78 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
79 /*                                            resulting in version 6.1    */
80 /*                                                                        */
81 /**************************************************************************/
_nx_nd_cache_find_entry(NX_IP * ip_ptr,ULONG * dest_ip,ND_CACHE_ENTRY ** nd_cache_entry)82 UINT _nx_nd_cache_find_entry(NX_IP *ip_ptr,
83                              ULONG *dest_ip, ND_CACHE_ENTRY **nd_cache_entry)
84 {
85 UINT i;
86 UINT index;
87 
88     /* Initialize the return value. */
89     *nd_cache_entry = NX_NULL;
90 
91     /* Compute a simple hash based on the dest_ip */
92     index = (UINT)((dest_ip[0] + dest_ip[1] + dest_ip[2] + dest_ip[3]) %
93                    (NX_IPV6_NEIGHBOR_CACHE_SIZE));
94 
95     for (i = 0; i < NX_IPV6_NEIGHBOR_CACHE_SIZE; i++)
96     {
97 
98         if ((ip_ptr -> nx_ipv6_nd_cache[index].nx_nd_cache_nd_status != ND_CACHE_STATE_INVALID) &&
99             (ip_ptr -> nx_ipv6_nd_cache[index].nx_nd_cache_interface_ptr) &&
100             (CHECK_IPV6_ADDRESSES_SAME(&ip_ptr -> nx_ipv6_nd_cache[index].nx_nd_cache_dest_ip[0], dest_ip)))
101         {
102 
103             /* find the entry */
104             *nd_cache_entry = &ip_ptr -> nx_ipv6_nd_cache[index];
105 
106             return(NX_SUCCESS);
107         }
108 
109         index++;
110 
111         /* Check for overflow */
112         if (index == NX_IPV6_NEIGHBOR_CACHE_SIZE)
113         {
114             index = 0;
115         }
116     }
117 
118     return(NX_NOT_SUCCESSFUL);
119 }
120 
121 #endif /* FEATURE_NX_IPV6 */
122 
123