summaryrefslogtreecommitdiffstats
path: root/src/router/device.rs
blob: 5dfd22cbdf38a10df37a95f940e084b0a8d59d98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
use arraydeque::{ArrayDeque, Wrapping};
use treebitmap::IpLookupTable;

use crossbeam_deque::{Injector, Steal};
use std::collections::HashMap;
use std::mem;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::{Arc, Mutex, Weak};
use std::thread;
use std::time::{Duration, Instant};

use spin;

use super::super::types::KeyPair;
use super::anti_replay::AntiReplay;

use std::u64;

const REJECT_AFTER_MESSAGES: u64 = u64::MAX - (1 << 4);
const MAX_STAGED_PACKETS: usize = 128;

struct DeviceInner {
    stopped: AtomicBool,
    injector: Injector<()>, // parallel enc/dec task injector
    threads: Vec<thread::JoinHandle<()>>,
    recv: spin::RwLock<HashMap<u32, DecryptionState>>,
    ipv4: IpLookupTable<Ipv4Addr, Weak<PeerInner>>,
    ipv6: IpLookupTable<Ipv6Addr, Weak<PeerInner>>,
}

struct PeerInner {
    stopped: AtomicBool,
    thread_outbound: spin::Mutex<thread::JoinHandle<()>>,
    thread_inbound: spin::Mutex<thread::JoinHandle<()>>,
    inorder_outbound: SyncSender<()>,
    inorder_inbound: SyncSender<()>,
    staged_packets: Mutex<ArrayDeque<[Vec<u8>; MAX_STAGED_PACKETS], Wrapping>>, // packets awaiting handshake
    rx_bytes: AtomicU64,                                                        // received bytes
    tx_bytes: AtomicU64,                                                        // transmitted bytes
    keys: spin::Mutex<KeyWheel>,                                                // key-wheel
    ekey: spin::Mutex<EncryptionState>,                                         // encryption state
    endpoint: spin::Mutex<Option<Arc<SocketAddr>>>,
}

struct EncryptionState {
    key: [u8; 32], // encryption key
    id: u32,       // sender id
    nonce: u64,    // next available nonce
    death: Instant, // time when the key no longer can be used for encryption
                   // (birth + reject-after-time - keepalive-timeout - rekey-timeout)
}

struct DecryptionState {
    key: [u8; 32],
    protector: Arc<spin::Mutex<AntiReplay>>,
    peer: Weak<PeerInner>,
    death: Instant, // time when the key can no longer be used for decryption
}

struct KeyWheel {
    next: Option<KeyPair>,     // next key state (unconfirmed)
    current: Option<KeyPair>,  // current key state (used for encryption)
    previous: Option<KeyPair>, // old key state (used for decryption)
}

pub struct Peer(Arc<PeerInner>);
pub struct Device(DeviceInner);

impl Drop for Peer {
    fn drop(&mut self) {
        // mark peer as stopped
        let inner = &self.0;
        inner.stopped.store(true, Ordering::SeqCst);

        // unpark threads to stop
        inner.thread_inbound.lock().thread().unpark();
        inner.thread_outbound.lock().thread().unpark();
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        // mark device as stopped
        let inner = &self.0;
        inner.stopped.store(true, Ordering::SeqCst);

        // eat all parallel jobs
        while inner.injector.steal() != Steal::Empty {}
    }
}

impl Peer {
    pub fn set_endpoint(&self, endpoint: SocketAddr) {
        *self.0.endpoint.lock() = Some(Arc::new(endpoint))
    }

    pub fn keypair_confirm(&self, ks: Arc<KeyPair>) {
        *self.0.ekey.lock() = EncryptionState {
            id: ks.send.id,
            key: ks.send.key,
            nonce: 0,
            death: ks.birth + Duration::from_millis(1337), // todo
        };
    }

    fn keypair_add(&self, new: KeyPair) -> Option<u32> {
        let mut keys = self.0.keys.lock();
        let release = keys.previous.map(|k| k.recv.id);

        // update key-wheel
        if new.confirmed {
            // start using key for encryption
            *self.0.ekey.lock() = EncryptionState {
                id: new.send.id,
                key: new.send.key,
                nonce: 0,
                death: new.birth + Duration::from_millis(1337), // todo
            };

            // move current into previous
            keys.previous = keys.current;
            keys.current = Some(new);
        } else {
            // store the key and await confirmation
            keys.previous = keys.next;
            keys.next = Some(new);
        };

        // return the released id (for handshake state machine)
        release
    }

    pub fn rx_bytes(&self) -> u64 {
        self.0.rx_bytes.load(Ordering::Relaxed)
    }

    pub fn tx_bytes(&self) -> u64 {
        self.0.tx_bytes.load(Ordering::Relaxed)
    }
}

