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 /**   Internet Protocol (IP)                                              */
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_interface_entries_delete                    PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function removes ARP entries associated with the interface     */
44 /*    specified by index from the IP's arp table.                         */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    ip_ptr                                Pointer to IP control block   */
49 /*    index                                 IP interface index            */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    status                                Completion status             */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    tx_mutex_get                          Obtain protection mutex       */
58 /*    _nx_arp_dynamic_entry_delete          Delete dynamic ARP entry      */
59 /*    _nx_arp_static_entry_delete_internal  Internal static ARP entry     */
60 /*                                            delete function             */
61 /*    tx_mutex_put                          release protection mutex      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
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_interface_entries_delete(NX_IP * ip_ptr,UINT index)76 UINT _nx_arp_interface_entries_delete(NX_IP *ip_ptr, UINT index)
77 {
78 
79 NX_ARP       *arp_entry;
80 NX_ARP       *next_arp_entry;
81 NX_ARP       *last_arp_entry;
82 NX_INTERFACE *interface_ptr;
83 
84     interface_ptr = &(ip_ptr -> nx_ip_interface[index]);
85 
86     /* Obtain protection on this IP instance for access into the ARP dynamic list. */
87     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
88 
89     /* Begain remove ARP entries from the dynamic list. */
90 
91     /* Setup pointers to the starting and ending ARP entries in the dynamic list. */
92     arp_entry = ip_ptr -> nx_ip_arp_dynamic_list;
93 
94     /* Walk through the dynamic ARP list until there are no more active entries. */
95     while ((arp_entry) && (ip_ptr -> nx_ip_arp_dynamic_active_count))
96     {
97 
98         /* Yes, there is one or more dynamic entries. */
99         /* Determine if this ARP entry is for the specified interface. */
100         if (arp_entry -> nx_arp_ip_interface == interface_ptr)
101         {
102 
103             _nx_arp_dynamic_entry_delete(ip_ptr, arp_entry);
104 
105             /* Cleanup the nx_arp_ip_interface filed. */
106             arp_entry -> nx_arp_ip_interface = NX_NULL;
107         }
108 
109         /* Determine if we are at the end of the dynamic list. */
110         if (arp_entry -> nx_arp_pool_next != ip_ptr -> nx_ip_arp_dynamic_list)
111         {
112             /* No, simply move to the next dynamic entry. */
113             arp_entry = arp_entry -> nx_arp_pool_next;
114         }
115         else
116         {
117             /* Yes, we are at the end of the dynamic list, break out of the loop. */
118             break;
119         }
120     }
121 
122     /* End remove ARP entries from the dynamic list. */
123 
124     /* Begain remove ARP entries from the static list. */
125 
126     /* Setup pointers to the starting/ending ARP entry in the static list. */
127     next_arp_entry = ip_ptr -> nx_ip_arp_static_list;
128     if (next_arp_entry)
129     {
130 
131         /* Pick up the last ARP entry. */
132         last_arp_entry = next_arp_entry -> nx_arp_pool_previous;
133 
134         /* Walk through the static ARP list until there are no more active entries. */
135         while (next_arp_entry)
136         {
137 
138             /* Yes, there is one or more static ARP entries. */
139             arp_entry = next_arp_entry;
140 
141             /* Move to the next pakcet in the queue. */
142             next_arp_entry = next_arp_entry -> nx_arp_pool_next;
143 
144             if (arp_entry -> nx_arp_ip_interface == interface_ptr)
145             {
146 
147                 /* The static entry was found.  It needs to be unlinked from the active
148                    list and the static list and re-linked to the end of the dynamic list.  */
149                 _nx_arp_static_entry_delete_internal(ip_ptr, arp_entry);
150             }
151 
152             if (arp_entry == last_arp_entry)
153             {
154                 break;
155             }
156         }
157     }
158 
159     /* End remove ARP entries from the static list. */
160 
161     /* Release the mutex. */
162     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
163 
164     /* Return successful status to the caller. */
165     return(NX_SUCCESS);
166 }
167 #endif /* !NX_DISABLE_IPV4  */
168 
169