ts_runtime 0.5.0

tailscale runtime
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
//! Direct UDP transport actor.
//!
//! This is an involved wrapper around a [`UdpSocket`] to make it function as an underlay transport.
//! Technically, it's actually two sockets, an IPv4 and an IPv6 (if available). Both are bound
//! to the unspecified address for their network type (`0.0.0.0` or `::`), and the socket to use to
//! send a packet is determined by its address type.
//!
//! The primary function of this actor is just to bridge UDP tx/rx to its dataplane queues. But
//! also, because it's the actor that holds the UDP socket(s) (and hence knows the ports each is
//! bound on), it's additionally responsible for reporting this node's UDP endpoints on the bus (the
//! control actor reports these to control, and the path discovery actor uses this to set its
//! `CallMeMaybe` endpoints). It just aggregates the netmon and stun discovered addresses to
//! (combining the netmon addresses with the local port) to make this report.
//!
//! No disco concerns are directly handled by this actor; these are done in [`disco`][crate::disco]
//! (incoming `CallMeMaybe` and `Ping`s from peers) and [`path_discoverer`][crate::path_discoverer]
//! (PD for outgoing traffic). Other parts of the system can trigger underlay packets to be sent
//! by this actor directly via [`dataplane::SendUnderlay`][crate::dataplane::SendUnderlay] (only to
//! be used when absolutely necessary, i.e. disco).

use std::{
    borrow::Borrow,
    collections::HashMap,
    io,
    io::ErrorKind,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
    sync::Arc,
};

use bytes::BytesMut;
use futures::Stream;
use futures_util::StreamExt;
use itertools::Itertools;
use kameo::{
    actor::ActorRef,
    message::{Context, Message, StreamMessage},
};
use tokio::net::UdpSocket;
use tokio_stream::wrappers::UnboundedReceiverStream;
use ts_bart::RoutingTable;
use ts_control::{Endpoint, EndpointType};
use ts_dataplane::async_tokio::{FromUnderlay, ToUnderlay, Tx};
use ts_packet::PacketMut;
use ts_transport::{DynEndpoint, UnderlayTransportId};

use crate::{
    dataplane::DataplaneActor,
    disco::SendDisco,
    env::Env,
    netmon,
    peer_tracker::{PeerDb, PeerState},
    stunner::StunAddress,
};

pub struct DirectActor {
    transport_id: UnderlayTransportId,

    sock4: Arc<UdpSocket>,
    sock6: Option<Arc<UdpSocket>>,

    tx: Tx<FromUnderlay>,

    peers: Arc<PeerDb>,

    stun_addr: Option<SocketAddr>,
    addrs_v4: Vec<Endpoint>,
    addrs_v6: Vec<Endpoint>,

    reachable: ts_bart::Table<()>,

    env: Env,
}

#[derive(Clone, Debug)]
pub struct NewEndpoints(pub Arc<[Endpoint]>);

struct UdpRx(io::Result<HashMap<SocketAddr, Vec<PacketMut>>>);
struct UdpTx(ToUnderlay);

impl DirectActor {
    async fn publish_endpoints(&self) -> Result<(), crate::Error> {
        self.env
            .publish(NewEndpoints(
                self.addrs_v4
                    .iter()
                    .copied()
                    .chain(self.addrs_v6.iter().copied())
                    .chain(self.stun_addr.into_iter().map(|addr| Endpoint {
                        endpoint: addr,
                        ty: EndpointType::Stun,
                    }))
                    .collect(),
            ))
            .await
    }

    fn sock(&self, ipv4: bool) -> Option<&UdpSocket> {
        if ipv4 {
            Some(self.sock4.as_ref())
        } else {
            self.sock6.as_deref()
        }
    }
}

impl kameo::Actor for DirectActor {
    type Args = Env;
    type Error = crate::Error;

    async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
        let (id, rx, tx) = env
            .ask::<DataplaneActor, _>(None, crate::dataplane::NewUnderlayTransport, true)
            .await?;

        let sock4 = UdpSocket::bind("0.0.0.0:0").await.unwrap();
        let sock4 = Arc::new(sock4);
        tracing::debug!(transport_id = ?id, local_addr = %sock4.local_addr().unwrap(), "direct udp4 socket bound");

        slf.attach_stream(udp_rx(sock4.clone()).boxed(), (), ());

        let sock6 = UdpSocket::bind("[::]:0").await.ok().map(Arc::new);
        if let Some(sock6) = sock6.as_ref() {
            tracing::debug!(transport_id = ?id, local_addr = %sock6.local_addr().unwrap(), "direct udp6 socket bound");
            slf.attach_stream(udp_rx(sock6.clone()).boxed(), (), ());
        } else {
            tracing::debug!("could not bind ipv6 direct");
        }

