systemd-resolved-rs 0.1.1

A compatibility-oriented reimplementation of systemd-resolved
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
// SPDX-License-Identifier: LGPL-2.1-or-later
use crate::cache::{Cache, CacheKey};
use crate::config::{Config, DnsServerSpec, Domain, SupportMode, TlsMode, ValidationMode};
use crate::edns::{self, FeatureLevel, ServerFeatureState};
use crate::hosts::Hosts;
use crate::native;
use crate::networkd::LinkState as NetworkdLinkState;
use crate::policy::{choose_server, update_rtt, ServerMetric};
use crate::routing::{LinkError, LinkState, RouteScope, RoutingTable, ScopeKind};
use crate::tls::TlsStream;
use crate::transport::{ServerTransportState, TransportMode, TRANSPORT_RETRY_ATTEMPTS};
use crate::wire::{
    self, extract_address_records, extract_answer_records, extract_ptr_names, first_question,
    local_response, make_query, make_query_with_class, response_matches, reverse_name,
    servfail_for, validate, Header, WireError, TYPE_A, TYPE_AAAA, TYPE_AXFR, TYPE_IXFR, TYPE_PTR,
};
use std::collections::{HashMap, HashSet, VecDeque};
use std::error::Error;
use std::fmt;
use std::io::{self, Read, Write};
use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket};
#[cfg(test)]
use std::net::{Ipv4Addr, Ipv6Addr};
use std::os::fd::AsRawFd;
use std::sync::atomic::{AtomicU16, AtomicU64, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Condvar, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::thread;
use std::time::{Duration, Instant};

fn dns_name_dont_resolve(name: &str) -> bool {
    let name = name.trim_end_matches('.').to_ascii_lowercase();
    dns_name_has_suffix(&name, "0.in-addr.arpa")
        || name == "255.255.255.255.in-addr.arpa"
        || name
            == "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa"
        || dns_name_has_suffix(&name, "invalid")
        || dns_name_has_suffix(&name, "alt")
}

fn dns_name_has_suffix(name: &str, suffix: &str) -> bool {
    name == suffix
        || name
            .strip_suffix(suffix)
            .is_some_and(|prefix| prefix.ends_with('.'))
}

const UDP_POOL_PER_SERVER_MAX: usize = 8;
const TCP_POOL_PER_SERVER_MAX: usize = 4;
const TLS_POOL_PER_SERVER_MAX: usize = 4;
const DNS_TRANSACTION_ATTEMPTS_MAX: usize = 24;
const DNS_QUERY_TIMEOUT: Duration = Duration::from_secs(120);
const DNS_TRANSACTION_UDP_TIMEOUT: Duration = Duration::from_secs(5);
const DNS_TRANSACTION_TCP_TIMEOUT: Duration = Duration::from_secs(10);
const QUERY_CANCELLATION_POLL_INTERVAL: Duration = Duration::from_millis(100);

#[derive(Debug)]
struct DnsAttemptBudget {
    attempts: usize,
    deadline: Instant,
}

impl DnsAttemptBudget {
    fn new() -> Self {
        let now = Instant::now();
        Self {
            attempts: 0,
            deadline: now.checked_add(DNS_QUERY_TIMEOUT).unwrap_or(now),
        }
    }

    fn remaining(&self) -> Result<Duration, ResolveError> {
        self.deadline
            .checked_duration_since(Instant::now())
            .filter(|duration| !duration.is_zero())
            .ok_or_else(|| io::Error::new(io::ErrorKind::TimedOut, "DNS query timed out").into())
    }

    fn begin_attempt(&mut self) -> Result<Duration, ResolveError> {
        crate::query_cancel::check()?;
        if self.exhausted() {
            return Err(ResolveError::MaxAttemptsReached);
        }
        let remaining = self.remaining()?;
        self.attempts += 1;
        Ok(remaining)
    }

    #[cfg(test)]
    const fn attempts(&self) -> usize {
        self.attempts
    }

    const fn exhausted(&self) -> bool {
        self.attempts >= DNS_TRANSACTION_ATTEMPTS_MAX
    }

    fn expired(&self) -> bool {
        Instant::now() >= self.deadline
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum QueryMode {
    Full,
    Proxy,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct ServerKey {
    scope: ScopeKind,
    server: SocketAddr,
    slot: usize,
}

impl ServerKey {
    const fn new(scope: ScopeKind, server: SocketAddr) -> Self {
        Self::with_slot(scope, server, 0)
    }

    const fn with_slot(scope: ScopeKind, server: SocketAddr, slot: usize) -> Self {
        Self {
            scope,
            server,
            slot,
        }
    }

    const fn server(self) -> SocketAddr {
        self.server
    }

    const fn scope_kind(self) -> ScopeKind {
        self.scope
    }

    const fn slot(self) -> usize {
        self.slot
    }

    const fn ifindex(self) -> Option<i32> {
        match self.scope {
            ScopeKind::Link(ifindex) => Some(ifindex),
            ScopeKind::Global | ScopeKind::Delegate(_) | ScopeKind::Fallback => None,
        }
    }

    const fn delegate_index(self) -> Option<usize> {
        match self.scope {
            ScopeKind::Delegate(index) => Some(index),
            ScopeKind::Global | ScopeKind::Link(_) | ScopeKind::Fallback => None,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct TlsPoolKey {
    server: ServerKey,
    strict: bool,
}

impl TlsPoolKey {
    const fn new(server: ServerKey, strict: bool) -> Self {
        Self { server, strict }
    }
}

#[derive(Debug, Default)]
struct ServerState {
    metric: ServerMetric,
    cooldown_until: Option<Instant>,
    features: ServerFeatureState,
    transport: ServerTransportState,
    missing_root_rrsig: bool,
    packet_do_off: bool,
    packet_invalid: bool,
}

#[derive(Debug, Default)]
struct Counters {
    current_transactions: AtomicU64,
    transactions: AtomicU64,
    timeouts: AtomicU64,
    timeouts_served_stale: AtomicU64,
    failures_served_stale: AtomicU64,
    cache_hits: AtomicU64,
    cache_misses: AtomicU64,
    failures: AtomicU64,
    local_answers: AtomicU64,
    dnssec_secure: AtomicU64,
    dnssec_insecure: AtomicU64,
    dnssec_bogus: AtomicU64,
    dnssec_indeterminate: AtomicU64,
}

struct ActiveTransaction<'a> {
    counter: &'a AtomicU64,
}

impl Drop for ActiveTransaction<'_> {
    fn drop(&mut self) {
        self.counter.fetch_sub(1, Ordering::Relaxed);
    }
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct InflightKey {
    route: u64,
    query: Vec<u8>,
}

impl InflightKey {
    fn new(route: u64, query: &[u8]) -> Result<Self, WireError> {
        let mut query = query.to_vec();
        wire::rewrite_id(&mut query, 0)?;
        Ok(Self { route, query })
    }
}

#[derive(Debug, Default)]
struct InflightState {
    running: bool,
    response: Option<SharedResponse>,
}

#[derive(Clone, Debug)]
struct SharedResponse {
    packet: Vec<u8>,
    ifindex: Option<i32>,
}

#[derive(Debug, Default)]
struct InflightEntry {
    state: Mutex<InflightState>,
    ready: Condvar,
}

impl InflightEntry {
    fn wait(&self) -> Result<Option<SharedResponse>, ResolveError> {
        let mut state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        while state.running {
            crate::query_cancel::check()?;
            let (next_state, _) = self
                .ready
                .wait_timeout(state, QUERY_CANCELLATION_POLL_INTERVAL)
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            state = next_state;
        }
        Ok(state.response.clone())
    }
}

#[derive(Debug, Default)]
struct Inflight {
    entries: Mutex<HashMap<InflightKey, Arc<InflightEntry>>>,
}

impl Inflight {
    fn begin(&self, key: InflightKey) -> InflightRole<'_> {
        let mut entries = self
            .entries
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if let Some(entry) = entries.get(&key) {
            return InflightRole::Follower(Arc::clone(entry));
        }

        let entry = Arc::new(InflightEntry {
            state: Mutex::new(InflightState {
                running: true,
                response: None,
            }),
            ready: Condvar::new(),
        });
        entries.insert(key.clone(), Arc::clone(&entry));
        InflightRole::Leader(InflightLeader {
            owner: self,
            key,
            entry,
            completed: false,
        })
    }

    fn finish(
        &self,
        key: &InflightKey,
        entry: &Arc<InflightEntry>,
        response: Option<SharedResponse>,
    ) {
        {
            let mut state = entry
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            state.response = response;
            state.running = false;
            entry.ready.notify_all();
        }

        let mut entries = self
            .entries
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if entries
            .get(key)
            .is_some_and(|current| Arc::ptr_eq(current, entry))
        {
            entries.remove(key);
        }
    }
}

#[derive(Debug)]
enum InflightRole<'a> {
    Leader(InflightLeader<'a>),
    Follower(Arc<InflightEntry>),
}

#[derive(Debug)]
struct InflightLeader<'a> {
    owner: &'a Inflight,
    key: InflightKey,
    entry: Arc<InflightEntry>,
    completed: bool,
}

impl InflightLeader<'_> {
    fn complete(mut self, response: Option<SharedResponse>) {
        self.owner.finish(&self.key, &self.entry, response);
        self.completed = true;
    }
}

impl Drop for InflightLeader<'_> {
    fn drop(&mut self) {
        if !self.completed {
            self.owner.finish(&self.key, &self.entry, None);
        }
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ResolverStats {
    pub current_transactions: u64,
    pub transactions: u64,
    pub timeouts: u64,
    pub timeouts_served_stale: u64,
    pub failures_served_stale: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub failures: u64,
    pub local_answers: u64,
    pub dnssec_secure: u64,
    pub dnssec_insecure: u64,
    pub dnssec_bogus: u64,
    pub dnssec_indeterminate: u64,
    pub cache_entries: usize,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolverServerState {
    pub server: String,
    pub server_type: String,
    pub interface: Option<String>,
    pub interface_index: Option<i32>,
    pub verified_feature_level: String,
    pub possible_feature_level: String,
    pub dnssec_mode: String,
    pub dnssec_supported: bool,
    pub received_udp_fragment_max: u32,
    pub failed_udp_attempts: u8,
    pub failed_tcp_attempts: u8,
    pub packet_truncated: bool,
    pub packet_bad_opt: bool,
    pub packet_rrsig_missing: bool,
    pub packet_invalid: bool,
    pub packet_do_off: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolverResourceKey {
    pub class: u16,
    pub rr_type: u16,
    pub name: String,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolverQueryAnswer {
    pub raw: Vec<u8>,
    pub ifindex: Option<i32>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ResolverQueryEvent {
    pub sequence: u64,
    pub state: String,
    pub result: Option<String>,
    pub rcode: Option<u16>,
    pub errno: Option<i32>,
    pub extended_dns_error_code: Option<u16>,
    pub extended_dns_error_message: Option<String>,
    pub question: Vec<ResolverResourceKey>,
    pub collected_questions: Vec<ResolverResourceKey>,
    pub answer: Vec<ResolverQueryAnswer>,
}

#[derive(Debug, Default)]
struct QueryMonitor {
    sequence: AtomicU64,
    events: Mutex<VecDeque<ResolverQueryEvent>>,
    changed: Condvar,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
struct DnskeyCacheKey {
    server: ServerKey,
    zone: String,
}

#[derive(Clone, Debug)]
struct DnskeyCacheEntry {
    keys: Vec<wire::ResourceRecord>,
    expires: Instant,
}

#[derive(Debug)]
pub struct Resolver {
    config: RwLock<Config>,
    states: Mutex<HashMap<ServerKey, ServerState>>,
    udp_sockets: Mutex<HashMap<ServerKey, Vec<UdpSocket>>>,
    tcp_streams: Mutex<HashMap<ServerKey, Vec<TcpStream>>>,
    tls_streams: Mutex<HashMap<TlsPoolKey, Vec<TlsStream>>>,
    routing: RwLock<RoutingTable>,
    networkd_links: RwLock<HashMap<i32, NetworkdLinkState>>,
    link_server_specs: RwLock<HashMap<i32, Vec<DnsServerSpec>>>,
    link_dns_over_tls_overrides: RwLock<HashMap<i32, TlsMode>>,
    link_dnssec_overrides: RwLock<HashMap<i32, ValidationMode>>,
    routing_generation: AtomicU64,
    inflight: Inflight,
    cache: Cache,
    hosts: RwLock<Hosts>,
    next_id: AtomicU16,
    counters: Counters,
    query_monitor: QueryMonitor,
    dnskey_cache: Mutex<HashMap<DnskeyCacheKey, DnskeyCacheEntry>>,
    llmnr_client: RwLock<Option<crate::llmnr::LlmnrClient>>,
    llmnr_mode: RwLock<SupportMode>,
    multicast_dns_mode: RwLock<SupportMode>,
}