ipv6-router-advertisement 0.2.0

Low-level, policy-free processing of IPv6 Router Advertisements
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Detector for Router Advertisement (RA) information.
//!
//! This module translates a parsed IPv6 Router Advertisement into
//! semantically meaningful events.
//!
//! The detector is deliberately stateless and performs no aggregation,
//! expiration, or policy decisions.
//!
//! Responsibilities:
//! - Decode RA content into domain-specific events.
//! - Preserve per-router separation of information.
//! - Attach lifetimes as specified by the RA.
//!
//! Higher layers (lifecycle, aggregation, FSM) are responsible for
//! interpreting and combining these events.

use super::router_advertisement::RouterAdvertisement;
use super::Lifetime;
use super::{InterfaceIndex, PrefixInfo, RouterIp};
use core::net::Ipv6Addr;
use core::time::Duration;

/// Unique identity of a `(interface, router)` combination.
///
/// A `Key` identifies the *source* of router advertisement information.
/// It is stable across updates and expiries and used throughout detector,
/// lifecycle, and aggregation layers to associate data with its origin.
///
/// Semantics:
/// - `ifindex` identifies the network interface.
/// - `router_ip` identifies the advertising router.
///
/// The key does **not** encode the category of information (router, DNS,
/// prefix, etc.). That distinction is expressed by the event type itself.
///
/// Keys are intentionally lightweight and copyable.
#[derive(Hash, Eq, PartialEq, Clone, Copy, Debug)]
pub struct Key {
    /// Network interface on which the router advertisement was received.
    pub ifindex: InterfaceIndex,
    /// IPv6 address of the advertising router.
    ///
    /// This uniquely identifies the router on the link. It is kept private
    /// to prevent accidental use as an externally visible identifier.
    router_ip: RouterIp,
}

impl Key {
    /// Creates a new router identity key.
    pub fn new(ifindex: InterfaceIndex, router_ip: RouterIp) -> Self {
        Self { ifindex, router_ip }
    }
}

/// DNS server information advertised by a router.
///
/// This structure corresponds to a single RDNSS option as defined in
/// RFC 8106.
///
/// All DNS servers contained in this structure share the same lifetime.
/// If a router wishes to advertise servers with different lifetimes,
/// multiple `DnsServers` entries will be emitted.
#[derive(Debug, Clone)]
pub struct DnsServers {
    /// List of DNS servers provided by this router.
    pub servers: Vec<Ipv6Addr>,
    /// Lifetime of the DNS server information.
    pub lifetime: Duration,
}

impl Lifetime for DnsServers {
    /// Returns the lifetime of this DNS server group.
    ///
    /// All DNS servers contained in this structure share the same lifetime,
    /// as defined by the RDNSS option semantics.
    ///
    /// A lifetime of zero indicates an explicit withdrawal of previously
    /// advertised DNS server information.
    fn lifetime(&self) -> Duration {
        self.lifetime
    }
}

/// DNS search domain information advertised by a router.
///
/// This structure corresponds to a single DNSSL option as defined in
/// RFC 8106.
///
/// All domain names contained in this structure share the same lifetime.
/// Multiple `DnsDomains` entries may be present if different lifetimes
/// are required.
#[derive(Debug, Clone)]
pub struct DnsDomains {
    /// List of DNS servers provided by this router.
    pub domains: Vec<String>,
    /// Lifetime of the DNS domain information.
    pub lifetime: Duration,
}

impl Lifetime for DnsDomains {
    /// Returns the lifetime of this DNS server group.
    ///
    /// All DNS servers contained in this structure share the same lifetime,
    /// as defined by the RDNSS option semantics.
    ///
    /// A lifetime of zero indicates an explicit withdrawal of previously
    /// advertised DNS server information.
    fn lifetime(&self) -> Duration {
        self.lifetime
    }
}

