2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-04-02 16:46:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-03-12 22:16:13 +03:00
|
|
|
#include <AK/IPv4Address.h>
|
2023-04-11 03:50:15 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2021-08-22 00:31:15 +03:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Thread.h>
|
2019-04-02 16:46:44 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-19 18:26:07 +03:00
|
|
|
struct Route final : public AtomicRefCounted<Route> {
|
2023-04-11 03:50:15 +03:00
|
|
|
Route(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, NonnullRefPtr<NetworkAdapter> adapter)
|
2022-03-12 22:16:13 +03:00
|
|
|
: destination(destination)
|
|
|
|
, gateway(gateway)
|
|
|
|
, netmask(netmask)
|
2022-05-09 14:23:02 +03:00
|
|
|
, flags(flags)
|
2022-03-12 22:16:13 +03:00
|
|
|
, adapter(adapter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-30 05:54:06 +03:00
|
|
|
bool operator==(Route const& other) const
|
2022-03-12 22:16:13 +03:00
|
|
|
{
|
2022-07-01 18:23:22 +03:00
|
|
|
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();
|
2022-03-12 22:16:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const IPv4Address destination;
|
|
|
|
const IPv4Address gateway;
|
|
|
|
const IPv4Address netmask;
|
2022-05-09 14:23:02 +03:00
|
|
|
const u16 flags;
|
2023-04-11 03:50:15 +03:00
|
|
|
NonnullRefPtr<NetworkAdapter> const adapter;
|
2022-03-12 22:16:13 +03:00
|
|
|
|
2023-04-11 03:50:15 +03:00
|
|
|
IntrusiveListNode<Route, RefPtr<Route>> route_list_node {};
|
2022-03-12 22:16:13 +03:00
|
|
|
using RouteList = IntrusiveList<&Route::route_list_node>;
|
|
|
|
};
|
|
|
|
|
2020-03-22 03:12:45 +03:00
|
|
|
struct RoutingDecision {
|
2023-04-11 03:50:15 +03:00
|
|
|
RefPtr<NetworkAdapter> adapter;
|
2019-08-29 04:18:32 +03:00
|
|
|
MACAddress next_hop;
|
2019-08-29 04:18:38 +03:00
|
|
|
|
|
|
|
bool is_zero() const;
|
2019-08-28 14:58:01 +03:00
|
|
|
};
|
|
|
|
|
2022-03-12 21:56:23 +03:00
|
|
|
enum class UpdateTable {
|
2021-07-25 02:37:54 +03:00
|
|
|
Set,
|
|
|
|
Delete,
|
|
|
|
};
|
|
|
|
|
2022-03-12 21:56:23 +03:00
|
|
|
void update_arp_table(IPv4Address const&, MACAddress const&, UpdateTable update);
|
2023-04-11 03:50:15 +03:00
|
|
|
ErrorOr<void> update_routing_table(IPv4Address const& destination, IPv4Address const& gateway, IPv4Address const& netmask, u16 flags, RefPtr<NetworkAdapter> const adapter, UpdateTable update);
|
2021-12-02 01:27:24 +03:00
|
|
|
|
|
|
|
enum class AllowUsingGateway {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2023-12-24 20:01:09 +03:00
|
|
|
enum class AllowBroadcast {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
|
|
|
RoutingDecision route_to(IPv4Address const& target, IPv4Address const& source, RefPtr<NetworkAdapter> const through = nullptr, AllowBroadcast = AllowBroadcast::No, AllowUsingGateway = AllowUsingGateway::Yes);
|
2019-08-28 14:58:01 +03:00
|
|
|
|
2022-11-09 13:39:58 +03:00
|
|
|
SpinlockProtected<HashMap<IPv4Address, MACAddress>, LockRank::None>& arp_table();
|
|
|
|
SpinlockProtected<Route::RouteList, LockRank::None>& routing_table();
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|