1
//! Code for building paths to an exit relay.
2

            
3
use std::time::SystemTime;
4

            
5
use rand::Rng;
6
use tracing::instrument;
7

            
8
use super::{AnonymousPathBuilder, TorPath};
9
use crate::path::pick_path;
10
use crate::{DirInfo, Error, PathConfig, Result, TargetPort};
11

            
12
use tor_guardmgr::{GuardMgr, GuardMonitor, GuardUsable};
13
use tor_linkspec::OwnedChanTarget;
14
use tor_netdir::{NetDir, Relay};
15
use tor_relay_selection::{RelayExclusion, RelaySelectionConfig, RelaySelector, RelayUsage};
16
use tor_rtcompat::Runtime;
17
#[cfg(feature = "geoip")]
18
use {tor_geoip::CountryCode, tor_relay_selection::RelayRestriction};
19

            
20
/// Internal representation of PathBuilder.
21
enum ExitPathBuilderInner {
22
    /// Request a path that allows exit to the given `TargetPort`s.
23
    WantsPorts(Vec<TargetPort>),
24

            
25
    /// Request a path that allows exit with a relay in the given country.
26
    // TODO GEOIP: refactor this builder to allow conjunction!
27
    // See discussion here:
28
    // https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1537#note_2942218
29
    #[cfg(feature = "geoip")]
30
    ExitInCountry {
31
        /// The country to exit in.
32
        country: CountryCode,
33
        /// Some target ports to use (works like `WantsPorts`).
34
        ///
35
        /// HACK(eta): This is a horrible hack to work around the lack of conjunction.
36
        ports: Vec<TargetPort>,
37
    },
38

            
39
    /// Request a path that allows exit to _any_ port.
40
    AnyExit {
41
        /// If false, then we fall back to non-exit nodes if we can't find an
42
        /// exit.
43
        strict: bool,
44
    },
45
}
46

            
47
/// A PathBuilder that builds a path to an exit relay supporting a given
48
/// set of ports.
49
///
50
/// NOTE: The name of this type is no longer completely apt: given some circuits,
51
/// it is happy to build a circuit ending at a non-exit.
52
pub(crate) struct ExitPathBuilder {
53
    /// The inner ExitPathBuilder state.
54
    inner: ExitPathBuilderInner,
55
    /// If present, a "target" that every chosen relay must be able to share a circuit with with.
56
    compatible_with: Option<OwnedChanTarget>,
57
    /// If true, all relays on this path must be Stable.
58
    require_stability: bool,
59
}
60

            
61
impl ExitPathBuilder {
62
    /// Create a new builder that will try to get an exit relay
63
    /// containing all the ports in `ports`.
64
    ///
65
    /// If the list of ports is empty, tries to get any exit relay at all.
66
12264
    pub(crate) fn from_target_ports(wantports: impl IntoIterator<Item = TargetPort>) -> Self {
67
12264
        let ports: Vec<TargetPort> = wantports.into_iter().collect();
68
12264
        if ports.is_empty() {
69
            return Self::for_any_exit();
70
12264
        }
71
12264
        Self {
72
12264
            inner: ExitPathBuilderInner::WantsPorts(ports),
73
12264
            compatible_with: None,
74
12264
            require_stability: true,
75
12264
        }
76
12264
    }
77

            
78
    #[cfg(feature = "geoip")]
79
    /// Create a new builder that will try to get an exit relay in `country`,
80
    /// containing all the ports in `ports`.
81
    ///
82
    /// If the list of ports is empty, it is disregarded.
83
    // TODO GEOIP: this method is hacky, and should be refactored.
84
    pub(crate) fn in_given_country(
85
        country: CountryCode,
86
        wantports: impl IntoIterator<Item = TargetPort>,
87
    ) -> Self {
88
        let ports: Vec<TargetPort> = wantports.into_iter().collect();
89
        Self {
90
            inner: ExitPathBuilderInner::ExitInCountry { country, ports },
91
            compatible_with: None,
92
            require_stability: true,
93
        }
94
    }
95

            
96
    /// Create a new builder that will try to get any exit relay at all.
97
12012
    pub(crate) fn for_any_exit() -> Self {
98
12012
        Self {
99
12012
            inner: ExitPathBuilderInner::AnyExit { strict: true },
100
12012
            compatible_with: None,
101
12012
            require_stability: false,
102
12012
        }
103
12012
    }
104

            
105
    /// Try to create and return a path corresponding to the requirements of
106
    /// this builder.
107
    #[instrument(skip_all, level = "trace")]
108
24312
    pub(crate) fn pick_path<'a, R: Rng, RT: Runtime>(
109
24312
        &self,
110
24312
        rng: &mut R,
111
24312
        netdir: DirInfo<'a>,
112
24312
        guards: &GuardMgr<RT>,
113
24312
        config: &PathConfig,
114
24312
        now: SystemTime,
115
24312
    ) -> Result<(TorPath<'a>, GuardMonitor, GuardUsable)> {
116
24312
        pick_path(self, rng, netdir, guards, config, now)
117
24312
    }
