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 /**   Internet Protocol (IP)                                              */
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_ip.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_ip_static_route_delete                          PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function deletes static routing entry from the routing table.  */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    ip_ptr                                Pointer to IP instance        */
48 /*    network_address                       network address, in host byte */
49 /*                                            order.                      */
50 /*    net_mask                              Network Mask, in host byte    */
51 /*                                            order.                      */
52 /*                                                                        */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    NX_SUCCESS                            Successful completion status  */
57 /*    NX_NOT_IMPLEMENTED                    Static routing not enabled    */
58 /*    NX_NOT_SUCCESSFUL                     No match found                */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_mutex_get                                                        */
63 /*    tx_mutex_put                                                        */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application                                                         */
68 /*                                                                        */
69 /*  NOTE:                                                                 */
70 /*                                                                        */
71 /*    None                                                                */
72 /*                                                                        */
73 /*  RELEASE HISTORY                                                       */
74 /*                                                                        */
75 /*    DATE              NAME                      DESCRIPTION             */
76 /*                                                                        */
77 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
78 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
79 /*                                            resulting in version 6.1    */
80 /*                                                                        */
81 /**************************************************************************/
_nx_ip_static_route_delete(NX_IP * ip_ptr,ULONG network_address,ULONG net_mask)82 UINT  _nx_ip_static_route_delete(NX_IP *ip_ptr, ULONG network_address, ULONG net_mask)
83 {
84 
85 #if !defined(NX_DISABLE_IPV4) && defined(NX_ENABLE_IP_STATIC_ROUTING)
86 UINT i;
87 UINT found_match    = NX_FALSE;
88 UINT status         = NX_NOT_SUCCESSFUL;
89 
90 
91     network_address = network_address & net_mask;
92 
93     /* If trace is enabled, insert this event into the trace buffer.  */
94     NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_STATIC_ROUTE_DELETE, ip_ptr, network_address, net_mask, 0, NX_TRACE_IP_EVENTS, 0, 0);
95 
96     /* Obtain the IP mutex so we can manipulate the internal routing table. */
97     /* This routine does not need to be protected by mask off interrupt
98        because it cannot be invoked from ISR. */
99     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
100 
101     /* Check whether the table is empty. */
102     if (ip_ptr -> nx_ip_routing_table_entry_count == 0)
103     {
104 
105         /* It is so release the lock and we're done. */
106         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
107         return(NX_SUCCESS);
108     }
109 
110     /* Search through the routing table, check whether the same entry exists. */
111     for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; i++)
112     {
113 
114         if (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_dest_ip == network_address &&
115             ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_net_mask == net_mask)
116         {
117 
118         UINT j;
119 
120             /* Found the entry.  */
121             found_match = NX_TRUE;
122 
123             /* If the entry is not the last one, we need to shift to the
124                reset of the table to fill the hole. */
125             for (j = i; j < ip_ptr -> nx_ip_routing_table_entry_count; j++)
126             {
127                 ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_dest_ip = ip_ptr -> nx_ip_routing_table[j + 1].nx_ip_routing_dest_ip;
128                 ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_net_mask = ip_ptr -> nx_ip_routing_table[j + 1].nx_ip_routing_net_mask;
129                 ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_next_hop_address = ip_ptr -> nx_ip_routing_table[j + 1].nx_ip_routing_next_hop_address;
130                 ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_entry_ip_interface = ip_ptr -> nx_ip_routing_table[j + 1].nx_ip_routing_entry_ip_interface;
131             }
132 
133             ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_dest_ip            = 0;
134             ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_net_mask           = 0;
135             ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_next_hop_address   = 0;
136             ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_entry_ip_interface = NX_NULL;
137 
138             break;
139         }
140     }
141 
142     /* Don't forget to decrease table count if we were
143        able to delete the requested static route. */
144     if (found_match)
145     {
146 
147         ip_ptr -> nx_ip_routing_table_entry_count--;
148 
149         /* Indicate successful deletion. */
150         status = NX_SUCCESS;
151     }
152 
153     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
154 
155     /* Return outcome status to the caller.  */
156     return(status);
157 #else /* !NX_DISABLE_IPV4 && NX_ENABLE_IP_STATIC_ROUTING  */
158     NX_PARAMETER_NOT_USED(ip_ptr);
159     NX_PARAMETER_NOT_USED(network_address);
160     NX_PARAMETER_NOT_USED(net_mask);
161 
162     return(NX_NOT_SUPPORTED);
163 
164 #endif /* !NX_DISABLE_IPV4 && NX_ENABLE_IP_STATIC_ROUTING  */
165 }
166 
167