ethox 0.0.2

A standalone network stack for user-space networking and unikernels
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Contains main TCP handling.
//!
//! Relevant material for reading:
//! Main TCP rfc (skip if confident): https://tools.ietf.org/html/rfc793
//! Errata and comments: https://tools.ietf.org/html/rfc1122#section-4.2
//!     Notably still assuming some good-faith on hosts
//! TCP congestion control: https://tools.ietf.org/html/rfc5681
//! Attack avoidance: https://tools.ietf.org/html/rfc5961
//! Selective ACKs: https://tools.ietf.org/html/rfc2018
//! RST handling specifically: https://www.snellman.net/blog/archive/2016-02-01-tcp-rst/
//!     OS comparison in particular
use crate::layer::ip;
use crate::managed::{Map, SlotMap, slotmap::Key};
use crate::wire::{ip::Address, tcp::SeqNumber, tcp::Packet as TcpPacket};
use crate::wire::PayloadMut;
use crate::time::{Duration, Expiration, Instant};

use super::connection::{
    Connection,
    Flow,
    Send,
    State,
    Receive};
use super::packet::{In, Raw};
use super::siphash::IsnGenerator;

/// Handles TCP connection states.
pub struct Endpoint<'a> {
    ports: Map<'a, FourTuple, Key>,
    states: SlotMap<'a, Slot>,
    isn_generator: IsnGenerator,
}

/// The TCP connection identifier, with four components.
///
/// In the model of TCP, there exists at most one bidirectional data stream for each unique tuple.
///
/// There is no inherent split into so called 'privileged' and unprivileged ports (the former often
/// refers to ports below 1024). This is mostly a best practise you *may* want to follow but it is
/// not built into the library.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FourTuple {
    /// The address identifying this machine/device for incoming segments.
    ///
    /// This address is not necessarily accurate (or unicast) until a connection has been
    /// established. In particular, it is used also for the netmask of a passively opened
    /// port and then modified when an incoming SYN is accepted.
    pub local: Address,
    /// The address to which outgoing segments are sent.
    ///
    /// This is filled in for actively opened connections but contains the reserved all-zero
    /// address for a passively opened port until a connection attempt.
    pub remote: Address,
    /// The local port of the connection.
    pub local_port: u16,
    /// The remote port of the connection.
    pub remote_port: u16,
}

/// A connection slot wrapping the internal connection state.
///
/// Contains the four-tuple which maps to the slot, completing the loop for lookups
/// (slotkey->4tuple,4tuple->slotkey). Usage of this acts similar to a slotmap where a dedicated
/// `SlotKey` allows referring to a connection outside of its lifetime without introducing
/// lifetime-tracked references and dependencies.
///
/// Internally used as the backing storage to open or accept a new connection.
#[derive(Clone, Copy, Debug, Hash)]
pub struct Slot {
    addr: FourTuple,
    connection: Connection,
}

/// The index of a connection.
///
/// Useful for storing in other structs to reference the connection at another point in time. Note
/// that the index will be invalidated when the connection itself is closed.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SlotKey {
    key: Key,
}

/// An endpoint borrowed for receiving.
///
/// Incoming packets are matched against open ports and connections. Many parts of connection
/// state transitions are then performed automatically. If no direct answer was required the packet
/// becomes available for other uses.
pub struct Receiver<'a, 'e, H> {
    endpoint: Borrow<'a, 'e>,

    /// The upper protocol receiver.
    handler: H,
}

/// An endpoint borrowed for sending.
///
/// The send handler gets exclusive access the the internal state in order to create new active
/// connections or listening sockets. Each packet that becomes available for sending can be
/// committed to any of the open connections, or for creating or closing one as well.
pub struct Sender<'a, 'e, H> {
    endpoint: Borrow<'a, 'e>,

    /// The upper protocol receiver.
    handler: H,
}

struct Borrow<'a, 'e> {
    // TODO: could be immutable as well, just disallowing updates. Evaluate whether this is useful
    // or needed somewhere.
    inner: &'a mut Endpoint<'e>,
}

