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_ip.h"
30 #include "nx_ipv6.h"
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nxd_ipv6_address_delete                            PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*  This function removes the IPv6 address at the specified address list  */
45 /*  index on the IP instance.                                             */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                            IP control block pointer          */
50 /*    address_index                     Index into IPv6 address list      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                            Completion status                 */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    tx_mutex_get                      Get protection mutex              */
59 /*    tx_mutex_put                      Put protection mutex              */
60 /*    nx_ipv6_multicast_leave           Leave IPv6 multicast group        */
61 /*    [ipv6_address_change_notify]      User callback function            */
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_address_delete(NX_IP * ip_ptr,UINT address_index)76 UINT  _nxd_ipv6_address_delete(NX_IP *ip_ptr, UINT address_index)
77 {
78 #ifdef FEATURE_NX_IPV6
79 UINT              result;
80 NXD_IPV6_ADDRESS *ipv6_address, *address_list;
81 #ifdef NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY
82 VOID              (*address_change_notify)(NX_IP *, UINT, UINT, UINT, ULONG *);
83 UINT              if_index;
84 ULONG             obsoleted_address[4];
85 
86     address_change_notify = NX_NULL;
87 #endif /* NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY */
88 
89 
90     result = NX_NO_INTERFACE_ADDRESS;
91 
92     /* Place protection while the IPv6 address is modified. */
93     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
94 
95 
96     /* Get the ip address.  */
97     ipv6_address = &ip_ptr -> nx_ipv6_address[address_index];
98 
99     /* Check the validity of the address.  */
100     if (ipv6_address -> nxd_ipv6_address_valid)
101     {
102 
103         /* Delete the information in the list.  */
104         address_list = ipv6_address -> nxd_ipv6_address_attached -> nxd_interface_ipv6_address_list_head;
105 
106 
107         /* The delete address is in the head of the list.  */
108         if (ipv6_address == address_list)
109         {
110 
111             ipv6_address -> nxd_ipv6_address_attached -> nxd_interface_ipv6_address_list_head = ipv6_address -> nxd_ipv6_address_next;
112             result = NX_SUCCESS;
113         }
114         else
115         {
116             /* Find the address in the list.  */
117             while (address_list && (address_list -> nxd_ipv6_address_next != ipv6_address))
118             {
119 
120                 /* Move to the next address. */
121                 address_list = address_list -> nxd_ipv6_address_next;
122             }
123 
124             /* Break out of the while loop, either the address has been found, or the end of the list has been reached. */
125             if (address_list)
126             {
127                 address_list -> nxd_ipv6_address_next = ipv6_address -> nxd_ipv6_address_next;
128                 result = NX_SUCCESS;
129             }
130         }
131 
132         if (result == NX_SUCCESS)
133         {
134         ULONG multicast_address[4];
135 
136             SET_SOLICITED_NODE_MULTICAST_ADDRESS(multicast_address, ipv6_address -> nxd_ipv6_address);
137             /* First remove the corresponding solicited node multicast address. */
138             _nx_ipv6_multicast_leave(ip_ptr, &multicast_address[0], ipv6_address -> nxd_ipv6_address_attached);
139 
140 #ifdef NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY
141             /* Pickup the current notification callback and additional information pointers.  */
142             address_change_notify =  ip_ptr -> nx_ipv6_address_change_notify;
143 
144             obsoleted_address[0] = ipv6_address -> nxd_ipv6_address[0];
145             obsoleted_address[1] = ipv6_address -> nxd_ipv6_address[1];
146             obsoleted_address[2] = ipv6_address -> nxd_ipv6_address[2];
147             obsoleted_address[3] = ipv6_address -> nxd_ipv6_address[3];
148 
149             if_index = ipv6_address -> nxd_ipv6_address_attached -> nx_interface_index;
150 #endif /* NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY */
151 
152             /* At this point ipv6_address is off the interface IPv6 address list. */
153             memset(ipv6_address, 0, sizeof(NXD_IPV6_ADDRESS));
154 
155             /* Set index of ipv6_address. */
156             ipv6_address -> nxd_ipv6_address_index = (UCHAR)address_index;
157         }
158     }
159 
160     /* Release the protection while the IPv6 address is modified. */
161     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
162 
163 
164 #ifdef NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY
165     /* Is the application configured for notification of address changes and/or
166        prefix_length change?  */
167     if (address_change_notify && (result == NX_SUCCESS))
168     {
169 
170         /* Yes, call the application's address change notify function.  */
171         /*lint -e{644} suppress variable might not be initialized, since "if_index" was initialized as long as result is NX_SUCCESS. */
172         (address_change_notify)(ip_ptr, NX_IPV6_ADDRESS_MANUAL_DELETE, if_index, address_index, &obsoleted_address[0]);
173     }
174 #endif /* NX_ENABLE_IPV6_ADDRESS_CHANGE_NOTIFY */
175 
176     /* Return completion status.  */
177     return(result);
178 
179 #else /* !FEATURE_NX_IPV6 */
180     NX_PARAMETER_NOT_USED(ip_ptr);
181     NX_PARAMETER_NOT_USED(address_index);
182 
183     return(NX_NOT_SUPPORTED);
184 
185 #endif /* FEATURE_NX_IPV6 */
186 }
187 
188