Sending a raw ethernet packet/frame in Rust

I have some code that is trying to send a ethernet type II frame directly to a network interface. I am not very familiar with this space. No matter what I try I get an io error of No such device or address (os error 6) every time. I can successfully get the mac address and network index using ioctl, so I think I am somewhat on the right track. I am stuck though. I have spent hours trying to debug this, trying different approaches and reviewing my code. The debugger does not go down far enough (nor am I sure I would understand that deep down) to see exactly why I am getting this error.

Does anyone have any suggestions on how to proceed from here? I can share code, but I am more interested in how people approach solving this sort of problem. I am hoping that talking through it will give me a lightbulb moment too.

For things like this where there's (probably) a ton of prior art/documentation/tutorials in other languages, it might make sense to check your own code against "known good" code in another language. E.g. find a C example and see if it runs successfully. Assuming it does, you might then try translating that C into unsafe Rust directly (i.e. calling all the same C functions via FFI/the libc crate) and then replace more and more with "normal" Rust versions. You could also narrow down the function call that is returning the error, and check that the arguments passed in C and Rust are the same (assuming the Rust API you're working with either exposes the same interface as C, or goes through the same underlying C functions).

(This approach works best if the core broken code is small and corresponds to a small piece of C/another language.)


Thank you for the response. I had a working C program already, but I had not gone through and compared byte by byte the two programs. I was using the wrong endianess for the hardware interface index.