/// A partially mutable reference to a connection.
///
/// Retrieving an `Entry` for a given `SlotKey` consists of slicing the internal representation of
/// the `Endpoint` in such a way that it is possible to consistently change the four-tuple mapping
/// to that connection, to modify the connection itself, and to access the initial sequence number
/// generator.
pub struct Entry<'a> {
    // TODO: add remapping to the `Entry` based api of the map if required for performance.
    key: SlotKey,
    ports: &'a mut dyn PortMap,
    isn: &'a IsnGenerator,
    slot: &'a mut Slot,
}

/// A mutable reference to the key-structure of a connection.
///
/// From an `Entry` split into the connection and the keys referring to it in such a manner that
/// the keys can be edited without affecting the connection itself.
pub struct EntryKey<'a> {
    ports: &'a mut dyn PortMap,
    isn: &'a IsnGenerator,
    key_in_slot: &'a mut FourTuple,
}

/// Provides remapping a `SlotKey` under a different four tuple.
///
/// Erases the lifetime from the underlying `Map` itself.
pub(crate) trait PortMap {
    /// Note: does not permit failure so we must never expose it.
    fn remap(&mut self, old: FourTuple, new: FourTuple);
}

impl Endpoint<'_> {
    /// Returns a reference to the slot containing the corresponding connection.
    ///
    /// Can be used to inspect the TCP state and connection information.
    pub fn get(&self, index: SlotKey)
        -> Option<&Slot>
    {
        self.states.get(index.key)
    }

    /// Returns a mutable reference to the slot containing the corresponding connection.
    pub fn get_mut(&mut self, index: SlotKey)
        -> Option<&mut Slot>
    {
        self.states.get_mut(index.key)
    }

    /// Returns the entry of the corresponding connection.
    pub(crate) fn entry(&mut self, index: SlotKey)
        -> Option<Entry>
    {
        let slot = self.states.get_mut(index.key)?;

        Some(Entry {
            key: SlotKey { key: index.key },
            ports: &mut self.ports,
            isn: &mut self.isn_generator,
            slot,
        })
    }

    /// Returns the entry of a connection identification tuple.
    pub fn key_from_tuple(&mut self, tuple: FourTuple)
        -> Option<SlotKey>
    {
        let key = self.ports.get(&tuple).cloned()?;
        Some(SlotKey { key })
    }

    /// Returns the entry of a connection identification tuple.
    pub(crate) fn entry_from_tuple(&mut self, tuple: FourTuple)
        -> Option<Entry>
    {
        let key = self.key_from_tuple(tuple)?;
        self.entry(key)
    }

    /// Forcibly drop a connection.
    ///
    /// Note that this will *not* send any termination messages and *not* wait for a graceful exit
    /// period. It simply sets the state of the slot to `Closed` and removes all other mappings.
    pub fn remove(&mut self, index: SlotKey) {
        let addr = match self.get_mut(index) {
            Some(connection) => {
                connection.connection.change_state(State::Closed);
                connection.addr
            },
            None => return,
        };

        self.ports.entry(addr).remove();
        let _ = self.states.remove(index.key);
    }

    /// Opens a new port for listening.
    ///
    /// The source address is chosen during the first send operation on the create connection.
    /// Returns the key to use to inspect or modify the connection state and parameters.
    fn listen(&mut self, ip: Address, port: u16)
        -> Option<SlotKey>
    {
        let key = FourTuple {
            local: ip,
            local_port: port,
            // Filled by the remote connection attempt.
            remote: Address::Unspecified,
            remote_port: 0,
        };

        let (key, state) = self.create_state(key)?;
        state.connection.current = State::Listen;
        Some(key)
    }

    /// Actively try to connect to a remote TCP.
    ///
    /// This is not public as the caller controls the complete tuple.
    fn open(&mut self, tuple: FourTuple)
        -> Option<SlotKey>
    {
        let (key, _) = self.create_state(tuple)?;
        // Don't set to open yet, only after having sent the packet.
        Some(key)
    }

    fn create_state(&mut self, addr: FourTuple)
        -> Option<(SlotKey, &mut Slot)>
    {
        let connection = self.create_connection();

        let vacant = self.ports
            .entry(addr)
            .vacant()?;

        // FIXME: would be nicer to have an `Entry` api on the slotmap for peace of mind. It is
        // however mostly inconsequential right now.
        //
        // Reserves a slot, don't lose the key or we'd leak that reservation.
        let (key, slot) = self.states
            .reserve()?;

        slot.connection = connection;
        slot.addr = addr;
        vacant.insert(key);

        let key = SlotKey {
            key,
        };

        Some((key, slot))
    }

    /// Initialize a closed connection.
    ///
    /// The raw method is near useless, transition the connection to an appropriate state
    /// afterwards.
    fn create_connection(&mut self) -> Connection {
        Connection {
            current: State::Closed,
            previous: State::Closed,
            flow_control: Flow {
                congestion_window: 0,
                ssthresh: u32::max_value(),
                recover: SeqNumber::default(),
            },
            receive_window: 0,
            sender_maximum_segment_size: 0,
            receiver_maximum_segment_size: 0,
            last_ack_receive_offset: SeqNumber::default(),
            ack_timer: Expiration::Never,
            ack_timeout: Duration::from_millis(500),
            retransmission_timer: Instant::from_millis(0),
            retransmission_timeout: Duration::from_millis(3000),
            restart_timeout: Duration::from_millis(30000),
            selective_acknowledgements: false,
            duplicate_ack: 0,
            send: Send {
                unacked: SeqNumber::default(),
                next: SeqNumber::default(),
                last_time: Instant::from_millis(0),
                unsent: 0,
                window: 0,
                window_scale: 0,
                initial_seq: SeqNumber::default(),
            },
            recv: Receive {
                acked: SeqNumber::default(),
                next: SeqNumber::default(),
                last_time: Instant::from_millis(0),
                window: 0,
                window_scale: 0,
                initial_seq: SeqNumber::default(),
            },
        }
    }

    fn initial_seq_num(&mut self, id: FourTuple, time: Instant) -> SeqNumber {
        self.isn_generator.get_isn(id, time)
    }
}

