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

sys::socket adding Linux packet filtering on ipv4/ipv6/loopback traffics #2581

Merged
merged 1 commit into from
Jan 12, 2025

Conversation

devnexen
Copy link
Contributor

Respectively sys::socket::SockProtocol::EthIp,
sys::socket::SockProtocol::EthIpv6 and
sys::socket::SockProtocol::EthLoop if we want more refined filtering as opposed to the existing sys::socket::SockProtocol::EthAll which captures everything.

@@ -241,6 +247,12 @@ impl SockProtocol {
#[cfg(apple_targets)]
#[allow(non_upper_case_globals)]
pub const KextEvent: SockProtocol = SockProtocol::Icmp; // Matches libc::SYSPROTO_EVENT

/// Packet filter on IPv4 traffic
/// NOTE: placed here due to conflict (little endian arch) with SockProtocol::NetLinkISCI
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SteveLauC let me know which direction you prefer me to take here. e.g. using this for all these Eth* flags to avoid future conflicts, not using it all ...

Copy link
Member

@SteveLauC SteveLauC Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current approach looks good, i.e., add them via associated constants when we see conflicts

@SteveLauC SteveLauC self-requested a review January 11, 2025 12:23
@@ -241,6 +247,12 @@ impl SockProtocol {
#[cfg(apple_targets)]
#[allow(non_upper_case_globals)]
pub const KextEvent: SockProtocol = SockProtocol::Icmp; // Matches libc::SYSPROTO_EVENT

/// Packet filter on IPv4 traffic
/// NOTE: placed here due to conflict (little endian arch) with SockProtocol::NetLinkISCI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// NOTE: placed here due to conflict (little endian arch) with SockProtocol::NetLinkISCI
// NOTE: placed here due to conflict (little endian arch) with SockProtocol::NetLinkISCI

This should be a code comment, not a doc comment.

conflict (little endian arch)

BTW, wouldn't this conflict always occur regardless of the byte order since they have the same value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, wouldn't this conflict always occur regardless of the byte order since they have the same value

I was wrong, they only have the same value on Little-endian machines. With big-endian, EthIp is 2048

@SteveLauC
Copy link
Member

Looks like we need to do conditional compilation to fix the UB issue that fails our CI:

    /// Packet filter on IPv4 traffic
    // NOTE: placed here due to conflict (little endian arch) with `SockProtocol::NetLinkISCI`
    #[cfg(linux_android)]
    #[allow(non_upper_case_globals)]
    #[cfg(target_endian = "little")]
    pub const EthIp: SockProtocol = unsafe { std::mem::transmute::<i32, SockProtocol>((libc::ETH_P_IP as u16).to_be() as i32) };
    
     /// Packet filter on IPv4 traffic
    #[cfg(linux_android)]
    #[cfg(target_endian = "big")]
    EthIp = (libc::ETH_P_IP as u16).to_be() as i32,

@devnexen devnexen force-pushed the linux_ethernet_proto_af_packet branch from 5e91463 to 9a7b5b2 Compare January 12, 2025 06:03
@devnexen devnexen marked this pull request as ready for review January 12, 2025 06:56
Comment on lines 258 to 262
/// Packet filter on IPv4 traffic
#[cfg(linux_android)]
#[allow(non_upper_case_globals)]
#[cfg(target_endian = "big")]
pub const EthIp: i32 = (libc::ETH_P_IP as u16).to_be() as i32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being unclear, this should be an enum variant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On big-endian targets, no enum variant has value 2048, so that std::mem::transmute() is UB, which is the reason why I said we should make it an enum variant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it should work now

@devnexen devnexen marked this pull request as draft January 12, 2025 07:05
@devnexen devnexen force-pushed the linux_ethernet_proto_af_packet branch 5 times, most recently from 0615efd to fb48acb Compare January 12, 2025 07:58
Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@SteveLauC SteveLauC marked this pull request as ready for review January 12, 2025 08:00
Respectively `sys::socket::SockProtocol::EthIp`,
`sys::socket::SockProtocol::EthIpv6` and
`sys::socket::SockProtocol::EthLoop` if we want more refined
filtering as opposed to the existing `sys::socket::SockProtocol::EthAll`
which captures everything.
@devnexen devnexen force-pushed the linux_ethernet_proto_af_packet branch from fb48acb to fbe50dc Compare January 12, 2025 08:05
@SteveLauC SteveLauC enabled auto-merge January 12, 2025 08:49
@SteveLauC SteveLauC added this pull request to the merge queue Jan 12, 2025
Merged via the queue into nix-rust:master with commit 34bb802 Jan 12, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants