Kernel/Net: Support removing route entries with unknown gateway

If you specify gateway as 0.0.0.0, the SIOCDELRT ioctl will remove all
route entries that match all the other arguments.
This commit is contained in:
Maciej 2022-07-01 17:23:22 +02:00 committed by Sam Atkins
parent 36676a1604
commit bea1668159
Notes: sideshowbarker 2024-07-17 09:34:52 +09:00
2 changed files with 11 additions and 2 deletions

View file

@ -137,6 +137,8 @@ SpinlockProtected<Route::RouteList>& routing_table()
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, RefPtr<NetworkAdapter> adapter, UpdateTable update)
{
dbgln_if(ROUTING_DEBUG, "update_routing_table {} {} {} {} {} {}", destination, gateway, netmask, flags, adapter, update == UpdateTable::Set ? "Set" : "Delete");
auto route_entry = adopt_ref_if_nonnull(new (nothrow) Route { destination, gateway, netmask, flags, adapter.release_nonnull() });
if (!route_entry)
return ENOMEM;
@ -151,7 +153,9 @@ ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address c
}
if (update == UpdateTable::Delete) {
for (auto& route : table) {
if (route == *route_entry) {
dbgln("candidate: {} {} {} {} {}", route.destination, route.gateway, route.netmask, route.flags, route.adapter);
if (route.matches(*route_entry)) {
// FIXME: Remove all entries, not only the first one.
table.remove(route);
return {};
}

View file

@ -26,7 +26,12 @@ struct Route : public RefCounted<Route> {
bool operator==(Route const& other) const
{
return destination == other.destination && gateway == other.gateway && netmask == other.netmask && flags == other.flags && adapter.ptr() == other.adapter.ptr();
return destination == other.destination && netmask == other.netmask && flags == other.flags && adapter.ptr() == other.adapter.ptr();
}
bool matches(Route const& other) const
{
return destination == other.destination && (gateway == other.gateway || other.gateway.is_zero()) && netmask == other.netmask && flags == other.flags && adapter.ptr() == other.adapter.ptr();
}
const IPv4Address destination;