impl Slot {
    /// Return the connection four tuple of the slot.
    ///
    /// Note that the source or destination ports may be `0` if they have not been chosen yet. This
    /// happens when the connection has only been opened passively or no active SYN has been sent.
    pub fn four_tuple(&self) -> FourTuple {
        self.addr
    }

    /// Returns a reference to the connection contained in the slot.
    pub(crate) fn connection(&self) -> &Connection {
        &self.connection
    }
}

impl<'ep> Endpoint<'ep> {
    /// Create a new endpoint.
    ///
    /// The map and states are **not** cleared.
    pub fn new(
        ports: Map<'ep, FourTuple, Key>,
        states: SlotMap<'ep, Slot>,
        isn_generator: IsnGenerator,
    ) -> Self {
        Endpoint {
            ports,
            states,
            isn_generator,
        }
    }

    /// Create a TCP receiver using this endpoint.
    pub fn recv<H>(&mut self, handler: H) -> Receiver<'_, 'ep, H> {
        Receiver { endpoint: self.borrow(), handler }
    }

    /// Create a TCP sender using this endpoint.
    pub fn send<H>(&mut self, handler: H) -> Sender<'_, 'ep, H> {
        Sender { endpoint: self.borrow(), handler }
    }

    fn borrow(&mut self) -> Borrow<'_, 'ep> {
        Borrow { inner: self, }
    }
}

impl<'a> Entry<'a> {
    /// Destructure into mapping metadata and a reference to the connection.
    pub fn into_key_value(self) -> (EntryKey<'a>, &'a mut Connection) {
        let entry_key = EntryKey {
            ports: self.ports,
            isn: self.isn,
            key_in_slot: &mut self.slot.addr,
        };

        let connection = &mut self.slot.connection;

        (entry_key, connection)
    }

    /// Get the slot key with which the entry was created.
    pub fn slot_key(&self) -> SlotKey {
        self.key
    }

    /// Get a mutable reference to the connection without destructuring.
    pub fn connection(&mut self) -> &mut Connection {
        &mut self.slot.connection
    }
}

