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