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 /**   Address Resolution Protocol (ARP)                                   */
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_arp.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_arp_entry_delete                                PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function searches for the specified IP address in the ARP      */
44 /*    lists.  If found, the associated ARP entry is deleted.              */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                IP instance pointer           */
49 /*    ip_address                            IP Address to search for      */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    status                                Completion status             */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    tx_mutex_get                          Obtain protection mutex       */
58 /*    tx_mutex_put                          Release protection mutex      */
59 /*    _nx_arp_static_entry_delete           Remove static ARP entry       */
60 /*    _nx_arp_dynamic_entry_delete          Remove dynamic ARP entry      */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_arp_entry_delete(NX_IP * ip_ptr,ULONG ip_address)75 UINT  _nx_arp_entry_delete(NX_IP *ip_ptr, ULONG ip_address)
76 {
77 
78 #ifndef NX_DISABLE_IPV4
79 UINT    status;
80 NX_ARP *arp_ptr;
81 NX_ARP *arp_list_head;
82 NX_ARP *search_ptr;
83 UINT    index;
84 
85 
86     /* Obtain protection on this IP instance for access into the ARP static  list.  */
87     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
88 
89     /* Search the static list for a matching IP and hardware mapping.
90        Compute the index that will tell us which linked list of ARP addresses to search,
91        since we don't care if it is a static or dynamic entry.
92        Search that linked list pointed in the IP ARP table.  */
93 
94     /* Calculate the hash index for the specified IP address.  */
95     index =  (UINT)((ip_address + (ip_address >> 8)) & NX_ARP_TABLE_MASK);
96 
97     /* Pickup the head pointer of the ARP entries for this IP instance.  */
98     arp_list_head =  ip_ptr -> nx_ip_arp_table[index];
99 
100     /* Search the ARP list for the same IP address.  */
101     search_ptr =  arp_list_head;
102     arp_ptr =     NX_NULL;
103 
104     while (search_ptr)
105     {
106 
107         /* Determine if we have a match.  */
108         if (search_ptr -> nx_arp_ip_address == ip_address)
109         {
110 
111             /* Yes, the IP address matches, setup the ARP entry pointer.  */
112             arp_ptr =  search_ptr;
113 
114             /* Get out of the loop.  */
115             break;
116         }
117 
118         /* Move to the next entry in the active list.  */
119         search_ptr =  search_ptr -> nx_arp_active_next;
120 
121         /* Determine if the search pointer is back at the head of
122            the list.  */
123         if (search_ptr == arp_list_head)
124         {
125 
126             /* End of the ARP list, end the search.  */
127             break;
128         }
129     }
130 
131     /* Determine if we didn't find an ARP entry matching the input IP address.   */
132     if (arp_ptr == NX_NULL)
133     {
134 
135         /* Release the protection on the ARP list.  */
136         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
137 
138         /* Return status to the caller.  */
139         return(NX_ENTRY_NOT_FOUND);
140     }
141 
142     /* Determine if this is a static entry. */
143     if (arp_ptr -> nx_arp_route_static == NX_TRUE)
144     {
145 
146         /* Remove from the static list. */
147         status = _nx_arp_static_entry_delete(ip_ptr, ip_address,
148                                              arp_ptr -> nx_arp_physical_address_msw,
149                                              arp_ptr -> nx_arp_physical_address_lsw);
150     }
151     else
152     {
153 
154         /* Remove from the dynamic list. */
155         status = _nx_arp_dynamic_entry_delete(ip_ptr, arp_ptr);
156     }
157 
158     /* Release the protection on the ARP list.  */
159     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
160 
161     /* Return status to the caller.  */
162     return(status);
163 #else /* NX_DISABLE_IPV4  */
164     NX_PARAMETER_NOT_USED(ip_ptr);
165     NX_PARAMETER_NOT_USED(ip_address);
166 
167     return(NX_NOT_SUPPORTED);
168 #endif /* !NX_DISABLE_IPV4  */
169 }
170 
171