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 #include "nx_icmpv6.h"
30 #endif /* FEATURE_NX_IPV6 */
31 
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _nxd_ipv6_default_router_delete                      PORTABLE C     */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yuxin Zhou, Microsoft Corporation                                   */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function deletes an IPv6 routing table entry.                  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    ip_ptr                                Pointer to IP control block   */
51 /*    router_addr                           Pointer to router  address to */
52 /*                                            delete                      */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    NX_SUCCESS                            Successful 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 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_nxd_ipv6_default_router_delete(NX_IP * ip_ptr,NXD_ADDRESS * router_address)76 UINT _nxd_ipv6_default_router_delete(NX_IP *ip_ptr, NXD_ADDRESS *router_address)
77 {
78 #ifdef FEATURE_NX_IPV6
79 
80 INT                           i;
81 NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry;
82 
83 
84     NX_TRACE_IN_LINE_INSERT(NXD_TRACE_IPV6_DEFAULT_ROUTER_DELETE,
85                             ip_ptr, router_address -> nxd_ip_address.v6[3], 0, 0, NX_TRACE_IP_EVENTS, 0, 0);
86 
87     /* Obtain protection on this IP instance for access into the default router table. */
88     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
89 
90     /* If our default route table is empty, just return */
91     if (ip_ptr -> nx_ipv6_default_router_table_size == 0)
92     {
93 
94         /* Release the mutex.  */
95         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
96 
97         return(NX_SUCCESS);
98     }
99 
100     /* Search the entire table for a matching entry. */
101     for (i = 0; i < NX_IPV6_DEFAULT_ROUTER_TABLE_SIZE; i++)
102     {
103 
104         /*Set local pointer for convenience. */
105         rt_entry = &ip_ptr -> nx_ipv6_default_router_table[i];
106 
107         /* Does this slot contain a router? */
108         if (rt_entry -> nx_ipv6_default_router_entry_flag & NX_IPV6_ROUTE_TYPE_VALID)
109         {
110 
111             /* Does it match the router address to search for? */
112             if (CHECK_IPV6_ADDRESSES_SAME(router_address -> nxd_ip_address.v6,
113                                           rt_entry -> nx_ipv6_default_router_entry_router_address))
114             {
115 
116                 /* Yes, does it have a pointer into the cache table? */
117                 if (rt_entry -> nx_ipv6_default_router_entry_neighbor_cache_ptr)
118                 {
119 
120                     /* Clear the router status. This will enable the entry
121                        to time out eventually. */
122                     rt_entry -> nx_ipv6_default_router_entry_neighbor_cache_ptr -> nx_nd_cache_is_router = NX_NULL;
123                 }
124 
125                 /* Clean any entries in the destination table for this router.  */
126                 _nx_invalidate_destination_entry(ip_ptr, rt_entry -> nx_ipv6_default_router_entry_router_address);
127 
128                 /* Mark the entry as empty. */
129                 rt_entry -> nx_ipv6_default_router_entry_flag = 0;
130 
131                 /* Clear the interface pointer .*/
132                 rt_entry -> nx_ipv6_default_router_entry_interface_ptr = NX_NULL;
133 
134                 /* Decrease the count of available routers. */
135                 ip_ptr -> nx_ipv6_default_router_table_size--;
136 
137                 /* Release the mutex.  */
138                 tx_mutex_put(&(ip_ptr -> nx_ip_protection));
139 
140                 return(NX_SUCCESS);
141             }
142         }
143     }
144 
145     /* Release the mutex.  */
146     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
147 
148     return(NX_NOT_FOUND);
149 
150 #else /* !FEATURE_NX_IPV6 */
151     NX_PARAMETER_NOT_USED(ip_ptr);
152     NX_PARAMETER_NOT_USED(router_address);
153 
154     return(NX_NOT_SUPPORTED);
155 
156 #endif /* FEATURE_NX_IPV6 */
157 }
158 
159