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