diff --git a/common/src/nx_ip_interface_detach.c b/common/src/nx_ip_interface_detach.c index 244832680..c9ccbaedd 100644 --- a/common/src/nx_ip_interface_detach.c +++ b/common/src/nx_ip_interface_detach.c @@ -8,6 +8,7 @@ * * SPDX-License-Identifier: MIT **************************************************************************/ +// Portions of this file were generated with AI assistance. /**************************************************************************/ @@ -144,7 +145,9 @@ NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry; #ifndef NX_DISABLE_IPV4 #ifdef NX_ENABLE_IP_STATIC_ROUTING /* Remove router iterms associated with the interface that will be detached from route table. */ - for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; i++) + /* Stop at the last entry and recheck the slot after a removal, because + the next route has shifted into it. */ + for (i = 0; i < ip_ptr -> nx_ip_routing_table_entry_count; ) { /* Is this router iterm related to the interface to be detached. */ @@ -153,7 +156,7 @@ NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry; /* Yes, we need to remove this iterm. */ /* If the entry is not the last one, we need to shift the table to fill the hole. */ - for (j = i; j < ip_ptr -> nx_ip_routing_table_entry_count; j++) + for (j = i; (j + 1) < 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; @@ -165,13 +168,17 @@ NX_IPV6_DEFAULT_ROUTER_ENTRY *rt_entry; 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; + ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_dest_ip = 0; + ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_net_mask = 0; + ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_next_hop_address = 0; + ip_ptr -> nx_ip_routing_table[j].nx_ip_routing_entry_ip_interface = NX_NULL; ip_ptr -> nx_ip_routing_table_entry_count--; } + else + { + i++; + } } #endif /* NX_ENABLE_IP_STATIC_ROUTING */ #endif /* !NX_DISABLE_IPV4 */ diff --git a/test/regression/netxduo_test/netx_ip_interface_detachment_test.c b/test/regression/netxduo_test/netx_ip_interface_detachment_test.c index 6f7b8d591..05ab1ceb2 100644 --- a/test/regression/netxduo_test/netx_ip_interface_detachment_test.c +++ b/test/regression/netxduo_test/netx_ip_interface_detachment_test.c @@ -8,6 +8,7 @@ /* */ /* SPDX-License-Identifier: MIT */ /***************************************************************************/ +// Portions of this file were generated with AI assistance. @@ -294,14 +295,54 @@ static void ntest_1_entry(ULONG thread_input) { UINT status; +#ifdef NX_ENABLE_IP_STATIC_ROUTING +UINT route; +#endif tx_mutex_get(&mutex_0, TX_WAIT_FOREVER); +#ifdef NX_ENABLE_IP_STATIC_ROUTING + /* Fill the route table before detaching. Its original route uses the + second interface; keep one route through the primary interface, then + add adjacent second-interface routes. This exercises both the last + table entry and routes shifted into the slot being examined. */ + status = nx_ip_static_route_add(&ip_1, IP_ADDRESS(10, 1, 0, 0), + 0xFFFFFF00UL, IP_ADDRESS(1, 2, 3, 6)); + if (status != NX_SUCCESS) + { + error_counter++; + } + for (route = 2; route < NX_IP_ROUTING_TABLE_SIZE; route++) + { + status = nx_ip_static_route_add(&ip_1, IP_ADDRESS(10, route, 0, 0), + 0xFFFFFF00UL, IP_ADDRESS(4, 3, 2, 10)); + if (status != NX_SUCCESS) + { + error_counter++; + } + } + if (ip_1.nx_ip_routing_table_entry_count != NX_IP_ROUTING_TABLE_SIZE) + { + error_counter++; + } +#endif + /* Detach the 2nd interface(4.3.2.11) from IP instance1. */ status = nx_ip_interface_detach(&ip_1, 1); if(status) error_counter++; +#ifdef NX_ENABLE_IP_STATIC_ROUTING + /* Every route through the detached interface must be gone. The route + through the primary interface must survive in the compacted slot. */ + if ((ip_1.nx_ip_routing_table_entry_count != 1) || + (ip_1.nx_ip_routing_table[0].nx_ip_routing_dest_ip != IP_ADDRESS(10, 1, 0, 0)) || + (ip_1.nx_ip_routing_table[0].nx_ip_routing_entry_ip_interface != &ip_1.nx_ip_interface[0])) + { + error_counter++; + } +#endif + tx_mutex_put(&mutex_0); tx_thread_sleep(1); tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);