2018-08-08 06:12:38 -05:00
|
|
|
// SPDX-License-Identifier: AGPL-1.0-only
|
|
|
|
// Copyright (C) 2018 Ludvig Strigeus <info@tunsafe.com>. All Rights Reserved.
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "tunsafe_types.h"
|
|
|
|
#include "wireguard_proto.h"
|
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
// todo: for multithreaded use case need to use atomic ops.
|
|
|
|
struct WgProcessorStats {
|
|
|
|
// Number of bytes sent/received over the physical UDP connection
|
|
|
|
uint64 udp_bytes_in, udp_bytes_out;
|
|
|
|
uint64 udp_packets_in, udp_packets_out;
|
|
|
|
|
|
|
|
// Number of valid packets sent/received over the TUN interface
|
|
|
|
uint64 tun_bytes_in, tun_bytes_out;
|
|
|
|
uint64 tun_packets_in, tun_packets_out;
|
|
|
|
|
|
|
|
// Error types
|
|
|
|
uint32 error_key_id;
|
|
|
|
uint32 error_mac;
|
|
|
|
uint32 error_duplicate;
|
|
|
|
uint32 error_source_addr;
|
|
|
|
uint32 error_header;
|
|
|
|
|
|
|
|
// Current speed of TUN packets
|
|
|
|
float tun_bytes_in_per_second, tun_bytes_out_per_second;
|
|
|
|
|
|
|
|
// Timestamp of handshakes
|
|
|
|
uint64 first_complete_handshake_timestamp;
|
|
|
|
uint64 last_complete_handshake_timestamp;
|
|
|
|
|
|
|
|
// How much saved from header compression
|
2018-08-08 06:12:38 -05:00
|
|
|
int64 compression_hdr_saved_in, compression_hdr_saved_out;
|
|
|
|
int64 compression_wg_saved_in, compression_wg_saved_out;
|
2018-08-11 20:27:14 -05:00
|
|
|
|
|
|
|
// Number of handshakes received and sent
|
|
|
|
// Number of successful handshakes in and out
|
|
|
|
uint32 handshakes_in, handshakes_out;
|
|
|
|
uint32 handshakes_in_success, handshakes_out_success;
|
|
|
|
|
|
|
|
// Key stuff
|
|
|
|
uint8 public_key[32];
|
|
|
|
|
|
|
|
// Address of the endpoint
|
|
|
|
IpAddr endpoint;
|
2018-11-16 08:07:52 -06:00
|
|
|
|
|
|
|
uint8 endpoint_protocol;
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class ProcessorDelegate {
|
|
|
|
public:
|
2018-08-11 20:27:14 -05:00
|
|
|
virtual void OnConnected() = 0;
|
|
|
|
virtual void OnConnectionRetry(uint32 attempts) = 0;
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
enum InternetBlockState {
|
2018-09-11 14:10:10 -05:00
|
|
|
kBlockInternet_Off = 0,
|
|
|
|
kBlockInternet_Route = 1,
|
|
|
|
kBlockInternet_Firewall = 2,
|
|
|
|
kBlockInternet_Both = 3,
|
2018-10-21 14:53:02 -05:00
|
|
|
kBlockInternet_TypeMask = 0xf,
|
2018-09-11 14:10:10 -05:00
|
|
|
|
|
|
|
kBlockInternet_BlockOnDisconnect = 16,
|
2018-10-21 14:53:02 -05:00
|
|
|
kBlockInternet_AllowLocalNetworks = 32,
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
kBlockInternet_Default = 255,
|
2018-10-21 14:53:02 -05:00
|
|
|
|
|
|
|
kBlockInternet_Active = 256,
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class WireguardProcessor {
|
2018-09-15 11:22:05 -05:00
|
|
|
friend class WgConfig;
|
2018-08-08 06:12:38 -05:00
|
|
|
public:
|
|
|
|
WireguardProcessor(UdpInterface *udp, TunInterface *tun, ProcessorDelegate *procdel);
|
|
|
|
~WireguardProcessor();
|
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
void SetListenPort(int listen_port);
|
2018-09-10 16:46:49 -05:00
|
|
|
void AddDnsServer(const IpAddr &sin);
|
2018-08-11 20:27:14 -05:00
|
|
|
bool SetTunAddress(const WgCidrAddr &addr);
|
2018-09-15 11:22:05 -05:00
|
|
|
void ClearTunAddress();
|
2018-08-11 20:27:14 -05:00
|
|
|
void AddExcludedIp(const WgCidrAddr &cidr_addr);
|
|
|
|
void SetMtu(int mtu);
|
|
|
|
void SetAddRoutesMode(bool mode);
|
|
|
|
void SetDnsBlocking(bool dns_blocking);
|
|
|
|
void SetInternetBlocking(InternetBlockState internet_blocking);
|
|
|
|
void SetHeaderObfuscation(const char *key);
|
2018-09-15 11:22:05 -05:00
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
void HandleTunPacket(Packet *packet);
|
|
|
|
void HandleUdpPacket(Packet *packet, bool overload);
|
2018-08-11 20:27:14 -05:00
|
|
|
static bool IsMainThreadPacket(Packet *packet);
|
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
void SecondLoop();
|
|
|
|
|
2018-10-07 12:40:09 -05:00
|
|
|
const WgProcessorStats &GetStats();
|
2018-08-08 06:12:38 -05:00
|
|
|
void ResetStats();
|
|
|
|
|
|
|
|
bool Start();
|
|
|
|
|
2018-09-15 11:22:05 -05:00
|
|
|
bool ConfigureUdp();
|
|
|
|
bool ConfigureTun();
|
|
|
|
|
2018-08-08 06:12:38 -05:00
|
|
|
WgDevice &dev() { return dev_; }
|
|
|
|
TunInterface::PrePostCommands &prepost() { return pre_post_; }
|
2018-10-22 17:48:20 -05:00
|
|
|
const std::vector<WgCidrAddr> &addr() { return addresses_; }
|
2018-08-11 20:27:14 -05:00
|
|
|
void RunAllMainThreadScheduled();
|
2018-08-08 06:12:38 -05:00
|
|
|
private:
|
|
|
|
void DoWriteUdpPacket(Packet *packet);
|
2018-08-11 20:27:14 -05:00
|
|
|
void WriteAndEncryptPacketToUdp_WillUnlock(WgPeer *peer, Packet *packet);
|
2018-08-08 06:12:38 -05:00
|
|
|
void SendHandshakeInitiation(WgPeer *peer);
|
2018-08-11 20:27:14 -05:00
|
|
|
void SendKeepalive_Locked(WgPeer *peer);
|
|
|
|
void SendQueuedPackets_Locked(WgPeer *peer);
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
void HandleHandshakeInitiationPacket(Packet *packet);
|
|
|
|
void HandleHandshakeResponsePacket(Packet *packet);
|
|
|
|
void HandleHandshakeCookiePacket(Packet *packet);
|
|
|
|
void HandleDataPacket(Packet *packet);
|
|
|
|
|
2018-08-11 20:27:14 -05:00
|
|
|
void HandleAuthenticatedDataPacket_WillUnlock(WgKeypair *keypair, Packet *packet, uint8 *data, size_t data_size);
|
2018-08-08 06:12:38 -05:00
|
|
|
void HandleShortHeaderFormatPacket(uint32 tag, Packet *packet);
|
|
|
|
bool CheckIncomingHandshakeRateLimit(Packet *packet, bool overload);
|
|
|
|
bool HandleIcmpv6NeighborSolicitation(const byte *data, size_t data_size);
|
|
|
|
void SetupCompressionHeader(WgPacketCompressionVer01 *c);
|
2018-08-11 20:27:14 -05:00
|
|
|
void NotifyHandshakeComplete();
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
ProcessorDelegate *procdel_;
|
|
|
|
TunInterface *tun_;
|
|
|
|
UdpInterface *udp_;
|
2018-09-15 11:22:05 -05:00
|
|
|
|
|
|
|
uint16 listen_port_;
|
|
|
|
uint16 mtu_;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
bool dns_blocking_;
|
|
|
|
uint8 internet_blocking_;
|
|
|
|
bool add_routes_mode_;
|
|
|
|
bool network_discovery_spoofing_;
|
2018-08-11 20:27:14 -05:00
|
|
|
bool did_have_first_handshake_;
|
2018-09-15 11:22:05 -05:00
|
|
|
bool is_started_;
|
2018-08-08 06:12:38 -05:00
|
|
|
uint8 network_discovery_mac_[6];
|
|
|
|
|
|
|
|
WgDevice dev_;
|
|
|
|
|
2018-09-15 11:22:05 -05:00
|
|
|
WgProcessorStats stats_;
|
|
|
|
|
2018-10-22 17:48:20 -05:00
|
|
|
std::vector<WgCidrAddr> addresses_;
|
|
|
|
std::vector<IpAddr> dns_addr_;
|
2018-08-08 06:12:38 -05:00
|
|
|
|
|
|
|
TunInterface::PrePostCommands pre_post_;
|
2018-08-11 20:27:14 -05:00
|
|
|
|
|
|
|
uint64 stats_last_bytes_in_, stats_last_bytes_out_;
|
|
|
|
uint64 stats_last_ts_;
|
|
|
|
|
|
|
|
// IPs we want to map to the default route
|
|
|
|
std::vector<WgCidrAddr> excluded_ips_;
|
2018-08-08 06:12:38 -05:00
|
|
|
};
|
|
|
|
|