118

            
119
    /// Create a new builder that will try to get an exit relay, but which
120
    /// will be satisfied with a non-exit relay.
121
36
    pub(crate) fn for_timeout_testing() -> Self {
122
36
        Self {
123
36
            inner: ExitPathBuilderInner::AnyExit { strict: false },
124
36
            compatible_with: None,
125
36
            require_stability: false,
126
36
        }
127
36
    }
128

            
129
    /// Indicate that middle and exit relays on this circuit need (or do not
130
    /// need) to have the Stable flag.
131
36
    pub(crate) fn require_stability(&mut self, require_stability: bool) -> &mut Self {
132
36
        self.require_stability = require_stability;
133
36
        self
134
36
    }
135
}
136

            
137
impl AnonymousPathBuilder for ExitPathBuilder {
138
48624
    fn compatible_with(&self) -> Option<&OwnedChanTarget> {
139
48624
        self.compatible_with.as_ref()
140
48624
    }
141

            
142
24312
    fn pick_exit<'a, R: Rng>(
143
24312
        &self,
144
24312
        rng: &mut R,
145
24312
        netdir: &'a NetDir,
146
24312
        guard_exclusion: RelayExclusion<'a>,
147
24312
        rs_cfg: &RelaySelectionConfig<'_>,
148
24312
    ) -> Result<(Relay<'a>, RelayUsage)> {
149
24312
        let selector = match &self.inner {
150
12048
            ExitPathBuilderInner::AnyExit { strict } => {
151
12048
                let mut selector =
152
12048
                    RelaySelector::new(RelayUsage::any_exit(rs_cfg), guard_exclusion);
153
12048
                if !strict {
154
36
                    selector.mark_usage_flexible();
155
12012
                }
156
12048
                selector
157
            }
158

            
159
            #[cfg(feature = "geoip")]
160
            ExitPathBuilderInner::ExitInCountry { country, ports } => {
161
                let mut selector = RelaySelector::new(
162
                    RelayUsage::exit_to_all_ports(rs_cfg, ports.clone()),
163
                    guard_exclusion,
164
                );
165
                selector.push_restriction(RelayRestriction::require_country_code(*country));
166
                selector
167
            }
168

            
169
12264
            ExitPathBuilderInner::WantsPorts(wantports) => RelaySelector::new(
170
12264
                RelayUsage::exit_to_all_ports(rs_cfg, wantports.clone()),
171
12264
                guard_exclusion,
172
            ),
173
        };
174

            
175
24312
        let (relay, info) = selector.select_relay(rng, netdir);
176
24312
        let relay = relay.ok_or_else(|| Error::NoRelay {
177
24
            path_kind: self.path_kind(),
178
            role: "final hop",
179
24
            problem: info.to_string(),
180
24
        })?;
181
24288
        Ok((relay, RelayUsage::middle_relay(Some(selector.usage()))))
182
24312
    }
