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

adding ethhdr type for linux/android for proper packet filtering. #4239

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

devnexen
Copy link
Contributor

No description provided.

@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot
Copy link
Collaborator

rustbot commented Jan 12, 2025

Some changes occurred in the Android module

cc @maurer

@devnexen devnexen force-pushed the ethhdr_linux branch 5 times, most recently from 55f537d to d478bbb Compare January 12, 2025 12:55
@devnexen devnexen marked this pull request as draft January 12, 2025 13:00
@devnexen devnexen force-pushed the ethhdr_linux branch 9 times, most recently from da428da to 4f428e0 Compare January 12, 2025 16:55
Copy link

@maurer maurer left a comment

Choose a reason for hiding this comment

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

The main thing I see missing here is a use of ethhdr. A struct type with no usage by libc might not actually belong here, even though it is on the UAPI interface - this is a bindings crate for libc, not for the Linux kernel.

Is there a function in libc that you intend to call that uses the ethhdr struct? If so, layering on a commit binding that on top of this one would make a better PR IMO.

@@ -517,6 +517,11 @@ s! {
pub ifr6_prefixlen: u32,
pub ifr6_ifindex: c_int,
}

// is __be16 __bitwise __u16
pub struct __c_anonymous___be16 {
Copy link

Choose a reason for hiding this comment

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

Why __c_anonymous_? This isn't an anonymous type, it's name is __be16.

Unless I'm mistaken, __c_anonymous_fieldname is normally used for inline structs/unions attached to a field of that name - this is a named type, and that's not the field name.


impl Eq for ethhdr {}

impl PartialEq for ethhdr {
Copy link

Choose a reason for hiding this comment

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

This PartialEq definition appears to lack a check on the h_proto field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not forget but until I solve the alignment issue in some platforms and as I was playing with the h_proto type I do not bother with it just yet (still a draft).

self.h_dest
.iter()
.zip(other.h_dest.iter())
.all(|(a, b)| a == b)
Copy link

Choose a reason for hiding this comment

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

Do you not want them to compare equal if they have the same CStr value, but different null trailing bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

note that those fields represent mac addresses not as strings (note they re unsigned char).

Copy link

Choose a reason for hiding this comment

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

Ah, my bad, you're correct.

@@ -517,6 +517,11 @@ s! {
pub ifr6_prefixlen: u32,
pub ifr6_ifindex: c_int,
}

// is __be16 __bitwise __u16
pub struct __c_anonymous___be16 {
Copy link

Choose a reason for hiding this comment

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

Consider deriving Eq/PartialEq, adding a method to access/set it as host u16, and adding a Debug based on the host representation?

If you do that, your later stuff on ethhdr can just be a derive.


// linux/if_ether.h

#[repr(C, align(1))]
Copy link

Choose a reason for hiding this comment

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

This struct is packed and not align-1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know. was trying to solve few architectures build failures.

f.debug_struct("ethhdr")
.field("h_dest", &self.h_dest)
.field("h_source", &self.h_source)
.finish()
Copy link

Choose a reason for hiding this comment

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

Missing proto field

@@ -1319,6 +1319,11 @@ s! {
pub propagation: crate::__u64,
pub userns_fd: crate::__u64,
}

// is __be16 __bitwise __u16
pub struct __c_anonymous___be16 {
Copy link

Choose a reason for hiding this comment

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

As above re: __c_anonymous - it also might be helpful just to have a type like this higher up in the hierarchy to be shared, I find the idea that only Linux/Android have bigendian u16s unlikely.

@devnexen
Copy link
Contributor Author

The main thing I see missing here is a use of ethhdr. A struct type with no usage by libc might not actually belong here, even though it is on the UAPI interface - this is a bindings crate for libc, not for the Linux kernel.

There is, for packet filters purpose. sendto/recvfrom can use any buffer e.g. ethhdr for this case. since AF_PACKET family and ETH_P* constants are already there, I m just trying to finish it off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants