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 /** NetX Component                                                        */
14 /**                                                                       */
15 /**   Internet Protocol version 6 Default Router Table (IPv6 router)      */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 #define NX_SOURCE_CODE
20 
21 
22 /* Include necessary system files.  */
23 
24 #include "nx_api.h"
25 #include "nx_ipv6.h"
26 #ifdef FEATURE_NX_IPV6
27 #include "nx_nd_cache.h"
28 #endif /* FEATURE_NX_IPV6 */
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nxd_ipv6_default_router_number_of_entries_get      PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function gets router entries from the default IPv6 routing     */
44 /*    table.                                                              */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP instance pointer           */
49 /*    if_index                              Interface Index               */
50 /*    num_entries                           Destination for number of     */
51 /*                                            IPv6 routers on a specified */
52 /*                                            network interface           */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    tx_mutex_get                          Obtain protection mutex       */
61 /*    tx_mutex_put                          Release protection mutex      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application code                                                    */
66 /*                                                                        */
67 /*  NOTE                                                                  */
68 /*                                                                        */
69 /*    This function cannot be called from ISR.                            */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
76 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_nxd_ipv6_default_router_number_of_entries_get(NX_IP * ip_ptr,UINT if_index,UINT * num_entries)80 UINT  _nxd_ipv6_default_router_number_of_entries_get(NX_IP *ip_ptr, UINT if_index, UINT *num_entries)
81 {
82 #ifdef FEATURE_NX_IPV6
83 
84 UINT                          entries;
85 UINT                          i;
86 NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry;
87 
88 
89     /* Obtain protection on this IP instance for access into the router table. */
90 
91     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
92 
93     /* Initialize the return value to be 0. */
94     entries = 0;
95 
96     for (i = 0; i < NX_IPV6_DEFAULT_ROUTER_TABLE_SIZE; i++)
97     {
98 
99         rt_entry = &ip_ptr -> nx_ipv6_default_router_table[i];
100         /* Skip invalid entries. */
101         if (rt_entry -> nx_ipv6_default_router_entry_flag == 0)
102         {
103             continue;
104         }
105 
106         /* Match the interface. */
107         if (rt_entry -> nx_ipv6_default_router_entry_interface_ptr -> nx_interface_index != if_index)
108         {
109             continue;
110         }
111 
112         /* Find router entriy. */
113         entries++;
114     }
115 
116     /* Release the mutex.  */
117     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
118 
119     *num_entries = entries;
120 
121     return(NX_SUCCESS);
122 
123 #else /* !FEATURE_NX_IPV6 */
124     NX_PARAMETER_NOT_USED(ip_ptr);
125     NX_PARAMETER_NOT_USED(if_index);
126     NX_PARAMETER_NOT_USED(num_entries);
127 
128     return(NX_NOT_SUPPORTED);
129 
130 #endif /* FEATURE_NX_IPV6 */
131 }
132 
133