/// Events produced by the Router Advertisement detector.
///
/// Each detector event represents information *observed* in a single
/// Router Advertisement (RA). Detector events describe what the router
/// announced, not whether the information is still valid or expired.
///
/// All events are scoped to a single router and interface, identified
/// by the associated `Key`.
///
/// Detector events are converted into lifecycle events, which then add
/// time semantics and expiration handling.
#[derive(Debug, Clone)]
pub enum Event {
    /// Router configuration update.
    ///
    /// Represents the Managed (M) and Other Configuration (O) flags
    /// as advertised by the router, together with the router lifetime.
    Router {
        /// Identity of the advertising router.
        key: Key,
        /// Indicates whether the Managed (M) flag is set.
        managed: bool,
        /// Indicates whether the Other Configuration (O) flag is set.
        other_config: bool,
        /// Lifetime of the router configuration.
        lifetime: Duration,
    },
    /// DNS server information contributed by a router.
    ///
    /// The vector contains all RDNSS options present in the RA.
    /// An empty vector explicitly withdraws previously advertised DNS
    /// server information for this router.
    DnsServers(Key, Vec<DnsServers>),
    /// DNS search domain information contributed by a router.
    ///
    /// The vector contains all DNSSL options present in the RA.
    /// An empty vector explicitly withdraws previously advertised DNS
    /// search domains for this router.
    DnsDomains(Key, Vec<DnsDomains>),
    /// Prefix information contributed by a router.
    ///
    /// Prefixes are lease-based and handled individually in later
    /// lifecycle processing.
    Prefix {
        /// Identity of the advertising router.
        key: Key,
        /// Prefix information (address and prefix length).
        prefix_info: PrefixInfo,
        /// Valid lifetime of the prefix.
        lifetime: Duration,
        /// Preferred lifetime of the prefix.
        preferred_lifetime: Duration,
    },
}

/// Helper for detecting multi-option RA fields with lifetime semantics.
///
/// This function is used to process RA options that:
/// - may appear multiple times in a single RA,
/// - carry a lifetime,
/// - and represent *state*, not incremental updates.
///
/// Semantics:
/// - If **any option** has a zero lifetime, all previous information of
///   this category is explicitly withdrawn for the router, and an update
///   with an empty vector is emitted.
/// - Otherwise, all options are collected and emitted together.
/// - If no options are present, no event is emitted.
///
/// This matches the replace semantics required for DNS options
/// (RDNSS/DNSSL).
fn detect_multi_option<T, O, G>(
    out: &mut Vec<Event>,
    key: Key,
    options: impl IntoIterator<Item = O>,
    map: G,
    emit_update: impl FnOnce(Key, Vec<T>) -> Event,
) where
    G: Fn(O) -> T,
    O: Lifetime,
{
    let mut items = Vec::new();
    for opt in options {
        if opt.lifetime().is_zero() {
            out.push(emit_update(key, vec![]));
            return;
        }
        items.push(map(opt));
    }
    if !items.is_empty() {
        out.push(emit_update(key, items));
    }
}

/// Detects router-related events from a Router Advertisement.
///
/// This function translates a single RA into zero or more detector events:
///
/// - A router update event is always emitted.
/// - DNS server and domain events are emitted only if present in the RA.
///
/// Semantic behavior:
/// - If a router has lifetime == 0, then all dns servers, domains and prefixes must
///   also be removed.
/// - DNS servers, TNS domains and prefixes are grouped per RA.
/// - If a lifetime == 0 DNS servers or domains comes in, all servers or domains
///   will be removed by sending the Remove event.
/// - Prefixes are a little different.
///
/// The detector performs no aggregation and preserves per-router identity.
pub fn detect(ra: RouterAdvertisement) -> Vec<Event> {
    // We know we have 1-3 events only. Lets avoid unnecessary allocations
    let mut v = Vec::with_capacity(3);
    let key = Key::new(ra.ifindex, ra.router_ip);
    v.push(Event::Router {
        key,
        managed: ra.managed,
        other_config: ra.other_config,
        lifetime: Duration::from_secs(ra.router_lifetime as u64),
    });
    if ra.router_lifetime == 0 {
        return v;
    }
    // Semantic notes:
    // DNS servers and domains are separate updateable.
    // Practical this will happen very rarely, but spec allows it.
    //
    // - lifetime == 0 => remove dns servers / domains.
    // - empty vec => not nice but ok. just
    // - entrys in there => replace the dns for this router

    detect_multi_option(
        &mut v,
        key,
        ra.rdnss,
        |o| DnsServers {
            servers: o.servers,
            lifetime: Duration::from_secs(o.lifetime as u64),
        },
        Event::DnsServers,
    );

    detect_multi_option(
        &mut v,
        key,
        ra.dnssl,
        |o| DnsDomains {
            domains: o.domains,
            lifetime: Duration::from_secs(o.lifetime as u64),
        },
        Event::DnsDomains,
    );

    for prefix in ra.prefixes {
        v.push(Event::Prefix {
            key,
            prefix_info: PrefixInfo {
                prefix: prefix.prefix,
                prefix_len: prefix.prefix_len,
            },
            lifetime: Duration::from_secs(prefix.valid_lifetime as u64),
            preferred_lifetime: Duration::from_secs(prefix.preferred_lifetime as u64),
        });
    }

    v
}

