summaryrefslogtreecommitdiff
path: root/common/src/nx_nd_cache_delete_internal.c
blob: 2abd17927687011502ee7aea6546c06934b07f12 (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                                                        */
/**                                                                       */
/**   Neighbor Discovery Cache                                            */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_ipv6.h"
#include "nx_nd_cache.h"
#include "nx_packet.h"
#include "nx_icmpv6.h"

#ifdef FEATURE_NX_IPV6


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    nx_nd_cache_delete_internal                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function deletes an IPv6 and MAC mapping from the ND cache     */
/*    table.                                                              */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    ip_ptr                                Pointer to IP.                */
/*    dest_ip                               Pointer to the IP address.    */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_SUCCESS                            Address is deleted            */
/*    NX_ENTRY_NOT_FOUND                    Address not found in cache    */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_packet_transmit_releas            Packet Release                */
/*    memset                                                              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    nxd_ipv6_disable                                                    */
/*    nx_nd_cache_fast_periodic_update                                    */
/*    nxd_nd_cache_entry_delete                                           */
/*                                                                        */
/*  Note:                                                                 */
/*                                                                        */
/*    This routine is an internal function.  Therefore it assumes the     */
/*       mutex is already locked.  Caller is responsible for accquiring   */
/*       and releasing the mutex before invoking this routine.            */
/*                                                                        */
/*                                                                        */
/**************************************************************************/
UINT _nx_nd_cache_delete_internal(NX_IP *ip_ptr, ND_CACHE_ENTRY *entry)
{

UINT       i = 0, table_size;
NX_PACKET *pkt, *next_pkt;

    /* Free up the queued packets. */
    pkt = entry -> nx_nd_cache_packet_waiting_head;

    /* Flush any packets enqueued waiting on neighbor reachability confirmation. */
    while (pkt)
    {

        next_pkt = pkt -> nx_packet_queue_next;
        _nx_packet_transmit_release(pkt);
        pkt = next_pkt;
    }
    entry -> nx_nd_cache_packet_waiting_queue_length = 0;

    /* Clear the pointers to the original start and end of the packet queue. */
    entry -> nx_nd_cache_packet_waiting_head = NX_NULL;
    entry -> nx_nd_cache_packet_waiting_tail = NX_NULL;

    /* Initialize the rest of the fields. */
    memset(entry -> nx_nd_cache_mac_addr, 0, 6);

    /* Clear the entry out.  */
    entry -> nx_nd_cache_nd_status = ND_CACHE_STATE_INVALID;
    entry -> nx_nd_cache_is_static = 0;

    /* Is there a corresponding link in the default router list? */
    if (entry -> nx_nd_cache_is_router)
    {

        /* Set its pointer to this entry in the cache table to NULL. */
        entry -> nx_nd_cache_is_router -> nx_ipv6_default_router_entry_neighbor_cache_ptr = NX_NULL;
    }

    /* And indicate that this cache entry is no longer a router. */
    entry -> nx_nd_cache_is_router = NX_NULL;

    /* Set a local variable for convenience. */
    table_size = ip_ptr -> nx_ipv6_destination_table_size;

    while (table_size && i < NX_IPV6_DESTINATION_TABLE_SIZE)
    {

        /* Skip invalid entries. */
        if (!ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_valid)
        {
            i++;
            continue;
        }

        /* Keep track of valid entries we have checked. */
        table_size--;

        /* Find the destination unit. */
        if (ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_nd_entry == entry)
        {

            /* Set the status. */
            ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_valid = 0;

            /* Set its pointer to this entry in the destination table to NULL. */
            ip_ptr -> nx_ipv6_destination_table[i].nx_ipv6_destination_entry_nd_entry = NX_NULL;

            /* Update the destination_table size. */
            ip_ptr -> nx_ipv6_destination_table_size--;
        }

        i++;
    }

    return(NX_SUCCESS);
}

#endif /* FEATURE_NX_IPV6 */