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_static_entry_delete                         PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function removes a previously setup static IP to hardware      */
44 /*    mapping and returns the associated ARP entry back to the dynamic    */
45 /*    ARP pool.                                                           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    ip_ptr                                IP instance pointer           */
50 /*    ip_address                            IP Address binding to delete  */
51 /*    physical_msw                          Physical address MSW          */
52 /*    physical_lsw                          Physical address LSW          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    tx_mutex_get                          Obtain protection mutex       */
61 /*    _nx_arp_static_entry_delete_internal  Internal ARP entry delete     */
62 /*    tx_mutex_put                          Release protection mutex      */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*    _nx_arp_static_entries_delete         Delete all static entries     */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_nx_arp_static_entry_delete(NX_IP * ip_ptr,ULONG ip_address,ULONG physical_msw,ULONG physical_lsw)78 UINT  _nx_arp_static_entry_delete(NX_IP *ip_ptr, ULONG ip_address,
79                                   ULONG physical_msw, ULONG physical_lsw)
80 {
81 
82 #ifndef NX_DISABLE_IPV4
83 NX_ARP *arp_entry;
84 UINT    status;
85 
86 
87     /* If trace is enabled, insert this event into the trace buffer.  */
88     NX_TRACE_IN_LINE_INSERT(NX_TRACE_ARP_STATIC_ENTRY_DELETE, ip_ptr, ip_address, physical_msw, physical_lsw, NX_TRACE_ARP_EVENTS, 0, 0);
89 
90     /* Obtain protection on this IP instance for access into the ARP static
91        list.  */
92     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
93 
94     /* Search the static list for a matching IP and hardware mapping.  */
95     arp_entry =  ip_ptr -> nx_ip_arp_static_list;
96     while (arp_entry)
97     {
98 
99         /* Determine if we have a match.  */
100         if ((arp_entry -> nx_arp_ip_address == ip_address) &&
101             (arp_entry -> nx_arp_physical_address_msw ==  physical_msw) &&
102             (arp_entry -> nx_arp_physical_address_lsw ==  physical_lsw))
103         {
104 
105             /* Yes, we have found the ARP entry we are looking for.  */
106             break;
107         }
108         else
109         {
110 
111             /* Determine if we are at the end of the list.  */
112             if (arp_entry -> nx_arp_pool_next == ip_ptr -> nx_ip_arp_static_list)
113             {
114 
115                 /* Set the arp_entry to NULL to signify nothing was found and get
116                    out of the search loop.  */
117                 arp_entry =  NX_NULL;
118                 break;
119             }
120             else
121             {
122 
123                 /* Just move to the next ARP entry on the static list.  */
124                 arp_entry =  arp_entry -> nx_arp_pool_next;
125             }
126         }
127     }
128 
129     /* At this point the ARP entry pointer determines whether or not anything was found
130        on the static list.  */
131     if (arp_entry)
132     {
133 
134         /* The static entry was found.  It needs to be unlinked from the active
135            list and the static list and re-linked to the end of the dynamic list.  */
136         _nx_arp_static_entry_delete_internal(ip_ptr, arp_entry);
137 
138         /* Setup the return status.  */
139         status =  NX_SUCCESS;
140     }
141     else
142     {
143 
144         /* Indicate the entry was not found.  */
145         status =  NX_ENTRY_NOT_FOUND;
146     }
147 
148     /* Release the protection on the ARP list.  */
149     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
150 
151     /* Return status to the caller.  */
152     return(status);
153 #else /* NX_DISABLE_IPV4  */
154     NX_PARAMETER_NOT_USED(ip_ptr);
155     NX_PARAMETER_NOT_USED(ip_address);
156     NX_PARAMETER_NOT_USED(physical_msw);
157     NX_PARAMETER_NOT_USED(physical_lsw);
158 
159     return(NX_NOT_SUPPORTED);
160 #endif /* !NX_DISABLE_IPV4  */
161 }
162 
163