1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! An implementation of multicast forwarding.
//!
//! Multicast forwarding is the ability for netstack to forward multicast
//! packets that arrive on an interface out multiple interfaces (while also
//! optionally delivering the packet to the host itself if the arrival host has
//! an interest in the packet).
//!
//! Note that multicast forwarding decisions are made by consulting the
//! multicast routing table, a routing table entirely separate from the unicast
//! routing table(s).

pub(crate) mod api;
pub(crate) mod counters;
pub(crate) mod packet_queue;
pub(crate) mod route;
pub(crate) mod state;

use core::sync::atomic::Ordering;

use net_types::ip::{GenericOverIp, Ip, IpVersionMarker};
use netstack3_base::{
    AnyDevice, AtomicInstant, CounterContext, DeviceIdContext, EventContext, FrameDestination,
    HandleableTimer, InstantBindingsTypes, InstantContext, TimerBindingsTypes, TimerContext,
    WeakDeviceIdentifier,
};
use packet_formats::ip::IpPacket;
use zerocopy::SplitByteSlice;

use crate::internal::multicast_forwarding::counters::MulticastForwardingCounters;
use crate::internal::multicast_forwarding::packet_queue::QueuePacketOutcome;
use crate::internal::multicast_forwarding::route::{
    Action, MulticastRouteEntry, MulticastRouteTargets,
};
use crate::multicast_forwarding::{
    MulticastForwardingPendingPacketsContext, MulticastForwardingState,
    MulticastForwardingStateContext, MulticastRoute, MulticastRouteKey,
    MulticastRouteTableContext as _,
};
use crate::{IpLayerEvent, IpLayerIpExt};

/// Required types for multicast forwarding provided by Bindings.
pub trait MulticastForwardingBindingsTypes: InstantBindingsTypes + TimerBindingsTypes {}
impl<BT: InstantBindingsTypes + TimerBindingsTypes> MulticastForwardingBindingsTypes for BT {}

/// Required functionality for multicast forwarding provided by Bindings.
pub trait MulticastForwardingBindingsContext<I: IpLayerIpExt, D>:
    MulticastForwardingBindingsTypes + InstantContext + TimerContext + EventContext<IpLayerEvent<D, I>>
{
}
impl<
        I: IpLayerIpExt,
        D,
        BC: MulticastForwardingBindingsTypes
            + InstantContext
            + TimerContext
            + EventContext<IpLayerEvent<D, I>>,
    > MulticastForwardingBindingsContext<I, D> for BC
{
}

/// Device related functionality required by multicast forwarding.
pub trait MulticastForwardingDeviceContext<I: IpLayerIpExt>: DeviceIdContext<AnyDevice> {
    /// True if the given device has multicast forwarding enabled.
    fn is_device_multicast_forwarding_enabled(&mut self, dev: &Self::DeviceId) -> bool;
}

/// A timer event for multicast forwarding.
#[derive(Clone, Debug, Eq, GenericOverIp, Hash, PartialEq)]
#[generic_over_ip(I, Ip)]
pub enum MulticastForwardingTimerId<I: Ip> {
    /// A trigger to perform garbage collection on the pending packets table.
    PendingPacketsGc(IpVersionMarker<I>),
}

impl<
        I: IpLayerIpExt,
        BC: MulticastForwardingBindingsContext<I, CC::DeviceId>,
        CC: MulticastForwardingStateContext<I, BC> + CounterContext<MulticastForwardingCounters<I>>,
    > HandleableTimer<CC, BC> for MulticastForwardingTimerId<I>
{
    fn handle(self, core_ctx: &mut CC, bindings_ctx: &mut BC, _: BC::UniqueTimerId) {
        match self {
            MulticastForwardingTimerId::PendingPacketsGc(_) => {
                core_ctx.with_state(|state, ctx| match state {
                    // Multicast forwarding was disabled after GC was scheduled;
                    // there are no resources to GC now.
                    MulticastForwardingState::Disabled => {}
                    MulticastForwardingState::Enabled(state) => {
                        ctx.increment(|counters: &MulticastForwardingCounters<I>| {
                            &counters.pending_table_gc
                        });
                        let removed_count = ctx.with_pending_table_mut(state, |pending_table| {
                            pending_table.run_garbage_collection(bindings_ctx)
                        });
                        ctx.add(removed_count, |counters: &MulticastForwardingCounters<I>| {
                            &counters.pending_packet_drops_gc
                        });
                    }
                })
            }
        }
    }
}

