From 6a003d14577ffdafa4e9343e67efdf4cf039e876 Mon Sep 17 00:00:00 2001 From: Martin Thomson Date: Wed, 28 Oct 2020 17:31:57 +1100 Subject: [PATCH 1/2] Defer processing of saved datagrams This ensures that we don't jump into handling Handshake packets before we have finished processing the current datagram. This doesn't do much, but the moved assertion in start_handshake() should highlight why this was not great. Without the changes, that assertion fails. --- neqo-transport/src/connection/mod.rs | 40 +++++++++++++++++--------- neqo-transport/src/connection/saved.rs | 19 ++++++++++-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/neqo-transport/src/connection/mod.rs b/neqo-transport/src/connection/mod.rs index 18c1061b94..ee5e80ddf0 100644 --- a/neqo-transport/src/connection/mod.rs +++ b/neqo-transport/src/connection/mod.rs @@ -713,12 +713,18 @@ impl Connection { self.crypto.tls.peer_certificate() } - /// Call by application when the peer cert has been verified + /// Call by application when the peer cert has been verified. + /// + /// This panics if there is no active peer. It's OK to call this + /// when authentication isn't needed, that will likely only cause + /// the connection to fail. However, if no packets have been + /// exchanged, it's not OK. pub fn authenticated(&mut self, status: AuthenticationStatus, now: Instant) { qinfo!([self], "Authenticated {:?}", status); self.crypto.tls.authenticated(status); let res = self.handshake(now, PNSpace::Handshake, None); self.absorb_error(now, res); + self.process_saved(now); } /// Get the role of the connection. @@ -828,11 +834,11 @@ impl Connection { } } - /// Call in to process activity on the connection. Either new packets have - /// arrived or a timeout has expired (or both). + /// Process new input datagrams on the connection. pub fn process_input(&mut self, d: Datagram, now: Instant) { let res = self.input(d, now); self.absorb_error(now, res); + self.process_saved(now); self.cleanup_streams(); } @@ -925,6 +931,7 @@ impl Connection { if let Some(d) = dgram { let res = self.input(d, now); self.absorb_error(now, res); + self.process_saved(now); } self.process_output(now) } @@ -1030,12 +1037,16 @@ impl Connection { } } - /// Process any saved packets if the appropriate keys are available. - fn process_saved(&mut self, cspace: CryptoSpace) { - if self.crypto.states.rx_hp(cspace).is_some() { - for saved in self.saved_datagrams.take_saved(cspace) { - qtrace!([self], "process saved @{:?}: {:?}", saved.t, saved.d); - self.process_input(saved.d, saved.t); + /// Process any saved datagrams that might be available for processing. + fn process_saved(&mut self, now: Instant) { + while let Some(cspace) = self.saved_datagrams.available() { + qdebug!([self], "process saved for space {:?}", cspace); + if self.crypto.states.rx_hp(cspace).is_some() { + for saved in self.saved_datagrams.take_saved() { + qtrace!([self], "input saved @{:?}: {:?}", saved.t, saved.d); + let res = self.input(saved.d, saved.t); + self.absorb_error(now, res); + } } } } @@ -1339,10 +1350,11 @@ impl Connection { } fn start_handshake(&mut self, packet: &PublicPacket, d: &Datagram) -> Res<()> { + qtrace!([self], "starting handshake"); + debug_assert_eq!(packet.packet_type(), PacketType::Initial); self.remote_initial_source_cid = Some(ConnectionId::from(packet.scid())); - if self.role == Role::Server { - assert_eq!(packet.packet_type(), PacketType::Initial); + if self.role == Role::Server { // A server needs to accept the client's selected CID during the handshake. self.valid_cids.push(ConnectionId::from(packet.dcid())); // Install a path. @@ -1364,6 +1376,7 @@ impl Connection { .expect("should have a path for sending Initial"); p.set_remote_cid(packet.scid()); } + self.set_state(State::Handshaking); Ok(()) } @@ -1886,7 +1899,7 @@ impl Connection { self.set_initial_limits(); } if self.crypto.install_keys(self.role)? { - self.process_saved(CryptoSpace::Handshake); + self.saved_datagrams.make_available(CryptoSpace::Handshake); } } @@ -2282,7 +2295,8 @@ impl Connection { self.process_tps()?; self.set_state(State::Connected); self.create_resumption_token(now); - self.process_saved(CryptoSpace::ApplicationData); + self.saved_datagrams + .make_available(CryptoSpace::ApplicationData); self.stats.borrow_mut().resumed = self.crypto.tls.info().unwrap().resumed(); if self.role == Role::Server { self.state_signaling.handshake_done(); diff --git a/neqo-transport/src/connection/saved.rs b/neqo-transport/src/connection/saved.rs index 10ee9e0cf7..b2da0c644a 100644 --- a/neqo-transport/src/connection/saved.rs +++ b/neqo-transport/src/connection/saved.rs @@ -29,6 +29,7 @@ pub struct SavedDatagram { pub struct SavedDatagrams { handshake: Vec, application_data: Vec, + available: Option, } impl SavedDatagrams { @@ -51,7 +52,21 @@ impl SavedDatagrams { } } - pub fn take_saved(&mut self, cspace: CryptoSpace) -> Vec { - mem::take(self.store(cspace)) + pub fn make_available(&mut self, cspace: CryptoSpace) { + debug_assert_ne!(cspace, CryptoSpace::ZeroRtt); + debug_assert_ne!(cspace, CryptoSpace::Initial); + if !self.store(cspace).is_empty() { + self.available = Some(cspace); + } + } + + pub fn available(&self) -> Option { + self.available + } + + pub fn take_saved(&mut self) -> Vec { + self.available + .take() + .map_or_else(Vec::new, |cspace| mem::take(self.store(cspace))) } } From e90504791e7961ddf78f611d6ba3e33914c020a5 Mon Sep 17 00:00:00 2001 From: Martin Thomson Date: Fri, 30 Oct 2020 11:12:54 +1100 Subject: [PATCH 2/2] Don't check that we have keys, but assert it --- neqo-transport/src/connection/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/neqo-transport/src/connection/mod.rs b/neqo-transport/src/connection/mod.rs index ee5e80ddf0..3f08d3f63b 100644 --- a/neqo-transport/src/connection/mod.rs +++ b/neqo-transport/src/connection/mod.rs @@ -1041,12 +1041,11 @@ impl Connection { fn process_saved(&mut self, now: Instant) { while let Some(cspace) = self.saved_datagrams.available() { qdebug!([self], "process saved for space {:?}", cspace); - if self.crypto.states.rx_hp(cspace).is_some() { - for saved in self.saved_datagrams.take_saved() { - qtrace!([self], "input saved @{:?}: {:?}", saved.t, saved.d); - let res = self.input(saved.d, saved.t); - self.absorb_error(now, res); - } + debug_assert!(self.crypto.states.rx_hp(cspace).is_some()); + for saved in self.saved_datagrams.take_saved() { + qtrace!([self], "input saved @{:?}: {:?}", saved.t, saved.d); + let res = self.input(saved.d, saved.t); + self.absorb_error(now, res); } } }