tor_hsservice/ipt_establish.rs
1//! IPT Establisher
2//!
3//! Responsible for maintaining and establishing one introduction point.
4//!
5//! TODO (#1235): move docs from `hssvc-ipt-algorithm.md`
6//!
7//! See the docs for
8//! [`IptManager::idempotently_progress_things_now`](crate::ipt_mgr::IptManager::idempotently_progress_things_now)
9//! for details of our algorithm.
10
11use crate::internal_prelude::*;
12
13use tor_cell::relaycell::{
14 hs::est_intro::{self, EstablishIntroDetails},
15 msg::IntroEstablished,
16};
17use tor_circmgr::ServiceOnionServiceIntroTunnel;
18use tor_proto::TargetHop;
19use tracing::instrument;
20
21/// Handle onto the task which is establishing and maintaining one IPT
22pub(crate) struct IptEstablisher {
23 /// A oneshot sender which, when dropped, notifies the running task that it's time to shut
24 /// down.
25 _terminate_tx: oneshot::Sender<Void>,
26
27 /// Mutable state shared with the Establisher, Reactor, and MsgHandler.
28 state: Arc<Mutex<EstablisherState>>,
29}
30
31/// When the `IptEstablisher` is dropped it is torn down
32///
33/// Synchronously
34///
35/// * No rendezvous requests will be accepted
36/// that arrived after `Drop::drop` returns.
37///
38/// Asynchronously
39///
40/// * Circuits constructed for this IPT are torn down
41/// * The `rend_reqs` sink is closed (dropped)
42/// * `IptStatusStatus::Faulty` will be indicated
43impl Drop for IptEstablisher {
44 fn drop(&mut self) {
45 // Make sure no more requests are accepted once this returns.
46 //
47 // (Note that if we didn't care about the "no more rendezvous
48 // requests will be accepted" requirement, we could do away with this
49 // code and the corresponding check for `RequestDisposition::Shutdown` in
50 // `IptMsgHandler::handle_msg`.)
51 self.state.lock().expect("poisoned lock").accepting_requests = RequestDisposition::Shutdown;
52
53 // Tell the reactor to shut down... by doing nothing.
54 //
55 // (When terminate_tx is dropped, it will send an error to the
56 // corresponding terminate_rx.)
57 }
58}
59
60/// An error from trying to work with an IptEstablisher.
61#[derive(Clone, Debug, thiserror::Error)]
62pub(crate) enum IptEstablisherError {
63 /// We encountered a faulty IPT.
64 #[error("{0}")]
65 Ipt(#[from] IptError),
66
67 /// The network directory provider is shutting down without giving us the
68 /// netdir we asked for.
69 #[error("{0}")]
70 NetdirProviderShutdown(#[from] tor_netdir::NetdirProviderShutdown),
71
72 /// We encountered an error while building a circuit to an intro point.
73 #[error("Unable to build circuit to introduction point")]
74 BuildCircuit(#[source] tor_circmgr::Error),
75
76 /// We encountered an error while querying the circuit state.
77 #[error("Unable to query circuit state")]
78 CircuitState(#[source] tor_circmgr::Error),
79
80 /// We encountered an error while building and signing our establish_intro
81 /// message.
82 #[error("Unable to construct signed ESTABLISH_INTRO message")]
83 CreateEstablishIntro(#[source] tor_cell::Error),
84
85 /// We encountered a timeout after building the circuit.
86 #[error("Timeout during ESTABLISH_INTRO handshake.")]
87 EstablishTimeout,
88
89 /// We encountered an error while sending our establish_intro
90 /// message.
91 #[error("Unable to send an ESTABLISH_INTRO message")]
92 SendEstablishIntro(#[source] tor_circmgr::Error),
93
94 /// We did not receive an INTRO_ESTABLISHED message like we wanted; instead, the
95 /// circuit was closed.
96 #[error("Circuit closed during INTRO_ESTABLISHED handshake")]
97 ClosedWithoutAck,
98
99 /// We encountered a programming error.
100 #[error("Internal error")]
101 Bug(#[from] tor_error::Bug),
102}
103
104/// An error caused by a faulty IPT.
105#[derive(Clone, Debug, thiserror::Error)]
106#[non_exhaustive]
107pub enum IptError {
108 /// When we tried to establish this introduction point, we found that the
109 /// netdir didn't list it.
110 ///
111 /// This introduction point is not strictly "faulty", but unlisted in the directory means we
112 /// can't use the introduction point.
113 #[error("Introduction point not listed in network directory")]
114 IntroPointNotListed,
115
116 /// We received an invalid INTRO_ESTABLISHED message.
117 ///
118 /// This is definitely the introduction point's fault: it sent us
119 /// an authenticated message, but the contents of that message were
120 /// definitely wrong.
121 #[error("Got an invalid INTRO_ESTABLISHED message")]
122 // Eventually, once we expect intro_established extensions, we will make
123 // sure that they are well-formed.
124 #[allow(dead_code)]
125 BadEstablished,
126
127 /// We received a message that not a valid part of the introduction-point
128 /// protocol.
129 #[error("Invalid message: {0}")]
130 BadMessage(String),
131
132 /// This introduction point has gone too long without a success.
133 #[error("introduction point has gone too long without a success")]
134 Timeout,
135}
136
137impl tor_error::HasKind for IptEstablisherError {
138 fn kind(&self) -> tor_error::ErrorKind {
139 use IptEstablisherError as E;
140 use tor_error::ErrorKind as EK;
141 match self {
142 E::Ipt(e) => e.kind(),
143 E::NetdirProviderShutdown(e) => e.kind(),
144 E::BuildCircuit(e) => e.kind(),
145 E::CircuitState(e) => e.kind(),
146 E::EstablishTimeout => EK::TorNetworkTimeout,
147 E::SendEstablishIntro(e) => e.kind(),
148 E::ClosedWithoutAck => EK::CircuitCollapse,
149 E::CreateEstablishIntro(_) => EK::Internal,
150 E::Bug(e) => e.kind(),
151 }
152 }
153}
154
155impl tor_error::HasKind for IptError {
156 fn kind(&self) -> tor_error::ErrorKind {
157 use IptError as E;
158 use tor_error::ErrorKind as EK;
159 match self {
160 E::IntroPointNotListed => EK::TorDirectoryError, // TODO (#1255) Not correct kind.
161 E::BadEstablished => EK::RemoteProtocolViolation,
162 E::BadMessage(_) => EK::RemoteProtocolViolation,
163 E::Timeout => EK::RemoteProtocolViolation, // TODO: this is not necessarily correct
164 }
165 }
166}
167
168impl IptEstablisherError {
169 /// Return the underlying [`IptError`] if this error appears to be the introduction point's fault.
170 ///
171 /// This corresponds to [`IptStatusStatus::Faulty`]`: when we return `Some`,
172 /// it means that we should try another relay as an introduction point,
173 /// though we don't necessarily need to give up on this one.
174 ///
175 /// Note that the intro point may be to blame even if we return `None`;
176 /// we only return `true` when we are certain that the intro point is
177 /// unlisted, unusable, or misbehaving.
178 fn ipt_failure(&self) -> Option<&IptError> {
179 use IptEstablisherError as IE;
180 match self {
181 // If we don't have a netdir, then no intro point is better than any other.
182 IE::NetdirProviderShutdown(_) => None,
183 IE::Ipt(e) => Some(e),
184 // This _might_ be the introduction point's fault, but it might not.
185 // We can't be certain.
186 IE::BuildCircuit(_) => None,
187 IE::CircuitState(_) => None,
188 IE::EstablishTimeout => None,
189 IE::ClosedWithoutAck => None,
190 // These are, most likely, not the introduction point's fault,
191 // though they might or might not be survivable.
192 IE::CreateEstablishIntro(_) => None,
193 IE::SendEstablishIntro(_) => None,
194 IE::Bug(_) => None,
195 }
196 }
197}
198
199/// Parameters for an introduction point
200///
201/// Consumed by `IptEstablisher::new`.
202/// Primarily serves as a convenient way to bundle the many arguments required.
203///
204/// Does not include:
205/// * The runtime (which would force this struct to have a type parameter)
206/// * The circuit builder (leaving this out makes it possible to use this
207/// struct during mock execution, where we don't call `IptEstablisher::new`).
208#[derive(Educe)]
209#[educe(Debug)]
210pub(crate) struct IptParameters {
211 /// A receiver that we can use to tell us about updates in our configuration.
212 ///
213 /// Configuration changes may tell us to change our introduction points or build new
214 /// circuits to them.
215 //
216 // TODO (#1209):
217 //
218 // We want to make a new introduction circuit if our dos parameters change,
219 // which means that we should possibly be watching for changes in our
220 // configuration. Right now, though, we only copy out the configuration
221 // on startup.
222 #[educe(Debug(ignore))]
223 pub(crate) config_rx: watch::Receiver<Arc<OnionServiceConfig>>,
224 /// A `NetDirProvider` that we'll use to find changes in the network
225 /// parameters, and to look up information about routers.
226 #[educe(Debug(ignore))]
227 pub(crate) netdir_provider: Arc<dyn NetDirProvider>,
228 /// A shared sender that we'll use to report incoming INTRODUCE2 requests
229 /// for rendezvous circuits.
230 #[educe(Debug(ignore))]
231 pub(crate) introduce_tx: mpsc::Sender<RendRequest>,
232 /// Opaque local ID for this introduction point.
233 ///
234 /// This ID does not change within the lifetime of an [`IptEstablisher`].
235 /// See [`IptLocalId`] for information about what changes would require a
236 /// new ID (and hence a new `IptEstablisher`).
237 pub(crate) lid: IptLocalId,
238 /// Persistent log for INTRODUCE2 requests.
239 ///
240 /// We use this to record the requests that we see, and to prevent replays.
241 #[educe(Debug(ignore))]
242 pub(crate) replay_log: IptReplayLog,
243 /// A set of identifiers for the Relay that we intend to use as the
244 /// introduction point.
245 ///
246 /// We use this to identify the relay within a `NetDir`, and to make sure
247 /// we're connecting to the right introduction point.
248 pub(crate) target: RelayIds,
249 /// Keypair used to authenticate and identify ourselves to this introduction
250 /// point.
251 ///
252 /// Later, we publish the public component of this keypair in our HsDesc,
253 /// and clients use it to tell the introduction point which introduction circuit
254 /// should receive their requests.
255 ///
256 /// This is the `K_hs_ipt_sid` keypair.
257 pub(crate) k_sid: Arc<HsIntroPtSessionIdKeypair>,
258 /// Whether this `IptEstablisher` should begin by accepting requests, or
259 /// wait to be told that requests are okay.
260 pub(crate) accepting_requests: RequestDisposition,
261 /// Keypair used to decrypt INTRODUCE2 requests from clients.
262 ///
263 /// This is the `K_hss_ntor` keypair, used with the "HS_NTOR" handshake to
264 /// form a shared key set of keys with the client, and decrypt information
265 /// about the client's chosen rendezvous point and extensions.
266 pub(crate) k_ntor: Arc<HsSvcNtorKeypair>,
267}
268
269impl IptEstablisher {
270 /// Try to set up, and maintain, an IPT at `target`.
271 ///
272 /// Rendezvous requests will be rejected or accepted
273 /// depending on the value of `accepting_requests`
274 /// (which must be `Advertised` or `NotAdvertised`).
275 ///
276 /// Also returns a stream of events that is produced whenever we have a
277 /// change in the IptStatus for this intro point. Note that this stream is
278 /// potentially lossy.
279 ///
280 /// The returned `watch::Receiver` will yield `Faulty` if the IPT
281 /// establisher is shut down (or crashes).
282 ///
283 /// When the resulting `IptEstablisher` is dropped, it will cancel all tasks
284 /// and close all circuits used to establish this introduction point.
285 #[instrument(level = "trace", skip_all)]
286 pub(crate) fn launch<R: Runtime>(
287 runtime: &R,
288 params: IptParameters,
289 pool: Arc<HsCircPool<R>>,
290 keymgr: &Arc<KeyMgr>,
291 ) -> Result<(Self, postage::watch::Receiver<IptStatus>), FatalError> {
292 // This exhaustive deconstruction ensures that we don't
293 // accidentally forget to handle any of our inputs.
294 let IptParameters {
295 config_rx,
296 netdir_provider,
297 introduce_tx,
298 lid,
299 target,
300 k_sid,
301 k_ntor,
302 accepting_requests,
303 replay_log,
304 } = params;
305 let config = Arc::clone(&config_rx.borrow());
306 let nickname = config.nickname().clone();
307
308 if matches!(accepting_requests, RequestDisposition::Shutdown) {
309 return Err(bad_api_usage!(
310 "Tried to create a IptEstablisher that that was already shutting down?"
311 )
312 .into());
313 }
314
315 let state = Arc::new(Mutex::new(EstablisherState { accepting_requests }));
316
317 let request_context = Arc::new(RendRequestContext {
318 nickname: nickname.clone(),
319 keymgr: Arc::clone(keymgr),
320 kp_hss_ntor: Arc::clone(&k_ntor),
321 kp_hs_ipt_sid: k_sid.as_ref().as_ref().verifying_key().into(),
322 filter: config.filter_settings(),
323 netdir_provider: netdir_provider.clone(),
324 circ_pool: pool.clone(),
325 });
326
327 let reactor = Reactor {
328 runtime: runtime.clone(),
329 nickname,
330 pool,
331 netdir_provider,
332 lid,
333 target,
334 k_sid,
335 introduce_tx,
336 extensions: EstIntroExtensionSet {
337 // Updates to this are handled by the IPT manager: when it changes,
338 // this IPT will be replaced with one with the correct parameters.
339 dos_params: config.dos_extension()?,
340 },
341 state: state.clone(),
342 request_context,
343 replay_log: Arc::new(replay_log.into()),
344 };
345
346 let (status_tx, status_rx) = postage::watch::channel_with(IptStatus::new());
347 let (terminate_tx, mut terminate_rx) = oneshot::channel::<Void>();
348 let status_tx = DropNotifyWatchSender::new(status_tx);
349
350 // Spawn a task to keep the intro established. The task will shut down
351 // when terminate_tx is dropped.
352 runtime
353 .spawn(async move {
354 futures::select_biased!(
355 terminated = terminate_rx => {
356 // Only Err is possible, but the compiler can't tell that.
357 let oneshot::Canceled = terminated.void_unwrap_err();
358 }
359 outcome = reactor.keep_intro_established(status_tx).fuse() => {
360 warn_report!(outcome.void_unwrap_err(), "Error from intro-point establisher task");
361 }
362 );
363 })
364 .map_err(|e| FatalError::Spawn {
365 spawning: "introduction point establisher",
366 cause: Arc::new(e),
367 })?;
368 let establisher = IptEstablisher {
369 _terminate_tx: terminate_tx,
370 state,
371 };
372 Ok((establisher, status_rx))
373 }
374
375 /// Begin accepting requests from this introduction point.
376 ///
377 /// If any introduction requests are sent before we have called this method,
378 /// they are treated as an error and our connection to this introduction
379 /// point is closed.
380 pub(crate) fn start_accepting(&self) {
381 self.state.lock().expect("poisoned lock").accepting_requests =
382 RequestDisposition::Advertised;
383 }
384}
385
386/// The current status of an introduction point, as defined in
387/// `hssvc-ipt-algorithms.md`.
388///
389/// TODO (#1235) Make that file unneeded.
390#[derive(Clone, Debug)]
391pub(crate) enum IptStatusStatus {
392 /// We are (re)establishing our connection to the IPT
393 ///
394 /// But we don't think there's anything wrong with it.
395 ///
396 /// The IPT manager should *not* arrange to include this in descriptors.
397 Establishing,
398
399 /// The IPT is established and ready to accept rendezvous requests
400 ///
401 /// Also contains information about the introduction point
402 /// necessary for making descriptors,
403 /// including information from the netdir about the relay
404 ///
405 /// The IPT manager *should* arrange to include this in descriptors.
406 Good(GoodIptDetails),
407
408 /// We don't have the IPT and it looks like it was the IPT's fault
409 ///
410 /// This should be used whenever trying another IPT relay is likely to work better;
411 /// regardless of whether attempts to establish *this* IPT can continue.
412 ///
413 /// The IPT manager should *not* arrange to include this in descriptors.
414 /// If this persists, the IPT manager should replace this IPT
415 /// with a new IPT at a different relay.
416 Faulty(Option<IptError>),
417}
418
419/// Details of a good introduction point
420///
421/// This struct contains similar information to
422/// [`tor_linkspec::verbatim::VerbatimLinkSpecCircTarget`].
423/// However, that insists that the contained `T` is a [`CircTarget`],
424/// which `<NtorPublicKey>` isn't.
425/// And, we don't use this as a circuit target (at least, not here -
426/// the client will do so, as a result of us publishing the information).
427///
428/// See <https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1559#note_2937974>
429#[derive(Clone, Debug, Eq, PartialEq)]
430pub(crate) struct GoodIptDetails {
431 /// The link specifiers to be used in the descriptor
432 ///
433 /// As obtained and converted from the netdir.
434 pub(crate) link_specifiers: LinkSpecs,
435
436 /// The introduction point relay's ntor key (from the netdir)
437 pub(crate) ipt_kp_ntor: NtorPublicKey,
438}
439
440impl GoodIptDetails {
441 /// Try to copy out the relevant parts of a CircTarget into a GoodIptDetails.
442 fn try_from_circ_target(relay: &impl CircTarget) -> Result<Self, IptEstablisherError> {
443 Ok(Self {
444 link_specifiers: relay
445 .linkspecs()
446 .map_err(into_internal!("Unable to encode relay link specifiers"))?,
447 ipt_kp_ntor: *relay.ntor_onion_key(),
448 })
449 }
450}
451
452/// `Err(IptWantsToRetire)` indicates that the IPT Establisher wants to retire this IPT
453///
454/// This happens when the IPT has had (too) many rendezvous requests.
455///
456/// This must *not* be used for *errors*, because it will cause the IPT manager to
457/// *immediately* start to replace the IPT, regardless of rate limits etc.
458#[derive(Clone, Debug, Eq, PartialEq)]
459pub(crate) struct IptWantsToRetire;
460
461/// State shared between the IptEstablisher and the Reactor.
462struct EstablisherState {
463 /// True if we are accepting requests right now.
464 accepting_requests: RequestDisposition,
465}
466
467/// Current state of an introduction point; determines what we want to do with
468/// any incoming messages.
469#[derive(Copy, Clone, Debug)]
470pub(crate) enum RequestDisposition {
471 /// We are not yet advertised: the message handler should complain if it
472 /// gets any requests and shut down.
473 NotAdvertised,
474 /// We are advertised: the message handler should pass along any requests
475 Advertised,
476 /// We are shutting down cleanly: the message handler should exit but not complain.
477 Shutdown,
478}
479
480/// The current status of an introduction point.
481#[derive(Clone, Debug)]
482pub(crate) struct IptStatus {
483 /// The current state of this introduction point as defined by
484 /// `hssvc-ipt-algorithms.md`.
485 ///
486 /// TODO (#1235): Make that file unneeded.
487 pub(crate) status: IptStatusStatus,
488
489 /// The current status of whether this introduction point circuit wants to be
490 /// retired based on having processed too many requests.
491 pub(crate) wants_to_retire: Result<(), IptWantsToRetire>,
492
493 /// If Some, a time after which all attempts have been unsuccessful.
494 pub(crate) failing_since: Option<Instant>,
495}
496
497/// We declare an introduction point to be faulty if all of the attempts to
498/// reach it fail, over this much time.
499///
500/// TODO: This value is more or less arbitrary; we may want to tune it in the
501/// future.
502const FAULTY_IPT_THRESHOLD: Duration = Duration::from_secs(15 * 60);
503
504impl IptStatus {
505 /// Record that we have successfully connected to an introduction point.
506 fn note_open(&mut self, ipt_details: GoodIptDetails) {
507 self.status = IptStatusStatus::Good(ipt_details);
508 self.failing_since = None;
509 }
510
511 /// Record that we are trying to connect to an introduction point.
512 fn note_attempt(&mut self) {
513 use IptStatusStatus::*;
514 self.status = match &self.status {
515 Establishing | Good(..) => Establishing,
516 Faulty(e) => Faulty(e.clone()), // We don't change status if we think we're broken.
517 }
518 }
519
520 /// Record that an error has occurred.
521 fn note_error(&mut self, err: &IptEstablisherError, now: Instant) {
522 use IptStatusStatus::*;
523 let failing_since = *self.failing_since.get_or_insert(now);
524 #[allow(clippy::if_same_then_else)]
525 if let Some(ipt_err) = err.ipt_failure() {
526 // This error always indicates a faulty introduction point.
527 self.status = Faulty(Some(ipt_err.clone()));
528 } else if now.saturating_duration_since(failing_since) >= FAULTY_IPT_THRESHOLD {
529 // This introduction point has gone too long without a success.
530 self.status = Faulty(Some(IptError::Timeout));
531 }
532 }
533
534 /// Return an `IptStatus` representing an establisher that has not yet taken
535 /// any action.
536 fn new() -> Self {
537 Self {
538 status: IptStatusStatus::Establishing,
539 wants_to_retire: Ok(()),
540 failing_since: None,
541 }
542 }
543
544 /// Produce an `IptStatus` representing a shut down or crashed establisher
545 fn new_terminated() -> Self {
546 IptStatus {
547 status: IptStatusStatus::Faulty(None),
548 // If we're broken, we simply tell the manager that that is the case.
549 // It will decide for itself whether it wants to replace us.
550 wants_to_retire: Ok(()),
551 failing_since: None,
552 }
553 }
554}
555
556impl Default for IptStatus {
557 fn default() -> Self {
558 Self::new()
559 }
560}
561
562impl tor_async_utils::DropNotifyEofSignallable for IptStatus {
563 fn eof() -> IptStatus {
564 IptStatus::new_terminated()
565 }
566}
567
568tor_cell::restricted_msg! {
569 /// An acceptable message to receive from an introduction point.
570 enum IptMsg : RelayMsg {
571 IntroEstablished,
572 Introduce2,
573 }
574}
575
576/// A set of extensions to send with our `ESTABLISH_INTRO` message.
577///
578/// NOTE: we eventually might want to support unrecognized extensions. But
579/// that's potentially troublesome, since the set of extensions we sent might
580/// have an affect on how we validate the reply.
581#[derive(Clone, Debug)]
582pub(crate) struct EstIntroExtensionSet {
583 /// Parameters related to rate-limiting to prevent denial-of-service
584 /// attacks.
585 dos_params: Option<est_intro::DosParams>,
586}
587
588/// Implementation structure for the task that implements an IptEstablisher.
589struct Reactor<R: Runtime> {
590 /// A copy of our runtime, used for timeouts and sleeping.
591 runtime: R,
592 /// The nickname of the onion service we're running. Used when logging.
593 nickname: HsNickname,
594 /// A pool used to create circuits to the introduction point.
595 pool: Arc<HsCircPool<R>>,
596 /// A provider used to select the other relays in the circuit.
597 netdir_provider: Arc<dyn NetDirProvider>,
598 /// Identifier for the intro point.
599 lid: IptLocalId,
600 /// The target introduction point.
601 target: RelayIds,
602 /// The keypair to use when establishing the introduction point.
603 ///
604 /// Knowledge of this private key prevents anybody else from impersonating
605 /// us to the introduction point.
606 k_sid: Arc<HsIntroPtSessionIdKeypair>,
607 /// The extensions to use when establishing the introduction point.
608 ///
609 /// TODO (#1209): This should be able to change over time as we re-establish
610 /// the intro point.
611 extensions: EstIntroExtensionSet,
612
613 /// The stream that will receive INTRODUCE2 messages.
614 introduce_tx: mpsc::Sender<RendRequest>,
615
616 /// Mutable state shared with the Establisher, Reactor, and MsgHandler.
617 state: Arc<Mutex<EstablisherState>>,
618
619 /// Context information that we'll need to answer rendezvous requests.
620 request_context: Arc<RendRequestContext>,
621
622 /// Introduction request replay log
623 ///
624 /// Shared between multiple IPT circuit control message handlers -
625 /// [`IptMsgHandler`] contains the lock guard.
626 ///
627 /// Has to be an async mutex since it's locked for a long time,
628 /// so we mustn't block the async executor thread on it.
629 replay_log: Arc<futures::lock::Mutex<IptReplayLog>>,
630}
631
632/// An open session with a single introduction point.
633//
634// TODO: I've used Ipt and IntroPt in this module; maybe we shouldn't.
635pub(crate) struct IntroPtSession {
636 /// The circuit to the introduction point, on which we're receiving
637 /// Introduce2 messages.
638 intro_tunnel: Arc<ServiceOnionServiceIntroTunnel>,
639}
640
641impl<R: Runtime> Reactor<R> {
642 /// Run forever, keeping an introduction point established.
643 #[allow(clippy::blocks_in_conditions)]
644 #[instrument(level = "trace", skip_all)]
645 async fn keep_intro_established(
646 &self,
647 mut status_tx: DropNotifyWatchSender<IptStatus>,
648 ) -> Result<Void, IptEstablisherError> {
649 let mut retry_delay = tor_basic_utils::retry::RetryDelay::from_msec(1000);
650 loop {
651 status_tx.borrow_mut().note_attempt();
652 match self.establish_intro_once().await {
653 Ok((session, good_ipt_details)) => {
654 // TODO (#1239): we need to monitor the netdir for changes to this relay
655 // Eg,
656 // - if it becomes unlisted, we should declare the IPT faulty
657 // (until it perhaps reappears)
658 //
659 // TODO SPEC Continuing to use an unlisted relay is dangerous
660 // It might be malicious. We should withdraw our IPT then,
661 // and hope that clients find another, working, IPT.
662 //
663 // - if it changes its ntor key or link specs,
664 // we need to update the GoodIptDetails in our status report,
665 // so that the updated info can make its way to the descriptor
666 //
667 // Possibly some this could/should be done by the IPT Manager instead,
668 // but Diziet thinks it is probably cleanest to do it here.
669
670 status_tx.borrow_mut().note_open(good_ipt_details);
671
672 debug!(
673 "{}: Successfully established introduction point with {}",
674 &self.nickname,
675 self.target.display_relay_ids().redacted()
676 );
677 // Now that we've succeeded, we can stop backing off for our
678 // next attempt.
679 retry_delay.reset();
680
681 // Wait for the session to be closed.
682 session.wait_for_close().await;
683 }
684 Err(e @ IptEstablisherError::Ipt(IptError::IntroPointNotListed)) => {
685 // The network directory didn't include this relay. Wait
686 // until it does.
687 //
688 // Note that this `note_error` will necessarily mark the
689 // ipt as Faulty. That's important, since we may be about to
690 // wait indefinitely when we call wait_for_netdir_to_list.
691 status_tx.borrow_mut().note_error(&e, self.runtime.now());
692 self.netdir_provider
693 .wait_for_netdir_to_list(&self.target, Timeliness::Timely)
694 .await?;
695 }
696 Err(e) => {
697 status_tx.borrow_mut().note_error(&e, self.runtime.now());
698 debug_report!(
699 e,
700 "{}: Problem establishing introduction point with {}",
701 &self.nickname,
702 self.target.display_relay_ids().redacted()
703 );
704 let retry_after = retry_delay.next_delay(&mut rand::rng());
705 self.runtime.sleep(retry_after).await;
706 }
707 }
708 }
709 }
710
711 /// Try, once, to make a circuit to a single relay and establish an introduction
712 /// point there.
713 ///
714 /// Does not retry. Does not time out except via `HsCircPool`.
715 #[instrument(level = "trace", skip_all)]
716 async fn establish_intro_once(
717 &self,
718 ) -> Result<(IntroPtSession, GoodIptDetails), IptEstablisherError> {
719 let (protovers, tunnel, ipt_details) = {
720 let netdir = self
721 .netdir_provider
722 .wait_for_netdir(tor_netdir::Timeliness::Timely)
723 .await?;
724 let circ_target = netdir
725 .by_ids(&self.target)
726 .ok_or(IptError::IntroPointNotListed)?;
727 let ipt_details = GoodIptDetails::try_from_circ_target(&circ_target)?;
728
729 let protovers = circ_target.protovers().clone();
730 let tunnel = self
731 .pool
732 .get_or_launch_svc_intro(netdir.as_ref(), circ_target)
733 .await
734 .map_err(IptEstablisherError::BuildCircuit)?;
735 // note that netdir is dropped here, to avoid holding on to it any
736 // longer than necessary.
737 (protovers, tunnel, ipt_details)
738 };
739
740 let establish_intro = {
741 let ipt_sid_id = (*self.k_sid).as_ref().verifying_key().into();
742 let mut details = EstablishIntroDetails::new(ipt_sid_id);
743 if let Some(dos_params) = &self.extensions.dos_params {
744 // We only send the Dos extension when the relay is known to
745 // support it.
746 use tor_protover::named::HSINTRO_RATELIM;
747 if protovers.supports_named_subver(HSINTRO_RATELIM) {
748 details.set_extension_dos(dos_params.clone());
749 }
750 }
751 let circuit_binding_key = tunnel
752 .binding_key(TargetHop::LastHop)
753 .await
754 .map_err(IptEstablisherError::CircuitState)?
755 .ok_or(internal!("No binding key for introduction point!?"))?;
756 let body: Vec<u8> = details
757 .sign_and_encode((*self.k_sid).as_ref(), circuit_binding_key.hs_mac())
758 .map_err(IptEstablisherError::CreateEstablishIntro)?;
759
760 // TODO: This is ugly, but it is the sensible way to munge the above
761 // body into a format that AnyRelayMsgOuter will accept without doing a
762 // redundant parse step.
763 //
764 // One alternative would be allowing start_conversation to take an `impl
765 // RelayMsg` rather than an AnyRelayMsg.
766 //
767 // Or possibly, when we feel like it, we could rename one or more of
768 // these "Unrecognized"s to Unparsed or Uninterpreted. If we do that, however, we'll
769 // potentially face breaking changes up and down our crate stack.
770 AnyRelayMsg::Unrecognized(tor_cell::relaycell::msg::Unrecognized::new(
771 tor_cell::relaycell::RelayCmd::ESTABLISH_INTRO,
772 body,
773 ))
774 };
775
776 let (established_tx, established_rx) = oneshot::channel();
777
778 // In theory there ought to be only one IptMsgHandler in existence at any one time,
779 // for any one IptLocalId (ie for any one IptReplayLog). However, the teardown
780 // arrangements are (i) complicated (so might have bugs) and (ii) asynchronous
781 // (so we need to synchronise). Therefore:
782 //
783 // Make sure we don't start writing to the replay log until any previous
784 // IptMsgHandler has been torn down. (Using an async mutex means we
785 // don't risk blocking the whole executor even if we have teardown bugs.)
786 let replay_log = self.replay_log.clone().lock_owned().await;
787
788 let handler = IptMsgHandler {
789 established_tx: Some(established_tx),
790 introduce_tx: self.introduce_tx.clone(),
791 state: self.state.clone(),
792 lid: self.lid,
793 request_context: self.request_context.clone(),
794 replay_log,
795 };
796 let _conversation = tunnel
797 .start_conversation(Some(establish_intro), handler, TargetHop::LastHop)
798 .await
799 .map_err(IptEstablisherError::SendEstablishIntro)?;
800 // At this point, we have `await`ed for the Conversation to exist, so we know
801 // that the message was sent. We have to wait for any actual `established`
802 // message, though.
803
804 let length = tunnel
805 .n_hops()
806 .map_err(into_internal!("failed to get circuit length"))?;
807 let ack_timeout = self
808 .pool
809 .estimate_timeout(&tor_circmgr::timeouts::Action::RoundTrip { length });
810 let _established: IntroEstablished = self
811 .runtime
812 .timeout(ack_timeout, established_rx)
813 .await
814 .map_err(|_| IptEstablisherError::EstablishTimeout)?
815 .map_err(|_| IptEstablisherError::ClosedWithoutAck)??;
816
817 // This session will be owned by keep_intro_established(), and dropped
818 // when the circuit closes, or when the keep_intro_established() future
819 // is dropped.
820 let session = IntroPtSession {
821 intro_tunnel: tunnel.into(),
822 };
823 Ok((session, ipt_details))
824 }
825}
826
827impl IntroPtSession {
828 /// Wait for this introduction point session to be closed.
829 fn wait_for_close(&self) -> impl Future<Output = ()> + use<> {
830 self.intro_tunnel.wait_for_close()
831 }
832}
833
834/// MsgHandler type to implement a conversation with an introduction point.
835///
836/// This, like all MsgHandlers, is installed at the circuit's reactor, and used
837/// to handle otherwise unrecognized message types.
838struct IptMsgHandler {
839 /// A oneshot sender used to report our IntroEstablished message.
840 ///
841 /// If this is None, then we already sent an IntroEstablished and we shouldn't
842 /// send any more.
843 established_tx: Option<oneshot::Sender<Result<IntroEstablished, IptEstablisherError>>>,
844
845 /// A channel used to report Introduce2 messages.
846 introduce_tx: mpsc::Sender<RendRequest>,
847
848 /// Keys that we'll need to answer the introduction requests.
849 request_context: Arc<RendRequestContext>,
850
851 /// Mutable state shared with the Establisher, Reactor, and MsgHandler.
852 state: Arc<Mutex<EstablisherState>>,
853
854 /// Unique identifier for the introduction point (including the current
855 /// keys). Used to tag requests.
856 lid: IptLocalId,
857
858 /// A replay log used to detect replayed introduction requests.
859 replay_log: futures::lock::OwnedMutexGuard<IptReplayLog>,
860}
861
862impl tor_proto::MsgHandler for IptMsgHandler {
863 fn handle_msg(&mut self, any_msg: AnyRelayMsg) -> tor_proto::Result<MetaCellDisposition> {
864 let msg: IptMsg = any_msg.try_into().map_err(|m: AnyRelayMsg| {
865 if let Some(tx) = self.established_tx.take() {
866 let _ = tx.send(Err(IptError::BadMessage(format!(
867 "Invalid message type {}",
868 m.cmd()
869 ))
870 .into()));
871 }
872 // TODO: It's not completely clear whether CircProto is the right
873 // type for use in this function (here and elsewhere);
874 // possibly, we should add a different tor_proto::Error type
875 // for protocol violations at a higher level than the circuit
876 // protocol.
877 //
878 // For now, however, this error type is fine: it will cause the
879 // circuit to be shut down, which is what we want.
880 tor_proto::Error::CircProto(format!(
881 "Invalid message type {} on introduction circuit",
882 m.cmd()
883 ))
884 })?;
885
886 if match msg {
887 IptMsg::IntroEstablished(established) => match self.established_tx.take() {
888 Some(tx) => {
889 // TODO: Once we want to enforce any properties on the
890 // intro_established message (like checking for correct
891 // extensions) we should do it here.
892 let established = Ok(established);
893 tx.send(established).map_err(|_| ())
894 }
895 None => {
896 return Err(tor_proto::Error::CircProto(
897 "Received a redundant INTRO_ESTABLISHED".into(),
898 ));
899 }
900 },
901 IptMsg::Introduce2(introduce2) => {
902 if let Some(tx) = self.established_tx.take() {
903 let _ = tx.send(Err(IptError::BadMessage(
904 "INTRODUCE2 message without INTRO_ESTABLISHED.".to_string(),
905 )
906 .into()));
907 return Err(tor_proto::Error::CircProto(
908 "Received an INTRODUCE2 message before INTRO_ESTABLISHED".into(),
909 ));
910 }
911 let disp = self.state.lock().expect("poisoned lock").accepting_requests;
912 match disp {
913 RequestDisposition::NotAdvertised => {
914 return Err(tor_proto::Error::CircProto(
915 "Received an INTRODUCE2 message before we were accepting requests!"
916 .into(),
917 ));
918 }
919 RequestDisposition::Shutdown => return Ok(MetaCellDisposition::CloseCirc),
920 RequestDisposition::Advertised => {}
921 }
922 match self.replay_log.check_for_replay(&introduce2) {
923 Ok(()) => {}
924 Err(ReplayError::AlreadySeen) => {
925 // This is probably a replay, but maybe an accident. We
926 // just drop the request.
927
928 // TODO (#1233): Log that this has occurred, with a rate
929 // limit. Possibly, we should allow it to fail once or
930 // twice per circuit before we log, since we expect
931 // a nonzero false-positive rate.
932 //
933 // Note that we should NOT close the circuit in this
934 // case: the repeated message could come from a hostile
935 // introduction point trying to do traffic analysis, but
936 // it could also come from a user trying to make it look
937 // like the intro point is doing traffic analysis.
938 return Ok(MetaCellDisposition::Consumed);
939 }
940 Err(ReplayError::Log(_)) => {
941 // Uh-oh! We failed to write the data persistently!
942 //
943 // TODO (#1226): We need to decide what to do here. Right
944 // now we close the circuit, which is wrong.
945 return Ok(MetaCellDisposition::CloseCirc);
946 }
947 }
948
949 let request = RendRequest::new(self.lid, introduce2, self.request_context.clone());
950 let send_outcome = self.introduce_tx.try_send(request);
951
952 // We only want to report full-stream problems as errors here.
953 // Disconnected streams are expected.
954 let report_outcome = match &send_outcome {
955 Err(e) if e.is_full() => Err(StreamWasFull {}),
956 _ => Ok(()),
957 };
958 // TODO: someday we might want to start tracking this by
959 // introduction or service point separately, though we would
960 // expect their failures to be correlated.
961 log_ratelim!("sending rendezvous request to handler task"; report_outcome);
962
963 match send_outcome {
964 Ok(()) => Ok(()),
965 Err(e) => {
966 if e.is_disconnected() {
967 // The receiver is disconnected, meaning that
968 // messages from this intro point are no longer
969 // wanted. Close the circuit.
970 Err(())
971 } else {
972 // The receiver is full; we have no real option but
973 // to drop the request like C-tor does when the
974 // backlog is too large.
975 //
976 // See discussion at
977 // https://gitlab.torproject.org/tpo/core/arti/-/merge_requests/1465#note_2928349
978 Ok(())
979 }
980 }
981 }
982 }
983 } == Err(())
984 {
985 // If the above return an error, we failed to send. That means that
986 // we need to close the circuit, since nobody is listening on the
987 // other end of the tx.
988 return Ok(MetaCellDisposition::CloseCirc);
989 }
990
991 Ok(MetaCellDisposition::Consumed)
992 }
993}
994
995/// We failed to send a rendezvous request onto the handler test that should
996/// have handled it, because it was not handling requests fast enough.
997///
998/// (This is a separate type so that we can have it implement Clone.)
999#[derive(Clone, Debug, thiserror::Error)]
1000#[error("Could not send request; stream was full.")]
1001struct StreamWasFull {}