#[cfg(test)]
mod tests {
    use super::super::router_advertisement;
    use super::*;
    use anyhow::{Context, Result};
    use core::net::Ipv6Addr;
    use core::time::Duration;
    fn ip(a: u16) -> Ipv6Addr {
        Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, a)
    }

    fn base_ra() -> RouterAdvertisement {
        RouterAdvertisement {
            ifindex: 1,
            router_ip: ip(1),
            managed: true,
            other_config: false,
            router_lifetime: 30,
            rdnss: vec![],
            dnssl: vec![],
            prefixes: vec![],
        }
    }

    #[test]
    fn detect_emits_router_update() -> Result<()> {
        let ra = base_ra();
        let events = detect(ra);

        anyhow::ensure!(events.len() == 1);

        match &events[0] {
            Event::Router {
                managed,
                other_config,
                lifetime,
                ..
            } => {
                anyhow::ensure!(*managed);
                anyhow::ensure!(!*other_config);
                anyhow::ensure!(*lifetime == Duration::from_secs(30));
            }
            ev => anyhow::bail!("unexpected event: {:?}", ev),
        }

        Ok(())
    }

    #[test]
    fn detect_emits_dns_servers_event() -> Result<()> {
        let mut ra = base_ra();
        ra.rdnss = vec![router_advertisement::Rdnss {
            servers: vec![ip(2), ip(3)],
            lifetime: 60,
        }];

        let events = detect(ra);

        let dns = events
            .iter()
            .find_map(|e| match e {
                Event::DnsServers(_key, servers) => Some(servers),
                _ => None,
            })
            .context("missing RouterDnsServers event")?;

        anyhow::ensure!(dns.len() == 1);
        anyhow::ensure!(dns[0].servers.len() == 2);
        anyhow::ensure!(dns[0].lifetime == Duration::from_secs(60));
        Ok(())
    }

    #[test]
    fn detect_emits_dns_domains_event() -> Result<()> {
        let mut ra = base_ra();
        ra.dnssl = vec![
            router_advertisement::Dnssl {
                domains: vec!["example.com".into()],
                lifetime: 120,
            },
            router_advertisement::Dnssl {
                domains: vec!["example1.com".into(), "example2.com".into()],
                lifetime: 100,
            },
        ];

        let events = detect(ra);

        let domains = events
            .iter()
            .find_map(|e| match e {
                Event::DnsDomains(_key, domains) => Some(domains),
                _ => None,
            })
            .context("missing RouterDnsDomains event")?;

        anyhow::ensure!(domains.len() == 2);
        anyhow::ensure!(domains[0].domains.len() == 1);
        anyhow::ensure!(domains[0].lifetime == Duration::from_secs(120));
        anyhow::ensure!(domains[0].domains == vec!["example.com".to_string()]);
        anyhow::ensure!(domains[1].domains.len() == 2);
        anyhow::ensure!(domains[1].lifetime == Duration::from_secs(100));
        anyhow::ensure!(
            domains[1].domains == vec!["example1.com".to_string(), "example2.com".to_string()]
        );
        Ok(())
    }

    #[test]
    fn detect_allows_empty_dns_lists() -> Result<()> {
        let mut ra = base_ra();
        ra.rdnss = vec![router_advertisement::Rdnss {
            servers: Vec::new(),
            lifetime: 10,
        }];

        let events = detect(ra);

        let dns = events
            .iter()
            .find_map(|e| match e {
                Event::DnsServers(_key, servers) => Some(servers),
                _ => None,
            })
            .context("missing RouterDnsServers event")?;

        anyhow::ensure!(dns.len() == 1);
        anyhow::ensure!(dns[0].servers.is_empty());
        Ok(())
    }

    #[test]
    fn detect_emits_single_prefix() -> Result<()> {
        let mut ra = base_ra();
        ra.prefixes = vec![router_advertisement::PrefixInfo {
            prefix: ip(10),
            prefix_len: 64,
            valid_lifetime: 100,
            preferred_lifetime: 50,
            autonomous: false,
            on_link: false,
        }];

        let events = detect(ra);

        let prefix = events
            .iter()
            .find_map(|e| match e {
                Event::Prefix {
                    prefix_info,
                    lifetime,
                    preferred_lifetime,
                    ..
                } => Some((prefix_info, lifetime, preferred_lifetime)),
                _ => None,
            })
            .context("missing Prefix event")?;

        anyhow::ensure!(prefix.0.prefix == ip(10));
        anyhow::ensure!(*prefix.1 == Duration::from_secs(100));
        anyhow::ensure!(*prefix.2 == Duration::from_secs(50));
        Ok(())
    }

    #[test]
    fn detect_emits_prefix_with_zero_lifetime() -> Result<()> {
        let mut ra = base_ra();
        ra.prefixes = vec![router_advertisement::PrefixInfo {
            prefix: ip(30),
            prefix_len: 64,
            valid_lifetime: 0,
            preferred_lifetime: 0,
            autonomous: false,
            on_link: false,
        }];

        let events = detect(ra);

        let prefix = events
            .iter()
            .find(|e| matches!(e, Event::Prefix { .. }))
            .context("missing Prefix event")?;

        match prefix {
            Event::Prefix { lifetime, .. } => {
                anyhow::ensure!(lifetime.is_zero());
            }
            _ => unreachable!(),
        }

        Ok(())
    }

    #[test]
    fn detect_emits_router_dns_and_prefix() -> Result<()> {
        let mut ra = base_ra();
        ra.rdnss = vec![router_advertisement::Rdnss {
            servers: vec![ip(1)],
            lifetime: 100,
        }];
        ra.prefixes = vec![router_advertisement::PrefixInfo {
            prefix: ip(10),
            prefix_len: 64,
            valid_lifetime: 200,
            preferred_lifetime: 0,
            autonomous: false,
            on_link: false,
        }];

        let events = detect(ra);

        anyhow::ensure!(events.iter().any(|e| matches!(e, Event::Router { .. })));
        anyhow::ensure!(events.iter().any(|e| matches!(e, Event::DnsServers(_, _))));
        anyhow::ensure!(events.iter().any(|e| matches!(e, Event::Prefix { .. })));

        Ok(())
    }
}
#[cfg(test)]
mod detect_multi_option_tests {
    use super::*;
    use std::net::Ipv6Addr;
    use tokio::time::Duration;
    #[derive(Debug)]
    struct TestOpt {
        id: u8,
        lifetime: Duration,
    }

