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

phy::RawSocket incompatible with Medium::Ip #820

Open
toshipiazza opened this issue Jul 25, 2023 · 3 comments
Open

phy::RawSocket incompatible with Medium::Ip #820

toshipiazza opened this issue Jul 25, 2023 · 3 comments
Labels

Comments

@toshipiazza
Copy link

toshipiazza commented Jul 25, 2023

RawSocketDesc creates an AF_PACKET socket with libc::SOCK_RAW socket type in case of any medium

let protocol = match medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => imp::ETH_P_ALL,
#[cfg(feature = "medium-ip")]
Medium::Ip => imp::ETH_P_ALL,
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => imp::ETH_P_IEEE802154,
};
let lower = unsafe {
let lower = libc::socket(
libc::AF_PACKET,
libc::SOCK_RAW | libc::SOCK_NONBLOCK,
protocol.to_be() as i32,
);
if lower == -1 {
return Err(io::Error::last_os_error());
}
lower
};

However, this doesn't work when Medium::Ip is passed to phy::RawSocket and to RawSocketDesc, since the incoming packets have an L2 frame prepended but Interface expects packets to start from the IP header

According to PACKET(7) man page

The socket_type is either SOCK_RAW for raw packets including the link-level header or SOCK_DGRAM for cooked packets with the link-level header removed.

For Medium::Ip we should set socket_type = libc::SOCK_DGRAM | libc::SOCK_NONBLOCK and protocol = libc::ETH_P_IP so the resulting packets start with the IP header

@toshipiazza
Copy link
Author

I guess it's unclear if this is expected or desired to work, since the docs for Medium::Ip suggest that Medium::Ip is an abstraction designed for TUN devices...

If this is not intended to work it may make sense to assert_ne!(medium, Medium::Ip)

@Dirbaio
Copy link
Member

Dirbaio commented Jul 25, 2023

yes, it's intended to work. Can you send a PR?

@toshipiazza
Copy link
Author

This may be harder to fix than how I initially thought. When sending a packet on a socket created with AF_PACKET + SOCK_DGRAM it looks like you need to specify the L2 destination address in a struct sockaddr_ll to sendto(). This doesn't work well with the Medium::Ip abstraction/design

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

No branches or pull requests

2 participants