1
//! Define different restrictions that can be applied to relays.
2

            
3
#[cfg(feature = "geoip")]
4
use tor_geoip::HasCountryCode;
5
use tor_linkspec::{ChanTarget, HasAddrs, HasRelayIds, RelayIdSet};
6
use tor_netdir::{FamilyRules, NetDir, Relay, SubnetConfig};
7
use tor_netdoc::types::policy::AddrPortPattern;
8

            
9
use crate::{LowLevelRelayPredicate, RelaySelectionConfig, RelayUsage};
10
use std::{fmt, net::IpAddr};
11

            
12
/// A restriction that we use when picking relays.
13
///
14
/// Differs from [`RelayUsage`] in that it does not say what
15
/// the relay is _used for_;
16
/// instead, it describes an additional set of requirements that a relay must
17
/// satisfy.
18
#[derive(Clone, Debug)]
19
pub struct RelayRestriction<'a> {
20
    /// The actual restriction object.
21
    inner: RestrictionInner<'a>,
22
}
23

            
24
/// Enumeration of possible [`RelayRestriction`]s.
25
///
26
/// This is a separate type so that we can hide its variants.
27
///
28
// TODO: I'm not sure about having this be relative to `'a``,
29
// but that is the only way to hold a `Relay<'a>`
30
//
31
// NOTE: Any time that you are extending this type, make sure that you are not
32
// describing a new _mandatory_ restriction that all `RelaySelector` users
33
// need to consider adding (or not).  If you *are* describing such a restriction,
34
// then it should have its own type, and it should become a new argument to
35
// RelaySelector::new().
36
#[derive(Clone, Debug)]
37
enum RestrictionInner<'a> {
38
    /// Do not restrict any relays.
39
    ///
40
    /// This is present so that we can construct a no-op restriction when
41
    /// relaxing a selector.
42
    NoRestriction,
43
    /// Require a given usage.
44
    SupportsUsage(crate::RelayUsage),
45
    /// Exclude a set of relays explicitly, by family, or by identity.
46
    Exclude(RelayExclusion<'a>),
47
    /// Require that, if the relay's contact method uses addresses, the relay
48
    /// has at least one address matching one of the provided patterns.
49
    HasAddrInSet(Vec<AddrPortPattern>),
50
    /// Require that the relay has a given country code.
51
    #[cfg(feature = "geoip")]
52
    RequireCountry(tor_geoip::CountryCode),
53
}
54

            
55
impl<'a> RelayRestriction<'a> {
56
    /// Create a restriction that allows every relay.
57
2
    pub(crate) fn no_restriction() -> Self {
58
2
        RelayRestriction {
59
2
            inner: RestrictionInner::NoRestriction,
60
2
        }
61
2
    }
62

            
63
    /// Convert a usage into a restriction.
64
    ///
65
    /// This is crate-internal since we never want to support requiring a relay
66
    /// to provide multiple usages.
67
1146342
    pub(crate) fn for_usage(usage: crate::RelayUsage) -> Self {
68
1146342
        RelayRestriction {
69
1146342
            inner: RestrictionInner::SupportsUsage(usage),
70
1146342
        }
71
1146342
    }
72

            
73
    /// Require a relay that appears to be in the provided country,
74
    /// according ot our geoip subsystem.
75
    #[cfg(feature = "geoip")]
76
    pub fn require_country_code(cc: tor_geoip::CountryCode) -> Self {
77
        RelayRestriction {
78
            inner: RestrictionInner::RequireCountry(cc),
79
        }
80
    }
81

            
82
    /// Require that a relay has at least one address
83
    /// listed in `addr_patterns`.
84
542
    pub fn require_address(addr_patterns: Vec<AddrPortPattern>) -> Self {
85
        // TODO: It's plausible that this restriction should be mandatory
86
        // whenever we are picking new guards.
87
542
        RelayRestriction {
88
542
            inner: RestrictionInner::HasAddrInSet(addr_patterns),
89
542
        }
90
542
    }
91

            
92
    /// Return a restriction that represents having "relaxed" this restriction.
93
    ///
94
    /// (Relaxing a restriction replaces it with a no-op, or with an almost-no-op.)
95
542
    pub(crate) fn relax(&self) -> Self {
96
        use RestrictionInner::*;
97
542
        match &self.inner {
98
            // We must always have a usage, so relaxing a usage must always
99
            // return a usage.
100
540
            SupportsUsage(usage) => Self::for_usage(RelayUsage::middle_relay(Some(usage))),
101
            // Relaxing any other restriction returns a no-op
102
2
            _ => Self::no_restriction(),
103
        }
104
542
    }
105

            
106
    /// If this restriction represents a usage, return a reference to that usage.
107
1699455
    pub(crate) fn as_usage(&self) -> Option<&RelayUsage> {
108
        use RestrictionInner::*;
109
1699455
        match &self.inner {
110
1699455
            SupportsUsage(usage) => Some(usage),
111
            _ => None,
112
        }
113
1699455
    }
114

            
115
    /// Return a string describing why we rejected the relays that _don't_ match
116
    /// this restriction.
117
822
    pub(crate) fn rejection_description(&self) -> Option<&'static str> {
118
        use RestrictionInner::*;
119
822
        match &self.inner {
120
            NoRestriction => None,
121
548
            SupportsUsage(u) => Some(u.rejection_description()),
122
274
            Exclude(e) => e.rejection_description(),
123
            HasAddrInSet(_) => Some("not reachable (according to address filter)"),
124
            #[cfg(feature = "geoip")]
125
            RequireCountry(_) => Some("not in correct country"),
126
        }
127
822
    }
