Rustix::net and ethernet frames (ETH_P_ALL)

Ahoi hoi

I've a question about rustix and capturing ethernet frames.

If i want to capture these frames on a linux machine, the socket definition should be:

socket(af_packet, sock_raw, htons(ETH_P_ALL));

ETH_P_ALL is 0x0003

With rustix::net it looks like:

use rustix::net::*;

fn main() {

    let sock = socket( AddressFamily::PACKET, SocketType::RAW,  Protocol::ETHERNET ).unwrap();

    let mut sock_buff : [u8; 1500] = [0u8; 1500];

    println!("Socket created, fd: {:?}", sock);

    let _nread: usize = rustix::net::recv(&sock, &mut sock_buff, RecvFlags::PEEK).unwrap();

    println!("Data: {:?}", &sock_buff);

}

It compiles and runs but never capture any ethernet frame or even any data as well.

Ok lets check what strace says:

newfstatat(3, "", {st_mode=S_IFREG|0444, st_size=0, ...}, AT_EMPTY_PATH) = 0
read(3, "559ab304f000-559ab3055000 r--p 0"..., 1024) = 1024
read(3, " /usr/lib64/libc.so.6\n7fa3f9c280"..., 1024) = 1024
read(3, " /usr/lib64/ld-linux-x86-64.so.2"..., 1024) = 673
close(3)                                = 0
sched_getaffinity(2639, 32, [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31]) = 8
socket(AF_PACKET, SOCK_RAW, htons(0x8f00 /* ETH_P_??? */)) = 3
write(1, "Socket created, fd: OwnedFd { fd"..., 38Socket created, fd: OwnedFd { fd: 3 }
) = 38
recvfrom(3,

socket(AF_PACKET, SOCK_RAW, htons(0x8f00 /* ETH_P_??? */)) = 3

And here lies my question:

htons(0x8f00 /* ETH_P_??? */ ... should be 0x0003, but i can't find a Procol::"ETH_P_ALL".

Or did i an mistake on a other place and that mistake causes the problem of receiving nothing on the socket ?

Best Regards

Erich Zann

It appears Rustix is missing a proper way to pass ETH_P_ALL as the protocol value. Protocol::ETHERNET corresponds to IPPROTO_ETHERNET, which is a different thing.

I've now filed Protocol values for `ETH_P_ALL` etc. · Issue #688 · bytecodealliance/rustix · GitHub to track this.

2 Likes

wooow... thank you so much for you answer and the created issue on github.
thats awesome. :ok_hand:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.