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