aboutsummaryrefslogtreecommitdiffstats
path: root/src/wireguard
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-10-13 22:26:12 +0200
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-10-13 22:26:12 +0200
commita08fd4002bfae92072f64f8d5e0084e6f248f139 (patch)
treea50315318549056627adb05bdd0a4f1a02f8541d /src/wireguard
parentPort timer.c from WireGuard (diff)
downloadwireguard-rs-a08fd4002bfae92072f64f8d5e0084e6f248f139.tar.xz
wireguard-rs-a08fd4002bfae92072f64f8d5e0084e6f248f139.zip
Work on Linux platform code
Diffstat (limited to '')
-rw-r--r--src/wireguard/config.rs (renamed from src/config.rs)12
-rw-r--r--src/wireguard/constants.rs (renamed from src/constants.rs)0
-rw-r--r--src/wireguard/handshake/device.rs (renamed from src/handshake/device.rs)0
-rw-r--r--src/wireguard/handshake/macs.rs (renamed from src/handshake/macs.rs)0
-rw-r--r--src/wireguard/handshake/messages.rs (renamed from src/handshake/messages.rs)2
-rw-r--r--src/wireguard/handshake/mod.rs (renamed from src/handshake/mod.rs)0
-rw-r--r--src/wireguard/handshake/noise.rs (renamed from src/handshake/noise.rs)2
-rw-r--r--src/wireguard/handshake/peer.rs (renamed from src/handshake/peer.rs)0
-rw-r--r--src/wireguard/handshake/ratelimiter.rs (renamed from src/handshake/ratelimiter.rs)0
-rw-r--r--src/wireguard/handshake/timestamp.rs (renamed from src/handshake/timestamp.rs)0
-rw-r--r--src/wireguard/handshake/types.rs (renamed from src/handshake/types.rs)2
-rw-r--r--src/wireguard/mod.rs23
-rw-r--r--src/wireguard/router/anti_replay.rs (renamed from src/router/anti_replay.rs)0
-rw-r--r--src/wireguard/router/constants.rs (renamed from src/router/constants.rs)0
-rw-r--r--src/wireguard/router/device.rs (renamed from src/router/device.rs)24
-rw-r--r--src/wireguard/router/ip.rs (renamed from src/router/ip.rs)0
-rw-r--r--src/wireguard/router/messages.rs (renamed from src/router/messages.rs)0
-rw-r--r--src/wireguard/router/mod.rs (renamed from src/router/mod.rs)0
-rw-r--r--src/wireguard/router/peer.rs (renamed from src/router/peer.rs)2
-rw-r--r--src/wireguard/router/tests.rs (renamed from src/router/tests.rs)3
-rw-r--r--src/wireguard/router/types.rs (renamed from src/router/types.rs)2
-rw-r--r--src/wireguard/router/workers.rs (renamed from src/router/workers.rs)0
-rw-r--r--src/wireguard/tests.rs (renamed from src/tests.rs)6
-rw-r--r--src/wireguard/timers.rs (renamed from src/timers.rs)8
-rw-r--r--src/wireguard/types/bind.rs (renamed from src/types/bind.rs)0
-rw-r--r--src/wireguard/types/dummy.rs (renamed from src/types/dummy.rs)0
-rw-r--r--src/wireguard/types/endpoint.rs (renamed from src/types/endpoint.rs)0
-rw-r--r--src/wireguard/types/keys.rs (renamed from src/types/keys.rs)0
-rw-r--r--src/wireguard/types/mod.rs (renamed from src/types/mod.rs)0
-rw-r--r--src/wireguard/types/tun.rs (renamed from src/types/tun.rs)0
-rw-r--r--src/wireguard/wireguard.rs (renamed from src/wireguard.rs)27
31 files changed, 69 insertions, 44 deletions
diff --git a/src/config.rs b/src/wireguard/config.rs
index 3376dcc..0f2953d 100644
--- a/src/config.rs
+++ b/src/wireguard/config.rs
@@ -1,12 +1,10 @@
-use std::error::Error;
use std::net::{IpAddr, SocketAddr};
use x25519_dalek::{PublicKey, StaticSecret};
-use crate::wireguard::Wireguard;
-use crate::types::tun::Tun;
-use crate::types::bind::Bind;
+use super::wireguard::Wireguard;
+use super::types::bind::Bind;
+use super::types::tun::Tun;
-///
/// The goal of the configuration interface is, among others,
/// to hide the IO implementations (over which the WG device is generic),
/// from the configuration and UAPI code.
@@ -180,5 +178,9 @@ impl <T : Tun, B : Bind>Configuration for Wireguard<T, B> {
fn set_listen_port(&self, port : u16) -> Option<ConfigError> {
None
}
+
+ fn set_fwmark(&self, mark: Option<u32>) -> Option<ConfigError> {
+ None
+ }
} \ No newline at end of file
diff --git a/src/constants.rs b/src/wireguard/constants.rs
index 72de8d9..72de8d9 100644
--- a/src/constants.rs
+++ b/src/wireguard/constants.rs
diff --git a/src/handshake/device.rs b/src/wireguard/handshake/device.rs
index 6a55f6e..6a55f6e 100644
--- a/src/handshake/device.rs
+++ b/src/wireguard/handshake/device.rs
diff --git a/src/handshake/macs.rs b/src/wireguard/handshake/macs.rs
index 689826b..689826b 100644
--- a/src/handshake/macs.rs
+++ b/src/wireguard/handshake/macs.rs
diff --git a/src/handshake/messages.rs b/src/wireguard/handshake/messages.rs
index 796e3c0..29d80af 100644
--- a/src/handshake/messages.rs
+++ b/src/wireguard/handshake/messages.rs
@@ -4,7 +4,6 @@ use hex;
#[cfg(test)]
use std::fmt;
-use std::cmp;
use std::mem;
use byteorder::LittleEndian;
@@ -265,6 +264,7 @@ impl fmt::Debug for MacsFooter {
/* Equality (for testing purposes) */
+#[cfg(test)]
macro_rules! eq_as_bytes {
($type:path) => {
impl PartialEq for $type {
diff --git a/src/handshake/mod.rs b/src/wireguard/handshake/mod.rs
index 071a41f..071a41f 100644
--- a/src/handshake/mod.rs
+++ b/src/wireguard/handshake/mod.rs
diff --git a/src/handshake/noise.rs b/src/wireguard/handshake/noise.rs
index 1dc8402..a2a84b0 100644
--- a/src/handshake/noise.rs
+++ b/src/wireguard/handshake/noise.rs
@@ -27,7 +27,7 @@ use super::peer::{Peer, State};
use super::timestamp;
use super::types::*;
-use crate::types::{Key, KeyPair};
+use super::super::types::{KeyPair, Key};
use std::time::Instant;
diff --git a/src/handshake/peer.rs b/src/wireguard/handshake/peer.rs
index c9e1c40..c9e1c40 100644
--- a/src/handshake/peer.rs
+++ b/src/wireguard/handshake/peer.rs
diff --git a/src/handshake/ratelimiter.rs b/src/wireguard/handshake/ratelimiter.rs
index 63d728c..63d728c 100644
--- a/src/handshake/ratelimiter.rs
+++ b/src/wireguard/handshake/ratelimiter.rs
diff --git a/src/handshake/timestamp.rs b/src/wireguard/handshake/timestamp.rs
index b5bd9f0..b5bd9f0 100644
--- a/src/handshake/timestamp.rs
+++ b/src/wireguard/handshake/timestamp.rs
diff --git a/src/handshake/types.rs b/src/wireguard/handshake/types.rs
index ba71ec4..5f984cc 100644
--- a/src/handshake/types.rs
+++ b/src/wireguard/handshake/types.rs
@@ -3,7 +3,7 @@ use std::fmt;
use x25519_dalek::PublicKey;
-use crate::types::KeyPair;
+use super::super::types::KeyPair;
/* Internal types for the noise IKpsk2 implementation */
diff --git a/src/wireguard/mod.rs b/src/wireguard/mod.rs
new file mode 100644
index 0000000..9417e57
--- /dev/null
+++ b/src/wireguard/mod.rs
@@ -0,0 +1,23 @@
+mod wireguard;
+// mod config;
+mod constants;
+mod timers;
+
+mod handshake;
+mod router;
+mod types;
+
+#[cfg(test)]
+mod tests;
+
+/// The WireGuard sub-module contains a pure, configurable implementation of WireGuard.
+/// The implementation is generic over:
+///
+/// - TUN type, specifying how packets are received on the interface side: a reader/writer and MTU reporting interface.
+/// - Bind type, specifying how WireGuard messages are sent/received from the internet and what constitutes an "endpoint"
+
+pub use wireguard::{Wireguard, Peer};
+
+pub use types::bind;
+pub use types::tun;
+pub use types::Endpoint; \ No newline at end of file
diff --git a/src/router/anti_replay.rs b/src/wireguard/router/anti_replay.rs
index b0838bd..b0838bd 100644
--- a/src/router/anti_replay.rs
+++ b/src/wireguard/router/anti_replay.rs
diff --git a/src/router/constants.rs b/src/wireguard/router/constants.rs
index 0ca824a..0ca824a 100644
--- a/src/router/constants.rs
+++ b/src/wireguard/router/constants.rs
diff --git a/src/router/device.rs b/src/wireguard/router/device.rs
index 989c2c2..455020c 100644
--- a/src/router/device.rs
+++ b/src/wireguard/router/device.rs
@@ -21,9 +21,9 @@ use super::types::{Callbacks, RouterError};
use super::workers::{worker_parallel, JobParallel, Operation};
use super::SIZE_MESSAGE_PREFIX;
-use super::super::types::{KeyPair, Endpoint, bind, tun};
+use super::super::types::{bind, tun, Endpoint, KeyPair};
-pub struct DeviceInner<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
+pub struct DeviceInner<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
// inbound writer (TUN)
pub inbound: T,
@@ -47,7 +47,7 @@ pub struct EncryptionState {
pub death: Instant, // (birth + reject-after-time - keepalive-timeout - rekey-timeout)
}
-pub struct DecryptionState<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
+pub struct DecryptionState<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
pub keypair: Arc<KeyPair>,
pub confirmed: AtomicBool,
pub protector: Mutex<AntiReplay>,
@@ -55,12 +55,12 @@ pub struct DecryptionState<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::
pub death: Instant, // time when the key can no longer be used for decryption
}
-pub struct Device<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
- state: Arc<DeviceInner<E, C, T, B>>, // reference to device state
+pub struct Device<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> {
+ state: Arc<DeviceInner<E, C, T, B>>, // reference to device state
handles: Vec<thread::JoinHandle<()>>, // join handles for workers
}
-impl<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Drop for Device<E, C, T, B> {
+impl<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Drop for Device<E, C, T, B> {
fn drop(&mut self) {
debug!("router: dropping device");
@@ -85,7 +85,7 @@ impl<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Drop for De
}
#[inline(always)]
-fn get_route<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>>(
+fn get_route<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>>(
device: &Arc<DeviceInner<E, C, T, B>>,
packet: &[u8],
) -> Option<Arc<PeerInner<E, C, T, B>>> {
@@ -124,10 +124,10 @@ fn get_route<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>>(
}
}
-impl<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Device<E, C, T, B> {
+impl<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Device<E, C, T, B> {
pub fn new(num_workers: usize, tun: T) -> Device<E, C, T, B> {
// allocate shared device state
- let mut inner = DeviceInner {
+ let inner = DeviceInner {
inbound: tun,
outbound: RwLock::new(None),
queues: Mutex::new(Vec::with_capacity(num_workers)),
@@ -235,9 +235,9 @@ impl<E : Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> Device<E, C
}
/// Set outbound writer
- ///
- ///
- pub fn set_outbound_writer(&self, new : B) {
+ ///
+ ///
+ pub fn set_outbound_writer(&self, new: B) {
*self.state.outbound.write() = Some(new);
}
}
diff --git a/src/router/ip.rs b/src/wireguard/router/ip.rs
index e66144f..e66144f 100644
--- a/src/router/ip.rs
+++ b/src/wireguard/router/ip.rs
diff --git a/src/router/messages.rs b/src/wireguard/router/messages.rs
index bf4d13b..bf4d13b 100644
--- a/src/router/messages.rs
+++ b/src/wireguard/router/messages.rs
diff --git a/src/router/mod.rs b/src/wireguard/router/mod.rs
index 7a29cd9..7a29cd9 100644
--- a/src/router/mod.rs
+++ b/src/wireguard/router/mod.rs
diff --git a/src/router/peer.rs b/src/wireguard/router/peer.rs
index 13e5af4..4f47604 100644
--- a/src/router/peer.rs
+++ b/src/wireguard/router/peer.rs
@@ -291,7 +291,7 @@ impl<E: Endpoint, C: Callbacks, T: tun::Writer, B: bind::Writer<E>> PeerInner<E,
&self,
src: E,
dec: Arc<DecryptionState<E, C, T, B>>,
- mut msg: Vec<u8>,
+ msg: Vec<u8>,
) -> Option<JobParallel> {
let (tx, rx) = oneshot();
let key = dec.keypair.recv.key;
diff --git a/src/router/tests.rs b/src/wireguard/router/tests.rs
index 6c385a8..fbee39e 100644
--- a/src/router/tests.rs
+++ b/src/wireguard/router/tests.rs
@@ -10,7 +10,6 @@ use pnet::packet::ipv4::MutableIpv4Packet;
use pnet::packet::ipv6::MutableIpv6Packet;
use super::super::types::bind::*;
-use super::super::types::tun::*;
use super::super::types::*;
use super::{Callbacks, Device, SIZE_MESSAGE_PREFIX};
@@ -146,7 +145,7 @@ mod tests {
// create device
let (_fake, _reader, tun_writer, _mtu) = dummy::TunTest::create(1500, false);
- let router: Device< _, BencherCallbacks, dummy::TunWriter, dummy::VoidBind> =
+ let router: Device<_, BencherCallbacks, dummy::TunWriter, dummy::VoidBind> =
Device::new(num_cpus::get(), tun_writer);
// add new peer
diff --git a/src/router/types.rs b/src/wireguard/router/types.rs
index 4a72c27..b7c3ae0 100644
--- a/src/router/types.rs
+++ b/src/wireguard/router/types.rs
@@ -1,8 +1,6 @@
use std::error::Error;
use std::fmt;
-use super::super::types::Endpoint;
-
pub trait Opaque: Send + Sync + 'static {}
impl<T> Opaque for T where T: Send + Sync + 'static {}
diff --git a/src/router/workers.rs b/src/wireguard/router/workers.rs
index 2e89bb0..2e89bb0 100644
--- a/src/router/workers.rs
+++ b/src/wireguard/router/workers.rs
diff --git a/src/tests.rs b/src/wireguard/tests.rs
index 8e15037..0148d5d 100644
--- a/src/tests.rs
+++ b/src/wireguard/tests.rs
@@ -1,6 +1,6 @@
-use crate::types::tun::Tun;
-use crate::types::{bind, dummy, tun};
-use crate::wireguard::Wireguard;
+use super::types::tun::Tun;
+use super::types::{bind, dummy, tun};
+use super::wireguard::Wireguard;
use std::thread;
use std::time::Duration;
diff --git a/src/timers.rs b/src/wireguard/timers.rs
index 9854229..2792c7b 100644
--- a/src/timers.rs
+++ b/src/wireguard/timers.rs
@@ -7,10 +7,10 @@ use log::info;
use hjul::{Runner, Timer};
-use crate::constants::*;
-use crate::router::Callbacks;
-use crate::types::{bind, tun};
-use crate::wireguard::{Peer, PeerInner};
+use super::constants::*;
+use super::router::Callbacks;
+use super::types::{bind, tun};
+use super::wireguard::{Peer, PeerInner};
pub struct Timers {
handshake_pending: AtomicBool,
diff --git a/src/types/bind.rs b/src/wireguard/types/bind.rs
index 3d3f187..3d3f187 100644
--- a/src/types/bind.rs
+++ b/src/wireguard/types/bind.rs
diff --git a/src/types/dummy.rs b/src/wireguard/types/dummy.rs
index 2403c9b..2403c9b 100644
--- a/src/types/dummy.rs
+++ b/src/wireguard/types/dummy.rs
diff --git a/src/types/endpoint.rs b/src/wireguard/types/endpoint.rs
index f4f93da..f4f93da 100644
--- a/src/types/endpoint.rs
+++ b/src/wireguard/types/endpoint.rs
diff --git a/src/types/keys.rs b/src/wireguard/types/keys.rs
index 282c4ae..282c4ae 100644
--- a/src/types/keys.rs
+++ b/src/wireguard/types/keys.rs
diff --git a/src/types/mod.rs b/src/wireguard/types/mod.rs
index e0725f3..e0725f3 100644
--- a/src/types/mod.rs
+++ b/src/wireguard/types/mod.rs
diff --git a/src/types/tun.rs b/src/wireguard/types/tun.rs
index 2ba16ff..2ba16ff 100644
--- a/src/types/tun.rs
+++ b/src/wireguard/types/tun.rs
diff --git a/src/wireguard.rs b/src/wireguard/wireguard.rs
index f14a053..7a22280 100644
--- a/src/wireguard.rs
+++ b/src/wireguard/wireguard.rs
@@ -1,13 +1,12 @@
-use crate::constants::*;
-use crate::handshake;
-use crate::router;
-use crate::timers::{Events, Timers};
+use super::constants::*;
+use super::handshake;
+use super::router;
+use super::timers::{Events, Timers};
-use crate::types::bind::Reader as BindReader;
-use crate::types::bind::{Bind, Writer};
-use crate::types::tun::{Reader, Tun, MTU};
-
-use crate::types::Endpoint;
+use super::types::bind::Reader as BindReader;
+use super::types::bind::{Bind, Writer};
+use super::types::tun::{Reader, Tun, MTU};
+use super::types::Endpoint;
use hjul::Runner;
@@ -372,9 +371,13 @@ impl<T: Tun, B: Bind> Wireguard<T, B> {
msg.resize(size, 0);
// read a new IP packet
- let payload = reader
- .read(&mut msg[..], router::SIZE_MESSAGE_PREFIX)
- .unwrap();
+ let payload = match reader.read(&mut msg[..], router::SIZE_MESSAGE_PREFIX) {
+ Ok(payload) => payload,
+ Err(e) => {
+ debug!("TUN worker, failed to read from tun device: {}", e);
+ return;
+ }
+ };
debug!("TUN worker, IP packet of {} bytes (MTU = {})", payload, mtu);
// truncate padding