Skip to content

Commit d478bbb

Browse files
committed
adding ethhdr type for linux/android for proper packet filtering.
1 parent 4abcd81 commit d478bbb

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

libc-test/semver/android.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,7 @@ epoll_create1
32303230
epoll_ctl
32313231
epoll_event
32323232
epoll_wait
3233+
ethhdr
32333234
eventfd
32343235
eventfd_read
32353236
eventfd_write

libc-test/semver/linux.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,6 +3722,7 @@ epoll_params
37223722
epoll_pwait
37233723
epoll_wait
37243724
erand48
3725+
ethhdr
37253726
eventfd
37263727
eventfd_read
37273728
eventfd_write

src/unix/linux_like/android/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub type __u16 = c_ushort;
3434
pub type __s16 = c_short;
3535
pub type __u32 = c_uint;
3636
pub type __s32 = c_int;
37+
// __be16 is big endian __u16
38+
pub type __be16 = [__u16; 1];
3739

3840
// linux/elf.h
3941

@@ -638,6 +640,13 @@ s_no_extra_traits! {
638640
pub ifc_len: c_int,
639641
pub ifc_ifcu: __c_anonymous_ifc_ifcu,
640642
}
643+
644+
#[repr(align(1))]
645+
pub struct ethhdr {
646+
pub h_dest: [c_uchar; crate::ETH_ALEN as usize],
647+
pub h_source: [c_uchar; crate::ETH_ALEN as usize],
648+
pub h_proto: crate::__be16,
649+
}
641650
}
642651

643652
cfg_if! {
@@ -1020,6 +1029,31 @@ cfg_if! {
10201029
.finish()
10211030
}
10221031
}
1032+
1033+
impl Eq for ethhdr {}
1034+
1035+
impl PartialEq for ethhdr {
1036+
fn eq(&self, other: &ethhdr) -> bool {
1037+
self.h_dest
1038+
.iter()
1039+
.zip(other.h_dest.iter())
1040+
.all(|(a, b)| a == b)
1041+
&& self
1042+
.h_source
1043+
.iter()
1044+
.zip(other.h_source.iter())
1045+
.all(|(a, b)| a == b)
1046+
}
1047+
}
1048+
1049+
impl fmt::Debug for ethhdr {
1050+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1051+
f.debug_struct("ethhdr")
1052+
.field("h_dest", &self.h_dest)
1053+
.field("h_source", &self.h_source)
1054+
.finish()
1055+
}
1056+
}
10231057
}
10241058
}
10251059

src/unix/linux_like/linux/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub type __u16 = c_ushort;
2828
pub type __s16 = c_short;
2929
pub type __u32 = c_uint;
3030
pub type __s32 = c_int;
31+
// __be16 is big endian __u16
32+
pub type __be16 = [__u16; 1];
3133

3234
pub type Elf32_Half = u16;
3335
pub type Elf32_Word = u32;
@@ -1784,6 +1786,13 @@ s_no_extra_traits! {
17841786
pub request: xsk_tx_metadata_request,
17851787
pub completion: xsk_tx_metadata_completion,
17861788
}
1789+
1790+
#[repr(packed)]
1791+
pub struct ethhdr {
1792+
pub h_dest: [c_uchar; crate::ETH_ALEN as usize],
1793+
pub h_source: [c_uchar; crate::ETH_ALEN as usize],
1794+
pub h_proto: crate::__be16,
1795+
}
17871796
}
17881797

17891798
cfg_if! {
@@ -2211,6 +2220,31 @@ cfg_if! {
22112220
.finish()
22122221
}
22132222
}
2223+
2224+
impl Eq for ethhdr {}
2225+
2226+
impl PartialEq for ethhdr {
2227+
fn eq(&self, other: &ethhdr) -> bool {
2228+
self.h_dest
2229+
.iter()
2230+
.zip(other.h_dest.iter())
2231+
.all(|(a, b)| a == b)
2232+
&& self
2233+
.h_source
2234+
.iter()
2235+
.zip(other.h_source.iter())
2236+
.all(|(a, b)| a == b)
2237+
}
2238+
}
2239+
2240+
impl fmt::Debug for ethhdr {
2241+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2242+
f.debug_struct("ethhdr")
2243+
.field("h_dest", &self.h_dest)
2244+
.field("h_source", &self.h_source)
2245+
.finish()
2246+
}
2247+
}
22142248
}
22152249
}
22162250

0 commit comments

Comments
 (0)