summaryrefslogtreecommitdiff
path: root/common/src/nx_ip_static_route_delete.c
blob: a476e0db63f8da33ac87628a48e979880ccc56c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Internet Protocol (IP)                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_ip.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_ip_static_route_delete                          PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function deletes static routing entry from the routing table.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                Pointer to IP instance        */
/*    network_address                       network address, in host byte */
/*                                            order.                      */
/*    net_mask                              Network Mask, in host byte    */
/*                                            order.                      */
/*                                                                        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Successful completion status  */
/*    NX_NOT_IMPLEMENTED                    Static routing not enabled    */
/*    NX_NOT_SUCCESSFUL                     No match found                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                                                        */
/*    tx_mutex_put                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/*  NOTE:                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/**************************************************************************/
UINT  _nx_ip_static_route_delete(NX_IP *ip_ptr, ULONG network_address, ULONG net_mask)
{

#if !defined(NX_DISABLE_IPV4) && defined(NX_ENABLE_IP_STATIC_ROUTING)
UINT i;
UINT found_match    = NX_FALSE;
UINT status         = NX_NOT_SUCCESSFUL;


    network_address = network_address & net_mask;

    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_IP_STATIC_ROUTE_DELETE, ip_ptr, network_address, net_mask, 0, NX_TRACE_IP_EVENTS, 0, 0);

    /* Obtain the IP mutex so we can manipulate the internal routing table. */
    /* This routine does not need to be protected by mask off interrupt
       because it cannot be invoked from ISR. */
    tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);

    /* Check whether the table is empty. */
    if (ip_ptr -> nx_ip_routing_table_entry_count == 0)
    {

        /* It is so release the lock and we're done. */
        tx_mutex_put(&(ip_ptr -> nx_ip_protection));
        return(NX_SUCCESS);
    }

    /* Search through the routing table, check whether the same entry exists. */
    for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; i++)
    {

        if (ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_dest_ip == network_address &&
            ip_ptr -> nx_ip_routing_table[i].nx_ip_routing_net_mask == net_mask)
        {

        UINT j;

            /* Found the entry.  */
            found_match = NX_TRUE;

            /* If the entry is not the last one, we need to shift to the
               reset of the table to fill the hole. */
            for (j = i; j < ip_ptr -> nx_ip_routing_table_entry_count; j++)
            {
                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;
                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;
                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;
                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;
            }

            ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_dest_ip            = 0;
            ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_net_mask           = 0;
            ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_next_hop_address   = 0;
            ip_ptr -> nx_ip_routing_table[j - 1].nx_ip_routing_entry_ip_interface = NX_NULL;

            break;
        }
    }

    /* Don't forget to decrease table count if we were
       able to delete the requested static route. */
    if (found_match)
    {

        ip_ptr -> nx_ip_routing_table_entry_count--;

        /* Indicate successful deletion. */
        status = NX_SUCCESS;
    }

    tx_mutex_put(&(ip_ptr -> nx_ip_protection));

    /* Return outcome status to the caller.  */
    return(status);
#else /* !NX_DISABLE_IPV4 && NX_ENABLE_IP_STATIC_ROUTING  */
    NX_PARAMETER_NOT_USED(ip_ptr);
    NX_PARAMETER_NOT_USED(network_address);
    NX_PARAMETER_NOT_USED(net_mask);

    return(NX_NOT_SUPPORTED);

#endif /* !NX_DISABLE_IPV4 && NX_ENABLE_IP_STATIC_ROUTING  */
}