183

            
184
24
    fn path_kind(&self) -> &'static str {
185
        use ExitPathBuilderInner::*;
186
24
        match &self.inner {
187
12
            WantsPorts(_) => "exit circuit",
188
            #[cfg(feature = "geoip")]
189
            ExitInCountry { .. } => "country-specific exit circuit",
190
12
            AnyExit { .. } => "testing circuit",
191
        }
192
24
    }
193
}
194

            
195
#[cfg(test)]
196
mod test {
197
    // @@ begin test lint list maintained by maint/add_warning @@
198
    #![allow(clippy::bool_assert_comparison)]
199
    #![allow(clippy::clone_on_copy)]
200
    #![allow(clippy::dbg_macro)]
201
    #![allow(clippy::mixed_attributes_style)]
202
    #![allow(clippy::print_stderr)]
203
    #![allow(clippy::print_stdout)]
204
    #![allow(clippy::single_char_pattern)]
205
    #![allow(clippy::unwrap_used)]
206
    #![allow(clippy::unchecked_time_subtraction)]
207
    #![allow(clippy::useless_vec)]
208
    #![allow(clippy::needless_pass_by_value)]
209
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
210
    use super::*;
211
    use crate::path::{
212
        MaybeOwnedRelay, OwnedPath, TorPath, TorPathInner, assert_same_path_when_owned,
213
    };
214
    use std::collections::HashSet;
215
    use tor_basic_utils::test_rng::testing_rng;
216
    use tor_guardmgr::TestConfig;
217
    use tor_linkspec::{HasRelayIds, RelayIds};
218
    use tor_netdir::{FamilyRules, SubnetConfig, testnet};
219
    use tor_persist::TestingStateMgr;
220
    use tor_relay_selection::LowLevelRelayPredicate;
221
    use tor_rtcompat::SleepProvider;
222

            
223
    impl<'a> MaybeOwnedRelay<'a> {
224
        fn can_share_circuit(
225
            &self,
226
            other: &MaybeOwnedRelay<'_>,
227
            subnet_config: SubnetConfig,
228
            family_rules: FamilyRules,
229
        ) -> bool {
230
            use MaybeOwnedRelay as M;
231
            match (self, other) {
232
                (M::Relay(a), M::Relay(b)) => {
233
                    let ports = Default::default();
234
                    let cfg = RelaySelectionConfig {
235
                        long_lived_ports: &ports,
236
                        subnet_config,
237
                    };
238
                    // This use of "low_level_predicate_permits_relay" is okay because
239
                    // because we're in tests.
240
                    RelayExclusion::exclude_relays_in_same_family(
241
                        &cfg,
242
                        vec![a.clone()],
243
                        family_rules,
244
                    )
245
                    .low_level_predicate_permits_relay(b)
246
                }
247
                (a, b) => !subnet_config.any_addrs_in_same_subnet(a, b),
248
            }
249
        }
250
    }
251

            
252
    fn assert_exit_path_ok(relays: &[MaybeOwnedRelay<'_>], family_rules: FamilyRules) {
253
        assert_eq!(relays.len(), 3);
254

            
255
        let r1 = &relays[0];
256
        let r2 = &relays[1];
257
        let r3 = &relays[2];
258

            
259
        if let MaybeOwnedRelay::Relay(r1) = r1 {
260
            assert!(r1.low_level_details().is_suitable_as_guard());
261
        }
262

            
263
        assert!(!r1.same_relay_ids(r2));
264
        assert!(!r1.same_relay_ids(r3));
265
        assert!(!r2.same_relay_ids(r3));
266

            
267
        let subnet_config = SubnetConfig::default();
268
        assert!(r1.can_share_circuit(r2, subnet_config, family_rules));
269
        assert!(r2.can_share_circuit(r3, subnet_config, family_rules));
270
        assert!(r1.can_share_circuit(r3, subnet_config, family_rules));
271
    }
272

            
273
    #[test]
274
    fn by_ports() {
275
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
276
            let mut rng = testing_rng();
277
            let family_rules = FamilyRules::all_family_info();
278
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
279
            let ports = vec![TargetPort::ipv4(443), TargetPort::ipv4(1119)];
280
            let dirinfo = (&netdir).into();
281
            let config = PathConfig::default();
282
            let statemgr = TestingStateMgr::new();
283
            let guards =
284
                tor_guardmgr::GuardMgr::new(rt.clone(), statemgr, &TestConfig::default()).unwrap();
285
            guards.install_test_netdir(&netdir);
286
            let now = SystemTime::now();
287

            
288
            for _ in 0..1000 {
289
                let (path, _, _) = ExitPathBuilder::from_target_ports(ports.clone())
290
                    .pick_path(&mut rng, dirinfo, &guards, &config, now)
291
                    .unwrap();
292

            
293
                assert_same_path_when_owned(&path);
294

            
295
                if let TorPathInner::Path(p) = path.inner {
296
                    assert_exit_path_ok(&p[..], family_rules);
297
                    let exit = match &p[2] {
298
                        MaybeOwnedRelay::Relay(r) => r,
299
                        MaybeOwnedRelay::Owned(_) => panic!("Didn't asked for an owned target!"),
300
                    };
301
                    assert!(exit.low_level_details().ipv4_policy().allows_port(1119));
302
                } else {
303
                    panic!("Generated the wrong kind of path");
304
                }
305
            }
306
        });
307
    }
