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 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Internet Protocol (IP)                                              */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_ipv6.h"
30 #ifdef FEATURE_NX_IPV6
31 #include "nx_nd_cache.h"
32 #include "nx_icmpv6.h"
33 #endif /* FEATURE_NX_IPV6 */
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nxd_ipv6_disable                                   PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function disables the IPv6 for the specified IP instance.      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    ip_ptr                                IP instance pointer           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*    NX_ALREADY_ENABLED                    IPv6 already enabled on IP    */
57 /*    NX_NOT_SUPPORTED                      IPv6 not supported on this IP */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _nx_ipv6_multicast_leave              Leave the multicast group     */
62 /*    _nxd_ipv6_default_router_table_init   Initialize IPv6 routing table */
63 /*    _nx_nd_cache_delete_internal          Delete ND Cache Entry         */
64 /*    tx_mutex_get                          Obtain a protection mutex     */
65 /*    tx_mutex_put                          Release protection mutex      */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
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_disable(NX_IP * ip_ptr)80 UINT  _nxd_ipv6_disable(NX_IP *ip_ptr)
81 {
82 
83 
84 #ifdef FEATURE_NX_IPV6
85 
86 TX_INTERRUPT_SAVE_AREA
87 ULONG address[4];
88 UINT  i;
89 
90 
91     /* Make sure IPv6 is not already enabled. */
92     /* Cast the function pointer into a ULONG. Since this is exactly what we wish to do, disable the lint warning with the following comment:  */
93     /*lint -e{923} suppress cast of pointer to ULONG.  */
94     if ((ALIGN_TYPE)ip_ptr -> nx_ipv6_packet_receive == NX_NULL)
95     {
96         return(NX_SUCCESS);
97     }
98 
99     /* If trace is enabled, insert this event into the trace buffer.  */
100     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IPV6_DISABLE, ip_ptr, 0, 0, 0, NX_TRACE_IP_EVENTS, 0, 0);
101 
102 
103     /* Obtain the IP mutex so we can manipulate the internal routing table. */
104     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
105 
106     /* Disable Interrupt */
107     TX_DISABLE
108 
109     /* Install IPv6 packet receive processing function pointer */
110     ip_ptr -> nx_ipv6_packet_receive = NX_NULL;
111 
112     /* Enable Interrupt */
113     TX_RESTORE
114 
115     /* Zero out the IPv6 default router table */
116     _nxd_ipv6_default_router_table_init(ip_ptr);
117 
118     /* Clear IPv6 internal timers. */
119     ip_ptr -> nx_ipv6_reachable_timer = 0;
120     ip_ptr -> nx_ipv6_retrans_timer_ticks = 0;
121 
122     /* Check if router solicitation is not disabled. */
123 #ifndef NX_DISABLE_ICMPV6_ROUTER_SOLICITATION
124 
125     /* Create the all-node multicast group address, */
126     address[0] = 0xFF020000;
127     address[1] = 0;
128     address[2] = 0;
129     address[3] = 1;
130 #endif /* NX_DISABLE_ICMPV6_ROUTER_SOLICITATION */
131 
132 
133     /* Reset the router solicitation values . */
134     for (i = 0; i < NX_MAX_PHYSICAL_INTERFACES; i++)
135     {
136         if (ip_ptr -> nx_ip_interface[i].nx_interface_valid == NX_TRUE)
137         {
138             ip_ptr -> nx_ip_interface[i].nxd_interface_ipv6_address_list_head = NX_NULL;
139 
140 #ifndef NX_DISABLE_ICMPV6_ROUTER_SOLICITATION
141             ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_max = 0;
142             ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_count = 0;
143             ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_interval = 0;
144             ip_ptr -> nx_ip_interface[i].nx_ipv6_rtr_solicitation_timer = 0;
145 
146             /* Leave all-node multicast group. */
147             _nx_ipv6_multicast_leave(ip_ptr, address, &ip_ptr -> nx_ip_interface[i]);
148 
149 #endif /* NX_DISABLE_ICMPV6_ROUTER_SOLICITATION */
150         }
151     }
152 
153 
154     /* Remove the IPv6 addresses, and leave the corresponding multicast groups. */
155     for (i = 0; i < NX_MAX_IPV6_ADDRESSES; i++)
156     {
157         if (ip_ptr -> nx_ipv6_address[i].nxd_ipv6_address_valid)
158         {
159 
160             SET_SOLICITED_NODE_MULTICAST_ADDRESS(address, ip_ptr -> nx_ipv6_address[i].nxd_ipv6_address);
161 
162             _nx_ipv6_multicast_leave(ip_ptr, &address[0], ip_ptr -> nx_ipv6_address[i].nxd_ipv6_address_attached);
163         }
164     }
165 
166     /* Zero out the IPv6 address structure table. */
167     memset(&ip_ptr -> nx_ipv6_address[0], 0, sizeof(ip_ptr -> nx_ipv6_address));
168 
169     /* Clean up the ND Cache table.  */
170     if (ip_ptr -> nx_ip_icmpv6_packet_process)
171     {
172         for (i = 0; i < NX_IPV6_NEIGHBOR_CACHE_SIZE; i++)
173         {
174             if ((ip_ptr -> nx_ipv6_nd_cache[i].nx_nd_cache_nd_status != ND_CACHE_STATE_INVALID) &&
175                 (ip_ptr -> nx_ipv6_nd_cache[i].nx_nd_cache_interface_ptr))
176             {
177                 _nx_nd_cache_delete_internal(ip_ptr, &ip_ptr -> nx_ipv6_nd_cache[i]);
178             }
179         }
180     }
181 
182     /* Disable ND cache periodic update routine. */
183     ip_ptr -> nx_nd_cache_fast_periodic_update = NX_NULL;
184     ip_ptr -> nx_nd_cache_slow_periodic_update = NX_NULL;
185 
186     /* Disable ICMPv6 services. */
187     ip_ptr -> nx_ip_icmpv6_packet_process = NX_NULL;
188 
189 #ifdef NX_ENABLE_IPV6_PATH_MTU_DISCOVERY
190     ip_ptr -> nx_destination_table_periodic_update = NX_NULL;
191 #endif /* NX_ENABLE_IPV6_PATH_MTU_DISCOVERY */
192 
193     /* Clean up the loop back address. */
194 #ifndef NX_DISABLE_LOOPBACK_INTERFACE
195     ip_ptr -> nx_ip_interface[NX_LOOPBACK_INTERFACE].nxd_interface_ipv6_address_list_head = NX_NULL;
196 #endif /* NX_DISABLE_LOOPBACK_INTERFACE */
197 
198     /* Release the IP protection. */
199     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
200 
201     /* Return successful completion.  */
202     return(NX_SUCCESS);
203 
204 #else /* !FEATURE_NX_IPV6 */
205     NX_PARAMETER_NOT_USED(ip_ptr);
206 
207     return(NX_NOT_SUPPORTED);
208 
209 #endif /* FEATURE_NX_IPV6 */
210 }
211 
212