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

Ignore PTPv1 packets #533

Merged
merged 2 commits into from
Sep 20, 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
8 changes: 6 additions & 2 deletions statime-linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use statime::{
config::{ClockIdentity, InstanceConfig, SdoId, TimePropertiesDS, TimeSource},
filters::{Filter, KalmanConfiguration, KalmanFilter},
port::{
InBmca, Measurement, Port, PortAction, PortActionIterator, TimestampContext, MAX_DATA_LEN,
is_message_buffer_compatible, InBmca, Measurement, Port, PortAction, PortActionIterator,
TimestampContext, MAX_DATA_LEN,
},
time::Time,
PtpInstance, PtpInstanceState,
Expand Down Expand Up @@ -506,7 +507,10 @@ async fn port_task<A: NetworkAddress + PtpTargetAddress>(
let mut actions = tokio::select! {
result = event_socket.recv(&mut event_buffer) => match result {
Ok(packet) => {
if let Some(mut timestamp) = packet.timestamp {
if !is_message_buffer_compatible(&event_buffer[..packet.bytes_read]) {
// do not spam with missing timestamp error in mixed-version PTPv1+v2 networks
PortActionIterator::empty()
} else if let Some(mut timestamp) = packet.timestamp {
// get_tai gives zero if this is a hardware clock, and the needed
// correction when this port uses software timestamping
timestamp.seconds += clock.get_tai_offset().expect("Unable to get tai offset") as i64;
Expand Down
7 changes: 7 additions & 0 deletions statime/src/datastructures/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ fn base_header(
}
}

/// Checks whether message is of PTP revision compatible with Statime
pub fn is_compatible(buffer: &[u8]) -> bool {
// this ensures that versionPTP in the header is 2
// it will never happen in PTPv1 packets because this octet is the LSB of versionPTP there
(buffer.len() >= 2) && (buffer[1] & 0xF) == 2
}

impl Message<'_> {
pub(crate) fn sync(
default_ds: &InternalDefaultDS,
Expand Down
5 changes: 5 additions & 0 deletions statime/src/port/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rand::Rng;
use state::PortState;

use self::sequence_id::SequenceIdGenerator;
pub use crate::datastructures::messages::is_compatible as is_message_buffer_compatible;
pub use crate::datastructures::messages::MAX_DATA_LEN;
#[cfg(doc)]
use crate::PtpInstance;
Expand Down Expand Up @@ -437,6 +438,10 @@ impl<'a, A: AcceptableMasterList, C: Clock, F: Filter, R: Rng, S: PtpInstanceSta
&mut self,
data: &'b [u8],
) -> ControlFlow<PortActionIterator<'b>, Message<'b>> {
if !is_message_buffer_compatible(data) {
// do not spam with parse error in mixed-version PTPv1+v2 networks
return ControlFlow::Break(actions![]);
}
let message = match Message::deserialize(data) {
Ok(message) => message,
Err(error) => {
Expand Down
Loading