308

            
309
    #[test]
310
    fn any_exit() {
311
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
312
            let mut rng = testing_rng();
313
            let family_rules = FamilyRules::all_family_info();
314
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
315
            let dirinfo = (&netdir).into();
316
            let statemgr = TestingStateMgr::new();
317
            let guards =
318
                tor_guardmgr::GuardMgr::new(rt.clone(), statemgr, &TestConfig::default()).unwrap();
319
            guards.install_test_netdir(&netdir);
320
            let now = SystemTime::now();
321

            
322
            let config = PathConfig::default();
323
            for _ in 0..1000 {
324
                let (path, _, _) = ExitPathBuilder::for_any_exit()
325
                    .pick_path(&mut rng, dirinfo, &guards, &config, now)
326
                    .unwrap();
327
                assert_same_path_when_owned(&path);
328
                if let TorPathInner::Path(p) = path.inner {
329
                    assert_exit_path_ok(&p[..], family_rules);
330
                    let exit = match &p[2] {
331
                        MaybeOwnedRelay::Relay(r) => r,
332
                        MaybeOwnedRelay::Owned(_) => panic!("Didn't asked for an owned target!"),
333
                    };
334
                    assert!(exit.low_level_details().policies_allow_some_port());
335
                } else {
336
                    panic!("Generated the wrong kind of path");
337
                }
338
            }
339
        });
340
    }
341

            
342
    #[test]
343
    fn empty_path() {
344
        // This shouldn't actually be constructable IRL, but let's test to
345
        // make sure our code can handle it.
346
        let bogus_path = TorPath {
347
            inner: TorPathInner::Path(vec![]),
348
        };
349

            
350
        assert!(bogus_path.exit_relay().is_none());
351
        assert!(bogus_path.exit_policy().is_none());
352
        assert_eq!(bogus_path.len(), 0);
353

            
354
        let owned: Result<OwnedPath> = (&bogus_path).try_into();
355
        assert!(owned.is_err());
356
    }
357

            
358
    #[test]
359
    fn no_exits() {
360
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
361
            // Construct a netdir with no exits.
362
            let netdir = testnet::construct_custom_netdir(|_idx, bld, _| {
363
                bld.md.parse_ipv4_policy("reject 1-65535").unwrap();
364
            })
365
            .unwrap()
366
            .unwrap_if_sufficient()
367
            .unwrap();
368
            let mut rng = testing_rng();
369
            let dirinfo = (&netdir).into();
370
            let statemgr = TestingStateMgr::new();
371
            let guards =
372
                tor_guardmgr::GuardMgr::new(rt.clone(), statemgr, &TestConfig::default()).unwrap();
373
            guards.install_test_netdir(&netdir);
374
            let config = PathConfig::default();
375
            let now = SystemTime::now();
376

            
377
            // With target ports
378
            let outcome = ExitPathBuilder::from_target_ports(vec![TargetPort::ipv4(80)])
379
                .pick_path(&mut rng, dirinfo, &guards, &config, now);
380
            assert!(outcome.is_err());
381
            assert!(matches!(outcome, Err(Error::NoRelay { .. })));
382

            
383
            // For any exit
384
            let outcome =
385
                ExitPathBuilder::for_any_exit().pick_path(&mut rng, dirinfo, &guards, &config, now);
386
            assert!(outcome.is_err());
387
            assert!(matches!(outcome, Err(Error::NoRelay { .. })));
388

            
389
            // For any exit (non-strict, so this will work).
390
            let outcome = ExitPathBuilder::for_timeout_testing()
391
                .pick_path(&mut rng, dirinfo, &guards, &config, now);
392
            assert!(outcome.is_ok());
393
        });
394
    }
395

            
396
    #[test]
397
    fn exitpath_with_guards() {
398
        use tor_guardmgr::GuardStatus;
399

            
400
        tor_rtcompat::test_with_all_runtimes!(|rt| async move {
401
            let netdir = testnet::construct_netdir().unwrap_if_sufficient().unwrap();
402
            let family_rules = FamilyRules::all_family_info();
403
            let mut rng = testing_rng();
404
            let dirinfo = (&netdir).into();
405
            let statemgr = TestingStateMgr::new();
406
            let guards =
407
                tor_guardmgr::GuardMgr::new(rt.clone(), statemgr, &TestConfig::default()).unwrap();
408
            let config = PathConfig::default();
409
            guards.install_test_netdir(&netdir);
410
            let port443 = TargetPort::ipv4(443);
411

            
412
            // We're going to just have these all succeed and make sure
413
            // that they pick the same guard.  We won't test failing
414
            // cases here, since those are tested in guardmgr.
415
            let mut distinct_guards = HashSet::new();
416
            let mut distinct_mid = HashSet::new();
417
            let mut distinct_exit = HashSet::new();
418
            for _ in 0..20 {
419
                let (path, mon, usable) = ExitPathBuilder::from_target_ports(vec![port443])
420
                    .pick_path(&mut rng, dirinfo, &guards, &config, rt.wallclock())
421
                    .unwrap();
422
                assert_eq!(path.len(), 3);
423
                assert_same_path_when_owned(&path);
424
                if let TorPathInner::Path(p) = path.inner {
425
                    assert_exit_path_ok(&p[..], family_rules);
426
                    distinct_guards.insert(RelayIds::from_relay_ids(&p[0]));
427
                    distinct_mid.insert(RelayIds::from_relay_ids(&p[1]));
428
                    distinct_exit.insert(RelayIds::from_relay_ids(&p[2]));
429
                } else {
430
                    panic!("Wrong kind of path");
431
                }
432
                assert!(matches!(
433
                    mon.inspect_pending_status(),
434
                    (GuardStatus::AttemptAbandoned, false)
435
                ));
436
                mon.succeeded();
437
                assert!(usable.await.unwrap());
438
            }
439
            assert_eq!(distinct_guards.len(), 1);
440
            assert_ne!(distinct_mid.len(), 1);
441
            assert_ne!(distinct_exit.len(), 1);
442
        });
443
    }
444
}