128
}
129

            
130
impl<'a> LowLevelRelayPredicate for RelayRestriction<'a> {
131
75750478
    fn low_level_predicate_permits_relay(&self, relay: &tor_netdir::Relay<'_>) -> bool {
132
        use RestrictionInner::*;
133
75750478
        match &self.inner {
134
56
            NoRestriction => true,
135
44879120
            SupportsUsage(usage) => usage.low_level_predicate_permits_relay(relay),
136
30866362
            Exclude(exclusion) => exclusion.low_level_predicate_permits_relay(relay),
137
4940
            HasAddrInSet(patterns) => relay_has_addr_in_set(relay, patterns),
138
            #[cfg(feature = "geoip")]
139
            RequireCountry(cc) => relay.country_code() == Some(*cc),
140
        }
141
75750478
    }
142
}
143

            
144
impl<'a> From<RelayExclusion<'a>> for RelayRestriction<'a> {
145
1148592
    fn from(value: RelayExclusion<'a>) -> Self {
146
1148592
        RelayRestriction {
147
1148592
            inner: RestrictionInner::Exclude(value),
148
1148592
        }
149
1148592
    }
150
}
151

            
152
/// Return true if `relay` has at least one address matching at least one member
153
/// of `patterns`.
154
4940
fn relay_has_addr_in_set(relay: &Relay<'_>, patterns: &[AddrPortPattern]) -> bool {
155
    // NOTE: If we ever make this apply to ChanTarget instead of Relay, we will
156
    // need it to call chan_method().socket_addrs() instead, and handle the case
157
    // where the transport doesn't use an address.
158
4940
    relay
159
4940
        .addrs()
160
5152
        .any(|addr| patterns.iter().any(|pat| pat.matches_sockaddr(&addr)))
161
4940
}
162

            
163
/// A set of relays that we must not use when picking a given
164
/// relays.
165
///
166
/// Exclusions are generally used to make sure that we obey
167
/// family-based path-selection rules,
168
/// that we avoid putting the same relay into a set more than once,
169
/// or similar purposes.
170
///
171
/// (This is a separate type from [`RelayRestriction`] so that we can
172
/// enforce our rule that every [`RelaySelector`](crate::RelaySelector) must
173
/// have a `RelayExclusion`.)
174
#[derive(Clone, Debug)]
175
pub struct RelayExclusion<'a> {
176
    /// A list of identities to exclude.
177
    ///
178
    /// Any relay with any one of these identities is rejecteed.
179
    exclude_ids: RelayIdSet,
180
    /// A list of subnets from which to exclude addresses.
181
    ///
182
    /// The side of the subnet is determined by subnet_config.
183
    exclude_subnets: Vec<IpAddr>,
