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_entry_get                   PORTABLE C     */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function gets a router entry from the default IPv6 routing     */
44 /*    table.                                                              */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP instance pointer           */
49 /*    interface_index                       Index to the interface        */
50 /*    entry_index                           Entry Index                   */
51 /*    router_addr                           Router IPv6 Address           */
52 /*    router_lifetime                       Pointer to router life time   */
53 /*    prefix_length                         Pointer to prefix length      */
54 /*    configuration_method                  Pointer to the information    */
55 /*                                            on how the entry was        */
56 /*                                            configured                  */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    status                                Completion status             */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    tx_mutex_get                          Obtain protection mutex       */
65 /*    tx_mutex_put                          Release protection mutex      */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application code                                                    */
70 /*    _nxd_ipv6_default_router_get                                        */
71 /*                                                                        */
72 /*  NOTE                                                                  */
73 /*                                                                        */
74 /*                                                                        */
75 /*    This function cannot be called from ISR.                            */
76 /*                                                                        */
77 /*  RELEASE HISTORY                                                       */
78 /*                                                                        */
79 /*    DATE              NAME                      DESCRIPTION             */
80 /*                                                                        */
81 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
82 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
83 /*                                            resulting in version 6.1    */
84 /*                                                                        */
85 /**************************************************************************/
_nxd_ipv6_default_router_entry_get(NX_IP * ip_ptr,UINT interface_index,UINT entry_index,NXD_ADDRESS * router_addr,ULONG * router_lifetime,ULONG * prefix_length,ULONG * configuration_method)86 UINT  _nxd_ipv6_default_router_entry_get(NX_IP *ip_ptr, UINT interface_index, UINT entry_index,
87                                          NXD_ADDRESS *router_addr, ULONG *router_lifetime,
88                                          ULONG *prefix_length, ULONG *configuration_method)
89 {
90 #ifdef FEATURE_NX_IPV6
91 
92 UINT                          status;
93 UINT                          i;
94 NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry;
95 
96 
97     /* Obtain protection on this IP instance for access into the router table. */
98 
99     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
100 
101     /* Initialize the return value to be NX_NOT_FOUND.  Once the router address is set,
102        the return value is set to NX_SUCCESS. */
103     status = NX_NOT_FOUND;
104 
105     for (i = 0; i < NX_IPV6_DEFAULT_ROUTER_TABLE_SIZE; i++)
106     {
107 
108         rt_entry = &ip_ptr -> nx_ipv6_default_router_table[i];
109         /* Skip invalid entries. */
110         if (rt_entry -> nx_ipv6_default_router_entry_flag == 0)
111         {
112             continue;
113         }
114 
115         /* Match the interface. */
116         if (rt_entry -> nx_ipv6_default_router_entry_interface_ptr -> nx_interface_index != (UCHAR)interface_index)
117         {
118             continue;
119         }
120 
121         /* Match the entry index */
122         if (entry_index > 0)
123         {
124             entry_index--;
125         }
126         else
127         {
128 
129             if (router_addr)
130             {
131                 router_addr -> nxd_ip_version = NX_IP_VERSION_V6;
132 
133                 COPY_IPV6_ADDRESS(&(rt_entry -> nx_ipv6_default_router_entry_router_address[0]),
134                                   &router_addr -> nxd_ip_address.v6[0]);
135             }
136 
137 
138             if (router_lifetime)
139             {
140                 *router_lifetime = rt_entry -> nx_ipv6_default_router_entry_life_time;
141             }
142 
143             if (prefix_length)
144             {
145                 if ((rt_entry -> nx_ipv6_default_router_entry_router_address[0] & (UINT)0xFFC00000) == (UINT)0xFE800000)
146                 {
147                     *prefix_length = 10;
148                 }
149                 else
150                 {
151                     *prefix_length = 64;
152                 }
153             }
154 
155             if (configuration_method)
156             {
157                 *configuration_method = (rt_entry -> nx_ipv6_default_router_entry_flag &
158                                          (UCHAR)(~NX_IPV6_ROUTE_TYPE_VALID));
159             }
160 
161             status = NX_SUCCESS;
162 
163             break;
164         }
165     }
166 
167     /* Release the mutex.  */
168     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
169 
170     return(status);
171 
172 #else /* !FEATURE_NX_IPV6 */
173     NX_PARAMETER_NOT_USED(ip_ptr);
174     NX_PARAMETER_NOT_USED(interface_index);
175     NX_PARAMETER_NOT_USED(entry_index);
176     NX_PARAMETER_NOT_USED(router_addr);
177     NX_PARAMETER_NOT_USED(router_lifetime);
178     NX_PARAMETER_NOT_USED(prefix_length);
179     NX_PARAMETER_NOT_USED(configuration_method);
180 
181     return(NX_NOT_SUPPORTED);
182 
183 #endif /* FEATURE_NX_IPV6 */
184 }
185 
186