Skip to main content

netstack3_ip/
socket.rs

1// Copyright 2019 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! IPv4 and IPv6 sockets.
6
7use core::cmp::Ordering;
8use core::num::NonZeroU8;
9
10use log::{debug, error};
11use net_types::ip::{Ip, IpVersionMarker, Ipv6Addr, Mtu};
12use net_types::{MulticastAddress, ScopeableAddress, SpecifiedAddr, Witness as _};
13use netstack3_base::socket::{SocketIpAddr, SocketIpAddrExt as _};
14use netstack3_base::{
15    AnyDevice, CounterContext, DeviceIdContext, DeviceIdentifier, EitherDeviceId, InstantContext,
16    InterfaceProperties, IpDeviceAddr, IpExt, Marks, Mms, NetworkSerializationContext, PacketType,
17    SendFrameErrorReason, StrongDeviceIdentifier, TxMetadata, TxMetadataBindingsTypes,
18    WeakDeviceIdentifier,
19};
20use netstack3_filter::{
21    self as filter, DynTransportSerializer, DynamicTransportSerializer, FilterBindingsContext,
22    FilterHandler as _, FilterIpExt, RawIpBody, SocketEgressFilterResult, SocketOpsFilter,
23    SocketOpsFilterBindingContext, TransportPacketSerializer,
24};
25use netstack3_trace::trace_duration;
26use packet::{
27    BufferMut, NestablePacketBuilder as _, PacketConstraints, SerializeError, Serializer,
28};
29use packet_formats::ip::{DscpAndEcn, IpPacketBuilder as _};
30use thiserror::Error;
31
32use crate::icmp::IcmpErrorHandler;
33use crate::internal::base::{
34    FilterHandlerProvider, IpDeviceMtuContext, IpLayerIpExt, IpLayerPacketMetadata,
35    IpPacketDestination, IpSendFrameError, IpSendFrameErrorReason, ResolveRouteError,
36    SendIpPacketMeta, SplitMulticastPacketMetadata, reject_type_to_icmpv4_error,
37    reject_type_to_icmpv6_error,
38};
39use crate::internal::counters::IpCounters;
40use crate::internal::device::state::IpDeviceStateIpExt;
41use crate::internal::gso::MaybeSegmentableTransportSerializer;
42use crate::internal::routing::PacketOrigin;
43use crate::internal::routing::rules::RuleInput;
44use crate::internal::types::{InternalForwarding, ResolvedRoute, RoutableIpAddr};
45use crate::{HopLimits, NextHop};
46
47/// The arguments used for creating an [`IpSock`]
48pub struct IpSocketArgs<'a, D: StrongDeviceIdentifier, I: IpExt, O> {
49    /// The device the socket is bound to.
50    pub device: Option<EitherDeviceId<&'a D, &'a D::Weak>>,
51    /// The local IP to use for the connection. One is selected if not provided
52    /// based on the output route.
53    pub local_ip: Option<IpDeviceAddr<I::Addr>>,
54    /// The remote IP address for this connection.
55    pub remote_ip: RoutableIpAddr<I::Addr>,
56    /// The IP protocol in use.
57    pub proto: I::Proto,
58    /// Additional IP layer options.
59    pub options: &'a O,
60}
61/// An execution context defining a type of IP socket.
62pub trait IpSocketHandler<I: IpExt + FilterIpExt, BC: TxMetadataBindingsTypes>:
63    DeviceIdContext<AnyDevice>
64{
65    /// Constructs a new [`IpSock`].
66    ///
67    /// `new_ip_socket` constructs a new `IpSock` to the given remote IP
68    /// address from the given local IP address with the given IP protocol. If
69    /// no local IP address is given, one will be chosen automatically. If
70    /// `device` is `Some`, the socket will be bound to the given device - only
71    /// routes which egress over the device will be used. If no route is
72    /// available which egresses over the device - even if routes are available
73    /// which egress over other devices - the socket will be considered
74    /// unroutable.
75    ///
76    /// `new_ip_socket` returns an error if no route to the remote was found in
77    /// the forwarding table or if the given local IP address is not valid for
78    /// the found route.
79    fn new_ip_socket<O>(
80        &mut self,
81        bindings_ctx: &mut BC,
82        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
83    ) -> Result<IpSock<I, Self::WeakDeviceId>, IpSockCreationError>
84    where
85        O: RouteResolutionOptions<I>;
86
87    /// Sends an IP packet on a socket.
88    ///
89    /// The generated packet has its metadata initialized from `socket`,
90    /// including the source and destination addresses, the Time To Live/Hop
91    /// Limit, and the Protocol/Next Header. The outbound device is also chosen
92    /// based on information stored in the socket.
93    ///
94    /// `mtu` may be used to optionally impose an MTU on the outgoing packet.
95    /// Note that the device's MTU will still be imposed on the packet. That is,
96    /// the smaller of `mtu` and the device's MTU will be imposed on the packet.
97    ///
98    /// If the socket is currently unroutable, an error is returned.
99    fn send_ip_packet<S, O>(
100        &mut self,
101        bindings_ctx: &mut BC,
102        socket: &IpSock<I, Self::WeakDeviceId>,
103        body: S,
104        options: &O,
105        tx_metadata: BC::TxMetadata,
106    ) -> Result<(), IpSockSendError>
107    where
108        S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
109        S::Buffer: BufferMut,
110        O: SendOptions<I> + RouteResolutionOptions<I>;
111
112    /// Confirms the provided IP socket destination is reachable.
113    ///
114    /// Implementations must retrieve the next hop given the provided
115    /// IP socket and confirm neighbor reachability for the resolved target
116    /// device.
117    fn confirm_reachable<O>(
118        &mut self,
119        bindings_ctx: &mut BC,
120        socket: &IpSock<I, Self::WeakDeviceId>,
121        options: &O,
122    ) where
123        O: RouteResolutionOptions<I>;
124
125    /// Creates a temporary IP socket and sends a single packet on it.
126    ///
127    /// `local_ip`, `remote_ip`, `proto`, and `options` are passed directly to
128    /// [`IpSocketHandler::new_ip_socket`]. `get_body_from_src_ip` is given the
129    /// source IP address for the packet - which may have been chosen
130    /// automatically if `local_ip` is `None` - and returns the body to be
131    /// encapsulated. This is provided in case the body's contents depend on the
132    /// chosen source IP address.
133    ///
134    /// If `device` is specified, the available routes are limited to those that
135    /// egress over the device.
136    ///
137    /// `mtu` may be used to optionally impose an MTU on the outgoing packet.
138    /// Note that the device's MTU will still be imposed on the packet. That is,
139    /// the smaller of `mtu` and the device's MTU will be imposed on the packet.
140    ///
141    /// # Errors
142    ///
143    /// If an error is encountered while constructing the temporary IP socket
144    /// or sending the packet, `options` will be returned along with the
145    /// error. `get_body_from_src_ip` is fallible, and if there's an error,
146    /// it will be returned as well.
147    fn send_oneshot_ip_packet_with_fallible_serializer<S, E, F, O>(
148        &mut self,
149        bindings_ctx: &mut BC,
150        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
151        tx_metadata: BC::TxMetadata,
152        get_body_from_src_ip: F,
153    ) -> Result<(), SendOneShotIpPacketError<E>>
154    where
155        S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
156        S::Buffer: BufferMut,
157        F: FnOnce(IpDeviceAddr<I::Addr>) -> Result<S, E>,
158        O: SendOptions<I> + RouteResolutionOptions<I>,
159    {
160        let options = args.options;
161        let tmp = self
162            .new_ip_socket(bindings_ctx, args)
163            .map_err(|err| SendOneShotIpPacketError::CreateAndSendError { err: err.into() })?;
164        let packet = get_body_from_src_ip(*tmp.local_ip())
165            .map_err(SendOneShotIpPacketError::SerializeError)?;
166        self.send_ip_packet(bindings_ctx, &tmp, packet, options, tx_metadata)
167            .map_err(|err| SendOneShotIpPacketError::CreateAndSendError { err: err.into() })
168    }
169
170    /// Like `send_oneshot_ip_packet_with_fallible_serializer`, but a dynamic
171    /// transport serializer is used.
172    ///
173    /// This reduces code generation cost at the expense of some runtime
174    /// overhead.
175    fn send_oneshot_ip_packet_with_dyn_fallible_serializer<S, E, F, O>(
176        &mut self,
177        bindings_ctx: &mut BC,
178        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
179        tx_metadata: BC::TxMetadata,
180        get_body_from_src_ip: F,
181    ) -> Result<(), SendOneShotIpPacketError<E>>
182    where
183        S: DynamicTransportSerializer<I>,
184        F: FnOnce(IpDeviceAddr<I::Addr>) -> Result<S, E>,
185        O: SendOptions<I> + RouteResolutionOptions<I>,
186    {
187        let options = args.options;
188        let tmp = self
189            .new_ip_socket(bindings_ctx, args)
190            .map_err(|err| SendOneShotIpPacketError::CreateAndSendError { err: err.into() })?;
191        let mut packet = get_body_from_src_ip(*tmp.local_ip())
192            .map_err(SendOneShotIpPacketError::SerializeError)?;
193        self.send_ip_packet(
194            bindings_ctx,
195            &tmp,
196            DynTransportSerializer::new(&mut packet),
197            options,
198            tx_metadata,
199        )
200        .map_err(|err| SendOneShotIpPacketError::CreateAndSendError { err: err.into() })
201    }
202
203    /// Sends a one-shot IP packet but with a non-fallible serializer.
204    fn send_oneshot_ip_packet<S, F, O>(
205        &mut self,
206        bindings_ctx: &mut BC,
207        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
208        tx_metadata: BC::TxMetadata,
209        get_body_from_src_ip: F,
210    ) -> Result<(), IpSockCreateAndSendError>
211    where
212        S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
213        S::Buffer: BufferMut,
214        F: FnOnce(IpDeviceAddr<I::Addr>) -> S,
215        O: SendOptions<I> + RouteResolutionOptions<I>,
216    {
217        self.send_oneshot_ip_packet_with_fallible_serializer(
218            bindings_ctx,
219            args,
220            tx_metadata,
221            |ip| Ok::<_, !>(get_body_from_src_ip(ip)),
222        )
223        .map_err(|err| match err {
224            SendOneShotIpPacketError::CreateAndSendError { err } => err,
225        })
226    }
227
228    /// Like `send_oneshot_ip_packet`, but a dynamic transport serializer is
229    /// used.
230    ///
231    /// This reduces code generation cost at the expense of some runtime
232    /// overhead.
233    fn send_oneshot_ip_packet_with_dyn_serializer<S, F, O>(
234        &mut self,
235        bindings_ctx: &mut BC,
236        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
237        tx_metadata: BC::TxMetadata,
238        get_body_from_src_ip: F,
239    ) -> Result<(), IpSockCreateAndSendError>
240    where
241        S: DynamicTransportSerializer<I>,
242        F: FnOnce(IpDeviceAddr<I::Addr>) -> S,
243        O: SendOptions<I> + RouteResolutionOptions<I>,
244    {
245        self.send_oneshot_ip_packet_with_dyn_fallible_serializer(
246            bindings_ctx,
247            args,
248            tx_metadata,
249            |ip| Ok::<_, !>(get_body_from_src_ip(ip)),
250        )
251        .map_err(|err| match err {
252            SendOneShotIpPacketError::CreateAndSendError { err } => err,
253        })
254    }
255}
256
257/// An error in sending a packet on an IP socket.
258#[derive(Error, Copy, Clone, Debug, Eq, PartialEq)]
259pub enum IpSockSendError {
260    /// An MTU was exceeded.
261    ///
262    /// This could be caused by an MTU at any layer of the stack, including both
263    /// device MTUs and packet format body size limits.
264    #[error("a maximum transmission unit (MTU) was exceeded")]
265    Mtu,
266    /// The socket is currently unroutable.
267    #[error("the socket is currently unroutable: {0}")]
268    Unroutable(#[from] ResolveRouteError),
269    /// The socket operation would've resulted in illegal loopback addresses on
270    /// a non-loopback device.
271    #[error("illegal loopback address")]
272    IllegalLoopbackAddress,
273    /// Broadcast send is not allowed.
274    #[error("broadcast send is not enabled for the socket")]
275    BroadcastNotAllowed,
276}
277
278impl From<SerializeError<!>> for IpSockSendError {
279    fn from(err: SerializeError<!>) -> IpSockSendError {
280        match err {
281            SerializeError::SizeLimitExceeded => IpSockSendError::Mtu,
282        }
283    }
284}
285
286impl IpSockSendError {
287    /// Constructs a `Result` from an [`IpSendFrameErrorReason`] with
288    /// application-visible [`IpSockSendError`]s in the `Err` variant.
289    ///
290    /// Errors that are not bubbled up to applications are dropped.
291    fn from_ip_send_frame(e: IpSendFrameErrorReason) -> Result<(), Self> {
292        match e {
293            IpSendFrameErrorReason::Device(d) => Self::from_send_frame(d),
294            IpSendFrameErrorReason::IllegalLoopbackAddress => Err(Self::IllegalLoopbackAddress),
295        }
296    }
297
298    /// Constructs a `Result` from a [`SendFrameErrorReason`] with
299    /// application-visible [`IpSockSendError`]s in the `Err` variant.
300    ///
301    /// Errors that are not bubbled up to applications are dropped.
302    fn from_send_frame(e: SendFrameErrorReason) -> Result<(), Self> {
303        match e {
304            SendFrameErrorReason::Alloc
305            | SendFrameErrorReason::QueueFull
306            | SendFrameErrorReason::AddressResolutionFailed => Ok(()),
307            SendFrameErrorReason::SizeConstraintsViolation => Err(Self::Mtu),
308        }
309    }
310}
311
312/// An error in sending a packet on a temporary IP socket.
313#[derive(Error, Copy, Clone, Debug)]
314pub enum IpSockCreateAndSendError {
315    /// Cannot send via temporary socket.
316    #[error("cannot send via temporary socket: {0}")]
317    Send(#[from] IpSockSendError),
318    /// The temporary socket could not be created.
319    #[error("the temporary socket could not be created: {0}")]
320    Create(#[from] IpSockCreationError),
321}
322
323/// The error returned by
324/// [`IpSocketHandler::send_oneshot_ip_packet_with_fallible_serializer`].
325#[derive(Debug)]
326#[allow(missing_docs)]
327pub enum SendOneShotIpPacketError<E> {
328    CreateAndSendError { err: IpSockCreateAndSendError },
329    SerializeError(E),
330}
331
332/// Possible errors when retrieving the maximum transport message size.
333#[derive(Error, Copy, Clone, Debug, Eq, PartialEq)]
334pub enum MmsError {
335    /// Cannot find the device that is used for the ip socket, possibly because
336    /// there is no route.
337    #[error("cannot find the device: {0}")]
338    NoDevice(#[from] ResolveRouteError),
339    /// The MTU provided by the device is too small such that there is no room
340    /// for a transport message at all.
341    #[error("invalid MTU: {0:?}")]
342    MTUTooSmall(Mtu),
343}
344
345/// Gets device related information of an IP socket.
346pub trait DeviceIpSocketHandler<I: IpExt, BC>: DeviceIdContext<AnyDevice> {
347    /// Gets the maximum message size for the transport layer, it equals the
348    /// device MTU minus the IP header size.
349    ///
350    /// This corresponds to the GET_MAXSIZES call described in:
351    /// https://www.rfc-editor.org/rfc/rfc1122#section-3.4
352    fn get_mms<O: RouteResolutionOptions<I>>(
353        &mut self,
354        bindings_ctx: &mut BC,
355        ip_sock: &IpSock<I, Self::WeakDeviceId>,
356        options: &O,
357    ) -> Result<Mms, MmsError>;
358}
359
360/// An error encountered when creating an IP socket.
361#[derive(Error, Copy, Clone, Debug, Eq, PartialEq)]
362pub enum IpSockCreationError {
363    /// An error occurred while looking up a route.
364    #[error("a route cannot be determined: {0}")]
365    Route(#[from] ResolveRouteError),
366}
367
368/// An IP socket.
369#[derive(Clone, Debug)]
370#[cfg_attr(test, derive(PartialEq))]
371pub struct IpSock<I: IpExt, D> {
372    /// The definition of the socket.
373    ///
374    /// This does not change for the lifetime of the socket.
375    definition: IpSockDefinition<I, D>,
376}
377
378impl<I: IpExt, D> IpSock<I, D> {
379    /// Returns the socket's definition.
380    #[cfg(any(test, feature = "testutils"))]
381    pub fn definition(&self) -> &IpSockDefinition<I, D> {
382        &self.definition
383    }
384}
385
386/// The definition of an IP socket.
387///
388/// These values are part of the socket's definition, and never change.
389#[derive(Clone, Debug, PartialEq)]
390pub struct IpSockDefinition<I: IpExt, D> {
391    /// The socket's remote address.
392    pub remote_ip: SocketIpAddr<I::Addr>,
393    /// The socket's local address.
394    ///
395    /// Guaranteed to be unicast in its subnet since it's always equal to an
396    /// address assigned to the local device. We can't use the `UnicastAddr`
397    /// witness type since `Ipv4Addr` doesn't implement `UnicastAddress`.
398    //
399    // TODO(joshlf): Support unnumbered interfaces. Once we do that, a few
400    // issues arise: A) Does the unicast restriction still apply, and is that
401    // even well-defined for IPv4 in the absence of a subnet? B) Presumably we
402    // have to always bind to a particular interface?
403    pub local_ip: IpDeviceAddr<I::Addr>,
404    /// The socket's bound output device.
405    pub device: Option<D>,
406    /// The IP protocol the socket is bound to.
407    pub proto: I::Proto,
408}
409
410impl<I: IpExt, D> IpSock<I, D> {
411    /// Returns the socket's local IP address.
412    pub fn local_ip(&self) -> &IpDeviceAddr<I::Addr> {
413        &self.definition.local_ip
414    }
415    /// Returns the socket's remote IP address.
416    pub fn remote_ip(&self) -> &SocketIpAddr<I::Addr> {
417        &self.definition.remote_ip
418    }
419    /// Returns the selected output interface for the socket, if any.
420    pub fn device(&self) -> Option<&D> {
421        self.definition.device.as_ref()
422    }
423    /// Returns the socket's protocol.
424    pub fn proto(&self) -> I::Proto {
425        self.definition.proto
426    }
427}
428
429// TODO(joshlf): Once we support configuring transport-layer protocols using
430// type parameters, use that to ensure that `proto` is the right protocol for
431// the caller. We will still need to have a separate enforcement mechanism for
432// raw IP sockets once we support those.
433
434/// The bindings execution context for IP sockets.
435pub trait IpSocketBindingsContext<D>:
436    InstantContext
437    + FilterBindingsContext<D>
438    + TxMetadataBindingsTypes
439    + SocketOpsFilterBindingContext<D>
440{
441}
442impl<
443    D,
444    BC: InstantContext
445        + FilterBindingsContext<D>
446        + TxMetadataBindingsTypes
447        + SocketOpsFilterBindingContext<D>,
448> IpSocketBindingsContext<D> for BC
449{
450}
451
452/// The context required in order to implement [`IpSocketHandler`].
453///
454/// Blanket impls of `IpSocketHandler` are provided in terms of
455/// `IpSocketContext`.
456pub trait IpSocketContext<I, BC>:
457    DeviceIdContext<AnyDevice, DeviceId: InterfaceProperties<BC::DeviceClass>>
458    + FilterHandlerProvider<I, BC>
459    + IcmpErrorHandler<I, BC>
460where
461    I: IpLayerIpExt,
462    BC: IpSocketBindingsContext<Self::DeviceId>,
463{
464    /// Returns a route for a socket.
465    ///
466    /// If `device` is specified, the available routes are limited to those that
467    /// egress over the device.
468    fn lookup_route(
469        &mut self,
470        bindings_ctx: &mut BC,
471        device: Option<&Self::DeviceId>,
472        src_ip: Option<IpDeviceAddr<I::Addr>>,
473        dst_ip: RoutableIpAddr<I::Addr>,
474        transparent: bool,
475        marks: &Marks,
476    ) -> Result<ResolvedRoute<I, Self::DeviceId>, ResolveRouteError>;
477
478    /// Send an IP packet to the next-hop node.
479    fn send_ip_packet<S>(
480        &mut self,
481        bindings_ctx: &mut BC,
482        meta: SendIpPacketMeta<I, &Self::DeviceId, SpecifiedAddr<I::Addr>>,
483        body: S,
484        packet_metadata: IpLayerPacketMetadata<I, Self::WeakAddressId, BC>,
485    ) -> Result<(), IpSendFrameError<S>>
486    where
487        S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
488        S::Buffer: BufferMut;
489
490    /// Returns `DeviceId` for the loopback device.
491    fn get_loopback_device(&mut self) -> Option<Self::DeviceId>;
492
493    /// Confirms the provided IP socket destination is reachable.
494    ///
495    /// Implementations must retrieve the next hop given the provided
496    /// IP socket and confirm neighbor reachability for the resolved target
497    /// device.
498    fn confirm_reachable(
499        &mut self,
500        bindings_ctx: &mut BC,
501        dst: SpecifiedAddr<I::Addr>,
502        input: RuleInput<'_, I, Self::DeviceId>,
503    );
504}
505
506/// Enables a blanket implementation of [`IpSocketHandler`].
507///
508/// Implementing this marker trait for a type enables a blanket implementation
509/// of `IpSocketHandler` given the other requirements are met.
510pub trait UseIpSocketHandlerBlanket {}
511
512impl<I, BC, CC> IpSocketHandler<I, BC> for CC
513where
514    I: IpLayerIpExt + IpDeviceStateIpExt,
515    BC: IpSocketBindingsContext<Self::DeviceId>,
516    CC: IpSocketContext<I, BC> + CounterContext<IpCounters<I>> + UseIpSocketHandlerBlanket,
517    CC::DeviceId: netstack3_base::InterfaceProperties<BC::DeviceClass>,
518{
519    fn new_ip_socket<O>(
520        &mut self,
521        bindings_ctx: &mut BC,
522        args: IpSocketArgs<'_, Self::DeviceId, I, O>,
523    ) -> Result<IpSock<I, CC::WeakDeviceId>, IpSockCreationError>
524    where
525        O: RouteResolutionOptions<I>,
526    {
527        let IpSocketArgs { device, local_ip, remote_ip, proto, options } = args;
528        let device = device
529            .as_ref()
530            .map(|d| d.as_strong_ref().ok_or(ResolveRouteError::Unreachable))
531            .transpose()?;
532        let device = device.as_ref().map(|d| d.as_ref());
533
534        // Make sure the remote is routable with a local address before creating
535        // the socket. We do not care about the actual destination here because
536        // we will recalculate it when we send a packet so that the best route
537        // available at the time is used for each outgoing packet.
538        let resolved_route = self.lookup_route(
539            bindings_ctx,
540            device,
541            local_ip,
542            remote_ip,
543            options.transparent(),
544            options.marks(),
545        )?;
546        Ok(new_ip_socket(device, resolved_route, remote_ip, proto))
547    }
548
549    fn send_ip_packet<S, O>(
550        &mut self,
551        bindings_ctx: &mut BC,
552        ip_sock: &IpSock<I, CC::WeakDeviceId>,
553        body: S,
554        options: &O,
555        tx_metadata: BC::TxMetadata,
556    ) -> Result<(), IpSockSendError>
557    where
558        S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
559        S::Buffer: BufferMut,
560        O: SendOptions<I> + RouteResolutionOptions<I>,
561    {
562        send_ip_packet(self, bindings_ctx, ip_sock, body, options, tx_metadata)
563    }
564
565    fn confirm_reachable<O>(
566        &mut self,
567        bindings_ctx: &mut BC,
568        socket: &IpSock<I, CC::WeakDeviceId>,
569        options: &O,
570    ) where
571        O: RouteResolutionOptions<I>,
572    {
573        let bound_device = socket.device().and_then(|weak| weak.upgrade());
574        let bound_device = bound_device.as_ref();
575        let bound_address = Some((*socket.local_ip()).into());
576        let destination = (*socket.remote_ip()).into();
577        IpSocketContext::confirm_reachable(
578            self,
579            bindings_ctx,
580            destination,
581            RuleInput {
582                packet_origin: PacketOrigin::Local { bound_address, bound_device },
583                marks: options.marks(),
584            },
585        )
586    }
587}
588
589/// Provides hooks for altering route resolution behavior of [`IpSock`].
590///
591/// Must be implemented by the socket option type of an `IpSock` when using it
592/// to call [`IpSocketHandler::new_ip_socket`] or
593/// [`IpSocketHandler::send_ip_packet`]. This is implemented as a trait instead
594/// of an inherent impl on a type so that users of sockets that don't need
595/// certain option types can avoid allocating space for those options.
596// TODO(https://fxbug.dev/323389672): We need a mechanism to inform `IpSock` of
597// changes in the route resolution options when it starts caching previously
598// calculated routes. Any changes to the options here *MUST* cause the route to
599// be re-calculated.
600pub trait RouteResolutionOptions<I: Ip> {
601    /// Whether the socket is transparent.
602    ///
603    /// This allows transparently proxying traffic to the socket, and allows the
604    /// socket to be bound to a non-local address.
605    fn transparent(&self) -> bool;
606
607    /// Returns the marks carried by packets created on the socket.
608    fn marks(&self) -> &Marks;
609}
610
611/// Provides hooks for altering sending behavior of [`IpSock`].
612///
613/// Must be implemented by the socket option type of an `IpSock` when using it
614/// to call [`IpSocketHandler::send_ip_packet`]. This is implemented as a trait
615/// instead of an inherent impl on a type so that users of sockets that don't
616/// need certain option types, like TCP for anything multicast-related, can
617/// avoid allocating space for those options.
618pub trait SendOptions<I: IpExt> {
619    /// Returns the hop limit to set on a packet going to the given destination.
620    ///
621    /// If `Some(u)`, `u` will be used as the hop limit (IPv6) or TTL (IPv4) for
622    /// a packet going to the given destination. Otherwise the default value
623    /// will be used.
624    fn hop_limit(&self, destination: &SpecifiedAddr<I::Addr>) -> Option<NonZeroU8>;
625
626    /// Returns true if outgoing multicast packets should be looped back and
627    /// delivered to local receivers who joined the multicast group.
628    fn multicast_loop(&self) -> bool;
629
630    /// `Some` if the socket can be used to send broadcast packets.
631    fn allow_broadcast(&self) -> Option<I::BroadcastMarker>;
632
633    /// Returns TCLASS/TOS field value that should be set in IP headers.
634    fn dscp_and_ecn(&self) -> DscpAndEcn;
635
636    /// The IP MTU to use for this transmission.
637    ///
638    /// Note that the minimum overall MTU is used considering the device and
639    /// path. This option can be used to restrict an MTU to an upper bound.
640    fn mtu(&self) -> Mtu;
641}
642
643/// Empty send and creation options that never overrides default values.
644#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
645pub struct DefaultIpSocketOptions;
646
647impl<I: IpExt> SendOptions<I> for DefaultIpSocketOptions {
648    fn hop_limit(&self, _destination: &SpecifiedAddr<I::Addr>) -> Option<NonZeroU8> {
649        None
650    }
651
652    fn multicast_loop(&self) -> bool {
653        false
654    }
655
656    fn allow_broadcast(&self) -> Option<I::BroadcastMarker> {
657        None
658    }
659
660    fn dscp_and_ecn(&self) -> DscpAndEcn {
661        DscpAndEcn::default()
662    }
663
664    fn mtu(&self) -> Mtu {
665        Mtu::no_limit()
666    }
667}
668
669impl<I: Ip> RouteResolutionOptions<I> for DefaultIpSocketOptions {
670    fn transparent(&self) -> bool {
671        false
672    }
673
674    fn marks(&self) -> &Marks {
675        &Marks::UNMARKED
676    }
677}
678
679/// A trait providing send options delegation to an inner type.
680///
681/// A blanket impl of [`SendOptions`] is provided to all implementers. This
682/// trait has the same shape as `SendOptions` but all the methods provide
683/// default implementations that delegate to the value returned by
684/// `DelegatedSendOptions::Delegate`. For brevity, the default `delegate` is
685/// [`DefaultIpSocketOptions`].
686#[allow(missing_docs)]
687pub trait DelegatedSendOptions<I: IpExt>: OptionDelegationMarker {
688    /// Returns the delegate providing the impl for all default methods.
689    fn delegate(&self) -> &impl SendOptions<I> {
690        &DefaultIpSocketOptions
691    }
692
693    fn hop_limit(&self, destination: &SpecifiedAddr<I::Addr>) -> Option<NonZeroU8> {
694        self.delegate().hop_limit(destination)
695    }
696
697    fn multicast_loop(&self) -> bool {
698        self.delegate().multicast_loop()
699    }
700
701    fn allow_broadcast(&self) -> Option<I::BroadcastMarker> {
702        self.delegate().allow_broadcast()
703    }
704
705    fn dscp_and_ecn(&self) -> DscpAndEcn {
706        self.delegate().dscp_and_ecn()
707    }
708
709    fn mtu(&self) -> Mtu {
710        self.delegate().mtu()
711    }
712}
713
714impl<O: DelegatedSendOptions<I> + OptionDelegationMarker, I: IpExt> SendOptions<I> for O {
715    fn hop_limit(&self, destination: &SpecifiedAddr<I::Addr>) -> Option<NonZeroU8> {
716        self.hop_limit(destination)
717    }
718
719    fn multicast_loop(&self) -> bool {
720        self.multicast_loop()
721    }
722
723    fn allow_broadcast(&self) -> Option<I::BroadcastMarker> {
724        self.allow_broadcast()
725    }
726
727    fn dscp_and_ecn(&self) -> DscpAndEcn {
728        self.dscp_and_ecn()
729    }
730
731    fn mtu(&self) -> Mtu {
732        self.mtu()
733    }
734}
735
736/// A trait providing route resolution options delegation to an inner type.
737///
738/// A blanket impl of [`RouteResolutionOptions`] is provided to all
739/// implementers. This trait has the same shape as `RouteResolutionOptions` but
740/// all the methods provide default implementations that delegate to the value
741/// returned by `DelegatedRouteResolutionOptions::Delegate`. For brevity, the
742/// default `delegate` is [`DefaultIpSocketOptions`].
743#[allow(missing_docs)]
744pub trait DelegatedRouteResolutionOptions<I: Ip>: OptionDelegationMarker {
745    /// Returns the delegate providing the impl for all default methods.
746    fn delegate(&self) -> &impl RouteResolutionOptions<I> {
747        &DefaultIpSocketOptions
748    }
749
750    fn transparent(&self) -> bool {
751        self.delegate().transparent()
752    }
753
754    fn marks(&self) -> &Marks {
755        self.delegate().marks()
756    }
757}
758
759impl<O: DelegatedRouteResolutionOptions<I> + OptionDelegationMarker, I: IpExt>
760    RouteResolutionOptions<I> for O
761{
762    fn transparent(&self) -> bool {
763        self.transparent()
764    }
765
766    fn marks(&self) -> &Marks {
767        self.marks()
768    }
769}
770
771/// A marker trait to allow option delegation traits.
772///
773/// This trait sidesteps trait resolution rules around the delegation traits
774/// because of the `Ip` parameter in them.
775pub trait OptionDelegationMarker {}
776
777/// The configurable hop limits for a socket.
778#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
779pub struct SocketHopLimits<I: Ip> {
780    /// Unicast hop limit.
781    pub unicast: Option<NonZeroU8>,
782    /// Multicast hop limit.
783    // TODO(https://fxbug.dev/42059735): Make this an Option<u8> to allow sending
784    // multicast packets destined only for the local machine.
785    pub multicast: Option<NonZeroU8>,
786    /// An unused marker type signifying the IP version for which these hop
787    /// limits are valid. Including this helps prevent using the wrong hop limits
788    /// when operating on dualstack sockets.
789    pub version: IpVersionMarker<I>,
790}
791
792impl<I: Ip> SocketHopLimits<I> {
793    /// Returns a function that updates the unicast hop limit.
794    pub fn set_unicast(value: Option<NonZeroU8>) -> impl FnOnce(&mut Self) {
795        move |limits| limits.unicast = value
796    }
797
798    /// Returns a function that updates the multicast hop limit.
799    pub fn set_multicast(value: Option<NonZeroU8>) -> impl FnOnce(&mut Self) {
800        move |limits| limits.multicast = value
801    }
802
803    /// Returns the hop limits, or the provided defaults if unset.
804    pub fn get_limits_with_defaults(&self, defaults: &HopLimits) -> HopLimits {
805        let Self { unicast, multicast, version: _ } = self;
806        HopLimits {
807            unicast: unicast.unwrap_or(defaults.unicast),
808            multicast: multicast.unwrap_or(defaults.multicast),
809        }
810    }
811
812    /// Returns the appropriate hop limit to use for the given destination addr.
813    pub fn hop_limit_for_dst(&self, destination: &SpecifiedAddr<I::Addr>) -> Option<NonZeroU8> {
814        let Self { unicast, multicast, version: _ } = self;
815        if destination.is_multicast() { *multicast } else { *unicast }
816    }
817}
818
819fn new_ip_socket<I, D>(
820    requested_device: Option<&D>,
821    route: ResolvedRoute<I, D>,
822    remote_ip: SocketIpAddr<I::Addr>,
823    proto: I::Proto,
824) -> IpSock<I, D::Weak>
825where
826    I: IpExt,
827    D: StrongDeviceIdentifier,
828{
829    // TODO(https://fxbug.dev/323389672): Cache a reference to the route to
830    // avoid the route lookup on send as long as the routing table hasn't
831    // changed in between these operations.
832    let ResolvedRoute {
833        src_addr,
834        device: route_device,
835        local_delivery_device,
836        next_hop: _,
837        internal_forwarding: _,
838    } = route;
839
840    // If the source or destination address require a device, make sure to
841    // set that in the socket definition. Otherwise defer to what was provided.
842    let socket_device = (src_addr.as_ref().must_have_zone() || remote_ip.as_ref().must_have_zone())
843        .then(|| {
844            // NB: The route device might be loopback, and in such cases
845            // we want to bind the socket to the device the source IP is
846            // assigned to instead.
847            local_delivery_device.unwrap_or(route_device)
848        })
849        .as_ref()
850        .or(requested_device)
851        .map(|d| d.downgrade());
852
853    let definition =
854        IpSockDefinition { local_ip: src_addr, remote_ip, device: socket_device, proto };
855    IpSock { definition }
856}
857
858fn send_ip_packet<I, S, BC, CC, O>(
859    core_ctx: &mut CC,
860    bindings_ctx: &mut BC,
861    socket: &IpSock<I, CC::WeakDeviceId>,
862    mut body: S,
863    options: &O,
864    tx_metadata: BC::TxMetadata,
865) -> Result<(), IpSockSendError>
866where
867    I: IpLayerIpExt,
868    S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
869    S::Buffer: BufferMut,
870    BC: IpSocketBindingsContext<CC::DeviceId>,
871    CC: IpSocketContext<I, BC> + CounterContext<IpCounters<I>>,
872    CC::DeviceId: netstack3_base::InterfaceProperties<BC::DeviceClass>,
873    O: SendOptions<I> + RouteResolutionOptions<I>,
874{
875    trace_duration!("ip::send_packet");
876
877    // Extracted to a function without the serializer parameter to ease code
878    // generation.
879    fn resolve<
880        I: IpLayerIpExt,
881        CC: IpSocketContext<I, BC>,
882        BC: IpSocketBindingsContext<CC::DeviceId>,
883    >(
884        core_ctx: &mut CC,
885        bindings_ctx: &mut BC,
886        device: &Option<CC::WeakDeviceId>,
887        local_ip: IpDeviceAddr<I::Addr>,
888        remote_ip: RoutableIpAddr<I::Addr>,
889        transparent: bool,
890        marks: &Marks,
891    ) -> Result<ResolvedRoute<I, CC::DeviceId>, IpSockSendError> {
892        let device = match device.as_ref().map(|d| d.upgrade()) {
893            Some(Some(device)) => Some(device),
894            Some(None) => return Err(ResolveRouteError::Unreachable.into()),
895            None => None,
896        };
897        let route = core_ctx
898            .lookup_route(
899                bindings_ctx,
900                device.as_ref(),
901                Some(local_ip),
902                remote_ip,
903                transparent,
904                marks,
905            )
906            .map_err(|e| IpSockSendError::Unroutable(e))?;
907        assert_eq!(local_ip, route.src_addr);
908        Ok(route)
909    }
910
911    let IpSock {
912        definition: IpSockDefinition { remote_ip, local_ip, device: socket_device, proto },
913    } = socket;
914    let ResolvedRoute {
915        src_addr: local_ip,
916        device: mut egress_device,
917        mut next_hop,
918        mut local_delivery_device,
919        mut internal_forwarding,
920    } = resolve(
921        core_ctx,
922        bindings_ctx,
923        socket_device,
924        *local_ip,
925        *remote_ip,
926        options.transparent(),
927        options.marks(),
928    )?;
929
930    if matches!(next_hop, NextHop::Broadcast(_)) && options.allow_broadcast().is_none() {
931        return Err(IpSockSendError::BroadcastNotAllowed);
932    }
933
934    let previous_dst = remote_ip.addr();
935    let mut packet = filter::TxPacket::new(local_ip.addr(), remote_ip.addr(), *proto, &mut body);
936    // TODO(https://fxbug.dev/565891068): Support TCP GSO by populating GSO
937    // metadata when segment offloading is enabled for the socket.
938    let gso_info = None;
939    let mut packet_metadata =
940        IpLayerPacketMetadata::new_local_tx(tx_metadata, *options.marks(), gso_info);
941
942    let filter_result = core_ctx.filter_handler().local_egress_hook(
943        bindings_ctx,
944        &mut packet,
945        &egress_device,
946        &mut packet_metadata,
947    );
948    match filter_result {
949        filter::Verdict::Stop(filter::DropOrReject::Drop) => {
950            packet_metadata.acknowledge_drop();
951            return Ok(());
952        }
953        filter::Verdict::Stop(filter::DropOrReject::Reject(reject_type)) => {
954            packet_metadata.acknowledge_drop();
955
956            let Some(icmp_error): Option<I::IcmpError> = I::map_ip_out(
957                reject_type,
958                |reject_type| reject_type_to_icmpv4_error(reject_type),
959                |reject_type| reject_type_to_icmpv6_error(reject_type),
960            ) else {
961                debug!("Unsupported reject type: {:?}", reject_type);
962                return Ok(());
963            };
964
965            let src_ip = SocketIpAddr::new_from_witness(local_ip.into_inner().get());
966            let dst_ip = *remote_ip;
967            let ttl = options.hop_limit(&dst_ip.into()).map(|v| v.into()).unwrap_or(1);
968            let packet_builder = I::PacketBuilder::new(
969                src_ip.into_inner().get(),
970                dst_ip.into_inner().get(),
971                ttl,
972                *proto,
973            );
974            let header_len = packet_builder.constraints().header_len();
975            let ip_frame = packet_builder.wrap_body(body);
976            let packet = match ip_frame.serialize_outer(
977                &mut NetworkSerializationContext::default(),
978                packet::NoReuseBufferProvider(packet::new_buf_vec),
979            ) {
980                Ok(packet) => packet,
981                Err((error, _frame)) => {
982                    debug!("Failed to serialize packet {:?}", error);
983                    return Ok(());
984                }
985            };
986
987            // Invoke `send_icmp_error_message` with the `local_ip` as the
988            // `original_source_ip`, which will result in the ICMP error
989            // message getting sent back to the `socket`.
990            core_ctx.send_icmp_error_message(
991                bindings_ctx,
992                /*device=*/ None,
993                /*frame_dst=*/ None,
994                src_ip,
995                dst_ip,
996                packet,
997                icmp_error,
998                header_len,
999                *proto,
1000                &options.marks(),
1001            );
1002
1003            return Ok(());
1004        }
1005        filter::Verdict::Proceed(filter::Accept) => {}
1006    }
1007
1008    let Some(mut local_ip) = IpDeviceAddr::new(packet.src_addr()) else {
1009        packet_metadata.acknowledge_drop();
1010        return Err(IpSockSendError::Unroutable(ResolveRouteError::NoSrcAddr));
1011    };
1012    let Some(remote_ip) = RoutableIpAddr::new(packet.dst_addr()) else {
1013        packet_metadata.acknowledge_drop();
1014        return Err(IpSockSendError::Unroutable(ResolveRouteError::Unreachable));
1015    };
1016
1017    // If the LOCAL_EGRESS hook ended up rewriting the packet's destination, perform
1018    // re-routing based on the new destination.
1019    if remote_ip.addr() != previous_dst {
1020        let ResolvedRoute {
1021            src_addr: new_local_ip,
1022            device: new_device,
1023            next_hop: new_next_hop,
1024            local_delivery_device: new_local_delivery_device,
1025            internal_forwarding: new_internal_forwarding,
1026        } = match resolve(
1027            core_ctx,
1028            bindings_ctx,
1029            socket_device,
1030            local_ip,
1031            remote_ip,
1032            options.transparent(),
1033            options.marks(),
1034        ) {
1035            Ok(r) => r,
1036            Err(err) => {
1037                packet_metadata.acknowledge_drop();
1038                return Err(err);
1039            }
1040        };
1041        local_ip = new_local_ip;
1042        egress_device = new_device;
1043        next_hop = new_next_hop;
1044        local_delivery_device = new_local_delivery_device;
1045        internal_forwarding = new_internal_forwarding;
1046    }
1047
1048    // NB: Hit the forwarding hook if the route leverages internal forwarding.
1049    match internal_forwarding {
1050        InternalForwarding::Used(ingress_device) => {
1051            match core_ctx.filter_handler().forwarding_hook(
1052                &mut packet,
1053                &ingress_device,
1054                &egress_device,
1055                &mut packet_metadata,
1056            ) {
1057                filter::Verdict::Stop(filter::DropOrReject::Drop) => {
1058                    packet_metadata.acknowledge_drop();
1059                    return Ok(());
1060                }
1061                filter::Verdict::Stop(filter::DropOrReject::Reject(_reject_type)) => {
1062                    // TODO(https://fxbug.dev/466098884): Send reject packet.
1063                    packet_metadata.acknowledge_drop();
1064                    return Ok(());
1065                }
1066                filter::Verdict::Proceed(filter::Accept) => {}
1067            }
1068        }
1069        InternalForwarding::NotUsed => {}
1070    }
1071
1072    if let Some(socket_info) = packet_metadata.tx_metadata().socket_info() {
1073        let egress_filter_result = bindings_ctx.socket_ops_filter().on_egress(
1074            &packet,
1075            &egress_device,
1076            socket_info,
1077            packet_metadata.marks(),
1078            packet_metadata.gso_info(),
1079        );
1080
1081        // TODO(https://fxbug.dev/412426836): Implement congestion signal handling.
1082        match egress_filter_result {
1083            SocketEgressFilterResult::Pass { congestion: _ } => (),
1084            SocketEgressFilterResult::Drop { congestion: _ } => {
1085                core_ctx.counters().socket_egress_filter_dropped.increment();
1086                packet_metadata.acknowledge_drop();
1087                return Ok(());
1088            }
1089        }
1090    }
1091
1092    // The packet needs to be delivered locally if it's sent to a broadcast
1093    // or multicast address. For multicast packets this feature can be disabled
1094    // with IP_MULTICAST_LOOP.
1095
1096    let loopback_packet_and_meta = if !egress_device.is_loopback()
1097        && ((options.multicast_loop() && remote_ip.addr().is_multicast())
1098            || next_hop.is_broadcast())
1099    {
1100        let body_copy = body.serialize_new_buf(
1101            &mut NetworkSerializationContext::default(),
1102            PacketConstraints::UNCONSTRAINED,
1103            packet::new_buf_vec,
1104        )?;
1105        let mut loopback_metadata;
1106        SplitMulticastPacketMetadata { primary: packet_metadata, secondary: loopback_metadata } =
1107            packet_metadata.split_for_multicast();
1108        loopback_metadata.set_packet_type(PacketType::Loopback);
1109        Some((
1110            RawIpBody::new(*proto, local_ip.addr(), remote_ip.addr(), body_copy),
1111            loopback_metadata,
1112        ))
1113    } else {
1114        None
1115    };
1116
1117    let destination = match &local_delivery_device {
1118        Some(d) => IpPacketDestination::Loopback(d),
1119        None => IpPacketDestination::from_next_hop(next_hop, remote_ip.into()),
1120    };
1121    let ttl = options.hop_limit(&remote_ip.into());
1122    let meta = SendIpPacketMeta {
1123        device: &egress_device,
1124        src_ip: local_ip.into(),
1125        dst_ip: remote_ip.into(),
1126        destination,
1127        ttl,
1128        proto: *proto,
1129        mtu: options.mtu(),
1130        dscp_and_ecn: options.dscp_and_ecn(),
1131    };
1132    let result =
1133        IpSocketContext::send_ip_packet(core_ctx, bindings_ctx, meta, body, packet_metadata)
1134            .or_else(|IpSendFrameError { serializer: _, error }| {
1135                IpSockSendError::from_ip_send_frame(error)
1136            });
1137
1138    match (result, loopback_packet_and_meta, core_ctx.get_loopback_device()) {
1139        (Ok(()), Some((loopback_packet, packet_metadata)), Some(loopback_device)) => {
1140            let meta = SendIpPacketMeta {
1141                device: &loopback_device,
1142                src_ip: local_ip.into(),
1143                dst_ip: remote_ip.into(),
1144                destination: IpPacketDestination::Loopback(&egress_device),
1145                ttl,
1146                proto: *proto,
1147                mtu: options.mtu(),
1148                dscp_and_ecn: options.dscp_and_ecn(),
1149            };
1150
1151            // The loopback packet will hit the egress hook. LOCAL_EGRESS hook
1152            // is not called again.
1153            IpSocketContext::send_ip_packet(
1154                core_ctx,
1155                bindings_ctx,
1156                meta,
1157                loopback_packet,
1158                packet_metadata,
1159            )
1160            .unwrap_or_else(|IpSendFrameError { serializer: _, error }| {
1161                error!("failed to send loopback packet: {error:?}")
1162            });
1163        }
1164        (Ok(()), Some((_loopback_packet, packet_metadata)), None) => {
1165            error!("can't send a loopback packet without the loopback device");
1166            packet_metadata.acknowledge_drop();
1167        }
1168        (Err(_), Some((_loopback_packet, packet_metadata)), _) => {
1169            // Don't send the loopback packet in case the original one wasn't sent.
1170            packet_metadata.acknowledge_drop();
1171        }
1172        (_, None, _) => (),
1173    }
1174
1175    result
1176}
1177
1178/// Enables a blanket implementation of [`DeviceIpSocketHandler`].
1179///
1180/// Implementing this marker trait for a type enables a blanket implementation
1181/// of `DeviceIpSocketHandler` given the other requirements are met.
1182pub trait UseDeviceIpSocketHandlerBlanket {}
1183
1184impl<I, BC, CC> DeviceIpSocketHandler<I, BC> for CC
1185where
1186    I: IpLayerIpExt + IpDeviceStateIpExt,
1187    BC: IpSocketBindingsContext<CC::DeviceId>,
1188    CC: IpDeviceMtuContext<I> + IpSocketContext<I, BC> + UseDeviceIpSocketHandlerBlanket,
1189{
1190    fn get_mms<O: RouteResolutionOptions<I>>(
1191        &mut self,
1192        bindings_ctx: &mut BC,
1193        ip_sock: &IpSock<I, Self::WeakDeviceId>,
1194        options: &O,
1195    ) -> Result<Mms, MmsError> {
1196        let IpSockDefinition { remote_ip, local_ip, device, proto: _ } = &ip_sock.definition;
1197        let device = device
1198            .as_ref()
1199            .map(|d| d.upgrade().ok_or(ResolveRouteError::Unreachable))
1200            .transpose()?;
1201
1202        let ResolvedRoute {
1203            src_addr: _,
1204            local_delivery_device: _,
1205            device,
1206            next_hop: _,
1207            internal_forwarding: _,
1208        } = self
1209            .lookup_route(
1210                bindings_ctx,
1211                device.as_ref(),
1212                Some(*local_ip),
1213                *remote_ip,
1214                options.transparent(),
1215                options.marks(),
1216            )
1217            .map_err(MmsError::NoDevice)?;
1218        let mtu = self.get_mtu(&device);
1219        // TODO(https://fxbug.dev/42072935): Calculate the options size when they
1220        // are supported.
1221        Mms::from_mtu::<I>(mtu, 0 /* no ip options used */).ok_or(MmsError::MTUTooSmall(mtu))
1222    }
1223}
1224
1225/// IPv6 source address selection as defined in [RFC 6724 Section 5].
1226pub(crate) mod ipv6_source_address_selection {
1227    use net_types::ip::{AddrSubnet, IpAddress as _};
1228
1229    use super::*;
1230
1231    use netstack3_base::Ipv6DeviceAddr;
1232
1233    /// A source address selection candidate.
1234    pub struct SasCandidate<D> {
1235        /// The candidate address and subnet.
1236        pub addr_sub: AddrSubnet<Ipv6Addr, Ipv6DeviceAddr>,
1237        /// True if the address is assigned (i.e. non tentative).
1238        pub assigned: bool,
1239        /// True if the address is deprecated (i.e. not preferred).
1240        pub deprecated: bool,
1241        /// True if the address is temporary (i.e. not permanent).
1242        pub temporary: bool,
1243        /// The device this address belongs to.
1244        pub device: D,
1245    }
1246
1247    /// Selects the source address for an IPv6 socket using the algorithm
1248    /// defined in [RFC 6724 Section 5].
1249    ///
1250    /// This algorithm is only applicable when the user has not explicitly
1251    /// specified a source address.
1252    ///
1253    /// `remote_ip` is the remote IP address of the socket, `outbound_device` is
1254    /// the device over which outbound traffic to `remote_ip` is sent (according
1255    /// to the forwarding table), and `addresses` is an iterator of all
1256    /// addresses on all devices. The algorithm works by iterating over
1257    /// `addresses` and selecting the address which is most preferred according
1258    /// to a set of selection criteria.
1259    pub fn select_ipv6_source_address<
1260        'a,
1261        D: PartialEq,
1262        A,
1263        I: Iterator<Item = A>,
1264        F: FnMut(&A) -> SasCandidate<D>,
1265    >(
1266        remote_ip: Option<SpecifiedAddr<Ipv6Addr>>,
1267        outbound_device: &D,
1268        addresses: I,
1269        mut get_candidate: F,
1270    ) -> Option<A> {
1271        // Source address selection as defined in RFC 6724 Section 5.
1272        //
1273        // The algorithm operates by defining a partial ordering on available
1274        // source addresses, and choosing one of the best address as defined by
1275        // that ordering (given multiple best addresses, the choice from among
1276        // those is implementation-defined). The partial order is defined in
1277        // terms of a sequence of rules. If a given rule defines an order
1278        // between two addresses, then that is their order. Otherwise, the next
1279        // rule must be consulted, and so on until all of the rules are
1280        // exhausted.
1281
1282        addresses
1283            .map(|item| {
1284                let candidate = get_candidate(&item);
1285                (item, candidate)
1286            })
1287            // Tentative addresses are not considered available to the source
1288            // selection algorithm.
1289            .filter(|(_, candidate)| candidate.assigned)
1290            .max_by(|(_, a), (_, b)| {
1291                select_ipv6_source_address_cmp(remote_ip, outbound_device, a, b)
1292            })
1293            .map(|(item, _candidate)| item)
1294    }
1295
1296    /// Comparison operator used by `select_ipv6_source_address`.
1297    fn select_ipv6_source_address_cmp<D: PartialEq>(
1298        remote_ip: Option<SpecifiedAddr<Ipv6Addr>>,
1299        outbound_device: &D,
1300        a: &SasCandidate<D>,
1301        b: &SasCandidate<D>,
1302    ) -> Ordering {
1303        // TODO(https://fxbug.dev/42123500): Implement rules 4, 5.5, and 6.
1304        let SasCandidate {
1305            addr_sub: a_addr_sub,
1306            assigned: a_assigned,
1307            deprecated: a_deprecated,
1308            temporary: a_temporary,
1309            device: a_device,
1310        } = a;
1311        let SasCandidate {
1312            addr_sub: b_addr_sub,
1313            assigned: b_assigned,
1314            deprecated: b_deprecated,
1315            temporary: b_temporary,
1316            device: b_device,
1317        } = b;
1318
1319        let a_addr = a_addr_sub.addr().into_specified();
1320        let b_addr = b_addr_sub.addr().into_specified();
1321
1322        // Assertions required in order for this implementation to be valid.
1323
1324        // Required by the implementation of Rule 1.
1325        if let Some(remote_ip) = remote_ip {
1326            debug_assert!(!(a_addr == remote_ip && b_addr == remote_ip));
1327        }
1328
1329        // Addresses that are not considered assigned are not valid source
1330        // addresses.
1331        debug_assert!(a_assigned);
1332        debug_assert!(b_assigned);
1333
1334        rule_1(remote_ip, a_addr, b_addr)
1335            .then_with(|| rule_2(remote_ip, a_addr, b_addr))
1336            .then_with(|| rule_3(*a_deprecated, *b_deprecated))
1337            .then_with(|| rule_5(outbound_device, a_device, b_device))
1338            .then_with(|| rule_7(*a_temporary, *b_temporary))
1339            .then_with(|| rule_8(remote_ip, *a_addr_sub, *b_addr_sub))
1340    }
1341
1342    // Assumes that `a` and `b` are not both equal to `remote_ip`.
1343    fn rule_1(
1344        remote_ip: Option<SpecifiedAddr<Ipv6Addr>>,
1345        a: SpecifiedAddr<Ipv6Addr>,
1346        b: SpecifiedAddr<Ipv6Addr>,
1347    ) -> Ordering {
1348        let remote_ip = match remote_ip {
1349            Some(remote_ip) => remote_ip,
1350            None => return Ordering::Equal,
1351        };
1352        if (a == remote_ip) != (b == remote_ip) {
1353            // Rule 1: Prefer same address.
1354            //
1355            // Note that both `a` and `b` cannot be equal to `remote_ip` since
1356            // that would imply that we had added the same address twice to the
1357            // same device.
1358            //
1359            // If `(a == remote_ip) != (b == remote_ip)`, then exactly one of
1360            // them is equal. If this inequality does not hold, then they must
1361            // both be unequal to `remote_ip`. In the first case, we have a tie,
1362            // and in the second case, the rule doesn't apply. In either case,
1363            // we move onto the next rule.
1364            if a == remote_ip { Ordering::Greater } else { Ordering::Less }
1365        } else {
1366            Ordering::Equal
1367        }
1368    }
1369
1370    fn rule_2(
1371        remote_ip: Option<SpecifiedAddr<Ipv6Addr>>,
1372        a: SpecifiedAddr<Ipv6Addr>,
1373        b: SpecifiedAddr<Ipv6Addr>,
1374    ) -> Ordering {
1375        // Scope ordering is defined by the Multicast Scope ID, see
1376        // https://datatracker.ietf.org/doc/html/rfc6724#section-3.1 .
1377        let remote_scope = match remote_ip {
1378            Some(remote_ip) => remote_ip.scope().multicast_scope_id(),
1379            None => return Ordering::Equal,
1380        };
1381        let a_scope = a.scope().multicast_scope_id();
1382        let b_scope = b.scope().multicast_scope_id();
1383        if a_scope < b_scope {
1384            if a_scope < remote_scope { Ordering::Less } else { Ordering::Greater }
1385        } else if a_scope > b_scope {
1386            if b_scope < remote_scope { Ordering::Greater } else { Ordering::Less }
1387        } else {
1388            Ordering::Equal
1389        }
1390    }
1391
1392    fn rule_3(a_deprecated: bool, b_deprecated: bool) -> Ordering {
1393        match (a_deprecated, b_deprecated) {
1394            (true, false) => Ordering::Less,
1395            (true, true) | (false, false) => Ordering::Equal,
1396            (false, true) => Ordering::Greater,
1397        }
1398    }
1399
1400    fn rule_5<D: PartialEq>(outbound_device: &D, a_device: &D, b_device: &D) -> Ordering {
1401        if (a_device == outbound_device) != (b_device == outbound_device) {
1402            // Rule 5: Prefer outgoing interface.
1403            if a_device == outbound_device { Ordering::Greater } else { Ordering::Less }
1404        } else {
1405            Ordering::Equal
1406        }
1407    }
1408
1409    // Prefer temporary addresses following rule 7.
1410    fn rule_7(a_temporary: bool, b_temporary: bool) -> Ordering {
1411        match (a_temporary, b_temporary) {
1412            (true, false) => Ordering::Greater,
1413            (true, true) | (false, false) => Ordering::Equal,
1414            (false, true) => Ordering::Less,
1415        }
1416    }
1417
1418    fn rule_8(
1419        remote_ip: Option<SpecifiedAddr<Ipv6Addr>>,
1420        a: AddrSubnet<Ipv6Addr, Ipv6DeviceAddr>,
1421        b: AddrSubnet<Ipv6Addr, Ipv6DeviceAddr>,
1422    ) -> Ordering {
1423        let remote_ip = match remote_ip {
1424            Some(remote_ip) => remote_ip,
1425            None => return Ordering::Equal,
1426        };
1427        // Per RFC 6724 Section 2.2:
1428        //
1429        //   We define the common prefix length CommonPrefixLen(S, D) of a
1430        //   source address S and a destination address D as the length of the
1431        //   longest prefix (looking at the most significant, or leftmost, bits)
1432        //   that the two addresses have in common, up to the length of S's
1433        //   prefix (i.e., the portion of the address not including the
1434        //   interface ID).  For example, CommonPrefixLen(fe80::1, fe80::2) is
1435        //   64.
1436        fn common_prefix_len(
1437            src: AddrSubnet<Ipv6Addr, Ipv6DeviceAddr>,
1438            dst: SpecifiedAddr<Ipv6Addr>,
1439        ) -> u8 {
1440            core::cmp::min(src.addr().common_prefix_len(&dst), src.subnet().prefix())
1441        }
1442
1443        // Rule 8: Use longest matching prefix.
1444        //
1445        // Note that, per RFC 6724 Section 5:
1446        //
1447        //   Rule 8 MAY be superseded if the implementation has other means of
1448        //   choosing among source addresses.  For example, if the
1449        //   implementation somehow knows which source address will result in
1450        //   the "best" communications performance.
1451        //
1452        // We don't currently make use of this option, but it's an option for
1453        // the future.
1454        common_prefix_len(a, remote_ip).cmp(&common_prefix_len(b, remote_ip))
1455    }
1456
1457    #[cfg(test)]
1458    mod tests {
1459        use net_declare::net_ip_v6;
1460
1461        use super::*;
1462
1463        #[test]
1464        fn test_select_ipv6_source_address() {
1465            // Test the comparison operator used by `select_ipv6_source_address`
1466            // by separately testing each comparison condition.
1467
1468            let remote = SpecifiedAddr::new(net_ip_v6!("2001:0db8:1::")).unwrap();
1469            let local0 = SpecifiedAddr::new(net_ip_v6!("2001:0db8:2::")).unwrap();
1470            let local1 = SpecifiedAddr::new(net_ip_v6!("2001:0db8:3::")).unwrap();
1471            let link_local_remote = SpecifiedAddr::new(net_ip_v6!("fe80::1:2:42")).unwrap();
1472            let link_local = SpecifiedAddr::new(net_ip_v6!("fe80::1:2:4")).unwrap();
1473            let dev0 = &0;
1474            let dev1 = &1;
1475            let dev2 = &2;
1476
1477            // Rule 1: Prefer same address
1478            assert_eq!(rule_1(Some(remote), remote, local0), Ordering::Greater);
1479            assert_eq!(rule_1(Some(remote), local0, remote), Ordering::Less);
1480            assert_eq!(rule_1(Some(remote), local0, local1), Ordering::Equal);
1481            assert_eq!(rule_1(None, local0, local1), Ordering::Equal);
1482
1483            // Rule 2: Prefer appropriate scope
1484            assert_eq!(rule_2(Some(remote), local0, local1), Ordering::Equal);
1485            assert_eq!(rule_2(Some(remote), local1, local0), Ordering::Equal);
1486            assert_eq!(rule_2(Some(remote), local0, link_local), Ordering::Greater);
1487            assert_eq!(rule_2(Some(remote), link_local, local0), Ordering::Less);
1488            assert_eq!(rule_2(Some(link_local_remote), local0, link_local), Ordering::Less);
1489            assert_eq!(rule_2(Some(link_local_remote), link_local, local0), Ordering::Greater);
1490            assert_eq!(rule_1(None, local0, link_local), Ordering::Equal);
1491
1492            // Rule 3: Avoid deprecated states
1493            assert_eq!(rule_3(false, true), Ordering::Greater);
1494            assert_eq!(rule_3(true, false), Ordering::Less);
1495            assert_eq!(rule_3(true, true), Ordering::Equal);
1496            assert_eq!(rule_3(false, false), Ordering::Equal);
1497
1498            // Rule 5: Prefer outgoing interface
1499            assert_eq!(rule_5(dev0, dev0, dev2), Ordering::Greater);
1500            assert_eq!(rule_5(dev0, dev2, dev0), Ordering::Less);
1501            assert_eq!(rule_5(dev0, dev0, dev0), Ordering::Equal);
1502            assert_eq!(rule_5(dev0, dev2, dev2), Ordering::Equal);
1503
1504            // Rule 7: Prefer temporary address.
1505            assert_eq!(rule_7(true, false), Ordering::Greater);
1506            assert_eq!(rule_7(false, true), Ordering::Less);
1507            assert_eq!(rule_7(true, true), Ordering::Equal);
1508            assert_eq!(rule_7(false, false), Ordering::Equal);
1509
1510            // Rule 8: Use longest matching prefix.
1511            {
1512                let new_addr_entry = |addr, prefix_len| AddrSubnet::new(addr, prefix_len).unwrap();
1513
1514                // First, test that the longest prefix match is preferred when
1515                // using addresses whose common prefix length is shorter than
1516                // the subnet prefix length.
1517
1518                // 4 leading 0x01 bytes.
1519                let remote = SpecifiedAddr::new(net_ip_v6!("1111::")).unwrap();
1520                // 3 leading 0x01 bytes.
1521                let local0 = new_addr_entry(net_ip_v6!("1110::"), 64);
1522                // 2 leading 0x01 bytes.
1523                let local1 = new_addr_entry(net_ip_v6!("1100::"), 64);
1524
1525                assert_eq!(rule_8(Some(remote), local0, local1), Ordering::Greater);
1526                assert_eq!(rule_8(Some(remote), local1, local0), Ordering::Less);
1527                assert_eq!(rule_8(Some(remote), local0, local0), Ordering::Equal);
1528                assert_eq!(rule_8(Some(remote), local1, local1), Ordering::Equal);
1529                assert_eq!(rule_8(None, local0, local1), Ordering::Equal);
1530
1531                // Second, test that the common prefix length is capped at the
1532                // subnet prefix length.
1533
1534                // 3 leading 0x01 bytes, but a subnet prefix length of 8 (1 byte).
1535                let local0 = new_addr_entry(net_ip_v6!("1110::"), 8);
1536                // 2 leading 0x01 bytes, but a subnet prefix length of 8 (1 byte).
1537                let local1 = new_addr_entry(net_ip_v6!("1100::"), 8);
1538
1539                assert_eq!(rule_8(Some(remote), local0, local1), Ordering::Equal);
1540                assert_eq!(rule_8(Some(remote), local1, local0), Ordering::Equal);
1541                assert_eq!(rule_8(Some(remote), local0, local0), Ordering::Equal);
1542                assert_eq!(rule_8(Some(remote), local1, local1), Ordering::Equal);
1543                assert_eq!(rule_8(None, local0, local1), Ordering::Equal);
1544            }
1545
1546            {
1547                let new_addr_entry = |addr, device| SasCandidate {
1548                    addr_sub: AddrSubnet::new(addr, 128).unwrap(),
1549                    deprecated: false,
1550                    assigned: true,
1551                    temporary: false,
1552                    device,
1553                };
1554
1555                // If no rules apply, then the two address entries are equal.
1556                assert_eq!(
1557                    select_ipv6_source_address_cmp(
1558                        Some(remote),
1559                        dev0,
1560                        &new_addr_entry(*local0, *dev1),
1561                        &new_addr_entry(*local1, *dev2),
1562                    ),
1563                    Ordering::Equal
1564                );
1565            }
1566        }
1567
1568        #[test]
1569        fn test_select_ipv6_source_address_no_remote() {
1570            // Verify that source address selection correctly applies all
1571            // applicable rules when the remote is `None`.
1572            let dev0 = &0;
1573            let dev1 = &1;
1574            let dev2 = &2;
1575
1576            let local0 = SpecifiedAddr::new(net_ip_v6!("2001:0db8:2::")).unwrap();
1577            let local1 = SpecifiedAddr::new(net_ip_v6!("2001:0db8:3::")).unwrap();
1578
1579            let new_addr_entry = |addr, deprecated, device| SasCandidate {
1580                addr_sub: AddrSubnet::new(addr, 128).unwrap(),
1581                deprecated,
1582                assigned: true,
1583                temporary: false,
1584                device,
1585            };
1586
1587            // Verify that Rule 3 still applies (avoid deprecated states).
1588            assert_eq!(
1589                select_ipv6_source_address_cmp(
1590                    None,
1591                    dev0,
1592                    &new_addr_entry(*local0, false, *dev1),
1593                    &new_addr_entry(*local1, true, *dev2),
1594                ),
1595                Ordering::Greater
1596            );
1597
1598            // Verify that Rule 5 still applies (Prefer outgoing interface).
1599            assert_eq!(
1600                select_ipv6_source_address_cmp(
1601                    None,
1602                    dev0,
1603                    &new_addr_entry(*local0, false, *dev0),
1604                    &new_addr_entry(*local1, false, *dev1),
1605                ),
1606                Ordering::Greater
1607            );
1608        }
1609    }
1610}
1611
1612/// Test fake implementations of the traits defined in the `socket` module.
1613#[cfg(any(test, feature = "testutils"))]
1614pub(crate) mod testutil {
1615    use alloc::boxed::Box;
1616    use alloc::vec::Vec;
1617    use core::num::NonZeroUsize;
1618
1619    use crate::internal::types::RoutePreference;
1620    use derivative::Derivative;
1621    use net_types::MulticastAddr;
1622    use net_types::ip::{GenericOverIp, IpAddr, IpAddress, Ipv4, Ipv4Addr, Ipv6, Subnet};
1623    use netstack3_base::testutil::{FakeCoreCtx, FakeStrongDeviceId, FakeWeakDeviceId};
1624    use netstack3_base::{SendFrameContext, SendFrameError};
1625    use netstack3_filter::Tuple;
1626    use netstack3_hashmap::HashMap;
1627
1628    use super::*;
1629    use crate::internal::base::{
1630        BaseTransportIpContext, DEFAULT_HOP_LIMITS, HopLimits, MulticastMembershipHandler,
1631    };
1632    use crate::internal::routing::testutil::FakeIpRoutingCtx;
1633    use crate::internal::routing::{self, RoutingTable};
1634    use crate::internal::types::{Destination, Entry, Metric, RawMetric};
1635
1636    /// A fake implementation of the traits required by the transport layer from
1637    /// the IP layer.
1638    #[derive(Derivative, GenericOverIp)]
1639    #[generic_over_ip(I, Ip)]
1640    #[derivative(Default(bound = ""))]
1641    pub struct FakeIpSocketCtx<I: Ip, D> {
1642        pub(crate) table: RoutingTable<I, D>,
1643        forwarding: FakeIpRoutingCtx<D>,
1644        devices: HashMap<D, FakeDeviceState<I>>,
1645    }
1646
1647    /// A trait enabling [`FakeIpSockeCtx`]'s implementations for
1648    /// [`FakeCoreCtx`] with types that hold a [`FakeIpSocketCtx`] internally,
1649    pub trait InnerFakeIpSocketCtx<I: Ip, D> {
1650        /// Gets a mutable reference to the inner fake context.
1651        fn fake_ip_socket_ctx_mut(&mut self) -> &mut FakeIpSocketCtx<I, D>;
1652    }
1653
1654    impl<I: Ip, D> InnerFakeIpSocketCtx<I, D> for FakeIpSocketCtx<I, D> {
1655        fn fake_ip_socket_ctx_mut(&mut self) -> &mut FakeIpSocketCtx<I, D> {
1656            self
1657        }
1658    }
1659
1660    impl<I: IpExt, D: FakeStrongDeviceId, BC> BaseTransportIpContext<I, BC> for FakeIpSocketCtx<I, D> {
1661        fn get_default_hop_limits(&mut self, device: Option<&D>) -> HopLimits {
1662            device.map_or(DEFAULT_HOP_LIMITS, |device| {
1663                let hop_limit = self.get_device_state(device).default_hop_limit;
1664                HopLimits { unicast: hop_limit, multicast: DEFAULT_HOP_LIMITS.multicast }
1665            })
1666        }
1667
1668        type DevicesWithAddrIter<'a> = Box<dyn Iterator<Item = D> + 'a>;
1669
1670        fn with_devices_with_assigned_addr<O, F: FnOnce(Self::DevicesWithAddrIter<'_>) -> O>(
1671            &mut self,
1672            addr: SpecifiedAddr<I::Addr>,
1673            cb: F,
1674        ) -> O {
1675            cb(Box::new(self.devices.iter().filter_map(move |(device, state)| {
1676                state.addresses.contains(&addr).then(|| device.clone())
1677            })))
1678        }
1679
1680        fn get_original_destination(&mut self, _tuple: &Tuple<I>) -> Option<(I::Addr, u16)> {
1681            unimplemented!()
1682        }
1683    }
1684
1685    impl<I: IpExt, D: FakeStrongDeviceId> DeviceIdContext<AnyDevice> for FakeIpSocketCtx<I, D> {
1686        type DeviceId = D;
1687        type WeakDeviceId = D::Weak;
1688    }
1689
1690    impl<I, State, D, Meta, BC> IpSocketHandler<I, BC> for FakeCoreCtx<State, Meta, D>
1691    where
1692        I: IpExt + FilterIpExt,
1693        State: InnerFakeIpSocketCtx<I, D>,
1694        D: FakeStrongDeviceId,
1695        BC: TxMetadataBindingsTypes,
1696        FakeCoreCtx<State, Meta, D>:
1697            SendFrameContext<BC, SendIpPacketMeta<I, Self::DeviceId, SpecifiedAddr<I::Addr>>>,
1698    {
1699        fn new_ip_socket<O>(
1700            &mut self,
1701            _bindings_ctx: &mut BC,
1702            args: IpSocketArgs<'_, Self::DeviceId, I, O>,
1703        ) -> Result<IpSock<I, Self::WeakDeviceId>, IpSockCreationError>
1704        where
1705            O: RouteResolutionOptions<I>,
1706        {
1707            self.state.fake_ip_socket_ctx_mut().new_ip_socket(args)
1708        }
1709
1710        fn send_ip_packet<S, O>(
1711            &mut self,
1712            bindings_ctx: &mut BC,
1713            socket: &IpSock<I, Self::WeakDeviceId>,
1714            body: S,
1715            options: &O,
1716            // NB: Tx metadata plumbing is not supported for fake socket
1717            // contexts. Drop at the end of the scope.
1718            _tx_meta: BC::TxMetadata,
1719        ) -> Result<(), IpSockSendError>
1720        where
1721            S: TransportPacketSerializer<I> + MaybeSegmentableTransportSerializer,
1722            S::Buffer: BufferMut,
1723            O: SendOptions<I> + RouteResolutionOptions<I>,
1724        {
1725            let meta = self.state.fake_ip_socket_ctx_mut().resolve_send_meta(socket, options)?;
1726            self.send_frame(bindings_ctx, meta, body).or_else(
1727                |SendFrameError { serializer: _, error }| IpSockSendError::from_send_frame(error),
1728            )
1729        }
1730
1731        fn confirm_reachable<O>(
1732            &mut self,
1733            _bindings_ctx: &mut BC,
1734            _socket: &IpSock<I, Self::WeakDeviceId>,
1735            _options: &O,
1736        ) {
1737        }
1738    }
1739
1740    impl<I: IpExt, D: FakeStrongDeviceId, BC> MulticastMembershipHandler<I, BC>
1741        for FakeIpSocketCtx<I, D>
1742    {
1743        fn join_multicast_group(
1744            &mut self,
1745            _bindings_ctx: &mut BC,
1746            device: &Self::DeviceId,
1747            addr: MulticastAddr<<I as Ip>::Addr>,
1748        ) {
1749            let value = self.get_device_state_mut(device).multicast_groups.entry(addr).or_insert(0);
1750            *value = value.checked_add(1).unwrap();
1751        }
1752
1753        fn leave_multicast_group(
1754            &mut self,
1755            _bindings_ctx: &mut BC,
1756            device: &Self::DeviceId,
1757            addr: MulticastAddr<<I as Ip>::Addr>,
1758        ) {
1759            let value = self
1760                .get_device_state_mut(device)
1761                .multicast_groups
1762                .get_mut(&addr)
1763                .unwrap_or_else(|| panic!("no entry for {addr} on {device:?}"));
1764            *value = value.checked_sub(1).unwrap();
1765        }
1766
1767        fn select_device_for_multicast_group(
1768            &mut self,
1769            addr: MulticastAddr<<I as Ip>::Addr>,
1770            _marks: &Marks,
1771        ) -> Result<Self::DeviceId, ResolveRouteError> {
1772            let remote_ip = SocketIpAddr::new_from_multicast(addr);
1773            self.lookup_route(None, None, remote_ip, /* transparent */ false)
1774                .map(|ResolvedRoute { device, .. }| device)
1775        }
1776    }
1777
1778    impl<I, BC, D, State, Meta> BaseTransportIpContext<I, BC> for FakeCoreCtx<State, Meta, D>
1779    where
1780        I: IpExt + FilterIpExt,
1781        D: FakeStrongDeviceId,
1782        State: InnerFakeIpSocketCtx<I, D>,
1783        BC: TxMetadataBindingsTypes,
1784        Self: IpSocketHandler<I, BC, DeviceId = D, WeakDeviceId = FakeWeakDeviceId<D>>,
1785    {
1786        type DevicesWithAddrIter<'a> = Box<dyn Iterator<Item = D> + 'a>;
1787
1788        fn with_devices_with_assigned_addr<O, F: FnOnce(Self::DevicesWithAddrIter<'_>) -> O>(
1789            &mut self,
1790            addr: SpecifiedAddr<I::Addr>,
1791            cb: F,
1792        ) -> O {
1793            BaseTransportIpContext::<I, BC>::with_devices_with_assigned_addr(
1794                self.state.fake_ip_socket_ctx_mut(),
1795                addr,
1796                cb,
1797            )
1798        }
1799
1800        fn get_default_hop_limits(&mut self, device: Option<&Self::DeviceId>) -> HopLimits {
1801            BaseTransportIpContext::<I, BC>::get_default_hop_limits(
1802                self.state.fake_ip_socket_ctx_mut(),
1803                device,
1804            )
1805        }
1806
1807        fn get_original_destination(&mut self, tuple: &Tuple<I>) -> Option<(I::Addr, u16)> {
1808            BaseTransportIpContext::<I, BC>::get_original_destination(
1809                self.state.fake_ip_socket_ctx_mut(),
1810                tuple,
1811            )
1812        }
1813    }
1814
1815    /// A fake context providing [`IpSocketHandler`] for tests.
1816    #[derive(Derivative)]
1817    #[derivative(Default(bound = ""))]
1818    pub struct FakeDualStackIpSocketCtx<D> {
1819        v4: FakeIpSocketCtx<Ipv4, D>,
1820        v6: FakeIpSocketCtx<Ipv6, D>,
1821    }
1822
1823    impl<D: FakeStrongDeviceId> FakeDualStackIpSocketCtx<D> {
1824        /// Creates a new [`FakeDualStackIpSocketCtx`] with `devices`.
1825        pub fn new<A: Into<SpecifiedAddr<IpAddr>>>(
1826            devices: impl IntoIterator<Item = FakeDeviceConfig<D, A>>,
1827        ) -> Self {
1828            let partition =
1829                |v: Vec<A>| -> (Vec<SpecifiedAddr<Ipv4Addr>>, Vec<SpecifiedAddr<Ipv6Addr>>) {
1830                    v.into_iter().fold((Vec::new(), Vec::new()), |(mut v4, mut v6), i| {
1831                        match IpAddr::from(i.into()) {
1832                            IpAddr::V4(a) => v4.push(a),
1833                            IpAddr::V6(a) => v6.push(a),
1834                        }
1835                        (v4, v6)
1836                    })
1837                };
1838
1839            let (v4, v6): (Vec<_>, Vec<_>) = devices
1840                .into_iter()
1841                .map(|FakeDeviceConfig { device, local_ips, remote_ips }| {
1842                    let (local_v4, local_v6) = partition(local_ips);
1843                    let (remote_v4, remote_v6) = partition(remote_ips);
1844                    (
1845                        FakeDeviceConfig {
1846                            device: device.clone(),
1847                            local_ips: local_v4,
1848                            remote_ips: remote_v4,
1849                        },
1850                        FakeDeviceConfig { device, local_ips: local_v6, remote_ips: remote_v6 },
1851                    )
1852                })
1853                .unzip();
1854            Self { v4: FakeIpSocketCtx::new(v4), v6: FakeIpSocketCtx::new(v6) }
1855        }
1856
1857        /// Returns the [`FakeIpSocketCtx`] for IP version `I`.
1858        pub fn inner_mut<I: Ip>(&mut self) -> &mut FakeIpSocketCtx<I, D> {
1859            I::map_ip_out(self, |s| &mut s.v4, |s| &mut s.v6)
1860        }
1861
1862        fn inner<I: Ip>(&self) -> &FakeIpSocketCtx<I, D> {
1863            I::map_ip_out(self, |s| &s.v4, |s| &s.v6)
1864        }
1865
1866        /// Adds a fake direct route to `ip` through `device`.
1867        pub fn add_route(&mut self, device: D, ip: SpecifiedAddr<IpAddr>) {
1868            match IpAddr::from(ip) {
1869                IpAddr::V4(ip) => {
1870                    routing::testutil::add_on_link_routing_entry(&mut self.v4.table, ip, device)
1871                }
1872                IpAddr::V6(ip) => {
1873                    routing::testutil::add_on_link_routing_entry(&mut self.v6.table, ip, device)
1874                }
1875            }
1876        }
1877
1878        /// Adds a fake route to `subnet` through `device`.
1879        pub fn add_subnet_route<A: IpAddress>(&mut self, device: D, subnet: Subnet<A>) {
1880            let entry = Entry {
1881                subnet,
1882                device,
1883                gateway: None,
1884                metric: Metric::ExplicitMetric(RawMetric(0)),
1885                route_preference: RoutePreference::Medium,
1886            };
1887            A::Version::map_ip::<_, ()>(
1888                entry,
1889                |entry_v4| {
1890                    let _ = routing::testutil::add_entry(&mut self.v4.table, entry_v4)
1891                        .expect("Failed to add route");
1892                },
1893                |entry_v6| {
1894                    let _ = routing::testutil::add_entry(&mut self.v6.table, entry_v6)
1895                        .expect("Failed to add route");
1896                },
1897            );
1898        }
1899
1900        /// Returns a mutable reference to fake device state.
1901        pub fn get_device_state_mut<I: IpExt>(&mut self, device: &D) -> &mut FakeDeviceState<I> {
1902            self.inner_mut::<I>().get_device_state_mut(device)
1903        }
1904
1905        /// Returns the fake multicast memberships.
1906        pub fn multicast_memberships<I: IpExt>(
1907            &self,
1908        ) -> HashMap<(D, MulticastAddr<I::Addr>), NonZeroUsize> {
1909            self.inner::<I>().multicast_memberships()
1910        }
1911    }
1912
1913    impl<I: IpExt, S: InnerFakeIpSocketCtx<I, D>, Meta, D: FakeStrongDeviceId, BC>
1914        MulticastMembershipHandler<I, BC> for FakeCoreCtx<S, Meta, D>
1915    {
1916        fn join_multicast_group(
1917            &mut self,
1918            bindings_ctx: &mut BC,
1919            device: &Self::DeviceId,
1920            addr: MulticastAddr<<I as Ip>::Addr>,
1921        ) {
1922            MulticastMembershipHandler::<I, BC>::join_multicast_group(
1923                self.state.fake_ip_socket_ctx_mut(),
1924                bindings_ctx,
1925                device,
1926                addr,
1927            )
1928        }
1929
1930        fn leave_multicast_group(
1931            &mut self,
1932            bindings_ctx: &mut BC,
1933            device: &Self::DeviceId,
1934            addr: MulticastAddr<<I as Ip>::Addr>,
1935        ) {
1936            MulticastMembershipHandler::<I, BC>::leave_multicast_group(
1937                self.state.fake_ip_socket_ctx_mut(),
1938                bindings_ctx,
1939                device,
1940                addr,
1941            )
1942        }
1943
1944        fn select_device_for_multicast_group(
1945            &mut self,
1946            addr: MulticastAddr<<I as Ip>::Addr>,
1947            marks: &Marks,
1948        ) -> Result<Self::DeviceId, ResolveRouteError> {
1949            MulticastMembershipHandler::<I, BC>::select_device_for_multicast_group(
1950                self.state.fake_ip_socket_ctx_mut(),
1951                addr,
1952                marks,
1953            )
1954        }
1955    }
1956
1957    impl<I: Ip, D, State: InnerFakeIpSocketCtx<I, D>, Meta> InnerFakeIpSocketCtx<I, D>
1958        for FakeCoreCtx<State, Meta, D>
1959    {
1960        fn fake_ip_socket_ctx_mut(&mut self) -> &mut FakeIpSocketCtx<I, D> {
1961            self.state.fake_ip_socket_ctx_mut()
1962        }
1963    }
1964
1965    impl<I: Ip, D: FakeStrongDeviceId> InnerFakeIpSocketCtx<I, D> for FakeDualStackIpSocketCtx<D> {
1966        fn fake_ip_socket_ctx_mut(&mut self) -> &mut FakeIpSocketCtx<I, D> {
1967            self.inner_mut::<I>()
1968        }
1969    }
1970
1971    /// A device configuration for fake socket contexts.
1972    #[derive(Clone, GenericOverIp)]
1973    #[generic_over_ip()]
1974    pub struct FakeDeviceConfig<D, A> {
1975        /// The device.
1976        pub device: D,
1977        /// The device's local IPs.
1978        pub local_ips: Vec<A>,
1979        /// The remote IPs reachable from this device.
1980        pub remote_ips: Vec<A>,
1981    }
1982
1983    /// State associated with a fake device in [`FakeIpSocketCtx`].
1984    pub struct FakeDeviceState<I: Ip> {
1985        /// The default hop limit used by the device.
1986        pub default_hop_limit: NonZeroU8,
1987        /// The assigned device addresses.
1988        pub addresses: Vec<SpecifiedAddr<I::Addr>>,
1989        /// The joined multicast groups.
1990        pub multicast_groups: HashMap<MulticastAddr<I::Addr>, usize>,
1991    }
1992
1993    impl<I: Ip> FakeDeviceState<I> {
1994        /// Returns whether this fake device has joined multicast group `addr`.
1995        pub fn is_in_multicast_group(&self, addr: &MulticastAddr<I::Addr>) -> bool {
1996            self.multicast_groups.get(addr).is_some_and(|v| *v != 0)
1997        }
1998    }
1999
2000    impl<I: IpExt, D: FakeStrongDeviceId> FakeIpSocketCtx<I, D> {
2001        /// Creates a new `FakeIpSocketCtx` with the given device
2002        /// configs.
2003        pub fn new(
2004            device_configs: impl IntoIterator<Item = FakeDeviceConfig<D, SpecifiedAddr<I::Addr>>>,
2005        ) -> Self {
2006            let mut table = RoutingTable::default();
2007            let mut devices = HashMap::default();
2008            for FakeDeviceConfig { device, local_ips, remote_ips } in device_configs {
2009                for addr in remote_ips {
2010                    routing::testutil::add_on_link_routing_entry(&mut table, addr, device.clone())
2011                }
2012                let state = FakeDeviceState {
2013                    default_hop_limit: DEFAULT_HOP_LIMITS.unicast,
2014                    addresses: local_ips,
2015                    multicast_groups: Default::default(),
2016                };
2017                assert!(
2018                    devices.insert(device.clone(), state).is_none(),
2019                    "duplicate entries for {device:?}",
2020                );
2021            }
2022
2023            Self { table, devices, forwarding: Default::default() }
2024        }
2025
2026        /// Returns an immutable reference to the fake device state.
2027        pub fn get_device_state(&self, device: &D) -> &FakeDeviceState<I> {
2028            self.devices.get(device).unwrap_or_else(|| panic!("no device {device:?}"))
2029        }
2030
2031        /// Returns a mutable reference to the fake device state.
2032        pub fn get_device_state_mut(&mut self, device: &D) -> &mut FakeDeviceState<I> {
2033            self.devices.get_mut(device).unwrap_or_else(|| panic!("no device {device:?}"))
2034        }
2035
2036        pub(crate) fn multicast_memberships(
2037            &self,
2038        ) -> HashMap<(D, MulticastAddr<I::Addr>), NonZeroUsize> {
2039            self.devices
2040                .iter()
2041                .map(|(device, state)| {
2042                    state.multicast_groups.iter().filter_map(|(group, count)| {
2043                        NonZeroUsize::new(*count).map(|count| ((device.clone(), *group), count))
2044                    })
2045                })
2046                .flatten()
2047                .collect()
2048        }
2049
2050        fn new_ip_socket<O>(
2051            &mut self,
2052            args: IpSocketArgs<'_, D, I, O>,
2053        ) -> Result<IpSock<I, D::Weak>, IpSockCreationError>
2054        where
2055            O: RouteResolutionOptions<I>,
2056        {
2057            let IpSocketArgs { device, local_ip, remote_ip, proto, options } = args;
2058            let device = device
2059                .as_ref()
2060                .map(|d| d.as_strong_ref().ok_or(ResolveRouteError::Unreachable))
2061                .transpose()?;
2062            let device = device.as_ref().map(|d| d.as_ref());
2063            let resolved_route =
2064                self.lookup_route(device, local_ip, remote_ip, options.transparent())?;
2065            Ok(new_ip_socket(device, resolved_route, remote_ip, proto))
2066        }
2067
2068        fn lookup_route(
2069            &mut self,
2070            device: Option<&D>,
2071            local_ip: Option<IpDeviceAddr<I::Addr>>,
2072            addr: RoutableIpAddr<I::Addr>,
2073            transparent: bool,
2074        ) -> Result<ResolvedRoute<I, D>, ResolveRouteError> {
2075            let Self { table, devices, forwarding } = self;
2076            let (destination, ()) = table
2077                .lookup_filter_map(forwarding, device, addr.addr(), |_, d| match &local_ip {
2078                    None => Some(()),
2079                    Some(local_ip) => {
2080                        if transparent {
2081                            return Some(());
2082                        }
2083                        devices.get(d).and_then(|state| {
2084                            state.addresses.contains(local_ip.as_ref()).then_some(())
2085                        })
2086                    }
2087                })
2088                .next()
2089                .ok_or(ResolveRouteError::Unreachable)?;
2090
2091            let Destination { device, next_hop } = destination;
2092            let mut addrs = devices.get(device).unwrap().addresses.iter();
2093            let local_ip = match local_ip {
2094                None => {
2095                    let addr = addrs.next().ok_or(ResolveRouteError::NoSrcAddr)?;
2096                    IpDeviceAddr::new(addr.get()).expect("not valid device addr")
2097                }
2098                Some(local_ip) => {
2099                    if !transparent {
2100                        // We already constrained the set of devices so this
2101                        // should be a given.
2102                        assert!(
2103                            addrs.any(|a| a.get() == local_ip.addr()),
2104                            "didn't find IP {:?} in {:?}",
2105                            local_ip,
2106                            addrs.collect::<Vec<_>>()
2107                        );
2108                    }
2109                    local_ip
2110                }
2111            };
2112
2113            Ok(ResolvedRoute {
2114                src_addr: local_ip,
2115                device: device.clone(),
2116                local_delivery_device: None,
2117                next_hop,
2118                // NB: Keep unit tests simple and skip internal forwarding
2119                // logic. Instead, this is verified by integration tests.
2120                internal_forwarding: InternalForwarding::NotUsed,
2121            })
2122        }
2123
2124        fn resolve_send_meta<O>(
2125            &mut self,
2126            socket: &IpSock<I, D::Weak>,
2127            options: &O,
2128        ) -> Result<SendIpPacketMeta<I, D, SpecifiedAddr<I::Addr>>, IpSockSendError>
2129        where
2130            O: SendOptions<I> + RouteResolutionOptions<I>,
2131        {
2132            let IpSockDefinition { remote_ip, local_ip, device, proto } = &socket.definition;
2133            let device = device
2134                .as_ref()
2135                .map(|d| d.upgrade().ok_or(ResolveRouteError::Unreachable))
2136                .transpose()?;
2137            let ResolvedRoute {
2138                src_addr,
2139                device,
2140                next_hop,
2141                local_delivery_device: _,
2142                internal_forwarding: _,
2143            } = self.lookup_route(
2144                device.as_ref(),
2145                Some(*local_ip),
2146                *remote_ip,
2147                options.transparent(),
2148            )?;
2149
2150            let remote_ip: &SpecifiedAddr<_> = remote_ip.as_ref();
2151
2152            let destination = IpPacketDestination::from_next_hop(next_hop, *remote_ip);
2153            Ok(SendIpPacketMeta {
2154                device,
2155                src_ip: src_addr.into(),
2156                dst_ip: *remote_ip,
2157                destination,
2158                proto: *proto,
2159                ttl: options.hop_limit(remote_ip),
2160                mtu: options.mtu(),
2161                dscp_and_ecn: DscpAndEcn::default(),
2162            })
2163        }
2164    }
2165}