        slf.attach_stream(UnboundedReceiverStream::new(rx).map(UdpTx), (), ());

        env.subscribe::<Arc<PeerState>>(&slf).await?;
        env.subscribe::<StunAddress>(&slf).await?;
        env.subscribe::<Arc<netmon::State>>(&slf).await?;
        env.subscribe::<SendDisco>(&slf).await?;

        env.register(None, &slf).await?;

        Ok(Self {
            transport_id: id,
            sock4,
            sock6,
            env,
            peers: Default::default(),
            stun_addr: None,
            addrs_v4: vec![],
            addrs_v6: vec![],
            reachable: Default::default(),
            tx,
        })
    }
}

fn udp_rx(sock: impl Borrow<UdpSocket>) -> impl Stream<Item = UdpRx> {
    // Required capacity to receive a UDP datagram. Any smaller and data may technically be dropped
    // with a large MTU or a large fragmented packet. We keep the buffer capacity topped off to this
    // size each time we try a receive in the loop below.
    const REQUIRED_CAPACITY: usize = u16::MAX as _;

    let buf = BytesMut::zeroed(REQUIRED_CAPACITY);

    futures_util::stream::try_unfold((sock, buf), async |(sock, mut buf)| {
        let mut v = vec![];

        // Batch receive: wait until any packets are available from the socket, then try
        // to read as many as possible until we exhaust the underlying buffer and see a wouldblock.
        {
            let sock = sock.borrow();
            sock.readable().await?;

            loop {
                if buf.len() < REQUIRED_CAPACITY {
                    buf.resize(REQUIRED_CAPACITY, 0);
                }

                let (n, who) = match sock.try_recv_from(&mut buf) {
                    Ok((n, who)) => (n, who),
                    Err(e) if e.kind() == ErrorKind::WouldBlock => {
                        break;
                    }
                    Err(e) => return Err(e),
                };

                let pkt_buf = buf.split_to(n);
                v.push((who, PacketMut::from(pkt_buf)));
            }
        };

        tracing::trace!(n_pkts = v.len(), "udp rx batch");

        Ok(Some((v.into_iter().into_group_map(), (sock, buf))))
    })
    .map(UdpRx)
}

impl Message<Arc<PeerState>> for DirectActor {
    type Reply = ();

    async fn handle(&mut self, msg: Arc<PeerState>, _ctx: &mut Context<Self, Self::Reply>) {
        self.peers = msg.peers.clone();
    }
}

impl Message<SendDisco> for DirectActor {
    type Reply = ();

    async fn handle(
        &mut self,
        SendDisco { ep, buf }: SendDisco,
        _ctx: &mut Context<Self, Self::Reply>,
    ) {
        let Some(ep) = ep.as_udp() else {
            return;
        };

        let Some(sock) = self.sock(ep.is_ipv4()) else {
            tracing::trace!(?ep, "can't send disco, no ipv6");
            return;
        };

        if let Err(e) = sock.send_to(&buf, ep).await {
            tracing::warn!(?ep, error = %e, ?sock, "sending disco msg");
        } else {
            tracing::trace!(?ep, "sent disco msg");
        }
    }
}

const CGNAT_RANGE: ipnet::Ipv4Net = ipnet::Ipv4Net::new_assert(Ipv4Addr::new(100, 64, 0, 0), 10);
const TS_IP6_ULA: ipnet::Ipv6Net =
    ipnet::Ipv6Net::new_assert(Ipv6Addr::new(0xfd7a, 0x115c, 0xa1e0, 0, 0, 0, 0, 0), 48);

fn is_tailscale(ip: &IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => CGNAT_RANGE.contains(v4),
        IpAddr::V6(v6) => TS_IP6_ULA.contains(v6),
    }
}

impl Message<Arc<netmon::State>> for DirectActor {
    type Reply = ();

