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 #include "nx_nd_cache.h"
27 
28 
29 #ifdef FEATURE_NX_IPV6
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _nxd_ipv6_find_default_router_from_address          PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Yuxin Zhou, Microsoft Corporation                                   */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function finds the specific IPv6 default router.               */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    ip_ptr                                IP instance pointer           */
47 /*    router_address                        The specific gateway address  */
48 /*                                            to search for.              */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    default router entry                  Pointer to the default router */
53 /*                                            entry.                      */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    tx_mutex_get                          Obtain IP protection mutex    */
57 /*    tx_mutex_put                          Release IP protection mutex   */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    _nxd_icmpv6_process_na                                              */
62 /*                                                                        */
63 /*  NOTE                                                                  */
64 /*                                                                        */
65 /*    This function acquires the nx_ipv6_protection mutex.                */
66 /*    Make sure the mutex is not locked before entering this function.    */
67 /*                                                                        */
68 /*    This function cannot be called from ISR.                            */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_nxd_ipv6_find_default_router_from_address(NX_IP * ip_ptr,ULONG * router_address)79 NX_IPV6_DEFAULT_ROUTER_ENTRY *_nxd_ipv6_find_default_router_from_address(NX_IP *ip_ptr, ULONG *router_address)
80 {
81 
82 INT                           i;
83 NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry;
84 
85 
86     /* Get exclusive access to the IP task lock. */
87     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
88 
89     /* Start the search at the top of the list...*/
90     i = 0;
91 
92     /* And go through the entire table or until a match is found. */
93     while (i < NX_IPV6_DEFAULT_ROUTER_TABLE_SIZE)
94     {
95 
96         /* Local pointer to table entry. */
97         rt_entry = &ip_ptr -> nx_ipv6_default_router_table[i];
98 
99         /* Does this slot contain a valid router? */
100         if (rt_entry -> nx_ipv6_default_router_entry_flag)
101         {
102 
103             /* Yes, check if it matches the specified router address. */
104             if (CHECK_IPV6_ADDRESSES_SAME(router_address, rt_entry -> nx_ipv6_default_router_entry_router_address))
105             {
106 
107 
108                 /* Yes it does, we can release the IP protection. */
109                 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
110 
111                 /* Return the pointer to this router entry. */
112                 return(rt_entry);
113             }
114         }
115 
116         i++;
117     }
118 
119     /* Release the lock. */
120     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
121 
122     /* Return a null pointer indicating no matching router found. */
123     return(NX_NULL);
124 }
125 
126 #endif /* FEATURE_NX_IPV6 */
127 
128