    impl Lifetime for TestOpt {
        fn lifetime(&self) -> Duration {
            self.lifetime
        }
    }

    #[test]
    fn detect_multi_option_collects_all_items() {
        let key = Key::new(1, Ipv6Addr::from(1));
        let mut out = Vec::new();

        let input = vec![
            TestOpt {
                id: 1,
                lifetime: Duration::from_secs(10),
            },
            TestOpt {
                id: 2,
                lifetime: Duration::from_secs(20),
            },
        ];

        detect_multi_option(
            &mut out,
            key,
            input,
            |o| o.id,
            |k, v| {
                Event::DnsServers(
                    k,
                    vec![DnsServers {
                        servers: vec![Ipv6Addr::from(v[0] as u128), Ipv6Addr::from(v[1] as u128)],
                        lifetime: Duration::from_secs(1),
                    }],
                )
            },
        );

        assert_eq!(out.len(), 1);
    }

    #[test]
    fn detect_multi_option_zero_lifetime_short_circuits() -> anyhow::Result<()> {
        let key = Key::new(1, Ipv6Addr::from(1));
        let mut out = Vec::new();

        let input = vec![
            TestOpt {
                id: 1,
                lifetime: Duration::from_secs(10),
            },
            TestOpt {
                id: 2,
                lifetime: Duration::ZERO,
            },
            TestOpt {
                id: 3,
                lifetime: Duration::from_secs(20),
            },
        ];

        detect_multi_option(
            &mut out,
            key,
            input,
            |o| o.id,
            |k, v| {
                Event::DnsDomains(
                    k,
                    vec![DnsDomains {
                        domains: v.iter().map(|i| i.to_string()).collect(),
                        lifetime: Duration::from_secs(1),
                    }],
                )
            },
        );

        assert_eq!(out.len(), 1);

        match &out[0] {
            Event::DnsDomains(_, domains) => {
                // Empty vector = explicit withdraw
                anyhow::ensure!(domains.len() == 1);
                anyhow::ensure!(domains[0].domains.len() == 0);
            }
            _ => panic!("unexpected event"),
        }
        Ok(())
    }

    #[test]
    fn detect_multi_option_empty_input_emits_nothing() {
        let key = Key::new(1, Ipv6Addr::from(1));
        let mut out = Vec::new();

        let input: Vec<TestOpt> = vec![];

        detect_multi_option(
            &mut out,
            key,
            input,
            |o| o.id,
            |k, _| Event::DnsServers(k, vec![]),
        );

        assert!(out.is_empty());
    }
}