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