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

Add support for Netlink sockets #1079

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/backend/libc/net/msghdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use crate::backend::c;
use crate::backend::conv::{msg_control_len, msg_iov_len};
#[cfg(target_os = "linux")]
use crate::backend::net::write_sockaddr::encode_sockaddr_xdp;
use crate::backend::net::write_sockaddr::{encode_sockaddr_nl, encode_sockaddr_xdp};
use crate::backend::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};

use crate::io::{self, IoSlice, IoSliceMut};
#[cfg(target_os = "linux")]
use crate::net::xdp::SocketAddrXdp;
use crate::net::{netlink::SocketAddrNl, xdp::SocketAddrXdp};
use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6};
use crate::utils::as_ptr;

Expand Down Expand Up @@ -78,7 +78,7 @@ pub(crate) fn with_v4_msghdr<R>(
f({
let mut h = zero_msghdr();
h.msg_name = as_ptr(&encoded) as _;
h.msg_namelen = size_of::<SocketAddrV4>() as _;
h.msg_namelen = size_of::<c::sockaddr_in>() as _;
h.msg_iov = iov.as_ptr() as _;
h.msg_iovlen = msg_iov_len(iov.len());
h.msg_control = control.as_control_ptr().cast();
Expand All @@ -99,7 +99,7 @@ pub(crate) fn with_v6_msghdr<R>(
f({
let mut h = zero_msghdr();
h.msg_name = as_ptr(&encoded) as _;
h.msg_namelen = size_of::<SocketAddrV6>() as _;
h.msg_namelen = size_of::<c::sockaddr_in6>() as _;
h.msg_iov = iov.as_ptr() as _;
h.msg_iovlen = msg_iov_len(iov.len());
h.msg_control = control.as_control_ptr().cast();
Expand Down Expand Up @@ -128,7 +128,7 @@ pub(crate) fn with_unix_msghdr<R>(
})
}

/// Create a message header intended to send with an IPv6 address.
/// Create a message header intended to send with an XDP address.
#[cfg(target_os = "linux")]
pub(crate) fn with_xdp_msghdr<R>(
addr: &SocketAddrXdp,
Expand All @@ -141,7 +141,29 @@ pub(crate) fn with_xdp_msghdr<R>(
f({
let mut h = zero_msghdr();
h.msg_name = as_ptr(&encoded) as _;
h.msg_namelen = size_of::<SocketAddrXdp>() as _;
h.msg_namelen = size_of::<c::sockaddr_xdp>() as _;
h.msg_iov = iov.as_ptr() as _;
h.msg_iovlen = msg_iov_len(iov.len());
h.msg_control = control.as_control_ptr().cast();
h.msg_controllen = msg_control_len(control.control_len());
h
})
}

/// Create a message header intended to send with an Netlink address.
#[cfg(target_os = "linux")]
pub(crate) fn with_nl_msghdr<R>(
addr: &SocketAddrNl,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
let encoded = encode_sockaddr_nl(addr);

f({
let mut h = zero_msghdr();
h.msg_name = as_ptr(&encoded) as _;
h.msg_namelen = size_of::<c::sockaddr_nl>() as _;
h.msg_iov = iov.as_ptr() as _;
h.msg_iovlen = msg_iov_len(iov.len());
h.msg_control = control.as_control_ptr().cast();
Expand Down
11 changes: 10 additions & 1 deletion src/backend/libc/net/read_sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::backend::c;
use crate::ffi::CStr;
use crate::io;
#[cfg(target_os = "linux")]
use crate::net::xdp::{SockaddrXdpFlags, SocketAddrXdp};
use crate::net::{
netlink::SocketAddrNl,
xdp::{SockaddrXdpFlags, SocketAddrXdp},
};
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddrAny, SocketAddrV4, SocketAddrV6};
use core::mem::size_of;

Expand Down Expand Up @@ -332,6 +335,12 @@ unsafe fn inner_read_sockaddr_os(
u32::from_be(decode.sxdp_shared_umem_fd),
))
}
#[cfg(target_os = "linux")]
c::AF_NETLINK => {
assert!(len >= size_of::<c::sockaddr_nl>());
let decode = &*storage.cast::<c::sockaddr_nl>();
SocketAddrAny::Nl(SocketAddrNl::new(decode.nl_pid, decode.nl_groups))
}
other => unimplemented!("{:?}", other),
}
}
53 changes: 50 additions & 3 deletions src/backend/libc/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#[cfg(unix)]
use super::addr::SocketAddrUnix;
#[cfg(target_os = "linux")]
use super::msghdr::with_xdp_msghdr;
use super::msghdr::{with_nl_msghdr, with_xdp_msghdr};
#[cfg(target_os = "linux")]
use super::write_sockaddr::encode_sockaddr_xdp;
use super::write_sockaddr::{encode_sockaddr_nl, encode_sockaddr_xdp};
use crate::backend::c;
use crate::backend::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_recv_len};
use crate::fd::{BorrowedFd, OwnedFd};
use crate::io;
#[cfg(target_os = "linux")]
use crate::net::xdp::SocketAddrXdp;
use crate::net::{netlink::SocketAddrNl, xdp::SocketAddrXdp};
use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
use crate::utils::as_ptr;
use core::mem::{size_of, MaybeUninit};
Expand Down Expand Up @@ -170,6 +170,25 @@ pub(crate) fn sendto_xdp(
}
}

#[cfg(target_os = "linux")]
pub(crate) fn sendto_nl(
fd: BorrowedFd<'_>,
buf: &[u8],
flags: SendFlags,
addr: &SocketAddrNl,
) -> io::Result<usize> {
unsafe {
ret_send_recv(c::sendto(
borrowed_fd(fd),
buf.as_ptr().cast(),
send_recv_len(buf.len()),
bitflags_bits!(flags),
as_ptr(&encode_sockaddr_nl(addr)).cast::<c::sockaddr>(),
size_of::<c::sockaddr_nl>() as _,
))
}
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
pub(crate) fn socket(
domain: AddressFamily,
Expand Down Expand Up @@ -253,6 +272,17 @@ pub(crate) fn bind_xdp(sockfd: BorrowedFd<'_>, addr: &SocketAddrXdp) -> io::Resu
}
}

#[cfg(target_os = "linux")]
pub(crate) fn bind_nl(sockfd: BorrowedFd<'_>, addr: &SocketAddrNl) -> io::Result<()> {
unsafe {
ret(c::bind(
borrowed_fd(sockfd),
as_ptr(&encode_sockaddr_nl(addr)).cast(),
size_of::<c::sockaddr_nl>() as c::socklen_t,
))
}
}

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
pub(crate) fn connect_v4(sockfd: BorrowedFd<'_>, addr: &SocketAddrV4) -> io::Result<()> {
unsafe {
Expand Down Expand Up @@ -455,6 +485,23 @@ pub(crate) fn sendmsg_xdp(
})
}

#[cfg(target_os = "linux")]
pub(crate) fn sendmsg_nl(
sockfd: BorrowedFd<'_>,
addr: &SocketAddrNl,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
msg_flags: SendFlags,
) -> io::Result<usize> {
with_nl_msghdr(addr, iov, control, |msghdr| unsafe {
ret_send_recv(c::sendmsg(
borrowed_fd(sockfd),
&msghdr,
bitflags_bits!(msg_flags),
))
})
}

#[cfg(not(any(
apple,
windows,
Expand Down
20 changes: 19 additions & 1 deletion src/backend/libc/net/write_sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::addr::SocketAddrUnix;
use super::ext::{in6_addr_new, in_addr_new, sockaddr_in6_new};
use crate::backend::c;
#[cfg(target_os = "linux")]
use crate::net::xdp::SocketAddrXdp;
use crate::net::{netlink::SocketAddrNl, xdp::SocketAddrXdp};
use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
use core::mem::size_of;

Expand All @@ -22,6 +22,8 @@ pub(crate) unsafe fn write_sockaddr(
SocketAddrAny::Unix(unix) => write_sockaddr_unix(unix, storage),
#[cfg(target_os = "linux")]
SocketAddrAny::Xdp(xdp) => write_sockaddr_xdp(xdp, storage),
#[cfg(target_os = "linux")]
SocketAddrAny::Nl(nl) => write_sockaddr_nl(nl, storage),
}
}

Expand Down Expand Up @@ -126,3 +128,19 @@ unsafe fn write_sockaddr_xdp(xdp: &SocketAddrXdp, storage: *mut SocketAddrStorag
core::ptr::write(storage.cast(), encoded);
size_of::<c::sockaddr_xdp>()
}

#[cfg(target_os = "linux")]
pub(crate) fn encode_sockaddr_nl(nl: &SocketAddrNl) -> c::sockaddr_nl {
let mut addr: c::sockaddr_nl = unsafe { std::mem::zeroed() };
addr.nl_family = c::AF_NETLINK as _;
addr.nl_pid = nl.pid();
addr.nl_groups = nl.groups();
addr
}

#[cfg(target_os = "linux")]
unsafe fn write_sockaddr_nl(nl: &SocketAddrNl, storage: *mut SocketAddrStorage) -> usize {
let encoded = encode_sockaddr_nl(nl);
core::ptr::write(storage.cast(), encoded);
size_of::<c::sockaddr_nl>()
}
31 changes: 26 additions & 5 deletions src/backend/linux_raw/net/msghdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

use crate::backend::c;
#[cfg(target_os = "linux")]
use crate::backend::net::write_sockaddr::encode_sockaddr_xdp;
use crate::backend::net::write_sockaddr::{encode_sockaddr_nl, encode_sockaddr_xdp};
use crate::backend::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};

