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 /**   Neighbor Discovery Cache                                            */
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 #include "nx_nd_cache.h"
31 #include "nx_packet.h"
32 #include "nx_icmpv6.h"
33 
34 #ifdef FEATURE_NX_IPV6
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    nx_nd_cache_delete_internal                         PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Yuxin Zhou, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function deletes an IPv6 and MAC mapping from the ND cache     */
50 /*    table.                                                              */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    ip_ptr                                Pointer to IP.                */
55 /*    dest_ip                               Pointer to the IP address.    */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    NX_SUCCESS                            Address is deleted            */
60 /*    NX_ENTRY_NOT_FOUND                    Address not found in cache    */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _nx_packet_transmit_releas            Packet Release                */
65 /*    memset                                                              */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    nxd_ipv6_disable                                                    */
70 /*    nx_nd_cache_fast_periodic_update                                    */
71 /*    nxd_nd_cache_entry_delete                                           */
72 /*                                                                        */
73 /*  Note:                                                                 */
74 /*                                                                        */
75 /*    This routine is an internal function.  Therefore it assumes the     */
76 /*       mutex is already locked.  Caller is responsible for accquiring   */
77 /*       and releasing the mutex before invoking this routine.            */
78 /*                                                                        */
79 /*                                                                        */
80 /*  RELEASE HISTORY                                                       */
81 /*                                                                        */
82 /*    DATE              NAME                      DESCRIPTION             */
83 /*                                                                        */
84 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
85 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
86 /*                                            resulting in version 6.1    */
87 /*                                                                        */
88 /**************************************************************************/
_nx_nd_cache_delete_internal(NX_IP * ip_ptr,ND_CACHE_ENTRY * entry)89 UINT _nx_nd_cache_delete_internal(NX_IP *ip_ptr, ND_CACHE_ENTRY *entry)
90 {
91 
92 UINT       i = 0, table_size;
93 NX_PACKET *pkt, *next_pkt;
94 
95     /* Free up the queued packets. */
96     pkt = entry -> nx_nd_cache_packet_waiting_head;
97 
98     /* Flush any packets enqueued waiting on neighbor reachability confirmation. */
99     while (pkt)
100     {
101 
102         next_pkt = pkt -> nx_packet_queue_next;
103         _nx_packet_transmit_release(pkt);
104         pkt = next_pkt;
105     }
106     entry -> nx_nd_cache_packet_waiting_queue_length = 0;
107 
108     /* Clear the pointers to the original start and end of the packet queue. */
109     entry -> nx_nd_cache_packet_waiting_head = NX_NULL;
110     entry -> nx_nd_cache_packet_waiting_tail = NX_NULL;
111 
112     /* Initialize the rest of the fields. */
113     memset(entry -> nx_nd_cache_mac_addr, 0, 6);
114 
115     /* Clear the entry out.  */
116     entry -> nx_nd_cache_nd_status = ND_CACHE_STATE_INVALID;
117     entry -> nx_nd_cache_is_static = 0;
118 
119     /* Is there a corresponding link in the default router list? */
120     if (entry -> nx_nd_cache_is_router)
121     {
122 
123         /* Set its pointer to this entry in the cache table to NULL. */
124         entry -> nx_nd_cache_is_router -> nx_ipv6_default_router_entry_neighbor_cache_ptr = NX_NULL;
125     }
126 
127     /* And indicate that this cache entry is no longer a router. */
128     entry -> nx_nd_cache_is_router = NX_NULL;
129 
130     /* Set a local variable for convenience. */
131     table_size = ip_ptr -> nx_ipv6_destination_table_size;
132 
133     while (table_size && i < NX_IPV6_DESTINATION_TABLE_SIZE)
134     {
135 
136         /* Skip invalid entries. */
137         if (!ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_valid)
138         {
139             i++;
140             continue;
141         }
142 
143         /* Keep track of valid entries we have checked. */
144         table_size--;
145 
146         /* Find the destination unit. */
147         if (ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_nd_entry == entry)
148         {
149 
150             /* Set the status. */
151             ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_valid = 0;
152 
153             /* Set its pointer to this entry in the destination table to NULL. */
154             ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_nd_entry = NX_NULL;
155 
156             /* Update the destination_table size. */
157             ip_ptr -> nx_ipv6_destination_table_size--;
158         }
159 
160         i++;
161     }
162 
163     return(NX_SUCCESS);
164 }
165 
166 #endif /* FEATURE_NX_IPV6 */
167 
168