/// Events that may be published by the multicast forwarding engine.
#[derive(Debug, Eq, Hash, PartialEq, GenericOverIp)]
#[generic_over_ip(I, Ip)]
pub enum MulticastForwardingEvent<I: IpLayerIpExt, D> {
    /// A multicast packet was received for which there was no applicable route.
    MissingRoute {
        /// The key of the route that's missing.
        key: MulticastRouteKey<I>,
        /// The interface on which the packet was received.
        input_interface: D,
    },
    /// A multicast packet was received on an unexpected input interface.
    WrongInputInterface {
        /// The key of the route with the unexpected input interface.
        key: MulticastRouteKey<I>,
        /// The interface on which the packet was received.
        actual_input_interface: D,
        /// The interface on which the packet was expected (as specified in the
        /// multicast route).
        expected_input_interface: D,
    },
}

impl<I: IpLayerIpExt, D> MulticastForwardingEvent<I, D> {
    pub(crate) fn map_device<O, F: Fn(D) -> O>(self, map: F) -> MulticastForwardingEvent<I, O> {
        match self {
            MulticastForwardingEvent::MissingRoute { key, input_interface } => {
                MulticastForwardingEvent::MissingRoute {
                    key,
                    input_interface: map(input_interface),
                }
            }
            MulticastForwardingEvent::WrongInputInterface {
                key,
                actual_input_interface,
                expected_input_interface,
            } => MulticastForwardingEvent::WrongInputInterface {
                key,
                actual_input_interface: map(actual_input_interface),
                expected_input_interface: map(expected_input_interface),
            },
        }
    }
}

impl<I: IpLayerIpExt, D: WeakDeviceIdentifier> MulticastForwardingEvent<I, D> {
    /// Upgrades the device IDs held by this event.
    pub fn upgrade_device_id(self) -> Option<MulticastForwardingEvent<I, D::Strong>> {
        match self {
            MulticastForwardingEvent::MissingRoute { key, input_interface } => {
                Some(MulticastForwardingEvent::MissingRoute {
                    key,
                    input_interface: input_interface.upgrade()?,
                })
            }
            MulticastForwardingEvent::WrongInputInterface {
                key,
                actual_input_interface,
                expected_input_interface,
            } => Some(MulticastForwardingEvent::WrongInputInterface {
                key,
                actual_input_interface: actual_input_interface.upgrade()?,
                expected_input_interface: expected_input_interface.upgrade()?,
            }),
        }
    }
}

