use std::{
mem, net, num,
os::fd::{AsRawFd as _, BorrowedFd},
ptr, slice,
};
use dope_sys::{
Errno,
io::socket::{
gso,
msg::source,
route::install::{Error, Operation},
},
kernel,
};
use o3::base::Either;
use crate::Backend;
const LENGTH: u32 = mem::size_of::<u16>() as u32;
const SPACE: usize = unsafe { libc::CMSG_SPACE(LENGTH) as usize };
const HEADER: usize = unsafe { libc::CMSG_LEN(0) as usize };
const fn header<T>(level: libc::c_int, kind: libc::c_int) -> libc::cmsghdr {
libc::cmsghdr {
cmsg_len: HEADER + mem::size_of::<T>(),
cmsg_level: level,
cmsg_type: kind,
}
}
#[derive(Clone, Copy)]
pub struct Permit(());
#[repr(C, align(8))]
pub struct Control {
header: libc::cmsghdr,
size: num::NonZero<u16>,
padding: [u8; SPACE - HEADER - LENGTH as usize],
}
impl gso::Mode for Control {
type Capability = self::Permit;
const CAPABILITY: Option<Self::Capability> = Some(Permit(()));
fn control(cap: Self::Capability, size: num::NonZero<u16>) -> Self {
let Permit(()) = cap;
Self {
header: header::<u16>(libc::SOL_UDP, libc::UDP_SEGMENT),
size,
padding: [0; SPACE - HEADER - LENGTH as usize],
}
}
fn release(self) -> (Self::Capability, num::NonZero<u16>) {
(Permit(()), self.size)
}
fn bytes(&mut self) -> &[u8] {
unsafe { slice::from_raw_parts(ptr::from_ref(self).cast(), SPACE) }
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C, align(8))]
pub struct Source {
header: libc::cmsghdr,
address: [u8; 16],
index: u32,
}
impl From<source::Address> for Source {
fn from(source: source::Address) -> Self {
match source.get() {
net::SocketAddr::V4(address) => {
const DEST: usize = mem::offset_of!(libc::in_pktinfo, ipi_spec_dst);
let mut packet = [0; 16];
packet[DEST..DEST + 4].copy_from_slice(&address.ip().octets());
Self {
header: header::<libc::in_pktinfo>(libc::IPPROTO_IP, libc::IP_PKTINFO),
address: packet,
index: 0,
}
}
net::SocketAddr::V6(address) => Self {
header: header::<libc::in6_pktinfo>(libc::IPPROTO_IPV6, libc::IPV6_PKTINFO),
address: address.ip().octets(),
index: address.scope_id(),
},
}
}
}
impl AsRef<[u8]> for Source {
fn as_ref(&self) -> &[u8] {
unsafe { slice::from_raw_parts(ptr::from_ref(self).cast(), self.header.cmsg_len) }
}
}
#[repr(transparent)]
pub struct Combined(Either<Source, Segmented>);
#[repr(C)]
struct Segmented {
gso: Control,
source: Source,
}
impl AsRef<[u8]> for Combined {
fn as_ref(&self) -> &[u8] {
match &self.0 {
Either::Left(source) => source.as_ref(),
Either::Right(control) => {
let len = SPACE + control.source.header.cmsg_len;
unsafe { slice::from_raw_parts(ptr::from_ref(control).cast(), len) }
}
}
}
}
impl source::Source for Backend {
type Control = Source;
type Combined = Combined;
fn compose(source: Source, gso: Option<Control>) -> Combined {
Combined(match gso {
Some(gso) => Either::Right(Segmented { gso, source }),
None => Either::Left(source),
})
}
fn release(control: Combined) -> (Source, Option<Control>) {
match control.0 {
Either::Left(source) => (source, None),
Either::Right(control) => (control.source, Some(control.gso)),
}
}
}
pub(super) fn attach(
socket: BorrowedFd<'_>,
code: &[libc::sock_filter],
op: Operation,
) -> Result<(), Error> {
let program = libc::sock_fprog {
len: code.len() as u16,
filter: code.as_ptr().cast_mut(),
};
let option = match op {
Operation::Program => libc::SO_ATTACH_REUSEPORT_CBPF,
Operation::Filter => libc::SO_ATTACH_FILTER,
};
unsafe {
kernel::raw::Status::new(libc::setsockopt(
socket.as_raw_fd(),
libc::SOL_SOCKET,
option,
ptr::from_ref(&program).cast(),
mem::size_of_val(&program) as libc::socklen_t,
))
.try_zero::<Backend, Errno>()
}
.map_err(|errno| Error {
operation: op,
errno,
})
}
const _: () = {
assert!(mem::size_of::<Permit>() == 0);
assert!(mem::size_of::<libc::cmsghdr>() == HEADER);
assert!(mem::offset_of!(libc::cmsghdr, cmsg_len) == 0);
assert!(mem::offset_of!(libc::cmsghdr, cmsg_level) == mem::size_of::<usize>());
assert!(mem::offset_of!(libc::cmsghdr, cmsg_type) == HEADER - mem::size_of::<libc::c_int>());
assert!(HEADER == mem::size_of::<usize>() + 2 * mem::size_of::<libc::c_int>());
assert!(mem::offset_of!(Control, size) == HEADER);
assert!(mem::offset_of!(Control, padding) == HEADER + LENGTH as usize);
assert!(mem::size_of::<Control>() == SPACE);
assert!(mem::size_of::<Control>() == 24);
assert!(mem::align_of::<Control>() == 8);
assert!(mem::size_of::<Option<Control>>() == 24);
assert!(mem::align_of::<Control>() >= mem::align_of::<libc::cmsghdr>());
assert!(mem::size_of::<libc::in_pktinfo>() == 12);
assert!(mem::size_of::<libc::in_addr>() == 4);
assert!(mem::offset_of!(libc::in_addr, s_addr) == 0);
assert!(mem::size_of::<libc::in6_pktinfo>() == 20);
assert!(mem::offset_of!(Source, address) == HEADER);
assert!(mem::offset_of!(libc::in6_pktinfo, ipi6_addr) == 0);
assert!(mem::offset_of!(libc::in6_addr, s6_addr) == 0);
assert!(
mem::offset_of!(Source, index) == HEADER + mem::offset_of!(libc::in6_pktinfo, ipi6_ifindex)
);
assert!(mem::align_of::<Source>() >= mem::align_of::<libc::in_pktinfo>());
assert!(mem::align_of::<Source>() >= mem::align_of::<libc::in6_pktinfo>());
assert!(mem::size_of::<Source>() == 40);
assert!(mem::offset_of!(Segmented, source) == SPACE);
assert!(mem::size_of::<Segmented>() == SPACE + mem::size_of::<Source>());
};