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