184
    /// A list of relays to exclude, along with their families.
185
    exclude_relay_families: RelayList<'a>,
186
    /// The configuration to use when deciding whether two addresses are in the
187
    /// same subnet.
188
    subnet_config: SubnetConfig,
189
    /// The rules to use when deciding whether two relays are in the same family.
190
    family_rules: FamilyRules,
191
}
192

            
193
/// Helper: wraps `Vec[Relay]`, but implements Debug.
194
#[derive(Clone)]
195
struct RelayList<'a>(Vec<Relay<'a>>);
196
impl<'a> fmt::Debug for RelayList<'a> {
197
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198
        write!(f, "[ ")?;
199
        for r in &self.0 {
200
            write!(f, "{}, ", r.display_relay_ids())?;
201
        }
202
        write!(f, "]")
203
    }
204
}
205

            
206
impl<'a> RelayExclusion<'a> {
207
    /// Exclude no relays at all.
208
    ///
209
    /// This kind of restriction is useful when picking the first relay for
210
    /// something,
211
    ///
212
    // (Note that this is _not_ Default::default, since we don't want people
213
    // picking it by mistake.)
214
3339744
    pub fn no_relays_excluded() -> Self {
215
3339744
        RelayExclusion {
216
3339744
            exclude_ids: RelayIdSet::new(),
217
3339744
            exclude_subnets: Vec::new(),
218
3339744
            exclude_relay_families: RelayList(Vec::new()),
219
3339744
            subnet_config: SubnetConfig::no_addresses_match(),
220
3339744
            family_rules: FamilyRules::ignore_declared_families(),
221
3339744
        }
222
3339744
    }
223

            
224
    /// Exclude every relay that has an identity in `ids`.
225
38532
    pub fn exclude_identities(ids: RelayIdSet) -> Self {
226
38532
        RelayExclusion {
227
38532
            exclude_ids: ids,
228
38532
            ..RelayExclusion::no_relays_excluded()
229
38532
        }
230
38532
    }
231

            
232
    /// Exclude every relay that appears in `relays`.
233
2
    pub fn exclude_specific_relays(relays: &[Relay<'a>]) -> Self {
234
2
        let ids: RelayIdSet = relays
235
2
            .iter()
236
2
            .flat_map(Relay::identities)
237
9
            .map(|id_ref| id_ref.to_owned())
238
2
            .collect();
239

            
240
2
        Self::exclude_identities(ids)
241
2
    }
242

            
243
    /// Try to exclude every relay in the same family as the [`ChanTarget`]
244
    /// `ct`.
245
    ///
246
    /// # Limitations
247
    ///
248
    /// A ChanTarget does not have a listed family.  Thus, if it does not correspond
249
    /// to a relay listed in `netdir`, we can only exclude relays that share the
250
    /// same identity, or relays that are in the same subnet.
251
    ///
252
    /// Whenever possible, it's better to use exclude_relays_in_same_family.
253
    pub fn exclude_channel_target_family<CT: ChanTarget>(
254
        cfg: &RelaySelectionConfig,
255
        ct: &CT,
256
        netdir: &'a NetDir,
257
    ) -> Self {
258
        if let Some(r) = netdir.by_ids(ct) {
259
            return Self::exclude_relays_in_same_family(
260
                cfg,
261
                vec![r],
262
                FamilyRules::from(netdir.params()),
263
            );
264
        }
265

            
266
        let exclude_ids = ct.identities().map(|id_ref| id_ref.to_owned()).collect();
267
        let exclude_addr_families = ct.addrs().map(|a| a.ip()).collect();
268

            
269
        Self {
270
            exclude_ids,
271
            exclude_subnets: exclude_addr_families,
272
            subnet_config: cfg.subnet_config,
273
            ..Self::no_relays_excluded()
274
        }
275
    }
276

            
277
    /// Exclude every relay that is in the same family as any member of
278
    /// `relays`.
279
    ///
280
    /// (Remember that every relay is considered to be in the same family as
281
    /// itself, so you don't typically need to use `exclude_specific_relays`
282
    /// along with this.)
283
    ///
284
    /// Considers relays that are in the same subnets (according to `cfg`) to
285
    /// belong to the same family.
286
2747845
    pub fn exclude_relays_in_same_family(
287
2747845
        cfg: &RelaySelectionConfig,
288
2747845
        relays: Vec<Relay<'a>>,
289
2747845
        family_rules: FamilyRules,
290
2747845
    ) -> Self {
291
2747845
        RelayExclusion {
292
2747845
            exclude_relay_families: RelayList(relays),
293
2747845
            subnet_config: cfg.subnet_config,
294
2747845
            family_rules,
295
2747845
            ..RelayExclusion::no_relays_excluded()
296
2747845
        }
297
2747845
    }
298

            
299
    /// Modify this `RelayExclusion` by adding every exclusion from `other`.
300
    ///
301
    /// (Any subnet configuration becomes the _union_ of previous subnet
302
    /// configurations.)
303
1667160
    pub fn extend(&mut self, other: &RelayExclusion<'a>) {
304
        let RelayExclusion {
305
1667160
            exclude_ids,
306
1667160
            exclude_subnets: exclude_addr_families,
307
1667160
            exclude_relay_families,
308
1667160
            subnet_config,
309
1667160
            family_rules,
310
1667160
        } = other;
311
1667160
        self.exclude_ids
312
1667564
            .extend(exclude_ids.iter().map(|id_ref| id_ref.to_owned()));
313
1667160
        self.exclude_subnets
314
1667160
            .extend_from_slice(&exclude_addr_families[..]);
315
1667160
        self.exclude_relay_families
316
1667160
            .0
317
1667160
            .extend_from_slice(&exclude_relay_families.0[..]);
318
1667160
        self.subnet_config = self.subnet_config.union(subnet_config);
319
1667160
        self.family_rules = self.family_rules.union(family_rules);
320
1667160
    }
321

            
322
    /// Return a string describing why we rejected the relays that _don't_ match
323
    /// this exclusion.
324
274
    pub(crate) fn rejection_description(&self) -> Option<&'static str> {
325
274
        if self.exclude_relay_families.0.is_empty() && self.exclude_subnets.is_empty() {
326
182
            if self.exclude_ids.is_empty() {
327
                None
328
            } else {
329
182
                Some("already selected")
330
            }
331
        } else {
332
92
            Some("in same family as already selected")
333
        }
334
274
    }
335
}
336

            
337
impl<'a> LowLevelRelayPredicate for RelayExclusion<'a> {
338
32503178
    fn low_level_predicate_permits_relay(&self, relay: &Relay<'_>) -> bool {
339
65611470
        if relay.identities().any(|id| self.exclude_ids.contains(id)) {
340
130000
            return false;
341
32373178
        }
342

            
343
33095634
        if relay.addrs().any(|addr| {
344
32373178
            self.exclude_subnets
345
32373178
                .iter()
346
32373178
                .any(|fam| self.subnet_config.addrs_in_same_subnet(&addr.ip(), fam))
347
32373178
        }) {
348
            return false;
349
32373178
        }
350

            
351
49893229
        if self.exclude_relay_families.0.iter().any(|r| {
352
49167933
            relays_in_same_extended_family(&self.subnet_config, relay, r, self.family_rules)
353
49167933
        }) {
354
11368923
            return false;
355
21004255
        }
356

            
357
21004255
        true
358
32503178
    }
359
}
360

            
361
/// Return true if `r1` and `r2` are in the same "extended" family,
362
/// considering both explicitly declared families
363
/// and subnet-based extended families.
364
49167933
fn relays_in_same_extended_family(
365
49167933
    subnet_config: &SubnetConfig,
366
49167933
    r1: &Relay<'_>,
367
49167933
    r2: &Relay<'_>,
368
49167933
    family_rules: FamilyRules,
369
49167933
) -> bool {
370
49167933
    r1.low_level_details().in_same_family(r2, family_rules)
371
46808686
        || subnet_config.any_addrs_in_same_subnet(r1, r2)
372
49167933
}
373

            
374
#[cfg(test)]
375
mod test {
376
    // @@ begin test lint list maintained by maint/add_warning @@
377
    #![allow(clippy::bool_assert_comparison)]
378
    #![allow(clippy::clone_on_copy)]
379
    #![allow(clippy::dbg_macro)]
380
    #![allow(clippy::mixed_attributes_style)]
381
    #![allow(clippy::print_stderr)]
382
    #![allow(clippy::print_stdout)]
383
    #![allow(clippy::single_char_pattern)]
384
    #![allow(clippy::unwrap_used)]
385
    #![allow(clippy::unchecked_time_subtraction)]
386
    #![allow(clippy::useless_vec)]
387
    #![allow(clippy::needless_pass_by_value)]
388
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
389

            
390
    use tor_linkspec::RelayId;
391
    use tor_netdir::testnet::construct_custom_netdir;
392

            
393
    use super::*;
394
    use crate::testing::{cfg, split_netdir, testnet};
395

            
396
    #[test]
397
    fn exclude_nothing() {
398
        let nd = testnet();
399
        let usage = RelayExclusion::no_relays_excluded();
400
        assert!(
401
            nd.relays()
402
                .all(|r| usage.low_level_predicate_permits_relay(&r))
403
        );
404
    }
405

            
406
    #[test]
407
    fn exclude_ids() {
408
        let nd = testnet();
409
        let id_0 = "$0000000000000000000000000000000000000000".parse().unwrap();
410
        let id_5 = "ed25519:BQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU"
411
            .parse()
412
            .unwrap();
413
        let ids: RelayIdSet = [id_0, id_5].into_iter().collect();
414
        let (yes, no) = split_netdir(&nd, &RelayExclusion::exclude_identities(ids));
415

            
416
        let p = |r: &Relay<'_>| !(r.has_identity(id_0.as_ref()) || r.has_identity(id_5.as_ref()));
417
        assert_eq!(yes.len(), 38);
418
        assert_eq!(no.len(), 2);
419
        assert!(yes.iter().all(p));
420
        assert!(no.iter().all(|r| !p(r)));
421
    }
422

            
423
    #[test]
424
    fn exclude_relays() {
425
        let nd = testnet();
426
        let id_0: RelayId = "$0000000000000000000000000000000000000000".parse().unwrap();
427
        let id_5: RelayId = "ed25519:BQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU"
428
            .parse()
429
            .unwrap();
430
        let relay_0 = nd.by_id(&id_0).unwrap();
431
        let relay_5 = nd.by_id(&id_5).unwrap();
432

            
433
        let (yes, no) = split_netdir(
434
            &nd,
435
            &RelayExclusion::exclude_specific_relays(&[relay_0.clone(), relay_5.clone()]),
436
        );
437
        let p = |r: &Relay<'_>| !(r.same_relay_ids(&relay_0) || r.same_relay_ids(&relay_5));
438
        assert_eq!(yes.len(), 38);
439
        assert_eq!(no.len(), 2);
440
        assert!(yes.iter().all(p));
441
        assert!(no.iter().all(|r| !p(r)));
442
    }
443

            
444
    /// Helper for testing family exclusions.  Requires a netdir where,
445
    /// for every N, relays 2N and 2N+1 are in a family.
446
    fn exclude_families_impl(nd: &NetDir, family_rules: FamilyRules) {
447
        let id_0: RelayId = "$0000000000000000000000000000000000000000".parse().unwrap();
448
        let id_5: RelayId = "ed25519:BQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU"
449
            .parse()
450
            .unwrap();
451
        let relay_0 = nd.by_id(&id_0).unwrap();
452
        let relay_5 = nd.by_id(&id_5).unwrap();
453
        let excluding_relays = vec![relay_0, relay_5];
454

            
455
        // in the test netdir, all (2n, 2n+1) pairs are in a family.
456
        let id_1 = "$0101010101010101010101010101010101010101".parse().unwrap();
457
        let id_4 = "$0404040404040404040404040404040404040404".parse().unwrap();
458
        let expect_excluded_ids: RelayIdSet = [id_0, id_1, id_4, id_5].into_iter().collect();
459

            
460
        // Case one: No subnet-based exclusion.
461

            
462
        let cfg_no_subnet = RelaySelectionConfig {
463
            long_lived_ports: cfg().long_lived_ports,
464
            subnet_config: SubnetConfig::new(255, 255),
465
        };
466

            
467
        let (yes, no) = split_netdir(
468
            nd,
469
            &RelayExclusion::exclude_relays_in_same_family(
470
                &cfg_no_subnet,
471
                excluding_relays.clone(),
472
                family_rules,
473
            ),
474
        );
475
        let p = |r: &Relay<'_>| !r.identities().any(|id| expect_excluded_ids.contains(id));
476
        assert_eq!(yes.len(), 36);
477
        assert_eq!(no.len(), 4);
478
        assert!(yes.iter().all(p));
479
        assert!(no.iter().all(|r| !p(r)));
480

            
481
        // Case two: default subnet-based exclusion.
482
        //
483
        // In the test network, addresses are x.0.0.3 where x is the index of
484
        // the relay, modulo 5.  Since the default ipv4 subnet family rule looks at /16
485
        // prefixes, every one of the 40 relays in the testnet will be in a
486
        // family with 8 other relays.
487
        let expect_excluded_ids: RelayIdSet = nd
488
            .relays()
489
            .filter_map(|r| {
490
                let rsa = r.rsa_identity().unwrap();
491
                let b = rsa.as_bytes()[0];
492
                if [0, 1, 4, 5].contains(&b) || [0, 5].contains(&(b % 5)) {
493
                    Some(RelayId::from(*rsa))
494
                } else {
495
                    None
496
                }
497
            })
498
            .collect();
499

            
500
        let (yes, no) = split_netdir(
501
            nd,
502
            &RelayExclusion::exclude_relays_in_same_family(&cfg(), excluding_relays, family_rules),
503
        );
504
        for r in &no {
505
            dbg!(r.rsa_identity().unwrap());
506
        }
507
        dbg!(&expect_excluded_ids);
508
        dbg!(expect_excluded_ids.len());
509
        let p = |r: &Relay<'_>| !r.identities().any(|id| expect_excluded_ids.contains(id));
510
        assert_eq!(yes.len(), 30);
511
        assert_eq!(no.len(), 10);
512
        assert!(yes.iter().all(p));
513

            
514
        assert!(no.iter().all(|r| { !p(r) }));
515
    }
516

            
517
    #[test]
518
    fn exclude_families_by_list() {
519
        exclude_families_impl(
520
            &testnet(),
521
            *FamilyRules::ignore_declared_families().use_family_lists(true),
522
        );
523
    }
524

            
525
    #[test]
526
    fn exclude_families_by_id() {
527
        // Here we construct a network that matches our default testnet,
528
        // but without any family lists.
529
        // Instead we use "happy family" IDs to match the families from that default testnest.
530
        let netdir = construct_custom_netdir(|pos, nb, _| {
531
            // Clear the family list.
532
            nb.md.family("".parse().unwrap());
533
            // This will create an "Unrecognized" family id such that
534
            // pos:N  will be shared by nodes in positions 2N and 2N+1.
535
            let fam_id = format!("pos:{}", pos / 2);
536
            nb.md.add_family_id(fam_id.parse().unwrap());
537
        })
538
        .unwrap()
539
        .unwrap_if_sufficient()
540
        .unwrap();
541

            
542
        exclude_families_impl(
543
            &netdir,
544
            *FamilyRules::ignore_declared_families().use_family_ids(true),
545
        );
546
    }
547

            
548
    #[test]
549
    fn filter_addresses() {
550
        let nd = testnet();
551
        let reachable = vec![
552
            "1.0.0.0/8:*".parse().unwrap(),
553
            "2.0.0.0/8:*".parse().unwrap(),
554
        ];
555
        let reachable = RelayRestriction::require_address(reachable);
556

            
557
        let (yes, no) = split_netdir(&nd, &reachable);
558
        assert_eq!(yes.len(), 16);
559
        assert_eq!(no.len(), 24);
560

            
561
        let expected = ["1.0.0.3".parse().unwrap(), "2.0.0.3".parse().unwrap()];
562
        let p = |r: &Relay<'_>| expected.contains(&r.addrs().next().unwrap().ip());
563
        assert!(yes.iter().all(p));
564
        assert!(no.iter().all(|r| !p(r)));
565
    }
566

            
567
    // TODO: Write a geoip test?
568
}