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 #include "nx_packet.h"
30 
31 #ifndef NX_DISABLE_IPV4
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_arp_dynamic_entry_delete                        PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function deletes the ARP entry pointed to by the caller. Note  */
45 /*    the caller should already have searched the ARP list to verify a    */
46 /*    valid ARP entry to delete.  Also, it is assumed the caller already  */
47 /*    has obtained the IP protection mutex before invoking this service.  */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    ip_ptr                                IP instance of APR table      */
52 /*    arp_ptr                               ARP entry to delete           */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    status                                Completion status             */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _nx_packet_transmit_release           Release the transmitted packet*/
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Internal                                                            */
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_dynamic_entry_delete(NX_IP * ip_ptr,NX_ARP * arp_ptr)75 UINT  _nx_arp_dynamic_entry_delete(NX_IP *ip_ptr, NX_ARP *arp_ptr)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 NX_PACKET *packet_ptr, *next_packet_ptr;
80 
81 
82     /* Determine if this ARP entry is already active.  */
83     if (arp_ptr -> nx_arp_active_list_head)
84     {
85 
86         /* Remove this dynamic ARP entry from the associated list.  */
87 
88         /* Disable interrupts.  */
89         TX_DISABLE
90 
91         /* Determine if this is the only ARP entry on the list.  */
92         if (arp_ptr == arp_ptr -> nx_arp_active_next)
93         {
94 
95             /* Remove the entry from the list.  */
96             *(arp_ptr -> nx_arp_active_list_head) =  NX_NULL;
97         }
98         else
99         {
100 
101             /* Remove the entry from a list of more than one entry.  */
102 
103             /* Update the list head pointer.  */
104             if (*(arp_ptr -> nx_arp_active_list_head) == arp_ptr)
105             {
106                 *(arp_ptr -> nx_arp_active_list_head) =  arp_ptr -> nx_arp_active_next;
107             }
108 
109             /* Update the links of the adjacent ARP entries.  */
110             (arp_ptr -> nx_arp_active_next) -> nx_arp_active_previous = arp_ptr -> nx_arp_active_previous;
111             (arp_ptr -> nx_arp_active_previous) -> nx_arp_active_next =  arp_ptr -> nx_arp_active_next;
112         }
113 
114         /* No longer active, clear the active list head.  */
115         arp_ptr -> nx_arp_active_list_head =  NX_NULL;
116 
117         /* Decrease the number of active ARP entries.  */
118         ip_ptr -> nx_ip_arp_dynamic_active_count--;
119 
120         /* Pickup the queued packets head pointer.  */
121         next_packet_ptr =  arp_ptr -> nx_arp_packets_waiting;
122 
123         /* Clear the queued packets head pointer.  */
124         arp_ptr -> nx_arp_packets_waiting =  NX_NULL;
125 
126         /* Restore interrupts.  */
127         TX_RESTORE
128 
129         /* Loop to remove all queued packets.  */
130         while (next_packet_ptr)
131         {
132 
133             /* Pickup the packet pointer at the head of the queue.  */
134             packet_ptr =  next_packet_ptr;
135 
136             /* Move to the next packet in the queue.  */
137             next_packet_ptr =  next_packet_ptr -> nx_packet_queue_next;
138 
139             /* Clear the next packet queue pointer.  */
140             packet_ptr -> nx_packet_queue_next =  NX_NULL;
141 
142 #ifndef NX_DISABLE_IP_INFO
143 
144             /* Increment the IP send packets dropped count.  */
145             ip_ptr -> nx_ip_send_packets_dropped++;
146 #endif
147 
148             /* Release the packet that was queued from the previous ARP entry.  */
149             _nx_packet_transmit_release(packet_ptr);
150         }
151     }
152 
153     return(NX_SUCCESS);
154 }
155 #endif /* !NX_DISABLE_IPV4  */
156 
157