/// Query the multicast route table and return the forwarding targets.
///
/// `None` may be returned in several situations:
///   * if multicast forwarding is disabled (either stack-wide or for the
///     provided `dev`),
///   * if the packets src/dst addrs are not viable for multicast forwarding
///     (see the requirements on [`MulticastRouteKey`]), or
///   * if the route table does not have an entry suitable for this packet.
///
/// In the latter case, the packet is stashed in the
/// [`MulticastForwardingPendingPackets`] table, and a relevant event is
/// dispatched to bindings.
///
/// Note that the returned targets are not synchronized with the multicast route
/// table and may grow stale if the table is updated.
pub(crate) fn lookup_multicast_route_or_stash_packet<I, B, CC, BC>(
    core_ctx: &mut CC,
    bindings_ctx: &mut BC,
    packet: &I::Packet<B>,
    dev: &CC::DeviceId,
    frame_dst: Option<FrameDestination>,
) -> Option<MulticastRouteTargets<CC::DeviceId>>
where
    I: IpLayerIpExt,
    B: SplitByteSlice,
    CC: MulticastForwardingStateContext<I, BC>
        + MulticastForwardingDeviceContext<I>
        + CounterContext<MulticastForwardingCounters<I>>,
    BC: MulticastForwardingBindingsContext<I, CC::DeviceId>,
{
    core_ctx.increment(|counters: &MulticastForwardingCounters<I>| &counters.rx);
    // Short circuit if the packet's addresses don't constitute a valid
    // multicast route key (e.g. src is not unicast, or dst is not multicast).
    let key = MulticastRouteKey::new(packet.src_ip(), packet.dst_ip())?;
    core_ctx.increment(|counters: &MulticastForwardingCounters<I>| &counters.no_tx_invalid_key);

    // Short circuit if the device has forwarding disabled.
    if !core_ctx.is_device_multicast_forwarding_enabled(dev) {
        core_ctx
            .increment(|counters: &MulticastForwardingCounters<I>| &counters.no_tx_disabled_dev);
        return None;
    }

    core_ctx.with_state(|state, ctx| {
        // Short circuit if forwarding is disabled stack-wide.
        let Some(state) = state.enabled() else {
            ctx.increment(|counters: &MulticastForwardingCounters<I>| {
                &counters.no_tx_disabled_stack_wide
            });
            return None;
        };
        ctx.with_route_table(state, |route_table, ctx| {
            if let Some(MulticastRouteEntry {
                route: MulticastRoute { input_interface, action },
                stats,
            }) = route_table.get(&key)
            {
                if dev != input_interface {
                    ctx.increment(|counters: &MulticastForwardingCounters<I>| {
                        &counters.no_tx_wrong_dev
                    });
                    bindings_ctx.on_event(
                        MulticastForwardingEvent::WrongInputInterface {
                            key,
                            actual_input_interface: dev.clone(),
                            expected_input_interface: input_interface.clone(),
                        }
                        .into(),
                    );
                    return None;
                }

                stats.last_used.store_max(bindings_ctx.now(), Ordering::Relaxed);

                match action {
                    Action::Forward(targets) => {
                        ctx.increment(|counters: &MulticastForwardingCounters<I>| &counters.tx);
                        return Some(targets.clone());
                    }
                }
            }
            ctx.increment(|counters: &MulticastForwardingCounters<I>| &counters.pending_packets);
            match ctx.with_pending_table_mut(state, |pending_table| {
                pending_table.try_queue_packet(bindings_ctx, key.clone(), packet, dev, frame_dst)
            }) {
                QueuePacketOutcome::QueuedInNewQueue => {
                    bindings_ctx.on_event(
                        MulticastForwardingEvent::MissingRoute {
                            key,
                            input_interface: dev.clone(),
                        }
                        .into(),
                    );
                }
                QueuePacketOutcome::QueuedInExistingQueue => {}
                QueuePacketOutcome::ExistingQueueFull => {
                    ctx.increment(|counters: &MulticastForwardingCounters<I>| {
                        &counters.pending_packet_drops_queue_full
                    });
                }
            }
            return None;
        })
    })
}

#[cfg(test)]
mod testutil {
    use super::*;

    use alloc::collections::HashSet;
    use alloc::rc::Rc;
    use alloc::vec::Vec;
    use core::cell::RefCell;
    use derivative::Derivative;
    use net_declare::{net_ip_v4, net_ip_v6};
    use net_types::ip::{Ipv4, Ipv4Addr, Ipv6, Ipv6Addr, Mtu};
    use net_types::{MulticastAddr, SpecifiedAddr};
    use netstack3_base::testutil::{FakeStrongDeviceId, MultipleDevicesId};
    use netstack3_base::{CoreTimerContext, CounterContext, CtxPair, FrameDestination};
    use netstack3_filter::ProofOfEgressCheck;
    use packet::{BufferMut, InnerPacketBuilder, Serializer};
    use packet_formats::ip::{IpPacketBuilder, IpProto};

    use crate::device::IpDeviceSendContext;
    use crate::internal::icmp::{IcmpErrorHandler, IcmpHandlerIpExt};
    use crate::multicast_forwarding::{
        MulticastForwardingApi, MulticastForwardingEnabledState, MulticastForwardingPendingPackets,
        MulticastForwardingPendingPacketsContext, MulticastForwardingState, MulticastRouteTable,
        MulticastRouteTableContext,
    };
    use crate::{IpCounters, IpDeviceMtuContext, IpLayerEvent, IpPacketDestination};

    /// An IP extension trait providing constants for various IP addresses.
    pub(crate) trait TestIpExt: IpLayerIpExt {
        const SRC1: Self::Addr;
        const SRC2: Self::Addr;
        const DST1: Self::Addr;
        const DST2: Self::Addr;
    }

    impl TestIpExt for Ipv4 {
        const SRC1: Ipv4Addr = net_ip_v4!("192.0.2.1");
        const SRC2: Ipv4Addr = net_ip_v4!("192.0.2.2");
        const DST1: Ipv4Addr = net_ip_v4!("224.0.1.1");
        const DST2: Ipv4Addr = net_ip_v4!("224.0.1.2");
    }

    impl TestIpExt for Ipv6 {
        const SRC1: Ipv6Addr = net_ip_v6!("2001:0DB8::1");
        const SRC2: Ipv6Addr = net_ip_v6!("2001:0DB8::2");
        const DST1: Ipv6Addr = net_ip_v6!("ff0e::1");
        const DST2: Ipv6Addr = net_ip_v6!("ff0e::2");
    }

    /// Constructs a buffer containing an IP packet with sensible defaults.
    pub(crate) fn new_ip_packet_buf<I: IpLayerIpExt>(
        src_addr: I::Addr,
        dst_addr: I::Addr,
    ) -> impl AsRef<[u8]> {
        const TTL: u8 = 255;
        /// Arbitrary data to put inside of an IP packet.
        const IP_BODY: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        IP_BODY
            .into_serializer()
            .encapsulate(I::PacketBuilder::new(src_addr, dst_addr, TTL, IpProto::Udp.into()))
            .serialize_vec_outer()
            .unwrap()
    }

    #[derive(Debug, PartialEq)]
    pub(crate) struct SentPacket<I: IpLayerIpExt, D> {
        pub(crate) dst: MulticastAddr<I::Addr>,
        pub(crate) device: D,
    }