impl EntryKey<'_> {
    /// Generate a new initial sequence number.
    pub fn initial_seq_num(&self, time: Instant) -> SeqNumber {
        self.isn.get_isn(*self.key_in_slot, time)
    }

    pub fn four_tuple(&self) -> FourTuple {
        *self.key_in_slot
    }

    /// Move the connection state to a new connection tuple.
    ///
    /// # Panics
    /// `new` must not be taken by any other connection yet.
    pub fn set_four_tuple(&mut self, new: FourTuple) {
        self.ports.remap(*self.key_in_slot, new);
        *self.key_in_slot = new;
    }
}

#[cfg(test)]
impl<'a> EntryKey<'a> {
    pub(crate) fn fake(
        ports: &'a mut dyn PortMap,
        isn: &'a IsnGenerator,
        key_in_slot: &'a mut FourTuple,
    ) -> EntryKey<'a> {
        EntryKey { ports, isn, key_in_slot, }
    }
}

impl Default for Slot {
    fn default() -> Self {
       Slot {
           addr: FourTuple::default(),
           connection: Connection::zeroed(),
       }
    }
}

impl super::connection::Endpoint for Endpoint<'_> {
    fn get(&self, index: SlotKey) -> Option<&Slot> {
        Endpoint::get(self, index)
    }

    fn get_mut(&mut self, index: SlotKey) -> Option<&mut Slot> {
        Endpoint::get_mut(self, index)
    }

    fn remove(&mut self, index: SlotKey) {
        Endpoint::remove(self, index)
    }

    fn entry(&mut self, index: SlotKey) -> Option<Entry> {
        Endpoint::entry(self, index)
    }

    fn find_tuple(&mut self, tuple: FourTuple) -> Option<Entry> {
        if self.ports.entry(tuple).occupied().is_some() {
            Endpoint::entry_from_tuple(self, tuple)
        } else {
            Endpoint::entry_from_tuple(self, FourTuple {
                local: tuple.local,
                local_port: tuple.local_port,
                remote: Address::Unspecified,
                remote_port: 0,
            })
        }
    }

    fn source_port(&mut self, _: Address) -> Option<u16> {
        // FIXME: find a suitable source port....
        Some(80)
    }

    fn listen(&mut self, ip: Address, port: u16) -> Option<SlotKey> {
        Endpoint::listen(self, ip, port)
    }

    fn open(&mut self, tuple: FourTuple) -> Option<SlotKey> {
        Endpoint::open(self, tuple)
    }

    fn initial_seq_num(&mut self, id: FourTuple, time: Instant) -> SeqNumber {
        Endpoint::initial_seq_num(self, id, time)
    }
}

impl PortMap for Map<'_, FourTuple, Key> {
    fn remap(&mut self, old: FourTuple, new: FourTuple) {
        let old = self.entry(old)
            .occupied()
            // FIXME: unwrap justified? Seems like it may not.
            .unwrap();
        let value = *old.get();
        old.remove();

        self.entry(new)
            .vacant()
            // FIXME: nearly justified but how to ensure it was not mapped?
            .unwrap()
            .insert(value);
    }
}

impl<H, P> ip::Recv<P> for Receiver<'_, '_, H>
where
    P: PayloadMut,
    H: super::Recv<P>,
{
    fn receive(&mut self, ip_packet: ip::InPacket<P>) {
        let ip::InPacket { mut control, packet } = ip_packet;

        let repr = packet.repr();
        let capabilities = control.info().capabilities();
        let checksum = capabilities.tcp().rx_checksum(repr);

        let packet = match TcpPacket::new_checked(packet, checksum) {
            Ok(packet) => packet,
            // TODO: error logging.
            Err(_) => return (),
        };

        let arrived = match In::from_arriving(self.endpoint.inner, control.borrow_mut(), packet) {
            Ok(arrived) => arrived,

            // TODO: error logging.
            Err(_) => return (),
        };

        self.handler.receive(arrived)
    }
}

impl<H, P> ip::Send<P> for Sender<'_, '_, H>
where
    P: PayloadMut,
    H: super::Send<P>,
{
    fn send(&mut self, ip_raw: ip::RawPacket<P>) {
        let ip::RawPacket { mut control, payload } = ip_raw;

        let raw = Raw {
            ip: ip::RawPacket { control: control.borrow_mut(), payload },
            endpoint: self.endpoint.inner,
        };

        self.handler.send(raw)
    }
}