use crate::io::{self, IoSlice, IoSliceMut};
#[cfg(target_os = "linux")]
use crate::net::xdp::SocketAddrXdp;
use crate::net::{netlink::SocketAddrNl, xdp::SocketAddrXdp};
use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6};
use crate::utils::as_ptr;

Expand Down Expand Up @@ -89,7 +89,7 @@ pub(crate) fn with_v4_msghdr<R>(

f(c::msghdr {
msg_name: as_ptr(&encoded) as _,
msg_namelen: size_of::<SocketAddrV4>() as _,
msg_namelen: size_of::<c::sockaddr_in>() as _,
msg_iov: iov.as_ptr() as _,
msg_iovlen: msg_iov_len(iov.len()),
msg_control: control.as_control_ptr().cast(),
Expand All @@ -109,7 +109,7 @@ pub(crate) fn with_v6_msghdr<R>(

f(c::msghdr {
msg_name: as_ptr(&encoded) as _,
msg_namelen: size_of::<SocketAddrV6>() as _,
msg_namelen: size_of::<c::sockaddr_in6>() as _,
msg_iov: iov.as_ptr() as _,
msg_iovlen: msg_iov_len(iov.len()),
msg_control: control.as_control_ptr().cast(),
Expand Down Expand Up @@ -148,7 +148,28 @@ pub(crate) fn with_xdp_msghdr<R>(

f(c::msghdr {
msg_name: as_ptr(&encoded) as _,
msg_namelen: size_of::<SocketAddrXdp>() as _,
msg_namelen: size_of::<c::sockaddr_xdp>() as _,
msg_iov: iov.as_ptr() as _,
msg_iovlen: msg_iov_len(iov.len()),
msg_control: control.as_control_ptr().cast(),
msg_controllen: msg_control_len(control.control_len()),
msg_flags: 0,
})
}

/// Create a message header intended to send with an Netlink address.
#[cfg(target_os = "linux")]
pub(crate) fn with_nl_msghdr<R>(
addr: &SocketAddrNl,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
let encoded = encode_sockaddr_nl(addr);

f(c::msghdr {
msg_name: as_ptr(&encoded) as _,
msg_namelen: size_of::<c::sockaddr_xdp>() as _,
msg_iov: iov.as_ptr() as _,
msg_iovlen: msg_iov_len(iov.len()),
msg_control: control.as_control_ptr().cast(),
Expand Down
8 changes: 8 additions & 0 deletions src/backend/linux_raw/net/read_sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use crate::backend::c;
use crate::io;
#[cfg(target_os = "linux")]
use crate::net::netlink::SocketAddrNl;
#[cfg(target_os = "linux")]
use crate::net::xdp::{SockaddrXdpFlags, SocketAddrXdp};
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddrAny, SocketAddrUnix, SocketAddrV4, SocketAddrV6};
use core::mem::size_of;
Expand Down Expand Up @@ -216,6 +218,12 @@ pub(crate) unsafe fn read_sockaddr_os(storage: *const c::sockaddr, len: usize) -
u32::from_be(decode.sxdp_shared_umem_fd),
))
}
#[cfg(target_os = "linux")]
c::AF_NETLINK => {
assert!(len >= size_of::<c::sockaddr_nl>());
let decode = &*storage.cast::<c::sockaddr_nl>();
SocketAddrAny::Nl(SocketAddrNl::new(decode.nl_pid, decode.nl_groups))
}
other => unimplemented!("{:?}", other),
}
}
Loading
Loading