    #[derive(Derivative)]
    #[derivative(Default(bound = ""))]
    pub(crate) struct FakeCoreCtxState<I: IpLayerIpExt, D: FakeStrongDeviceId> {
        // NB: Hold in an `Rc<RefCell<...>>` to switch to runtime borrow
        // checking. This allows us to borrow the multicast forwarding state at
        // the same time as the outer `FakeCoreCtx` is mutably borrowed.
        pub(crate) multicast_forwarding:
            Rc<RefCell<MulticastForwardingState<I, D, FakeBindingsCtx<I, D>>>>,
        // The list of devices that have multicast forwarding enabled.
        pub(crate) forwarding_enabled_devices: HashSet<D>,
        // The list of packets sent by the netstack.
        pub(crate) sent_packets: Vec<SentPacket<I, D>>,
        counters: IpCounters<I>,
        multicast_forwarding_counters: MulticastForwardingCounters<I>,
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> FakeCoreCtxState<I, D> {
        pub(crate) fn set_multicast_forwarding_enabled_for_dev(&mut self, dev: D, enabled: bool) {
            if enabled {
                let _: bool = self.forwarding_enabled_devices.insert(dev);
            } else {
                let _: bool = self.forwarding_enabled_devices.remove(&dev);
            }
        }

        pub(crate) fn take_sent_packets(&mut self) -> Vec<SentPacket<I, D>> {
            core::mem::take(&mut self.sent_packets)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> CounterContext<IpCounters<I>>
        for FakeCoreCtxState<I, D>
    {
        fn with_counters<O, F: FnOnce(&IpCounters<I>) -> O>(&self, cb: F) -> O {
            cb(&self.counters)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> CounterContext<MulticastForwardingCounters<I>>
        for FakeCoreCtxState<I, D>
    {
        fn with_counters<O, F: FnOnce(&MulticastForwardingCounters<I>) -> O>(&self, cb: F) -> O {
            cb(&self.multicast_forwarding_counters)
        }
    }

    pub(crate) type FakeBindingsCtx<I, D> = netstack3_base::testutil::FakeBindingsCtx<
        MulticastForwardingTimerId<I>,
        IpLayerEvent<D, I>,
        (),
        (),
    >;
    pub(crate) type FakeCoreCtx<I, D> =
        netstack3_base::testutil::FakeCoreCtx<FakeCoreCtxState<I, D>, (), D>;

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId>
        MulticastForwardingStateContext<I, FakeBindingsCtx<I, D>> for FakeCoreCtx<I, D>
    {
        type Ctx<'a> = FakeCoreCtx<I, D>;
        fn with_state<
            O,
            F: FnOnce(
                &MulticastForwardingState<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
                &mut Self::Ctx<'_>,
            ) -> O,
        >(
            &mut self,
            cb: F,
        ) -> O {
            let state = self.state.multicast_forwarding.clone();
            let borrow = state.borrow();
            cb(&borrow, self)
        }
        fn with_state_mut<
            O,
            F: FnOnce(
                &mut MulticastForwardingState<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
                &mut Self::Ctx<'_>,
            ) -> O,
        >(
            &mut self,
            cb: F,
        ) -> O {
            let state = self.state.multicast_forwarding.clone();
            let mut borrow = state.borrow_mut();
            cb(&mut borrow, self)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId>
        MulticastRouteTableContext<I, FakeBindingsCtx<I, D>> for FakeCoreCtx<I, D>
    {
        type Ctx<'a> = FakeCoreCtx<I, D>;
        fn with_route_table<
            O,
            F: FnOnce(
                &MulticastRouteTable<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
                &mut Self::Ctx<'_>,
            ) -> O,
        >(
            &mut self,
            state: &MulticastForwardingEnabledState<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
            cb: F,
        ) -> O {
            let route_table = state.route_table().read();
            cb(&route_table, self)
        }
        fn with_route_table_mut<
            O,
            F: FnOnce(
                &mut MulticastRouteTable<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
                &mut Self::Ctx<'_>,
            ) -> O,
        >(
            &mut self,
            state: &MulticastForwardingEnabledState<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
            cb: F,
        ) -> O {
            let mut route_table = state.route_table().write();
            cb(&mut route_table, self)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId>
        MulticastForwardingPendingPacketsContext<I, FakeBindingsCtx<I, D>> for FakeCoreCtx<I, D>
    {
        fn with_pending_table_mut<
            O,
            F: FnOnce(
                &mut MulticastForwardingPendingPackets<I, Self::WeakDeviceId, FakeBindingsCtx<I, D>>,
            ) -> O,
        >(
            &mut self,
            state: &MulticastForwardingEnabledState<I, Self::DeviceId, FakeBindingsCtx<I, D>>,
            cb: F,
        ) -> O {
            let mut pending_table = state.pending_table().lock();
            cb(&mut pending_table)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> MulticastForwardingDeviceContext<I>
        for FakeCoreCtx<I, D>
    {
        fn is_device_multicast_forwarding_enabled(&mut self, device_id: &Self::DeviceId) -> bool {
            self.state.forwarding_enabled_devices.contains(device_id)
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId>
        CoreTimerContext<MulticastForwardingTimerId<I>, FakeBindingsCtx<I, D>>
        for FakeCoreCtx<I, D>
    {
        fn convert_timer(
            dispatch_id: MulticastForwardingTimerId<I>,
        ) -> MulticastForwardingTimerId<I> {
            dispatch_id
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> IpDeviceSendContext<I, FakeBindingsCtx<I, D>>
        for FakeCoreCtx<I, D>
    {
        fn send_ip_frame<S>(
            &mut self,
            _bindings_ctx: &mut FakeBindingsCtx<I, D>,
            device_id: &D,
            destination: IpPacketDestination<I, &D>,
            _body: S,
            _egress_proof: ProofOfEgressCheck,
        ) -> Result<(), netstack3_base::SendFrameError<S>>
        where
            S: Serializer,
            S::Buffer: BufferMut,
        {
            let dst = match destination {
                IpPacketDestination::Multicast(dst) => dst,
                dst => panic!("unexpected sent packet: destination={dst:?}"),
            };
            self.state.sent_packets.push(SentPacket { dst, device: device_id.clone() });
            Ok(())
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> IpDeviceMtuContext<I> for FakeCoreCtx<I, D> {
        fn get_mtu(&mut self, _device_id: &Self::DeviceId) -> Mtu {
            Mtu::max()
        }
    }

    impl<I: IpLayerIpExt, D: FakeStrongDeviceId> IcmpErrorHandler<I, FakeBindingsCtx<I, D>>
        for FakeCoreCtx<I, D>
    {
        fn send_icmp_error_message<B: BufferMut>(
            &mut self,
            _bindings_ctx: &mut FakeBindingsCtx<I, D>,
            _device: &D,
            _frame_dst: Option<FrameDestination>,
            _src_ip: <I as IcmpHandlerIpExt>::SourceAddress,
            _dst_ip: SpecifiedAddr<I::Addr>,
            _original_packet: B,
            _error: I::IcmpError,
        ) {
            unimplemented!()
        }
    }

    pub(crate) fn new_api<I: IpLayerIpExt>() -> MulticastForwardingApi<
        I,
        CtxPair<FakeCoreCtx<I, MultipleDevicesId>, FakeBindingsCtx<I, MultipleDevicesId>>,
    > {
        MulticastForwardingApi::new(CtxPair::with_core_ctx(FakeCoreCtx::with_state(
            Default::default(),
        )))
    }

    /// A test helper to access the [`MulticastForwardingPendingPackets`] table.
    ///
    /// # Panics
    ///
    /// Panics if multicast forwarding is disabled.
    pub(crate) fn with_pending_table<I, O, F, CC, BT>(core_ctx: &mut CC, cb: F) -> O
    where
        I: IpLayerIpExt,
        CC: MulticastForwardingStateContext<I, BT>,
        BT: MulticastForwardingBindingsTypes,
        F: FnOnce(&mut MulticastForwardingPendingPackets<I, CC::WeakDeviceId, BT>) -> O,
    {
        core_ctx.with_state(|state, ctx| {
            let state = state.enabled().unwrap();
            ctx.with_route_table(state, |_routing_table, ctx| {
                ctx.with_pending_table_mut(state, |pending_table| cb(pending_table))
            })
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use alloc::vec;
    use core::time::Duration;

    use ip_test_macro::ip_test;
    use netstack3_base::testutil::MultipleDevicesId;
    use packet::ParseBuffer;
    use test_case::test_case;
    use testutil::TestIpExt;

    use crate::internal::multicast_forwarding::route::MulticastRouteStats;
    use crate::multicast_forwarding::MulticastRouteTarget;

    struct LookupTestCase {
        // Whether multicast forwarding is enabled for the netstack.
        enabled: bool,
        // Whether multicast forwarding is enabled for the device.
        dev_enabled: bool,
        // Whether the packet has the correct src/dst addrs.
        right_key: bool,
        // Whether the packet arrived on the correct device.
        right_dev: bool,
    }
    const LOOKUP_SUCCESS_CASE: LookupTestCase =
        LookupTestCase { enabled: true, dev_enabled: true, right_key: true, right_dev: true };

    #[ip_test(I)]
    #[test_case(LOOKUP_SUCCESS_CASE => true; "success")]
    #[test_case(LookupTestCase{enabled: false, ..LOOKUP_SUCCESS_CASE} => false; "disabled")]
    #[test_case(LookupTestCase{dev_enabled: false, ..LOOKUP_SUCCESS_CASE} => false; "dev_disabled")]
    #[test_case(LookupTestCase{right_key: false, ..LOOKUP_SUCCESS_CASE} => false; "wrong_key")]
    #[test_case(LookupTestCase{right_dev: false, ..LOOKUP_SUCCESS_CASE} => false; "wrong_dev")]
    fn lookup_route<I: TestIpExt>(test_case: LookupTestCase) -> bool {
        let LookupTestCase { enabled, dev_enabled, right_key, right_dev } = test_case;
        const FRAME_DST: Option<FrameDestination> = None;
        let mut api = testutil::new_api::<I>();

        let expected_key = MulticastRouteKey::new(I::SRC1, I::DST1).unwrap();
        let actual_key = if right_key {
            expected_key.clone()
        } else {
            MulticastRouteKey::new(I::SRC2, I::DST2).unwrap()
        };

        let expected_dev = MultipleDevicesId::A;
        let actual_dev = if right_dev { expected_dev } else { MultipleDevicesId::B };

        if enabled {
            assert!(api.enable());
            // NB: Only attempt to install the route when enabled; Otherwise
            // installation fails.
            assert_eq!(
                api.add_multicast_route(
                    expected_key.clone(),
                    MulticastRoute::new_forward(
                        expected_dev,
                        [MulticastRouteTarget {
                            output_interface: MultipleDevicesId::C,
                            min_ttl: 0
                        }]
                        .into()
                    )
                    .unwrap()
                ),
                Ok(None)
            );
        }

        api.core_ctx().state.set_multicast_forwarding_enabled_for_dev(actual_dev, dev_enabled);

        let (core_ctx, bindings_ctx) = api.contexts();
        let creation_time = bindings_ctx.now();
        bindings_ctx.timers.instant.sleep(Duration::from_secs(5));
        let lookup_time = bindings_ctx.now();
        assert!(lookup_time > creation_time);

        let buf = testutil::new_ip_packet_buf::<I>(actual_key.src_addr(), actual_key.dst_addr());
        let mut buf_ref = buf.as_ref();
        let packet = buf_ref.parse::<I::Packet<_>>().expect("parse should succeed");

        let route = lookup_multicast_route_or_stash_packet(
            core_ctx,
            bindings_ctx,
            &packet,
            &actual_dev,
            FRAME_DST,
        );

        // Verify that multicast routing events are generated.
        let mut expected_events = vec![];
        if !right_key {
            expected_events.push(IpLayerEvent::MulticastForwarding(
                MulticastForwardingEvent::MissingRoute {
                    key: actual_key.clone(),
                    input_interface: actual_dev,
                },
            ));
        }
        if !right_dev {
            expected_events.push(IpLayerEvent::MulticastForwarding(
                MulticastForwardingEvent::WrongInputInterface {
                    key: actual_key,
                    actual_input_interface: actual_dev,
                    expected_input_interface: expected_dev,
                },
            ));
        }
        assert_eq!(bindings_ctx.take_events(), expected_events);

        let lookup_succeeded = route.is_some();

        if enabled {
            // Verify that on success, the last_used field in stats is updated.
            let expected_stats = if lookup_succeeded {
                MulticastRouteStats { last_used: lookup_time }
            } else {
                MulticastRouteStats { last_used: creation_time }
            };
            assert_eq!(api.get_route_stats(&expected_key), Ok(Some(expected_stats)));
        }

        // Verify that counters are updated.
        api.core_ctx().with_counters(|counters: &MulticastForwardingCounters<I>| {
            assert_eq!(counters.rx.get(), 1);
            assert_eq!(counters.tx.get(), if lookup_succeeded { 1 } else { 0 });
            assert_eq!(counters.no_tx_disabled_dev.get(), if dev_enabled { 0 } else { 1 });
            assert_eq!(counters.no_tx_disabled_stack_wide.get(), if enabled { 0 } else { 1 });
            assert_eq!(counters.no_tx_wrong_dev.get(), if right_dev { 0 } else { 1 });
            assert_eq!(counters.pending_packets.get(), if right_key { 0 } else { 1 });
        });

        lookup_succeeded
    }
}