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