Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(s2n-quic-dc): Handle secret control packets in dc Endpoint #2262

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion dc/s2n-quic-dc/src/path/secret/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
use rand::Rng as _;
use s2n_codec::EncoderBuffer;
use s2n_quic_core::{
dc::{self, ApplicationParams},
dc::{self, ApplicationParams, DatagramInfo},
ensure,
event::api::EndpointType,
};
use std::{
Expand Down Expand Up @@ -782,6 +783,30 @@ impl dc::Endpoint for Map {
fn new_path(&mut self, connection_info: &dc::ConnectionInfo) -> Option<Self::Path> {
Some(HandshakingPath::new(connection_info, self.clone()))
}

fn on_possible_secret_control_packet(
&mut self,
// TODO: Maybe we should confirm that the sender IP at least matches the IP for the
// corresponding control secret?
_datagram_info: &DatagramInfo,
payload: &mut [u8],
) -> bool {
let payload = s2n_codec::DecoderBufferMut::new(payload);
// TODO: Is 16 always right?
WesleyRosenblum marked this conversation as resolved.
Show resolved Hide resolved
return match control::Packet::decode(payload, 16) {
Ok((packet, tail)) => {
// Probably a bug somewhere? There shouldn't be anything trailing in the buffer
// after we decode a secret control packet.
ensure!(tail.is_empty(), false);

// If we successfully decoded a control packet, pass it into our map to handle.
self.handle_control_packet(&packet);

true
}
Err(_) => false,
};
}
}

impl dc::Path for HandshakingPath {
Expand Down
19 changes: 19 additions & 0 deletions quic/s2n-quic-core/src/dc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ impl<'a> ConnectionInfo<'a> {
}
}

/// Information about a received datagram that may be used
/// when parsing it for a secret control packet
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DatagramInfo<'a> {
/// The address (IP + Port) of the remote peer
pub remote_address: SocketAddress<'a>,
}

impl<'a> DatagramInfo<'a> {
#[inline]
#[doc(hidden)]
pub fn new(remote_address: &'a inet::SocketAddress) -> Self {
Self {
remote_address: remote_address.into_event(),
}
}
}

/// Various settings relevant to the dc path
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
Expand Down
10 changes: 9 additions & 1 deletion quic/s2n-quic-core/src/dc/disabled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
crypto::tls::TlsSession,
dc::{ConnectionInfo, Endpoint, Path},
dc::{ConnectionInfo, DatagramInfo, Endpoint, Path},
stateless_reset, transport,
};
use alloc::vec::Vec;
Expand All @@ -19,6 +19,14 @@ impl Endpoint for Disabled {
fn new_path(&mut self, _connection_info: &ConnectionInfo) -> Option<Self::Path> {
None
}

fn on_possible_secret_control_packet(
&mut self,
_datagram_info: &DatagramInfo,
_payload: &mut [u8],
) -> bool {
unreachable!()
}
}

// The Disabled Endpoint returns `None`, so this is not used
Expand Down
10 changes: 9 additions & 1 deletion quic/s2n-quic-core/src/dc/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
crypto::tls::TlsSession,
dc,
dc::{ApplicationParams, ConnectionInfo},
dc::{ApplicationParams, ConnectionInfo, DatagramInfo},
stateless_reset, transport,
varint::VarInt,
};
Expand Down Expand Up @@ -39,6 +39,14 @@ impl dc::Endpoint for MockDcEndpoint {
..Default::default()
})
}

fn on_possible_secret_control_packet(
&mut self,
_datagram_info: &DatagramInfo,
_payload: &mut [u8],
) -> bool {
false
}
}

impl dc::Path for MockDcPath {
Expand Down
10 changes: 10 additions & 0 deletions quic/s2n-quic-core/src/dc/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ pub trait Endpoint: 'static + Send {
///
/// Return `None` if dc should not be used for this path
fn new_path(&mut self, connection_info: &dc::ConnectionInfo) -> Option<Self::Path>;

/// Called when a datagram arrives that cannot be decoded as a non-DC QUIC packet, and
/// thus may contain a secret control packet
///
/// Return `true` if a secret control packet was decoded from the datagram, `false` otherwise
fn on_possible_secret_control_packet(
&mut self,
datagram_info: &dc::DatagramInfo,
payload: &mut [u8],
) -> bool;
}

/// A dc path
Expand Down
10 changes: 9 additions & 1 deletion quic/s2n-quic-transport/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,14 @@ impl<Cfg: Config> Endpoint<Cfg> {
endpoint_context.connection_id_format,
) {
(packet, remaining)
} else if Cfg::DcEndpoint::ENABLED
&& endpoint_context
.dc
.on_possible_secret_control_packet(&dc::DatagramInfo::new(&remote_address), payload)
{
// This was a DC secret control packet, so we don't need to proceed
// with checking for a stateless reset
return;
} else {
//= https://www.rfc-editor.org/rfc/rfc9000#section-5.2.2
//# Servers MUST drop incoming packets under all other circumstances.
Expand All @@ -484,7 +492,7 @@ impl<Cfg: Config> Endpoint<Cfg> {
len: payload_len as u16,
reason: event::builder::DatagramDropReason::DecodingFailed,
});
}
};

return;
};
Expand Down
Loading