diff --git a/litebox/src/inet_socket/mod.rs b/litebox/src/inet_socket/mod.rs index a9813bf4b8..6b0d21467b 100644 --- a/litebox/src/inet_socket/mod.rs +++ b/litebox/src/inet_socket/mod.rs @@ -16,7 +16,7 @@ use litebox_broker_protocol::{ error::ErrorCode, fs::{FileOpenFlags, FileStatusFlags}, socket::{ - MAX_TCP_LISTEN_BACKLOG, ShutdownMode, SocketConnectionStatus, + MAX_TCP_LISTEN_BACKLOG, SendFlags as BrokerSendFlags, ShutdownMode, SocketConnectionStatus, SocketError as BrokerSocketError, SocketOptionName, SocketOptionValue, SocketOutcome, SocketType, }, @@ -360,18 +360,22 @@ impl InetSocket { flags: SendFlags, destination: Option, ) -> Result { - if !flags.is_empty() { + if !flags.difference(SendFlags::REPORT_PENDING_ERROR).is_empty() { unimplemented!() } + let mut broker_flags = BrokerSendFlags::NONE; + if flags.contains(SendFlags::REPORT_PENDING_ERROR) { + broker_flags.0 |= BrokerSendFlags::REPORT_PENDING_ERROR.0; + } if let Some(addr) = destination && (addr.port() == 0 || addr.ip().is_unspecified()) { return Err(SocketWriteError::Unaddressable); } if self.is_stream() { - self.write_stream(buf) + self.write_stream(buf, broker_flags) } else { - self.write_datagram(buf, destination) + self.write_datagram(buf, broker_flags, destination) } } @@ -485,6 +489,10 @@ bitflags! { const NOSIGNAL = 0x4000; /// `MSG_OOB`: sends out-of-band data. const OOB = 0x1; + /// Report a pending error in place of sending a datagram, as the + /// broker's [`REPORT_PENDING_ERROR`](BrokerSendFlags::REPORT_PENDING_ERROR) + /// defines. A stream send rejects it. + const REPORT_PENDING_ERROR = 1 << 31; } } diff --git a/litebox/src/inet_socket/tcp.rs b/litebox/src/inet_socket/tcp.rs index 650f6f323f..b82c609613 100644 --- a/litebox/src/inet_socket/tcp.rs +++ b/litebox/src/inet_socket/tcp.rs @@ -134,13 +134,16 @@ impl InetSocket { } /// Writes up to [`MAX_SOCKET_TRANSFER_SIZE`] stream bytes. - pub(super) fn write_stream(&self, buffer: impl WriteBuffer) -> Result { + pub(super) fn write_stream( + &self, + buffer: impl WriteBuffer, + flags: BrokerSendFlags, + ) -> Result { let length = buffer.len().min(MAX_SOCKET_TRANSFER_SIZE as usize); - match self.broker.send_socket( - self.handle, - &buffer.sub_buffer(0, length), - BrokerSendFlags::NONE, - ) { + match self + .broker + .send_socket(self.handle, &buffer.sub_buffer(0, length), flags) + { Ok(SocketOutcome::Completed(sent)) => Ok(sent), Ok(SocketOutcome::Failed(BrokerSocketError::Shutdown)) => { Err(SocketWriteError::WriteShutdown) diff --git a/litebox/src/inet_socket/udp.rs b/litebox/src/inet_socket/udp.rs index 7a7743010c..aff8346c4e 100644 --- a/litebox/src/inet_socket/udp.rs +++ b/litebox/src/inet_socket/udp.rs @@ -63,6 +63,7 @@ impl InetSocket { pub(super) fn write_datagram( &self, buffer: impl WriteBuffer, + flags: BrokerSendFlags, destination: Option, ) -> Result { if buffer.len() > MAX_UDP_DATAGRAM_SIZE as usize { @@ -75,7 +76,7 @@ impl InetSocket { }; match self .broker - .send_to_socket(self.handle, &buffer, BrokerSendFlags::NONE, destination) + .send_to_socket(self.handle, &buffer, flags, destination) { Ok(SocketOutcome::Completed(sent)) => Ok(sent), Ok(SocketOutcome::Failed(BrokerSocketError::Shutdown)) => { diff --git a/litebox_broker_core/src/socket.rs b/litebox_broker_core/src/socket.rs index 0cacb892ef..2c7aca0e7d 100644 --- a/litebox_broker_core/src/socket.rs +++ b/litebox_broker_core/src/socket.rs @@ -575,6 +575,12 @@ pub trait PlatformSocket: Send + Sync { /// socket shut down for writing fails with `Shutdown`. Partial successful /// sends are invalid. Resource failures are broker errors rather than /// ordinary socket failures. + /// + /// With [`SendFlags::REPORT_PENDING_ERROR`], a pending error the platform + /// holds fails the send in place of sending, and is taken. Without it, the + /// send neither reports nor takes a pending error, which a later status or + /// receive reports: a platform whose host send would take one takes it + /// first and keeps it. fn send_to( &self, data: &dyn WriteBuffer, @@ -752,6 +758,9 @@ pub fn create( /// fails every later attempt the same way. UDP connects complete /// synchronously, and a failed attempt preserves the previous peer. Policy /// denial is also a [`SocketOutcome::Failed`] that preserves existing state. +/// A UDP connect to the broadcast address, to which the broker sends no +/// datagrams, is denied so, with `PolicyDenied`, as Linux fails one without +/// `SO_BROADCAST` with `EACCES`. pub fn connect( process: &BrokerProcess, handle: ObjectHandle, @@ -822,6 +831,9 @@ fn connect_datagram( if is_unaddressable(address) { return Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)); } + if is_broadcast(address) { + return Ok(SocketOutcome::Failed(SocketError::PolicyDenied)); + } Ok(match transport_connect(process, object, address)? { SocketOutcome::Completed(SocketConnectionStatus::Failed(error)) => { SocketOutcome::Failed(error) @@ -835,6 +847,12 @@ fn is_unaddressable(address: SocketAddrV4) -> bool { address.port() == 0 || address.ip().is_unspecified() } +/// Returns whether `address` is the broadcast address, to which the broker +/// sends no datagrams, whatever a socket's `Broadcast` option. +fn is_broadcast(address: SocketAddrV4) -> bool { + *address.ip() == Ipv4Addr::BROADCAST +} + /// Reports the progress of a connection attempt. /// /// A connected stream is reported connected, so a later [`connect`] fails @@ -1309,9 +1327,10 @@ pub fn accept( /// Sends TCP stream bytes without waiting for readiness. /// -/// A socket shut down for writing fails with `Shutdown` once any connection -/// attempt has finished, and an unconnected socket fails with `NotConnected`. -/// A temporarily full socket, or one whose connection attempt is still being +/// No send flag applies to a stream, so any flag is rejected. A socket shut +/// down for writing fails with `Shutdown` once any connection attempt has +/// finished, and an unconnected socket fails with `NotConnected`. A +/// temporarily full socket, or one whose connection attempt is still being /// started, fails with [`BrokerError::WouldBlock`], or with /// [`BrokerError::NonBlockingWouldBlock`] if the socket is non-blocking. pub fn send( @@ -1320,7 +1339,7 @@ pub fn send( data: &dyn WriteBuffer, flags: SendFlags, ) -> Result> { - if flags.has_unsupported_bits() || data.len() > MAX_SOCKET_TRANSFER_SIZE as usize { + if flags != SendFlags::NONE || data.len() > MAX_SOCKET_TRANSFER_SIZE as usize { return Err(BrokerError::UnsupportedOperation); } let object = process.authorized_object(handle, ObjectRights::WRITE)?; @@ -1403,14 +1422,20 @@ fn transport_send( /// Sends `data` as one complete datagram without waiting for readiness. /// -/// A pending error fails the send and is consumed, and a socket shut down for -/// writing fails with `Shutdown`. Without `destination`, an unconnected -/// socket fails with `DestinationAddressRequired`. The first valid send with -/// an explicit destination on an unbound datagram socket reserves and binds a -/// guest-visible local endpoint before invoking the platform. A temporarily -/// full socket fails with a pending error if there is one, and otherwise with -/// [`BrokerError::WouldBlock`], or with [`BrokerError::NonBlockingWouldBlock`] -/// if the socket is non-blocking. +/// The destination is checked first: without `destination`, an unconnected +/// socket fails with `DestinationAddressRequired`, and a send to the broadcast +/// address, to which the broker sends no datagrams, fails with `PolicyDenied`. +/// The first valid send with an explicit destination on an unbound datagram +/// socket reserves and binds a guest-visible local endpoint. Then a socket +/// shut down for writing fails with `Shutdown`. A temporarily full socket +/// fails with [`BrokerError::WouldBlock`], or with +/// [`BrokerError::NonBlockingWouldBlock`] if the socket is non-blocking. +/// +/// A send leaves pending errors pending unless `flags` has +/// [`SendFlags::REPORT_PENDING_ERROR`]. Then, once the destination is checked, +/// a pending error fails the send and is consumed, before a write shutdown +/// does, and a temporarily full socket fails with a pending error if there is +/// one. pub fn send_to( process: &BrokerProcess, handle: ObjectHandle, @@ -1437,33 +1462,36 @@ fn send_datagram( if !read_socket(object, |socket| is_udp(socket.create_request))? { return Ok(SocketOutcome::Failed(SocketError::InvalidArgument)); } - if let Some(error) = take_pending_error(object) { + let destination = match route_datagram(process, object, destination)? { + SocketOutcome::Completed(destination) => destination, + SocketOutcome::Failed(error) => return Ok(SocketOutcome::Failed(error)), + }; + let report_pending_error = flags.contains(SendFlags::REPORT_PENDING_ERROR); + if report_pending_error && let Some(error) = take_pending_error(object) { return Ok(SocketOutcome::Failed(error)); } - let (write_shutdown, view) = - read_socket(object, |socket| (socket.write_shutdown, socket.view()))?; - if write_shutdown { + if read_socket(object, |socket| socket.write_shutdown)? { return Ok(SocketOutcome::Failed(SocketError::Shutdown)); } - if destination.is_none() && view != SocketConnectionStatus::Connected { - return Ok(SocketOutcome::Failed( - SocketError::DestinationAddressRequired, - )); - } finish_datagram_transfer( object, - transport_send_to(process, object, data, flags, destination), + transport_send_to(object, data, flags, destination), + report_pending_error, ) } -/// Reports a datagram transfer's pending error in place of a would-block -/// failure, and records any error that follows a failed transfer. +/// Records any error that follows a failed datagram transfer, and, with +/// `report_pending_error`, reports a pending error in place of a would-block +/// failure. fn finish_datagram_transfer( object: &RwLock, result: Result>, + report_pending_error: bool, ) -> Result> { match result { - Err(error @ (BrokerError::WouldBlock | BrokerError::NonBlockingWouldBlock)) => { + Err(error @ (BrokerError::WouldBlock | BrokerError::NonBlockingWouldBlock)) + if report_pending_error => + { match take_pending_error(object) { Some(error) => Ok(SocketOutcome::Failed(error)), None => Err(error), @@ -1477,25 +1505,32 @@ fn finish_datagram_transfer( } } -/// Sends one datagram, first binding an unbound socket. -fn transport_send_to( +/// Checks a datagram's destination, and that the socket has a source for it, +/// first binding an unbound socket, and returns the normalized destination. +fn route_datagram( process: &BrokerProcess, object: &RwLock, - data: &dyn WriteBuffer, - flags: SendFlags, destination: Option, -) -> Result> { - let length = data.len(); - let create_request = read_socket(object, |socket| socket.create_request)?; +) -> Result>> { + let (create_request, view) = + read_socket(object, |socket| (socket.create_request, socket.view()))?; let destination = match destination { + None if view != SocketConnectionStatus::Connected => { + return Ok(SocketOutcome::Failed( + SocketError::DestinationAddressRequired, + )); + } + None => None, Some(destination) if destination.port() == 0 => { return Ok(SocketOutcome::Failed(SocketError::InvalidArgument)); } + Some(destination) if is_broadcast(destination) => { + return Ok(SocketOutcome::Failed(SocketError::PolicyDenied)); + } Some(destination) => match normalize_socket_destination(destination) { Ok(destination) => Some(destination), Err(error) => return Ok(SocketOutcome::Failed(error)), }, - None => None, }; if let Some(destination) = destination { match process.core.policy.authorize_socket_connect( @@ -1510,7 +1545,7 @@ fn transport_send_to( Err(error) => return Err(error), } } - let (resource, needs_bind, nonblocking) = { + let (resource, needs_bind) = { let mut object = object.write(); let ObjectEntry::Socket(socket) = &mut *object else { return Err(BrokerError::InvalidRights); @@ -1531,11 +1566,7 @@ fn transport_send_to( } socket.configuration_in_flight = true; } - ( - Arc::clone(&socket.resource), - needs_bind, - socket.status.nonblocking(), - ) + (Arc::clone(&socket.resource), needs_bind) }; if needs_bind { match reserve_and_bind(process, create_request, &resource, DEFAULT_LOCAL_ADDRESS) { @@ -1564,10 +1595,26 @@ fn transport_send_to( }) { return Ok(SocketOutcome::Failed(SocketError::InvalidArgument)); } - let outcome = resource + Ok(SocketOutcome::Completed(destination)) +} + +/// Sends one datagram to a destination [`route_datagram`] returned. +fn transport_send_to( + object: &RwLock, + data: &dyn WriteBuffer, + flags: SendFlags, + destination: Option, +) -> Result> { + let length = data.len(); + let state = socket_state(object)?; + if state.resource_retired { + return Ok(SocketOutcome::Failed(SocketError::NotConnected)); + } + let outcome = state + .resource .platform_socket .send_to(data, flags, destination) - .map_err(|error| would_block_error(error, nonblocking))?; + .map_err(|error| would_block_error(error, state.nonblocking))?; if let SocketOutcome::Completed(sent) = outcome && sent != length { @@ -1755,7 +1802,7 @@ fn receive_datagram( { Ok(SocketOutcome::Failed(SocketError::Shutdown)) } - result => finish_datagram_transfer(object, result), + result => finish_datagram_transfer(object, result, true), } } diff --git a/litebox_broker_core/src/socket/tests.rs b/litebox_broker_core/src/socket/tests.rs index c4fdd7ab3e..ec47ce155f 100644 --- a/litebox_broker_core/src/socket/tests.rs +++ b/litebox_broker_core/src/socket/tests.rs @@ -2751,7 +2751,7 @@ fn check_socket_operations_and_policy(broker: &TestBroker, provider: &TestSocket Ok(SocketOutcome::Completed(())) ); assert_eq!( - send(&process, handle, &[], SendFlags(1)), + send(&process, handle, &[], SendFlags::REPORT_PENDING_ERROR), Err(BrokerError::UnsupportedOperation) ); let in_flight = socket_resource(&process, handle, ObjectRights::WAIT).unwrap(); @@ -3097,7 +3097,7 @@ fn check_udp_socket_operations(broker: &TestBroker, provider: &TestSocketProvide &process, handle, b"udp", - SendFlags(1), + SendFlags(1 << 1), Some(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 53)), ), Err(BrokerError::UnsupportedOperation) @@ -4746,7 +4746,9 @@ fn check_pending_errors_and_readiness(broker: &TestBroker, provider: &TestSocket ); process.close_object_reference(reported).unwrap(); - // A datagram transfer reports a set error before a queued one. + // Without REPORT_PENDING_ERROR, a datagram send leaves pending errors for + // a take or a receive to report, set before queued ones; with it, the + // send reports one in place of sending. let datagram = new_socket(create_udp_request()); assert_eq!( connect(&process, datagram, loopback_address()), @@ -4763,14 +4765,39 @@ fn check_pending_errors_and_readiness(broker: &TestBroker, provider: &TestSocket set_error(&process, datagram, SocketError::ConnectionRefused).unwrap(); assert_eq!( send_to(&process, datagram, &[1], SendFlags::NONE, None), - Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + Ok(SocketOutcome::Completed(1)) + ); + assert_eq!( + take_error(&process, datagram), + Ok(Some(SocketError::ConnectionRefused)) + ); + assert_eq!( + send_to(&process, datagram, &[1], SendFlags::NONE, None), + Ok(SocketOutcome::Completed(1)) ); assert_eq!( receive_data_from(&process, datagram, 2, ReceiveFromFlags::NONE), Ok(SocketOutcome::Failed(SocketError::NetworkUnreachable)) ); + set_error(&process, datagram, SocketError::ConnectionRefused).unwrap(); + assert_eq!( + send_to( + &process, + datagram, + &[1], + SendFlags::REPORT_PENDING_ERROR, + None + ), + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + ); assert_eq!( - send_to(&process, datagram, &[1], SendFlags::NONE, None), + send_to( + &process, + datagram, + &[1], + SendFlags::REPORT_PENDING_ERROR, + None + ), Ok(SocketOutcome::Completed(1)) ); process.close_object_reference(datagram).unwrap(); diff --git a/litebox_broker_host/src/lib.rs b/litebox_broker_host/src/lib.rs index c8cd130091..81aef2b295 100644 --- a/litebox_broker_host/src/lib.rs +++ b/litebox_broker_host/src/lib.rs @@ -74,7 +74,7 @@ use litebox_broker_protocol::socket::{ AcceptSocketResponse, BindSocketResponse, ConnectSocketResponse, CreateSocketResponse, ListenSocketResponse, MAX_SOCKET_PEEK_SIZE, MAX_SOCKET_TRANSFER_SIZE, MAX_TCP_LISTEN_BACKLOG, MAX_UDP_DATAGRAM_SIZE, ReceiveFlags, ReceiveFromSocketResponse, ReceiveSocketResponse, - SendSocketResponse, SendToSocketResponse, SocketOutcome, + SendFlags, SendSocketResponse, SendToSocketResponse, SocketOutcome, }; use litebox_broker_protocol::timer::{ CreateTimerResponse, GetTimerResponse, ReadTimerResponse, SetTimerResponse, @@ -1456,7 +1456,7 @@ fn handle_socket_request( } } SocketRequest::Send(request) => { - if request.flags.has_unsupported_bits() + if request.flags != SendFlags::NONE || request.buffer.length() > MAX_SOCKET_TRANSFER_SIZE { return Err(RequestFailure::Abort(ErrorCode::MalformedRequest)); @@ -1708,7 +1708,7 @@ fn handle_vsock_socket_request( }, ), VsockSocketRequest::Send(request) => { - if request.flags.has_unsupported_bits() { + if request.flags != SendFlags::NONE { return Err(RequestFailure::Abort(ErrorCode::MalformedRequest)); } let data = shared_buffer(shared_buffers, request.buffer, MAX_SOCKET_TRANSFER_SIZE)?; diff --git a/litebox_broker_protocol/src/socket.rs b/litebox_broker_protocol/src/socket.rs index 7b2ac1229d..4771da0075 100644 --- a/litebox_broker_protocol/src/socket.rs +++ b/litebox_broker_protocol/src/socket.rs @@ -165,7 +165,7 @@ pub struct Port(pub u16); /// rejects anything outside it rather than masking it away, which would silently /// perform an operation the caller did not ask for. /// -/// No send flag is supported yet, so any nonzero value is rejected. +/// A stream send takes no flag, and rejects any nonzero value. /// /// [`ReadinessFlags`]: crate::readiness::ReadinessFlags #[repr(transparent)] @@ -175,15 +175,33 @@ pub struct SendFlags(pub u32); impl SendFlags { /// No flags. pub const NONE: Self = Self(0); + /// Makes a datagram send report a pending error in place of sending. + /// + /// The send checks its destination first, binding an unbound socket, and + /// then, before checking for a write shutdown, fails with the socket's + /// pending error if it has one, taking it. A send that would block also + /// fails with a pending error if there is one. Without this flag, a + /// datagram send leaves pending errors pending. + /// + /// A stream send rejects it: with no destination to check first, the + /// flag would only take a pending error before sending, which a sender + /// can do itself. + pub const REPORT_PENDING_ERROR: Self = Self(1 << 0); /// Every send flag this protocol version defines. - pub const SUPPORTED: Self = Self(0); + pub const SUPPORTED: Self = Self(Self::REPORT_PENDING_ERROR.0); /// Returns whether any bit outside [`Self::SUPPORTED`] is set. #[must_use] pub const fn has_unsupported_bits(self) -> bool { self.0 & !Self::SUPPORTED.0 != 0 } + + /// Returns whether every flag in `other` is set. + #[must_use] + pub const fn contains(self, other: Self) -> bool { + self.0 & other.0 == other.0 + } } /// Flags for a receive operation. @@ -290,7 +308,8 @@ pub enum SocketError { /// An argument or socket state is invalid for the operation. #[error("invalid socket operation argument")] InvalidArgument, - /// The policy engine refused the destination. + /// The destination is refused: the policy engine denied it, or it is a + /// broadcast address, to which brokers send no datagrams. #[error("socket policy denied the operation")] PolicyDenied, /// The host stack failed in a way this protocol does not distinguish. diff --git a/litebox_broker_protocol/src/wire.rs b/litebox_broker_protocol/src/wire.rs index e767b67334..cc94d8c69e 100644 --- a/litebox_broker_protocol/src/wire.rs +++ b/litebox_broker_protocol/src/wire.rs @@ -1573,6 +1573,12 @@ mod tests { buffer, flags: SendFlags(unsupported), })), + BrokerOperation::Socket(SocketRequest::SendTo(SendToSocketRequest { + handle, + buffer, + flags: SendFlags(unsupported), + destination: None, + })), BrokerOperation::Socket(SocketRequest::Receive(ReceiveSocketRequest { handle, buffer, @@ -1598,9 +1604,8 @@ mod tests { assert!(SendFlags(unsupported).has_unsupported_bits()); assert!(ReceiveFlags(unsupported).has_unsupported_bits()); - // No send flag exists yet, so every bit is unsupported there. - assert!(SendFlags(ReceiveFlags::PEEK.0).has_unsupported_bits()); assert!(!SendFlags::NONE.has_unsupported_bits()); + assert!(!SendFlags::REPORT_PENDING_ERROR.has_unsupported_bits()); assert!(!ReceiveFlags::PEEK.has_unsupported_bits()); assert!(ReceiveFlags::PEEK.contains(ReceiveFlags::PEEK)); assert!(!ReceiveFlags::NONE.contains(ReceiveFlags::PEEK)); diff --git a/litebox_broker_smoltcp/src/socket.rs b/litebox_broker_smoltcp/src/socket.rs index 2df6833d4d..bd86084cd0 100644 --- a/litebox_broker_smoltcp/src/socket.rs +++ b/litebox_broker_smoltcp/src/socket.rs @@ -166,12 +166,13 @@ impl PlatformSocket for SmoltcpSocket { fn send_to( &self, data: &dyn WriteBuffer, - _flags: SendFlags, + flags: SendFlags, destination: Option, ) -> BrokerResult> { + let report_pending_error = flags.contains(SendFlags::REPORT_PENDING_ERROR); self.run(|stack| match stack.parts(self.id)?.0 { Kind::Tcp(_) => Ok(invalid()), - Kind::Udp(_) => stack.send_udp(self.id, data, destination), + Kind::Udp(_) => stack.send_udp(self.id, data, destination, report_pending_error), }) } diff --git a/litebox_broker_smoltcp/src/tests.rs b/litebox_broker_smoltcp/src/tests.rs index 42fc06f5a8..255428cc37 100644 --- a/litebox_broker_smoltcp/src/tests.rs +++ b/litebox_broker_smoltcp/src/tests.rs @@ -2193,7 +2193,7 @@ fn a_closing_connection_cannot_be_made_again() { } #[test] -fn a_pending_udp_error_fails_the_next_send() { +fn a_pending_udp_error_fails_only_a_send_that_reports_it() { let wire: &'static Wire = Box::leak(Box::default()); let test = Test::new(Some(wire)); let mut host = Host::new(wire, &test.clock); @@ -2204,22 +2204,36 @@ fn a_pending_udp_error_fails_the_next_send() { core_socket::connect(&process, socket, closed), Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) ); - let send = || core_socket::send_to(&process, socket, b"x", SendFlags::NONE, None); + let send = |flags| core_socket::send_to(&process, socket, b"x", flags, None); + let refused = |host: &mut Host| { + pump(&test, host, |_| { + test.published(socket) + .is_some_and(|flags| flags.contains(ReadinessFlags::ERROR)) + }); + }; // Longer than an ICMP error quotes. assert_eq!( core_socket::send_to(&process, socket, &vec![0; 1000], SendFlags::NONE, None), Ok(SocketOutcome::Completed(1000)) ); - pump(&test, &mut host, |_| { - test.published(socket) - .is_some_and(|flags| flags.contains(ReadinessFlags::ERROR)) - }); - // As Linux: the port unreachable fails the next send, once. + refused(&mut host); + // Sends that do not report the port unreachable leave it for a receive. + assert_eq!(send(SendFlags::NONE), Ok(SocketOutcome::Completed(1))); + assert_eq!(send(SendFlags::NONE), Ok(SocketOutcome::Completed(1))); + assert_eq!( + receive_data_from(&process, socket, 100, ReceiveFromFlags::NONE), + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + ); + refused(&mut host); + // One that reports it fails with it, once. assert_eq!( - send(), + send(SendFlags::REPORT_PENDING_ERROR), Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) ); - assert_eq!(send(), Ok(SocketOutcome::Completed(1))); + assert_eq!( + send(SendFlags::REPORT_PENDING_ERROR), + Ok(SocketOutcome::Completed(1)) + ); } #[test] diff --git a/litebox_broker_smoltcp/src/udp.rs b/litebox_broker_smoltcp/src/udp.rs index f094526bcb..4cef775909 100644 --- a/litebox_broker_smoltcp/src/udp.rs +++ b/litebox_broker_smoltcp/src/udp.rs @@ -519,11 +519,14 @@ impl Stack { Ok(SocketConnectionStatus::Connected) } + /// Sends a datagram; with `report_pending_error`, a pending error fails + /// the send and is taken, and otherwise it stays pending. pub(super) fn send_udp( &mut self, id: u64, data: &dyn WriteBuffer, destination: Option, + report_pending_error: bool, ) -> BrokerResult> { let process = self.entries.get(&id).ok_or(BrokerError::Internal)?.process; let udp = self.udp_entry(id)?; @@ -541,8 +544,7 @@ impl Stack { Ok(source) => source, Err(error) => return Ok(SocketOutcome::Failed(error)), }; - // As Linux, where allocating the datagram reports it. - if let Some(error) = udp.pending_error.take() { + if report_pending_error && let Some(error) = udp.pending_error.take() { return Ok(SocketOutcome::Failed(error)); } if !is_internal_socket_address(destination) { diff --git a/litebox_broker_userland/src/socket.rs b/litebox_broker_userland/src/socket.rs index 35e2d2167e..1d3ea0870e 100644 --- a/litebox_broker_userland/src/socket.rs +++ b/litebox_broker_userland/src/socket.rs @@ -171,6 +171,9 @@ struct ProviderCore { /// Test callback run at every [`HookPoint`]. #[cfg(test)] hook: RwLock>, + /// Makes the next UDP host send would block, for tests. + #[cfg(test)] + block_next_udp_send: AtomicBool, } /// Socket counts, guest bindings, and peer links shared between sockets. @@ -244,6 +247,8 @@ enum HookPoint { Receiving(u64), /// A TCP receive would block and is about to clear READ. Eagain(u64), + /// A UDP send is about to send through its host socket. + HostSend(u64), /// A socket's host socket refused the guest's TCP options, and the /// socket is about to check whether it may. OptionsRefused(u64), @@ -370,6 +375,8 @@ impl ProviderCore { thread: Mutex::new(None), #[cfg(test)] hook: RwLock::new(None), + #[cfg(test)] + block_next_udp_send: AtomicBool::new(false), }); let weak = Arc::downgrade(&core); let (started, startup) = sync_channel(1); @@ -903,11 +910,12 @@ impl PlatformSocket for UserlandSocket { fn send_to( &self, data: &dyn WriteBuffer, - _flags: SendFlags, + flags: SendFlags, destination: Option, ) -> BrokerResult> { + let report_pending_error = flags.contains(SendFlags::REPORT_PENDING_ERROR); send_from(data, |data| { - self.call(|socket| socket.send_to_socket(data, destination)) + self.call(|socket| socket.send_to_socket(data, destination, report_pending_error)) }) } @@ -1163,15 +1171,18 @@ impl<'a> SocketEntry<'a> { self.bind_tcp_socket(binding, already_bound) } + /// Sends a datagram; with `report_pending_error`, a pending error fails + /// the send and is taken, and otherwise it stays pending. fn send_to_socket( &mut self, data: RawParts<'_>, destination: Option, + report_pending_error: bool, ) -> BrokerResult> { if self.kind() != SocketKind::Udp || data.len() > MAX_UDP_DATAGRAM_SIZE as usize { return Ok(SocketOutcome::Failed(SocketError::InvalidArgument)); } - if let Some(error) = self.take_udp_cached_error() { + if report_pending_error && let Some(error) = self.take_udp_cached_error() { return Ok(SocketOutcome::Failed(error)); } if self.write_shutdown { @@ -1190,9 +1201,12 @@ impl<'a> SocketEntry<'a> { }; match peer { UdpPeer::Internal(destination) => self.send_internal_udp(destination, data), - UdpPeer::External(destination) => { - self.send_to_external_udp(data, destination, authorize_external_reply) - } + UdpPeer::External(destination) => self.send_to_external_udp( + data, + destination, + authorize_external_reply, + report_pending_error, + ), } } diff --git a/litebox_broker_userland/src/socket/host.rs b/litebox_broker_userland/src/socket/host.rs index 6825f4e3d4..02ec4ead44 100644 --- a/litebox_broker_userland/src/socket/host.rs +++ b/litebox_broker_userland/src/socket/host.rs @@ -118,14 +118,20 @@ pub(crate) trait HostErrno: /// A UDP socket reports ICMP errors only while connected, and only those its /// datagrams to its peer cause, a port unreachable as /// [`HostErrno::CONNREFUSED`]. It reports each at most once: a receive that -/// reports one, even a peek, takes it, and so may a send. The rule is the -/// broker's own, whether or not a host's sockets follow it natively. An -/// error that the poller reports on a UDP socket as [`SocketEvents::ERROR`] -/// stays until a call or [`Self::take_error`] takes it, and -/// [`Self::error_pending`] reports it until then. +/// reports one, even a peek, takes it, and so may a send where +/// [`Self::SEND_TAKES_UDP_ERRORS`]. The rule is the broker's own, whether or +/// not a host's sockets follow it natively. An error that the poller reports +/// on a UDP socket as [`SocketEvents::ERROR`] stays until a call or +/// [`Self::take_error`] takes it, and [`Self::error_pending`] reports it +/// until then. pub(crate) trait HostSocket: Sized + Send { type Errno; + /// Whether a UDP send may take the socket's pending error and fail with + /// it. A UDP send fails with [`HostErrno::CONNREFUSED`] only by taking + /// one. + const SEND_TAKES_UDP_ERRORS: bool; + /// Opens a TCP socket. fn tcp() -> Result; diff --git a/litebox_broker_userland/src/socket/host/linux.rs b/litebox_broker_userland/src/socket/host/linux.rs index 9b8cfabbf0..8dc9abdbe6 100644 --- a/litebox_broker_userland/src/socket/host/linux.rs +++ b/litebox_broker_userland/src/socket/host/linux.rs @@ -115,9 +115,20 @@ impl Socket { } } +#[cfg(test)] +impl Socket { + /// Opens another handle to the same host socket. + pub(crate) fn try_clone_udp(&self) -> IoResult { + Ok(self.0.try_clone()?.into()) + } +} + impl HostSocket for Socket { type Errno = Errno; + /// Allocating the datagram takes and reports the socket's pending error. + const SEND_TAKES_UDP_ERRORS: bool = true; + fn tcp() -> Result { Self::open(SocketType::STREAM, ipproto::TCP) } diff --git a/litebox_broker_userland/src/socket/host/windows.rs b/litebox_broker_userland/src/socket/host/windows.rs index a89d1a6378..3366aaa39a 100644 --- a/litebox_broker_userland/src/socket/host/windows.rs +++ b/litebox_broker_userland/src/socket/host/windows.rs @@ -302,6 +302,10 @@ impl Socket { impl HostSocket for Socket { type Errno = Errno; + /// Winsock queues a UDP socket's ICMP errors among its datagrams, for + /// receives to report. + const SEND_TAKES_UDP_ERRORS: bool = false; + fn tcp() -> Result { let socket = open(Type::STREAM, Protocol::TCP)?; Ok(Self::new(Source::Tcp(TcpStream::from_std(socket.into())))) diff --git a/litebox_broker_userland/src/socket/tests/udp.rs b/litebox_broker_userland/src/socket/tests/udp.rs index 1d22dfb7d8..e48063e565 100644 --- a/litebox_broker_userland/src/socket/tests/udp.rs +++ b/litebox_broker_userland/src/socket/tests/udp.rs @@ -749,7 +749,13 @@ fn unmatched_guest_udp_destinations_are_port_unreachable() { ); assert!(has_error()); assert_eq!( - send_datagram(&process, socket, b"blocked", SendFlags::NONE, None), + send_datagram( + &process, + socket, + b"blocked", + SendFlags::REPORT_PENDING_ERROR, + None + ), Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) ); assert!(!has_error()); @@ -783,6 +789,261 @@ fn unmatched_guest_udp_destinations_are_port_unreachable() { ); } +/// Sends that do not report pending errors leave a refusal pending, whether +/// the provider or the host socket holds it, for a later call to report. +#[test] +fn unreported_udp_refusals_stay_pending_across_sends() { + let provider = Arc::new(UserlandSocketProvider::new(3, 3).unwrap()); + let broker = test_broker_core( + PolicyEngine::with_unauthenticated_rights(ObjectRights::all()) + .with_socket_policy(gateway_udp_policy()), + BrokerCoreLimits::new_with_all_limits(6, 0, 3, 3), + provider.clone(), + ) + .unwrap(); + let process = broker + .create_process(CallerCredential::Unauthenticated) + .unwrap(); + let (published, publications) = channel(); + let (retired, _retirements) = channel(); + let readiness = Arc::new(TestReadinessSink { published, retired }); + let send = |socket| send_datagram(&process, socket, b"refused", SendFlags::NONE, None); + + // The provider holds the refusal of a guest port that no socket is bound + // to. Once one is, sends reach it and leave the refusal. + let internal_peer = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 4000); + let internal = create_udp_socket(&process, readiness.clone()); + assert_eq!( + litebox_broker_core::socket::connect(&process, internal, internal_peer), + Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) + ); + assert_eq!(send(internal), Ok(SocketOutcome::Completed(7))); + let receiver = create_udp_socket(&process, readiness.clone()); + assert_eq!( + litebox_broker_core::socket::bind(&process, receiver, internal_peer), + Ok(SocketOutcome::Completed(internal_peer)) + ); + for _ in 0..2 { + assert_eq!(send(internal), Ok(SocketOutcome::Completed(7))); + } + assert_eq!( + litebox_broker_core::socket::take_error(&process, internal), + Ok(Some(SocketError::ConnectionRefused)) + ); + for _ in 0..2 { + assert!(matches!( + receive_datagram_into(&process, receiver, &mut [0; 7], ReceiveFromFlags::NONE), + Ok(SocketOutcome::Completed(PlatformDatagramReceive { + received: 7, + .. + })) + )); + } + + // A host port's refusal stays pending too, though a host send could + // take it: a send takes it from the host socket first, so the host send + // meets no refusal. Once a socket is bound to the port, sends reach it + // and leave the refusal. + let (refusing, refused_address) = crate::test_support::refusing_udp_address(); + let external = create_udp_socket(&process, readiness); + assert_eq!( + litebox_broker_core::socket::connect(&process, external, gateway_address(refused_address)), + Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) + ); + assert_eq!(send(external), Ok(SocketOutcome::Completed(7))); + wait_for_readiness_publication(&publications, external, ReadinessFlags::ERROR); + drop(refusing); + let host_peer = UdpSocket::bind(refused_address).unwrap(); + host_peer.set_read_timeout(Some(TEST_TIMEOUT)).unwrap(); + let hook = TestHook::install(&provider); + for _ in 0..2 { + assert_eq!(send(external), Ok(SocketOutcome::Completed(7))); + } + assert_eq!(host_sends(&hook), 2); + assert_eq!( + receive_datagram_into(&process, external, &mut [0; 7], ReceiveFromFlags::NONE), + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + ); + for _ in 0..2 { + assert_eq!(host_peer.recv(&mut [0; 7]).unwrap(), 7); + } +} + +/// How many host sends UDP sends made since `hook` was installed. +fn host_sends(hook: &TestHook) -> usize { + hook.points + .lock() + .unwrap() + .iter() + .filter(|point| matches!(point, HookPoint::HostSend(_))) + .count() +} + +/// A send that leaves pending errors keeps each refusal that reaches the +/// host socket just before a host send would take it, and still sends. +// Only a Linux host send takes the host socket's error. +#[cfg(target_os = "linux")] +#[test] +fn late_udp_refusals_stay_pending_while_the_datagram_is_sent() { + use rustix::event::{PollFd, PollFlags, Timespec, poll}; + + let provider = Arc::new(UserlandSocketProvider::new(1, 1).unwrap()); + let broker = test_broker_core( + PolicyEngine::with_unauthenticated_rights(ObjectRights::all()) + .with_socket_policy(gateway_udp_policy()), + BrokerCoreLimits::new_with_all_limits(2, 0, 1, 1), + provider.clone(), + ) + .unwrap(); + let process = broker + .create_process(CallerCredential::Unauthenticated) + .unwrap(); + let (published, _publications) = channel(); + let (retired, _retirements) = channel(); + let socket = create_udp_socket(&process, Arc::new(TestReadinessSink { published, retired })); + // Connected to itself, the host peer refuses the guest's datagrams. + let peer = Arc::new(UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap()); + let peer_address = socket_address_v4(peer.local_addr().unwrap()); + peer.connect(peer_address).unwrap(); + peer.set_read_timeout(Some(TEST_TIMEOUT)).unwrap(); + assert_eq!( + litebox_broker_core::socket::connect(&process, socket, gateway_address(peer_address)), + Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) + ); + let guest_port = litebox_broker_core::socket::status(&process, socket) + .unwrap() + .local_address + .unwrap() + .port(); + let host_socket = provider.core.udp_native_endpoint_handle(guest_port); + let host_source = SocketAddrV4::new( + Ipv4Addr::LOCALHOST, + host_socket.local_addr().unwrap().port(), + ); + + // Before each of the first two host sends, the peer refuses a datagram + // from the same host socket, so the refusal arrives after the send's + // take. Then the peer accepts the host socket's datagrams. + let hook = TestHook::install(&provider); + let accepting_peer = Arc::clone(&peer); + let host_sends_seen = AtomicUsize::new(0); + hook.act(move |_, _, point| { + if !matches!(point, HookPoint::HostSend(_)) { + return; + } + if host_sends_seen.fetch_add(1, Ordering::Relaxed) == 2 { + accepting_peer.connect(host_source).unwrap(); + return; + } + host_socket.send(b"late").unwrap(); + wait_until( + || { + let mut sockets = [PollFd::new(&host_socket, PollFlags::empty())]; + let now = Timespec { + tv_sec: 0, + tv_nsec: 0, + }; + poll(&mut sockets, Some(&now)).unwrap(); + sockets[0].revents().contains(PollFlags::ERR) + }, + "the refusal reached the host socket", + ); + }); + assert_eq!( + send_datagram(&process, socket, b"refused", SendFlags::NONE, None), + Ok(SocketOutcome::Completed(7)) + ); + hook.stop(); + assert_eq!(host_sends(&hook), 3); + let mut payload = [0; 7]; + assert_eq!(peer.recv(&mut payload).unwrap(), 7); + assert_eq!(&payload, b"refused"); + assert_eq!( + receive_datagram_into(&process, socket, &mut [0; 7], ReceiveFromFlags::NONE), + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + ); +} + +/// A send that would block while the socket keeps an error still learns +/// when the host socket can send, without a call that checks or takes the +/// error, and the error stays pending. +#[test] +fn udp_write_wait_is_published_while_an_error_is_kept() { + let provider = Arc::new(UserlandSocketProvider::new(1, 1).unwrap()); + let broker = test_broker_core( + PolicyEngine::with_unauthenticated_rights(ObjectRights::all()) + .with_socket_policy(gateway_udp_policy()), + BrokerCoreLimits::new_with_all_limits(2, 0, 1, 1), + provider.clone(), + ) + .unwrap(); + let process = broker + .create_process(CallerCredential::Unauthenticated) + .unwrap(); + let (published, publications) = channel(); + let (retired, _retirements) = channel(); + let socket = create_udp_socket(&process, Arc::new(TestReadinessSink { published, retired })); + let (_refusing, refused_address) = crate::test_support::refusing_udp_address(); + assert_eq!( + litebox_broker_core::socket::connect(&process, socket, gateway_address(refused_address)), + Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) + ); + #[cfg(target_os = "linux")] + let host_socket = provider.core.udp_native_endpoint_handle( + litebox_broker_core::socket::status(&process, socket) + .unwrap() + .local_address + .unwrap() + .port(), + ); + let send = || send_datagram(&process, socket, b"refused", SendFlags::NONE, None); + assert_eq!(send(), Ok(SocketOutcome::Completed(7))); + wait_for_readiness_publication(&publications, socket, ReadinessFlags::ERROR); + + // The send keeps the refusal, and its host send would block. + provider + .core + .block_next_udp_send + .store(true, Ordering::Relaxed); + assert_eq!(send(), Err(BrokerError::WouldBlock)); + let mut blocked = false; + loop { + let (handle, readiness) = publications.recv_timeout(TEST_TIMEOUT).unwrap(); + if handle != socket { + continue; + } + if !readiness.contains(ReadinessFlags::WRITE) { + blocked = true; + } else if blocked { + break; + } + } + + // An error that reaches the host socket while the endpoint keeps one is + // taken at once, rather than reported over and over. + #[cfg(target_os = "linux")] + { + use rustix::event::{PollFd, PollFlags, Timespec, poll}; + + let hook = TestHook::install(&provider); + let start = hook.len(); + host_socket.send(b"late").unwrap(); + hook.wait_for_received_handled(start, |token| token & UDP_EVENT_TOKEN_FLAG != 0); + let mut sockets = [PollFd::new(&host_socket, PollFlags::empty())]; + let now = Timespec { + tv_sec: 0, + tv_nsec: 0, + }; + poll(&mut sockets, Some(&now)).unwrap(); + assert!(!sockets[0].revents().contains(PollFlags::ERR)); + } + + assert_eq!( + receive_datagram_into(&process, socket, &mut [0; 7], ReceiveFromFlags::NONE), + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + ); +} + #[test] fn udp_status_republishes_when_another_error_remains_pending() { let provider = Arc::new(UserlandSocketProvider::new(1, 1).unwrap()); @@ -1233,11 +1494,11 @@ fn provider_preserves_udp_datagram_semantics() { send_datagram( &process, handle, - b"invalid", + b"broadcast", SendFlags::NONE, Some(SocketAddrV4::new(Ipv4Addr::BROADCAST, 9)), ), - Ok(SocketOutcome::Failed(SocketError::InvalidArgument)) + Ok(SocketOutcome::Failed(SocketError::PolicyDenied)) ); assert_eq!( litebox_broker_core::socket::status(&process, handle) @@ -1352,7 +1613,7 @@ fn provider_preserves_udp_datagram_semantics() { handle, SocketAddrV4::new(Ipv4Addr::BROADCAST, 9), ), - Ok(SocketOutcome::Failed(SocketError::InvalidArgument)) + Ok(SocketOutcome::Failed(SocketError::PolicyDenied)) ); assert_eq!( send_datagram(&process, handle, b"old peer", SendFlags::NONE, None), @@ -1362,43 +1623,6 @@ fn provider_preserves_udp_datagram_semantics() { assert_eq!(&packet[..received], b"old peer"); assert_eq!(socket_address_v4(preserved_source), source); - let (_refusing, refused_address) = crate::test_support::refusing_udp_address(); - assert_eq!( - litebox_broker_core::socket::connect(&process, handle, gateway_address(refused_address)), - Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) - ); - assert_eq!( - send_datagram(&process, handle, b"refused", SendFlags::NONE, None,), - Ok(SocketOutcome::Completed(7)) - ); - // Leave the error with the transport so that the failed send queues it. - // A guest cannot arrange this: waiting for the error queues it. - wait_for_readiness_publication(&publications, handle, ReadinessFlags::ERROR); - assert_eq!( - send_datagram( - &process, - handle, - b"broadcast", - SendFlags::NONE, - Some(SocketAddrV4::new(Ipv4Addr::BROADCAST, 9)), - ), - Ok(SocketOutcome::Failed(SocketError::InvalidArgument)) - ); - assert_eq!( - litebox_broker_core::socket::take_error(&process, handle), - Ok(Some(SocketError::ConnectionRefused)) - ); - assert!( - !process - .check_readiness(handle) - .unwrap() - .contains(ReadinessFlags::ERROR) - ); - assert_eq!( - litebox_broker_core::socket::connect(&process, handle, server_address), - Ok(SocketOutcome::Completed(SocketConnectionStatus::Connected)) - ); - assert_eq!( litebox_broker_core::socket::shutdown(&process, handle, ShutdownMode::Write), Ok(SocketOutcome::Completed(())) @@ -2439,7 +2663,13 @@ fn connected_guest_udp_enforces_barriers_peek_and_peer_addresses() { Ok(SocketOutcome::Completed(4)) ); assert_eq!( - send_datagram(&first_process, first, b"refused", SendFlags::NONE, None), + send_datagram( + &first_process, + first, + b"refused", + SendFlags::REPORT_PENDING_ERROR, + None + ), Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) ); let replacement = create_udp_socket(&second_process, readiness); @@ -2563,7 +2793,13 @@ fn connected_guest_udp_filters_other_wildcard_peer_aliases() { Ok(SocketOutcome::Completed(11)) ); assert_eq!( - send_datagram(&process, sender, b"wrong alias", SendFlags::NONE, None), + send_datagram( + &process, + sender, + b"wrong alias", + SendFlags::REPORT_PENDING_ERROR, + None + ), Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) ); } diff --git a/litebox_broker_userland/src/socket/udp.rs b/litebox_broker_userland/src/socket/udp.rs index 5a09f576dd..3c368604e2 100644 --- a/litebox_broker_userland/src/socket/udp.rs +++ b/litebox_broker_userland/src/socket/udp.rs @@ -32,10 +32,10 @@ use super::host::{ Errno, HostErrno as _, HostPoller as _, HostSocket as _, Interest, Poller, Socket, SocketEvents, }; use super::{ - PlatformSocketStatus, ProviderCore, ReceiveFromOutcome, SocketEntry, SocketInbox, Topology, - broker_error_from_errno, local_socket_address, lock, read_lock, republish_own_readiness, - set_own_readiness, socket_error_from_errno, socket_operation_error_from_errno, write_lock, - zeroed_vec, + HookPoint, PlatformSocketStatus, ProviderCore, ReceiveFromOutcome, SocketEntry, SocketInbox, + Topology, broker_error_from_errno, local_socket_address, lock, read_lock, + republish_own_readiness, set_own_readiness, socket_error_from_errno, + socket_operation_error_from_errno, write_lock, zeroed_vec, }; pub(super) const MAX_REJECTED_UDP_DATAGRAMS_PER_COMMAND: usize = 64; @@ -559,6 +559,12 @@ impl UdpNativeErrorState { !matches!(self, Self::None) } + /// Whether the host socket still holds the error, so that watching the + /// socket would report it again at once. + const fn is_held_by_host(self) -> bool { + matches!(self, Self::PendingKernel) + } + fn record_kernel(&mut self) { if matches!(self, Self::None) { *self = Self::PendingKernel; @@ -1043,19 +1049,23 @@ impl SocketEntry<'_> { /// Sends a datagram to an external host, staging the socket's native /// endpoint first if needed. With `authorize_reply`, the socket also - /// accepts replies from the destination. + /// accepts replies from the destination. With `report_pending_error`, a + /// pending error fails the send and is taken, and otherwise it stays + /// pending. pub(super) fn send_to_external_udp( &mut self, data: RawParts<'_>, external_destination: SocketAddrV4, authorize_reply: bool, + report_pending_error: bool, ) -> BrokerResult> { let added_peer = authorize_reply && match self.reserve_udp_external_peer(external_destination)? { SocketOutcome::Completed(added) => added, SocketOutcome::Failed(error) => return Ok(SocketOutcome::Failed(error)), }; - let outcome = self.stage_and_send_external_udp(data, external_destination); + let outcome = + self.stage_and_send_external_udp(data, external_destination, report_pending_error); if added_peer && !matches!(outcome, Ok(SocketOutcome::Completed(_))) { self.remove_udp_external_peer(external_destination); } @@ -1066,6 +1076,7 @@ impl SocketEntry<'_> { &mut self, data: RawParts<'_>, external_destination: SocketAddrV4, + report_pending_error: bool, ) -> BrokerResult> { if self.udp_state()?.native_endpoint.is_none() { let endpoint = match self.stage_udp_endpoint(external_destination, None)? { @@ -1074,7 +1085,7 @@ impl SocketEntry<'_> { }; self.replace_udp_endpoint(Some(endpoint))?; } - self.send_external_udp(data, external_destination) + self.send_external_udp(data, external_destination, report_pending_error) } /// Creates a native endpoint on a host port that no guest peer refers to. @@ -1399,7 +1410,17 @@ impl SocketEntry<'_> { } if error_event { endpoint.readable = true; - endpoint.error.record_kernel(); + if endpoint.error.consumed().is_some() { + // An error reached the host socket, watched while the endpoint + // keeps an older one. Take it, to coalesce into the kept one, + // so that watching the socket does not report it again at + // once. + if let Some(error) = take_udp_socket_error(&endpoint.socket)? { + endpoint.error.record_consumed(error); + } + } else { + endpoint.error.record_kernel(); + } } if readable_event && !error_event { self.drain_rejected_external_datagrams()?; @@ -1417,7 +1438,10 @@ impl SocketEntry<'_> { let Some(endpoint) = self.udp_state_mut()?.native_endpoint.as_mut() else { return Ok(()); }; - if endpoint.error.is_pending() { + // Watching the socket would report an error it holds again at once, + // so wait for a call to take it. One the endpoint keeps, the socket + // no longer holds. + if endpoint.error.is_held_by_host() { return Ok(()); } let interest = udp_rearm_interest(read_shutdown, endpoint.readable, endpoint.write_blocked); @@ -1435,7 +1459,7 @@ impl SocketEntry<'_> { udp_endpoint_needs_rearm( endpoint.readable, endpoint.write_blocked, - endpoint.error.is_pending(), + endpoint.error.is_held_by_host(), ) }); if should_rearm { @@ -1522,6 +1546,16 @@ impl SocketEntry<'_> { } } + /// Keeps an error taken from the host socket for a later call to report. + fn keep_udp_endpoint_error(&mut self, error: SocketError) -> BrokerResult<()> { + let endpoint = self.udp_endpoint_mut()?; + endpoint.error.record_consumed(error); + endpoint.readable = true; + // The error is already kept; ignore a failure to recompute readiness. + let _ = self.publish_udp_readiness(); + Ok(()) + } + /// Discards queued native datagrams the socket does not accept, leaving /// the endpoint readable only if an accepted datagram or an error is at /// the head. @@ -1564,20 +1598,32 @@ impl SocketEntry<'_> { Ok(()) } + /// Sends a datagram through the socket's native endpoint. With + /// `report_pending_error`, a pending error fails the send and is taken, + /// and otherwise it stays pending. pub(super) fn send_external_udp( &mut self, data: RawParts<'_>, external_destination: SocketAddrV4, + report_pending_error: bool, ) -> BrokerResult> { - if let Some(error) = self.take_consumed_udp_endpoint_error()? { + if report_pending_error && let Some(error) = self.take_consumed_udp_endpoint_error()? { return Ok(SocketOutcome::Failed(error)); } + // Where a host send takes the host socket's pending error and fails + // with it, a send that leaves pending errors takes the error first, + // and keeps it. + let keep_host_error = !report_pending_error && Socket::SEND_TAKES_UDP_ERRORS; + if keep_host_error && let Some(error) = take_udp_socket_error(&self.udp_endpoint()?.socket)? + { + self.keep_udp_endpoint_error(error)?; + } let destination = host_socket_destination(external_destination); let past_peer = matches!( self.udp_state()?.peer, Some(UdpPeer::External(peer)) if host_socket_destination(peer) != destination ); - let result = { + let (result, late_refusal) = { // Hold the port set so no broker endpoint appears on the // destination port between the check and the send. let ports = read_lock(&self.core().udp_ports); @@ -1585,7 +1631,15 @@ impl SocketEntry<'_> { return Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)); } let socket = &self.udp_endpoint()?.socket; - loop { + let send = || loop { + #[cfg(test)] + if self + .core() + .block_next_udp_send + .swap(false, Ordering::Relaxed) + { + break Err(BrokerError::WouldBlock); + } let sent = if past_peer { socket.send_as_unconnected(data, destination) } else { @@ -1600,8 +1654,34 @@ impl SocketEntry<'_> { break socket_operation_error_from_errno(error).map(SocketOutcome::Failed); } } - } + }; + let mut late_refusal = false; + let result = loop { + self.core().hook(HookPoint::HostSend(self.id())); + match send() { + // An error may still reach the host socket after the + // take above, and a host send takes it. A host send fails + // with a refusal only by taking one, so such refusals are + // kept, as one, and the datagram sent again until a host + // send succeeds or fails otherwise. Each refusal takes an + // error that reached the host socket since the take or + // the last host send, so only a flood of ICMP errors from + // the host's network could keep this going. Another error + // that arrives so fails the send, since a host send can + // also fail so on its own, as without a route. + Ok(SocketOutcome::Failed(SocketError::ConnectionRefused)) + if keep_host_error => + { + late_refusal = true; + } + result => break result, + } + }; + (result, late_refusal) }; + if late_refusal { + self.keep_udp_endpoint_error(SocketError::ConnectionRefused)?; + } match result { Err(BrokerError::WouldBlock) => { self.udp_endpoint_mut()?.write_blocked = true; @@ -1610,9 +1690,12 @@ impl SocketEntry<'_> { } // A send may take the error the host socket had pending, as a // receive does. If it did not, rearming reports the error again. - Ok(SocketOutcome::Failed(error)) if self.udp_endpoint()?.error.is_pending() => self - .finish_udp_endpoint_error(error) - .map(SocketOutcome::Failed), + Ok(SocketOutcome::Failed(error)) + if report_pending_error && self.udp_endpoint()?.error.is_pending() => + { + self.finish_udp_endpoint_error(error) + .map(SocketOutcome::Failed) + } result => result, } } @@ -1824,6 +1907,20 @@ impl ProviderCore { result } + /// Opens another handle to the host socket under the UDP socket bound + /// to `guest_port`. + #[cfg(target_os = "linux")] + pub(super) fn udp_native_endpoint_handle(&self, guest_port: u16) -> std::net::UdpSocket { + self.with_udp_socket(guest_port, |socket| { + socket + .udp_endpoint()? + .socket + .try_clone_udp() + .map_err(|_| BrokerError::Internal) + }) + .unwrap() + } + pub(super) fn inject_udp_status_errors( &self, guest_port: u16, @@ -1952,8 +2049,8 @@ fn take_udp_socket_error(socket: &Socket) -> BrokerResult> { } } -fn udp_endpoint_needs_rearm(readable: bool, waiting_for_write: bool, pending_error: bool) -> bool { - !pending_error && (!readable || waiting_for_write) +fn udp_endpoint_needs_rearm(readable: bool, waiting_for_write: bool, host_error: bool) -> bool { + !host_error && (!readable || waiting_for_write) } fn udp_rearm_interest(read_shutdown: bool, readable: bool, waiting_for_write: bool) -> Interest { diff --git a/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker b/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker index 8a0353f68b..97844edbe9 100755 Binary files a/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker and b/litebox_runner_linux_on_windows_userland/tests/test-bins/udp_broker differ diff --git a/litebox_runner_linux_userland/tests/udp_broker.c b/litebox_runner_linux_userland/tests/udp_broker.c index 76ce46b209..79d14aa64a 100644 --- a/litebox_runner_linux_userland/tests/udp_broker.c +++ b/litebox_runner_linux_userland/tests/udp_broker.c @@ -326,13 +326,18 @@ int main(int argc, char **argv) { assert(!error_reported(refused_fd)); assert(socket_error(refused_fd) == 0); - // A send that fails for another reason leaves the error. + // A send refused its destination leaves the error, which Linux takes only + // once the destination is checked. Linux refuses a broadcast destination + // without SO_BROADCAST, and the broker refuses any. send_refused(refused_fd); - struct sockaddr_in zero_port = refused; - zero_port.sin_port = 0; - assert(sendto(refused_fd, "x", 1, 0, (const struct sockaddr *)&zero_port, - sizeof(zero_port)) == -1); - assert(errno == EINVAL); + struct sockaddr_in broadcast = { + .sin_family = AF_INET, + .sin_port = htons(9), + .sin_addr.s_addr = htonl(INADDR_BROADCAST), + }; + assert(sendto(refused_fd, "x", 1, 0, (const struct sockaddr *)&broadcast, + sizeof(broadcast)) == -1); + assert(errno == EACCES); assert(error_reported(refused_fd)); assert(socket_error(refused_fd) == ECONNREFUSED); assert(!error_reported(refused_fd)); @@ -349,15 +354,9 @@ int main(int argc, char **argv) { // A connect that fails leaves the error. It comes last, as Linux sends // later datagrams to the address of a connect that failed. send_refused(refused_fd); - // Linux refuses a broadcast peer without SO_BROADCAST, and the broker - // refuses any. - struct sockaddr_in broadcast = { - .sin_family = AF_INET, - .sin_port = htons(9), - .sin_addr.s_addr = htonl(INADDR_BROADCAST), - }; assert(connect(refused_fd, (const struct sockaddr *)&broadcast, sizeof(broadcast)) == -1); + assert(errno == EACCES); assert(error_reported(refused_fd)); assert(socket_error(refused_fd) == ECONNREFUSED); assert(close(refused_fd) == 0); diff --git a/litebox_shim_linux/src/syscalls/inet.rs b/litebox_shim_linux/src/syscalls/inet.rs index d92b2d1789..dd3d6eb694 100644 --- a/litebox_shim_linux/src/syscalls/inet.rs +++ b/litebox_shim_linux/src/syscalls/inet.rs @@ -389,7 +389,7 @@ impl InetSocket { if !self.is_stream() && buf.len() > inet_socket::MAX_UDP_DATAGRAM_SIZE { return Err(Errno::EMSGSIZE); } - let send_flags = convert_flags!( + let mut send_flags = convert_flags!( flags, SendFlags, inet_socket::SendFlags, @@ -399,6 +399,12 @@ impl InetSocket { MORE, OOB, ); + // Like Linux, a UDP send fails with a pending error, such as one from + // an ICMP error, taking it, once its destination is checked and + // before a write shutdown is. + if !self.is_stream() { + send_flags |= inet_socket::SendFlags::REPORT_PENDING_ERROR; + } let is_empty_stream = buf.is_empty() && self.is_stream(); let (result, timeout) = self.wait( cx,