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