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 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_arp.h"
29 
30 #ifndef NX_DISABLE_IPV4
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_arp_static_entry_delete_internal                PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function deletes the ARP entry pointed to by the caller. Note  */
44 /*    the caller should already have searched the ARP list to verify a    */
45 /*    valid ARP entry to delete.  Also, it is assumed the caller already  */
46 /*    has obtained the IP protection mutex before invoking this service.  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    ip_ptr                                IP instance owner of APR table*/
51 /*    arp_entry                             ARP entry to delete           */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _nx_arp_interface_entries_delete      Delete ARP entry associated   */
64 /*                                            with the specified interface*/
65 /*    _nx_arp_static_entry_delete           Delete static ARP entry       */
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_static_entry_delete_internal(NX_IP * ip_ptr,NX_ARP * arp_entry)76 VOID _nx_arp_static_entry_delete_internal(NX_IP *ip_ptr, NX_ARP *arp_entry)
77 {
78 
79 TX_INTERRUPT_SAVE_AREA
80 
81 
82 #ifndef NX_DISABLE_ARP_INFO
83     /* Decrement the ARP static entry count.  */
84     ip_ptr -> nx_ip_arp_static_entries--;
85 #endif
86 
87     /* Disable interrupts temporarily.  */
88     TX_DISABLE
89 
90     /* Determine if this ARP entry is already active.  */
91     if (arp_entry -> nx_arp_active_list_head)
92     {
93 
94         /* Remove this dynamic ARP entry from the associated list.  */
95 
96         /* Determine if this is the only ARP entry on the list.  */
97         if (arp_entry == arp_entry -> nx_arp_active_next)
98         {
99 
100             /* Remove the entry from the list.  */
101             *(arp_entry -> nx_arp_active_list_head) =  NX_NULL;
102         }
103         else
104         {
105 
106             /* Remove the entry from a list of more than one entry.  */
107 
108             /* Update the list head pointer.  */
109             if (*(arp_entry -> nx_arp_active_list_head) == arp_entry)
110             {
111                 *(arp_entry -> nx_arp_active_list_head) =  arp_entry -> nx_arp_active_next;
112             }
113 
114             /* Update the links of the adjacent ARP entries.  */
115             (arp_entry -> nx_arp_active_next) -> nx_arp_active_previous =
116                 arp_entry -> nx_arp_active_previous;
117             (arp_entry -> nx_arp_active_previous) -> nx_arp_active_next =
118                 arp_entry -> nx_arp_active_next;
119         }
120     }
121 
122     /* Remove this entry from the static ARP list.  */
123 
124     /* Determine if this is the only ARP entry on the static list.  */
125     if (arp_entry == arp_entry -> nx_arp_pool_next)
126     {
127 
128         /* Remove the sole entry from the static list head.  */
129         ip_ptr -> nx_ip_arp_static_list =  NX_NULL;
130     }
131     else
132     {
133 
134         /* Remove the entry from a list of more than one entry.  */
135 
136         /* Update the links of the adjacent ARP dynamic pool entries.  */
137         (arp_entry -> nx_arp_pool_next) -> nx_arp_pool_previous =
138             arp_entry -> nx_arp_pool_previous;
139         (arp_entry -> nx_arp_pool_previous) -> nx_arp_pool_next =
140             arp_entry -> nx_arp_pool_next;
141 
142         /* Update the list head pointer.  */
143         if (ip_ptr -> nx_ip_arp_static_list == arp_entry)
144         {
145             ip_ptr -> nx_ip_arp_static_list =  arp_entry -> nx_arp_pool_next;
146         }
147     }
148 
149     /* Clear the fields that indicate the ARP entry is a static entry and make sure
150        it is viewed as inactive in preparation for returning it to the dynamic ARP
151        pool.  */
152     arp_entry -> nx_arp_route_static =      NX_FALSE;
153     arp_entry -> nx_arp_active_list_head =  NX_NULL;
154 
155     /* Place the ARP entry at the end of the dynamic ARP pool, which is where new
156        ARP requests are allocated from.  */
157 
158     /* Determine if the dynamic ARP pool is empty.  */
159     if (ip_ptr -> nx_ip_arp_dynamic_list)
160     {
161 
162         /* Dynamic list is not empty, add former static ARP entry to the end of the list.  */
163         arp_entry -> nx_arp_pool_next = ip_ptr -> nx_ip_arp_dynamic_list;
164         arp_entry -> nx_arp_pool_previous = (ip_ptr -> nx_ip_arp_dynamic_list) -> nx_arp_pool_previous;
165         ((ip_ptr -> nx_ip_arp_dynamic_list) -> nx_arp_pool_previous) -> nx_arp_pool_next = arp_entry;
166         (ip_ptr -> nx_ip_arp_dynamic_list) -> nx_arp_pool_previous = arp_entry;
167     }
168     else
169     {
170 
171         /* Dynamic list was empty, just place it at the head of the dynamic list.  */
172         ip_ptr -> nx_ip_arp_dynamic_list =  arp_entry;
173         arp_entry -> nx_arp_pool_next =     arp_entry;
174         arp_entry -> nx_arp_pool_previous = arp_entry;
175     }
176 
177     /* Restore interrupts.  */
178     TX_RESTORE
179 }
180 #endif /* !NX_DISABLE_IPV4  */
181 
182