    async fn handle(&mut self, msg: Arc<netmon::State>, _ctx: &mut Context<Self, Self::Reply>) {
        self.addrs_v4.clear();
        self.addrs_v6.clear();
        self.reachable.clear();

        for (_id, addr) in msg.up_addrs() {
            let ip = addr.addr();

            let invalid = match ip {
                IpAddr::V4(v4) => {
                    v4.is_broadcast()
                        || v4.is_loopback()
                        || v4.is_unspecified()
                        || v4.is_documentation()
                        || v4.is_multicast()
                }
                IpAddr::V6(v6) => v6.is_multicast() || v6.is_unspecified() || v6.is_loopback(),
            };

            if invalid {
                continue;
            }

            // NOTE(npry): this might be overly defensive -- while it probably makes sense to avoid
            // nesting our traffic through a VPN tun device if possible, this does cut off a
            // potential connectivity path, and we don't actually know for sure that it's tailscale.
            // Notionally, it's better to connect through another tailnet (via tailscaled) or a
            // 3rd-party VPN than not at all, but it may also make debugging horrible and this is
            // likely an edge case, so skip for the time being.
            if is_tailscale(&ip) {
                continue;
            }

            let Some(port) = self
                .sock(ip.is_ipv4())
                .map(|x| x.local_addr().unwrap().port())
            else {
                continue;
            };

            let sockaddr = SocketAddr::new(ip, port);

            let ty = match ip {
                IpAddr::V4(x) => {
                    if x.is_link_local() || x.is_private() {
                        EndpointType::Local
                    } else {
                        EndpointType::Unknown
                    }
                }
                IpAddr::V6(x) => {
                    if x.is_unique_local() || x.is_unicast_link_local() {
                        EndpointType::Local
                    } else {
                        EndpointType::Unknown
                    }
                }
            };

            let ep = Endpoint {
                ty,
                endpoint: sockaddr,
            };

            if ep.endpoint.is_ipv4() {
                self.addrs_v4.push(ep);
            } else if self.sock6.is_some() {
                self.addrs_v6.push(ep);
            }

            self.reachable.insert(addr, ());
        }

        self.publish_endpoints().await.unwrap()
    }
}

impl Message<StunAddress> for DirectActor {
    type Reply = ();

    async fn handle(&mut self, _msg: StunAddress, _ctx: &mut Context<Self, Self::Reply>) {
        // TODO(npry): currently we're STUNning with the wrong socket (the global one in the
        //  stunner), so the local addr + NAT mapping is going to be wrong. don't add the STUNned
        //  address to our endpoints until it can actually reach this socket.

        // self.stun_addr = Some(msg.addr);
        // self.publish_endpoints().await.unwrap()
    }
}

impl Message<StreamMessage<UdpTx, (), ()>> for DirectActor {
    type Reply = ();

    async fn handle(
        &mut self,
        msg: StreamMessage<UdpTx, (), ()>,
        ctx: &mut Context<Self, Self::Reply>,
    ) {
        let (ep_info, pkts) = match msg {
            StreamMessage::Next(msg) => msg.0,
            StreamMessage::Finished(_) => {
                tracing::warn!("udp tx stream shut down, closing");
                ctx.stop();
                return;
            }
            _ => return,
        };

        if pkts.is_empty() {
            return;
        }

        let Some(addr) = ep_info.as_udp() else {
            tracing::warn!(?ep_info, "invalid endpoint info for udp direct");
            return;
        };

        let Some(sock) = self.sock(addr.is_ipv4()) else {
            tracing::debug!("trying to send ipv6 traffic without ipv6 connectivity");
            return;
        };

        tracing::trace!(?ep_info, selected_addr = %addr, n_pkts = pkts.len(), "udp tx batch");

        for pkt in pkts {
            if let Err(e) = sock.send_to(&pkt, addr).await {
                tracing::error!(error = %e, "sending packet");
            }
        }
    }
}

impl Message<StreamMessage<UdpRx, (), ()>> for DirectActor {
    type Reply = ();

    async fn handle(
        &mut self,
        msg: StreamMessage<UdpRx, (), ()>,
        _ctx: &mut Context<Self, Self::Reply>,
    ) {
        let msg = match msg {
            StreamMessage::Next(msg) => msg.0,
            StreamMessage::Finished(_) => {
                tracing::warn!("udp rx stream shut down, closing");
                return;
            }
            _ => return,
        };

        const ACCEPTABLE_ERR: &[io::ErrorKind] = {
            use io::ErrorKind::*;
            &[NetworkDown, NetworkUnreachable, HostUnreachable, TimedOut]
        };

        match msg {
            Ok(mp) => {
                for (ep, pkts) in mp {
                    tracing::trace!(who = %ep, n_pkts = pkts.len(), "udp rx batch (by sender)");

                    self.tx
                        .send((self.transport_id, DynEndpoint::udp(ep), pkts))
                        .unwrap();
                }
            }
            Err(e) if ACCEPTABLE_ERR.contains(&e.kind()) => {
                tracing::error!(error = %e, "udp receive error");
            }
            Err(e) => {
                tracing::error!(error = %e, "unrecoverable error, die");
                panic!()
            }
        }
    }
}