impl Device {
    pub fn new(workers: usize) -> Device {
        Device(DeviceInner {
            threads: vec![],
            stopped: AtomicBool::new(false),
            injector: Injector::new(),
            recv: spin::RwLock::new(HashMap::new()),
            ipv4: IpLookupTable::new(),
            ipv6: IpLookupTable::new(),
        })
    }

    pub fn add_subnet(&mut self, ip: IpAddr, masklen: u32, peer: Peer) {
        match ip {
            IpAddr::V4(v4) => self.0.ipv4.insert(v4, masklen, Arc::downgrade(&peer.0)),
            IpAddr::V6(v6) => self.0.ipv6.insert(v6, masklen, Arc::downgrade(&peer.0)),
        };
    }

    pub fn subnets(&self, peer: Peer) -> Vec<(IpAddr, u32)> {
        let mut subnets = Vec::new();

        // extract ipv4 entries
        for subnet in self.0.ipv4.iter() {
            let (ip, masklen, p) = subnet;
            if let Some(p) = p.upgrade() {
                if Arc::ptr_eq(&p, &peer.0) {
                    subnets.push((IpAddr::V4(ip), masklen))
                }
            }
        }

        // extract ipv6 entries
        for subnet in self.0.ipv6.iter() {
            let (ip, masklen, p) = subnet;
            if let Some(p) = p.upgrade() {
                if Arc::ptr_eq(&p, &peer.0) {
                    subnets.push((IpAddr::V6(ip), masklen))
                }
            }
        }

        subnets
    }

    pub fn keypair_add(&self, peer: Peer, new: KeyPair) -> Option<u32> {
        // update key-wheel of peer
        let release = peer.keypair_add(new);

        // update incoming packet id map
        let mut recv = self.0.recv.write();

        // release id of previous keypair
        if let Some(id) = release {
            debug_assert!(recv.contains_key(&id));
            recv.remove(&id);
        };

        // map new id to keypair
        debug_assert!(!recv.contains_key(&new.recv.id));

        recv.insert(
            new.recv.id,
            DecryptionState {
                key: new.recv.key,
                protector: Arc::new(spin::Mutex::new(AntiReplay::new())),
                peer: Arc::downgrade(&peer.0),
                death: new.birth + Duration::from_millis(2600), // todo
            },
        );

        release
    }

    /// Adds a new peer to the device
    ///
    /// # Returns
    ///
    /// A atomic ref. counted peer (with liftime matching the device)
    pub fn add(&mut self) -> () {}

    /// Cryptkey routes and sends a plaintext message (IP packet)
    ///
    /// # Arguments
    ///
    /// - pt_msg: IP packet to cryptkey route
    ///
    /// # Returns
    ///
    /// A peer reference for the peer if no key-pair is currently valid for the destination.
    /// This indicates that a handshake should be initated (see the handshake module).
    /// If this occurs the packet is copied to an internal buffer
    /// and retransmission can be attempted using send_run_queue
    pub fn send(&self, pt_msg: &mut [u8]) -> Arc<Peer> {
        unimplemented!();
    }

    /// Sends a message directly to the peer.
    /// The router device takes care of discovering/managing the endpoint.
    /// This is used for handshake initiation/response messages
    ///
    /// # Arguments
    ///
    /// - peer: Reference to the destination peer
    /// - msg: Message to transmit
    pub fn send_raw(&self, peer: Arc<Peer>, msg: &mut [u8]) {
        unimplemented!();
    }

    /// Flush the queue of buffered messages awaiting transmission
    ///
    /// # Arguments
    ///
    /// - peer: Reference for the peer to flush
    pub fn flush_queue(&self, peer: Arc<Peer>) {
        unimplemented!();
    }

    /// Attempt to route, encrypt and send all elements buffered in the queue
    ///
    /// # Arguments
    ///
    /// # Returns
    ///
    /// A boolean indicating whether packages where sent.
    /// Note: This is used for implicit confirmation of handshakes.
    pub fn send_run_queue(&self, peer: Arc<Peer>) -> bool {
        unimplemented!();
    }

    /// Receive an encrypted transport message
    ///
    /// # Arguments
    ///
    /// - ct_msg: Encrypted transport message
    pub fn recv(&self, ct_msg: &mut [u8]) {
        unimplemented!();
    }

    /// Returns the current endpoint known for the peer
    ///
    /// # Arguments
    ///
    /// - peer: The peer to retrieve the endpoint for
    pub fn get_endpoint(&self, peer: Arc<Peer>) -> SocketAddr {
        unimplemented!();
    }

    pub fn set_endpoint(&self, peer: Arc<Peer>, endpoint: SocketAddr) {
        unimplemented!();
    }

    pub fn new_keypair(&self, peer: Arc<Peer>, keypair: KeyPair) {
        unimplemented!();
    }
}