microsandbox-network 0.3.13

Networking types and smoltcp engine for the microsandbox project.
Documentation
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
//! External ICMP echo-only relay: host probe + reply frame synthesis.
//!
//! Relays outbound ICMP Echo Request packets from the guest to the real
//! network via unprivileged `SOCK_DGRAM + IPPROTO_ICMP` sockets, then
//! synthesizes Echo Reply frames back into `rx_ring`.
//!
//! Only Echo Request/Reply is supported. Non-echo ICMP (traceroute,
//! destination unreachable, etc.) is intentionally not relayed.

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::os::fd::FromRawFd;
use std::sync::Arc;

use smoltcp::wire::{
    EthernetAddress, EthernetFrame, EthernetProtocol, EthernetRepr, Icmpv4Packet, Icmpv4Repr,
    Icmpv6Packet, Icmpv6Repr, IpProtocol, Ipv4Packet, Ipv4Repr, Ipv6Packet, Ipv6Repr,
};

use crate::policy::{NetworkPolicy, Protocol};
use crate::shared::SharedState;
use crate::stack::PollLoopConfig;

//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------

/// Timeout for each ICMP echo probe.
const ECHO_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);

/// Receive buffer size for ICMP replies.
const RECV_BUF_SIZE: usize = 1500;

/// Ethernet header length.
const ETH_HDR_LEN: usize = 14;

/// IPv4 header length (no options).
const IPV4_HDR_LEN: usize = 20;

/// IPv6 header length.
const IPV6_HDR_LEN: usize = 40;

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// Whether unprivileged ICMP echo sockets are available on this host.
///
/// Probed once at construction, per address family. Availability may differ
/// between IPv4 and IPv6 on the same host.
#[derive(Debug, Clone, Copy)]
enum EchoBackend {
    /// The address family-specific ping socket probe succeeded.
    Available,
    /// Probe failed — ICMP relay is disabled.
    Unavailable,
}

/// Relays ICMP echo requests from the guest to the real network via
/// unprivileged ICMP sockets.
///
/// Each echo request spawns a fire-and-forget tokio task. No session
/// table is needed — ping traffic is low-volume and each probe is
/// independent.
pub struct IcmpRelay {
    shared: Arc<SharedState>,
    gateway_mac: EthernetAddress,
    guest_mac: EthernetAddress,
    tokio_handle: tokio::runtime::Handle,
    backend_v4: EchoBackend,
    backend_v6: EchoBackend,
}

//--------------------------------------------------------------------------------------------------
// Methods
//--------------------------------------------------------------------------------------------------

impl IcmpRelay {
    /// Create a new ICMP relay, probing for unprivileged socket support.
    pub fn new(
        shared: Arc<SharedState>,
        gateway_mac: [u8; 6],
        guest_mac: [u8; 6],
        tokio_handle: tokio::runtime::Handle,
    ) -> Self {
        let backend_v4 = probe_icmp_socket_v4();
        let backend_v6 = probe_icmp_socket_v6();

        if matches!(backend_v4, EchoBackend::Unavailable) {
            tracing::debug!(
                "unprivileged ICMPv4 echo sockets unavailable — external ICMPv4 relay disabled"
            );
        }
        if matches!(backend_v6, EchoBackend::Unavailable) {
            tracing::debug!(
                "unprivileged ICMPv6 echo sockets unavailable — external ICMPv6 relay disabled"
            );
        }

        Self {
            shared,
            gateway_mac: EthernetAddress(gateway_mac),
            guest_mac: EthernetAddress(guest_mac),
            tokio_handle,
            backend_v4,
            backend_v6,
        }
    }

    /// Try to intercept an outbound frame as an ICMP echo request.
    ///
    /// Returns `true` if the frame was consumed (caller should
    /// `drop_staged_frame()`). Returns `false` if the frame is not an
    /// ICMP echo request or the backend is unavailable — caller should
    /// fall through to `classify_frame`.
    pub fn relay_outbound_if_echo(
        &self,
        frame: &[u8],
        config: &PollLoopConfig,
        policy: &NetworkPolicy,
    ) -> bool {
        let Ok(eth) = EthernetFrame::new_checked(frame) else {
            return false;
        };

        match eth.ethertype() {
            EthernetProtocol::Ipv4 if matches!(self.backend_v4, EchoBackend::Available) => {
                self.try_relay_icmpv4(&eth, config, policy)
            }
            EthernetProtocol::Ipv6 if matches!(self.backend_v6, EchoBackend::Available) => {
                self.try_relay_icmpv6(&eth, config, policy)
            }
            _ => false,
        }
    }
}

impl IcmpRelay {
    /// Try to relay an ICMPv4 echo request. Returns true if consumed.
    fn try_relay_icmpv4(
        &self,
        eth: &EthernetFrame<&[u8]>,
        config: &PollLoopConfig,
        policy: &NetworkPolicy,
    ) -> bool {
        let Ok(ipv4) = Ipv4Packet::new_checked(eth.payload()) else {
            return false;
        };
        if ipv4.next_header() != IpProtocol::Icmp {
            return false;
        }

        // Gateway echo is already handled upstream — skip.
        let dst_ip: Ipv4Addr = ipv4.dst_addr();
        if dst_ip == config.gateway_ipv4 {
            return false;
        }

        let Ok(icmp) = Icmpv4Packet::new_checked(ipv4.payload()) else {
            return false;
        };
        let Ok(Icmpv4Repr::EchoRequest {
            ident,
            seq_no,
            data,
        }) = Icmpv4Repr::parse(&icmp, &smoltcp::phy::ChecksumCapabilities::default())
        else {
            return false; // Not an echo request — fall through.
        };

        // Policy check.
        if policy
            .evaluate_egress_ip(IpAddr::V4(dst_ip), Protocol::Icmpv4)
            .is_deny()
        {
            tracing::debug!(dst = %dst_ip, "ICMP echo denied by policy");
            return true; // Consumed (silently dropped by policy).
        }

        let src_ip: Ipv4Addr = ipv4.src_addr();
        let guest_ident = ident;
        let echo_data = data.to_vec();

        let shared = self.shared.clone();
        let gateway_mac = self.gateway_mac;
        let guest_mac = self.guest_mac;

        tracing::debug!(dst = %dst_ip, seq_no, bytes = echo_data.len(), "relaying ICMPv4 echo request");

        self.tokio_handle.spawn(async move {
            if let Err(e) = icmpv4_echo_task(
                dst_ip,
                src_ip,
                guest_ident,
                seq_no,
                echo_data,
                shared,
                gateway_mac,
                guest_mac,
            )
            .await
            {
                tracing::debug!(dst = %dst_ip, error = %e, "ICMPv4 echo relay failed");
            }
        });

        true
    }

    /// Try to relay an ICMPv6 echo request. Returns true if consumed.
    fn try_relay_icmpv6(
        &self,
        eth: &EthernetFrame<&[u8]>,
        config: &PollLoopConfig,
        policy: &NetworkPolicy,
    ) -> bool {
        let Ok(ipv6) = Ipv6Packet::new_checked(eth.payload()) else {
            return false;
        };
        if ipv6.next_header() != IpProtocol::Icmpv6 {
            return false;
        }

        // Gateway echo is already handled upstream — skip.
        let dst_ip: Ipv6Addr = ipv6.dst_addr();
        if dst_ip == config.gateway_ipv6 {
            return false;
        }

        let Ok(icmp) = Icmpv6Packet::new_checked(ipv6.payload()) else {
            return false;
        };
        let Ok(Icmpv6Repr::EchoRequest {
            ident,
            seq_no,
            data,
        }) = Icmpv6Repr::parse(
            &ipv6.src_addr(),
            &ipv6.dst_addr(),
            &icmp,
            &smoltcp::phy::ChecksumCapabilities::default(),
        )
        else {
            return false; // Not an echo request — fall through.
        };

        // Policy check.
        if policy
            .evaluate_egress_ip(IpAddr::V6(dst_ip), Protocol::Icmpv6)
            .is_deny()
        {
            tracing::debug!(dst = %dst_ip, "ICMPv6 echo denied by policy");
            return true;
        }

        let src_ip: Ipv6Addr = ipv6.src_addr();
        let guest_ident = ident;
        let echo_data = data.to_vec();

        let shared = self.shared.clone();
        let gateway_mac = self.gateway_mac;
        let guest_mac = self.guest_mac;

        tracing::debug!(dst = %dst_ip, seq_no, bytes = echo_data.len(), "relaying ICMPv6 echo request");

        self.tokio_handle.spawn(async move {
            if let Err(e) = icmpv6_echo_task(
                dst_ip,
                src_ip,
                guest_ident,
                seq_no,
                echo_data,
                shared,
                gateway_mac,
                guest_mac,
            )
            .await
            {
                tracing::debug!(dst = %dst_ip, error = %e, "ICMPv6 echo relay failed");
            }
        });

        true
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

/// Probe whether `SOCK_DGRAM + IPPROTO_ICMP` is available.
fn probe_icmp_socket_v4() -> EchoBackend {
    // SAFETY: socket() with valid args; immediately closed on success.
    let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_ICMP) };
    if fd >= 0 {
        unsafe { libc::close(fd) };
        EchoBackend::Available
    } else {
        EchoBackend::Unavailable
    }
}

/// Probe whether `SOCK_DGRAM + IPPROTO_ICMPV6` is available.
fn probe_icmp_socket_v6() -> EchoBackend {
    // SAFETY: socket() with valid args; immediately closed on success.
    let fd = unsafe { libc::socket(libc::AF_INET6, libc::SOCK_DGRAM, libc::IPPROTO_ICMPV6) };
    if fd >= 0 {
        unsafe { libc::close(fd) };
        EchoBackend::Available
    } else {
        EchoBackend::Unavailable
    }
}

/// Open an unprivileged ICMPv4 socket connected to `dst`.
///
/// Uses `SOCK_DGRAM + IPPROTO_ICMP` which the kernel intercepts to
/// provide unprivileged ping. The socket behaves like a connected UDP
/// socket but carries ICMP echo payloads.
///
/// Note: the kernel rewrites the ICMP identifier field to match the
/// socket's ephemeral "port" assignment. The caller must restore the
/// guest's original identifier on the reply.
fn open_icmp_socket_v4(dst: Ipv4Addr) -> std::io::Result<tokio::net::UdpSocket> {
    // SAFETY: socket() + fcntl() + connect() with valid args.
    let fd = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_ICMP) };
    if fd < 0 {
        return Err(std::io::Error::last_os_error());
    }

    // Set non-blocking + close-on-exec via fcntl (portable across macOS/Linux).
    if let Err(e) = set_nonblock_cloexec(fd) {
        unsafe { libc::close(fd) };
        return Err(e);
    }

    let addr = libc::sockaddr_in {
        sin_family: libc::AF_INET as libc::sa_family_t,
        sin_port: 0,
        sin_addr: libc::in_addr {
            s_addr: u32::from(dst).to_be(),
        },
        sin_zero: [0; 8],
        #[cfg(target_os = "macos")]
        sin_len: std::mem::size_of::<libc::sockaddr_in>() as u8,
    };

    // SAFETY: connect() with valid sockaddr_in.
    let ret = unsafe {
        libc::connect(
            fd,
            &addr as *const libc::sockaddr_in as *const libc::sockaddr,
            std::mem::size_of::<libc::sockaddr_in>() as libc::socklen_t,
        )
    };
    if ret < 0 {
        let err = std::io::Error::last_os_error();
        unsafe { libc::close(fd) };
        return Err(err);
    }

    // SAFETY: fd is a valid, connected, non-blocking socket.
    let std_sock = unsafe { std::net::UdpSocket::from_raw_fd(fd) };
    tokio::net::UdpSocket::from_std(std_sock)
}

/// Open an unprivileged ICMPv6 socket connected to `dst`.
fn open_icmp_socket_v6(dst: Ipv6Addr) -> std::io::Result<tokio::net::UdpSocket> {
    let fd = unsafe { libc::socket(libc::AF_INET6, libc::SOCK_DGRAM, libc::IPPROTO_ICMPV6) };
    if fd < 0 {
        return Err(std::io::Error::last_os_error());
    }

    if let Err(e) = set_nonblock_cloexec(fd) {
        unsafe { libc::close(fd) };
        return Err(e);
    }

    let addr = libc::sockaddr_in6 {
        sin6_family: libc::AF_INET6 as libc::sa_family_t,
        sin6_port: 0,
        sin6_flowinfo: 0,
        sin6_addr: libc::in6_addr {
            s6_addr: dst.octets(),
        },
        sin6_scope_id: 0,
        #[cfg(target_os = "macos")]
        sin6_len: std::mem::size_of::<libc::sockaddr_in6>() as u8,
    };

    let ret = unsafe {
        libc::connect(
            fd,
            &addr as *const libc::sockaddr_in6 as *const libc::sockaddr,
            std::mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t,
        )
    };
    if ret < 0 {
        let err = std::io::Error::last_os_error();
        unsafe { libc::close(fd) };
        return Err(err);
    }

    let std_sock = unsafe { std::net::UdpSocket::from_raw_fd(fd) };
    tokio::net::UdpSocket::from_std(std_sock)
}

/// Set `O_NONBLOCK` and `FD_CLOEXEC` on a file descriptor.
fn set_nonblock_cloexec(fd: libc::c_int) -> std::io::Result<()> {
    unsafe {
        let flags = libc::fcntl(fd, libc::F_GETFL);
        if flags < 0 {
            return Err(std::io::Error::last_os_error());
        }
        if libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) < 0 {
            return Err(std::io::Error::last_os_error());
        }
        let flags = libc::fcntl(fd, libc::F_GETFD);
        if flags < 0 {
            return Err(std::io::Error::last_os_error());
        }
        if libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) < 0 {
            return Err(std::io::Error::last_os_error());
        }
    }
    Ok(())
}

/// Send one ICMPv4 echo request, receive reply, and inject a guest frame.
#[allow(clippy::too_many_arguments)]
async fn icmpv4_echo_task(
    dst_ip: Ipv4Addr,
    guest_src_ip: Ipv4Addr,
    guest_ident: u16,
    seq_no: u16,
    echo_data: Vec<u8>,
    shared: Arc<SharedState>,
    gateway_mac: EthernetAddress,
    guest_mac: EthernetAddress,
) -> std::io::Result<()> {
    let socket = open_icmp_socket_v4(dst_ip)?;

    // Build the ICMP echo request payload.
    // For SOCK_DGRAM+IPPROTO_ICMP, we send the ICMP header + data
    // (type, code, checksum, ident, seq_no, data). The kernel
    // rewrites ident to match the socket's ephemeral assignment.
    let icmp_repr = Icmpv4Repr::EchoRequest {
        ident: guest_ident,
        seq_no,
        data: &echo_data,
    };
    let mut icmp_buf = vec![0u8; icmp_repr.buffer_len()];
    icmp_repr.emit(
        &mut Icmpv4Packet::new_unchecked(&mut icmp_buf),
        &smoltcp::phy::ChecksumCapabilities::default(),
    );

    socket.send(&icmp_buf).await?;

    // Receive the echo reply. Different hosts may return either:
    // - a bare ICMP message, or
    // - an IP packet containing the ICMP message.
    let mut recv_buf = vec![0u8; RECV_BUF_SIZE];
    let n = tokio::time::timeout(ECHO_TIMEOUT, socket.recv(&mut recv_buf))
        .await
        .map_err(|_| std::io::Error::new(std::io::ErrorKind::TimedOut, "ICMP echo timeout"))??;

    let (reply_seq, reply_data) = parse_icmpv4_echo_reply(&recv_buf[..n])?;

    // Construct the reply frame with the guest's ORIGINAL ident restored.
    let frame = construct_icmpv4_echo_reply(
        dst_ip,
        guest_src_ip,
        guest_ident,
        reply_seq,
        reply_data,
        gateway_mac,
        guest_mac,
    );

    let frame_len = frame.len();
    if shared.rx_ring.push(frame).is_ok() {
        shared.add_rx_bytes(frame_len);
        shared.rx_wake.wake();
        tracing::debug!(dst = %dst_ip, seq_no = reply_seq, frame_len, "ICMPv4 echo reply injected");
    } else {
        tracing::debug!("ICMP echo reply dropped — rx_ring full");
    }

    Ok(())
}

/// Send one ICMPv6 echo request, receive reply, and inject a guest frame.
#[allow(clippy::too_many_arguments)]
async fn icmpv6_echo_task(
    dst_ip: Ipv6Addr,
    guest_src_ip: Ipv6Addr,
    guest_ident: u16,
    seq_no: u16,
    echo_data: Vec<u8>,
    shared: Arc<SharedState>,
    gateway_mac: EthernetAddress,
    guest_mac: EthernetAddress,
) -> std::io::Result<()> {
    let socket = open_icmp_socket_v6(dst_ip)?;

    let icmp_repr = Icmpv6Repr::EchoRequest {
        ident: guest_ident,
        seq_no,
        data: &echo_data,
    };
    let mut icmp_buf = vec![0u8; icmp_repr.buffer_len()];
    // For SOCK_DGRAM+IPPROTO_ICMPV6, the kernel computes the checksum,
    // so the addresses used here for emit are only for serialization.
    icmp_repr.emit(
        &guest_src_ip,
        &dst_ip,
        &mut Icmpv6Packet::new_unchecked(&mut icmp_buf),
        &smoltcp::phy::ChecksumCapabilities::default(),
    );

    socket.send(&icmp_buf).await?;

    let mut recv_buf = vec![0u8; RECV_BUF_SIZE];
    let n = tokio::time::timeout(ECHO_TIMEOUT, socket.recv(&mut recv_buf))
        .await
        .map_err(|_| std::io::Error::new(std::io::ErrorKind::TimedOut, "ICMPv6 echo timeout"))??;

    let (reply_seq, reply_data) = parse_icmpv6_echo_reply(&recv_buf[..n], dst_ip, guest_src_ip)?;

    let frame = construct_icmpv6_echo_reply(
        dst_ip,
        guest_src_ip,
        guest_ident,
        reply_seq,
        reply_data,
        gateway_mac,
        guest_mac,
    );

    let frame_len = frame.len();
    if shared.rx_ring.push(frame).is_ok() {
        shared.add_rx_bytes(frame_len);
        shared.rx_wake.wake();
        tracing::debug!(dst = %dst_ip, seq_no = reply_seq, frame_len, "ICMPv6 echo reply injected");
    } else {
        tracing::debug!("ICMPv6 echo reply dropped — rx_ring full");
    }

    Ok(())
}

/// Parse an ICMPv4 Echo Reply from a host ping socket receive buffer.
///
/// Some hosts return a bare ICMP message while others prepend the IPv4 header.
fn parse_icmpv4_echo_reply(buf: &[u8]) -> std::io::Result<(u16, &[u8])> {
    if let Ok(reply_icmp) = Icmpv4Packet::new_checked(buf)
        && let Ok(Icmpv4Repr::EchoReply {
            ident: _,
            seq_no,
            data,
        }) = Icmpv4Repr::parse(&reply_icmp, &smoltcp::phy::ChecksumCapabilities::default())
    {
        return Ok((seq_no, data));
    }

    let reply_icmp = Icmpv4Packet::new_checked(extract_ipv4_icmp_payload(buf)?)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
    let Icmpv4Repr::EchoReply {
        ident: _,
        seq_no,
        data,
    } = Icmpv4Repr::parse(&reply_icmp, &smoltcp::phy::ChecksumCapabilities::default())
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?
    else {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv4 reply was not an echo reply",
        ));
    };

    Ok((seq_no, data))
}

/// Parse an ICMPv6 Echo Reply from a host ping socket receive buffer.
///
/// Some hosts return a bare ICMPv6 message while others may prepend an IPv6
/// header. The checksum is validated against the expected remote/guest pair.
fn parse_icmpv6_echo_reply(
    buf: &[u8],
    remote_ip: Ipv6Addr,
    guest_ip: Ipv6Addr,
) -> std::io::Result<(u16, &[u8])> {
    if let Ok(reply_icmp) = Icmpv6Packet::new_checked(buf)
        && let Ok(Icmpv6Repr::EchoReply {
            ident: _,
            seq_no,
            data,
        }) = Icmpv6Repr::parse(
            &remote_ip,
            &guest_ip,
            &reply_icmp,
            &smoltcp::phy::ChecksumCapabilities::default(),
        )
    {
        return Ok((seq_no, data));
    }

    let reply_icmp = Icmpv6Packet::new_checked(extract_ipv6_icmp_payload(buf)?)
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
    let Icmpv6Repr::EchoReply {
        ident: _,
        seq_no,
        data,
    } = Icmpv6Repr::parse(
        &remote_ip,
        &guest_ip,
        &reply_icmp,
        &smoltcp::phy::ChecksumCapabilities::default(),
    )
    .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?
    else {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv6 reply was not an echo reply",
        ));
    };

    Ok((seq_no, data))
}

/// Extract the ICMP payload from an IPv4-framed host ping-socket reply.
///
/// Some hosts prepend an IPv4 header that is not a fully self-consistent
/// wire packet, so this parser intentionally validates only the fields we
/// need to locate the embedded ICMP payload.
fn extract_ipv4_icmp_payload(buf: &[u8]) -> std::io::Result<&[u8]> {
    if buf.len() < IPV4_HDR_LEN {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv4 reply was shorter than an IPv4 header",
        ));
    }

    let version = buf[0] >> 4;
    let header_len = usize::from(buf[0] & 0x0f) * 4;
    if version != 4 || header_len < IPV4_HDR_LEN || header_len > buf.len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv4 reply did not contain a usable IPv4 header",
        ));
    }
    if buf[9] != IpProtocol::Icmp.into() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv4 reply did not contain an ICMP payload",
        ));
    }

    Ok(&buf[header_len..])
}

/// Extract the ICMPv6 payload from an IPv6-framed host ping-socket reply.
fn extract_ipv6_icmp_payload(buf: &[u8]) -> std::io::Result<&[u8]> {
    if buf.len() < IPV6_HDR_LEN {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv6 reply was shorter than an IPv6 header",
        ));
    }

    let version = buf[0] >> 4;
    if version != 6 {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv6 reply did not contain a usable IPv6 header",
        ));
    }
    if buf[6] != IpProtocol::Icmpv6.into() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "host ICMPv6 reply did not contain an ICMPv6 payload",
        ));
    }

    Ok(&buf[IPV6_HDR_LEN..])
}

/// Construct an Ethernet + IPv4 + ICMPv4 Echo Reply frame for the guest.
fn construct_icmpv4_echo_reply(
    src_ip: Ipv4Addr,
    dst_ip: Ipv4Addr,
    ident: u16,
    seq_no: u16,
    data: &[u8],
    gateway_mac: EthernetAddress,
    guest_mac: EthernetAddress,
) -> Vec<u8> {
    let icmp_repr = Icmpv4Repr::EchoReply {
        ident,
        seq_no,
        data,
    };
    let ipv4_repr = Ipv4Repr {
        src_addr: src_ip,
        dst_addr: dst_ip,
        next_header: IpProtocol::Icmp,
        payload_len: icmp_repr.buffer_len(),
        hop_limit: 64,
    };
    let frame_len = ETH_HDR_LEN + ipv4_repr.buffer_len() + icmp_repr.buffer_len();
    let mut buf = vec![0u8; frame_len];

    // Ethernet header.
    let mut eth_frame = EthernetFrame::new_unchecked(&mut buf);
    EthernetRepr {
        src_addr: gateway_mac,
        dst_addr: guest_mac,
        ethertype: EthernetProtocol::Ipv4,
    }
    .emit(&mut eth_frame);

    // IPv4 header.
    ipv4_repr.emit(
        &mut Ipv4Packet::new_unchecked(&mut buf[ETH_HDR_LEN..ETH_HDR_LEN + IPV4_HDR_LEN]),
        &smoltcp::phy::ChecksumCapabilities::default(),
    );

    // ICMP header + payload.
    icmp_repr.emit(
        &mut Icmpv4Packet::new_unchecked(&mut buf[ETH_HDR_LEN + IPV4_HDR_LEN..]),
        &smoltcp::phy::ChecksumCapabilities::default(),
    );

    buf
}

/// Construct an Ethernet + IPv6 + ICMPv6 Echo Reply frame for the guest.
fn construct_icmpv6_echo_reply(
    src_ip: Ipv6Addr,
    dst_ip: Ipv6Addr,
    ident: u16,
    seq_no: u16,
    data: &[u8],
    gateway_mac: EthernetAddress,
    guest_mac: EthernetAddress,
) -> Vec<u8> {
    let icmp_repr = Icmpv6Repr::EchoReply {
        ident,
        seq_no,
        data,
    };
    let frame_len = ETH_HDR_LEN + IPV6_HDR_LEN + icmp_repr.buffer_len();
    let mut buf = vec![0u8; frame_len];

    // Ethernet header.
    let mut eth_frame = EthernetFrame::new_unchecked(&mut buf);
    EthernetRepr {
        src_addr: gateway_mac,
        dst_addr: guest_mac,
        ethertype: EthernetProtocol::Ipv6,
    }
    .emit(&mut eth_frame);

    // IPv6 header.
    Ipv6Repr {
        src_addr: src_ip,
        dst_addr: dst_ip,
        next_header: IpProtocol::Icmpv6,
        payload_len: icmp_repr.buffer_len(),
        hop_limit: 64,
    }
    .emit(&mut Ipv6Packet::new_unchecked(
        &mut buf[ETH_HDR_LEN..ETH_HDR_LEN + IPV6_HDR_LEN],
    ));

    // ICMPv6 header + payload (checksum computed from src/dst addresses).
    icmp_repr.emit(
        &src_ip,
        &dst_ip,
        &mut Icmpv6Packet::new_unchecked(&mut buf[ETH_HDR_LEN + IPV6_HDR_LEN..]),
        &smoltcp::phy::ChecksumCapabilities::default(),
    );

    buf
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

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

    use smoltcp::phy::ChecksumCapabilities;

    #[test]
    fn construct_icmpv4_reply_roundtrips() {
        let frame = construct_icmpv4_echo_reply(
            Ipv4Addr::new(8, 8, 8, 8),
            Ipv4Addr::new(100, 96, 0, 2),
            0x1234,
            0x0001,
            b"hello",
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]),
        );

        let eth = EthernetFrame::new_checked(&frame).unwrap();
        assert_eq!(eth.ethertype(), EthernetProtocol::Ipv4);
        assert_eq!(
            eth.src_addr(),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01])
        );
        assert_eq!(
            eth.dst_addr(),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02])
        );

        let ipv4 = Ipv4Packet::new_checked(eth.payload()).unwrap();
        assert_eq!(Ipv4Addr::from(ipv4.src_addr()), Ipv4Addr::new(8, 8, 8, 8));
        assert_eq!(
            Ipv4Addr::from(ipv4.dst_addr()),
            Ipv4Addr::new(100, 96, 0, 2)
        );
        assert_eq!(ipv4.next_header(), IpProtocol::Icmp);

        let icmp = Icmpv4Packet::new_checked(ipv4.payload()).unwrap();
        let repr = Icmpv4Repr::parse(&icmp, &ChecksumCapabilities::default()).unwrap();
        assert_eq!(
            repr,
            Icmpv4Repr::EchoReply {
                ident: 0x1234,
                seq_no: 0x0001,
                data: b"hello",
            }
        );
    }

    #[test]
    fn construct_icmpv6_reply_roundtrips() {
        let src: Ipv6Addr = "2001:db8::1".parse().unwrap();
        let dst: Ipv6Addr = "fd42:6d73:62::2".parse().unwrap();
        let frame = construct_icmpv6_echo_reply(
            src,
            dst,
            0x5678,
            0x0002,
            b"v6ping",
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]),
        );

        let eth = EthernetFrame::new_checked(&frame).unwrap();
        assert_eq!(eth.ethertype(), EthernetProtocol::Ipv6);

        let ipv6 = Ipv6Packet::new_checked(eth.payload()).unwrap();
        assert_eq!(ipv6.next_header(), IpProtocol::Icmpv6);

        let icmp = Icmpv6Packet::new_checked(ipv6.payload()).unwrap();
        let repr = Icmpv6Repr::parse(
            &src.into(),
            &dst.into(),
            &icmp,
            &ChecksumCapabilities::default(),
        )
        .unwrap();
        assert_eq!(
            repr,
            Icmpv6Repr::EchoReply {
                ident: 0x5678,
                seq_no: 0x0002,
                data: b"v6ping",
            }
        );

        // Verify ICMPv6 checksum is non-zero (mandatory per RFC 8200).
        assert_ne!(icmp.checksum(), 0, "ICMPv6 checksum must not be zero");
        assert!(
            icmp.verify_checksum(
                &smoltcp::wire::Ipv6Address::from(src),
                &smoltcp::wire::Ipv6Address::from(dst),
            ),
            "ICMPv6 checksum must be valid"
        );
    }

    #[test]
    fn construct_icmpv4_reply_preserves_ident_and_seqno() {
        let frame = construct_icmpv4_echo_reply(
            Ipv4Addr::new(1, 2, 3, 4),
            Ipv4Addr::new(10, 0, 0, 2),
            0xABCD,
            0xEF01,
            b"test-payload",
            EthernetAddress([0; 6]),
            EthernetAddress([0; 6]),
        );

        let eth = EthernetFrame::new_checked(&frame).unwrap();
        let ipv4 = Ipv4Packet::new_checked(eth.payload()).unwrap();
        let icmp = Icmpv4Packet::new_checked(ipv4.payload()).unwrap();
        let repr = Icmpv4Repr::parse(&icmp, &ChecksumCapabilities::default()).unwrap();
        assert_eq!(
            repr,
            Icmpv4Repr::EchoReply {
                ident: 0xABCD,
                seq_no: 0xEF01,
                data: b"test-payload",
            }
        );
    }

    #[test]
    fn construct_icmpv6_reply_preserves_ident_and_seqno() {
        let src: Ipv6Addr = "2001:db8::1".parse().unwrap();
        let dst: Ipv6Addr = "fd42:6d73:62::2".parse().unwrap();
        let frame = construct_icmpv6_echo_reply(
            src,
            dst,
            0xBEEF,
            0xCAFE,
            b"test6",
            EthernetAddress([0; 6]),
            EthernetAddress([0; 6]),
        );

        let eth = EthernetFrame::new_checked(&frame).unwrap();
        let ipv6 = Ipv6Packet::new_checked(eth.payload()).unwrap();
        let icmp = Icmpv6Packet::new_checked(ipv6.payload()).unwrap();
        let repr = Icmpv6Repr::parse(
            &src.into(),
            &dst.into(),
            &icmp,
            &ChecksumCapabilities::default(),
        )
        .unwrap();
        assert_eq!(
            repr,
            Icmpv6Repr::EchoReply {
                ident: 0xBEEF,
                seq_no: 0xCAFE,
                data: b"test6",
            }
        );
    }

    #[test]
    fn probe_does_not_panic() {
        // Result depends on host — just verify it doesn't panic.
        let _ = probe_icmp_socket_v4();
        let _ = probe_icmp_socket_v6();
    }

    #[test]
    fn parse_icmpv4_reply_accepts_bare_icmp() {
        let icmp_repr = Icmpv4Repr::EchoReply {
            ident: 0x1234,
            seq_no: 0x0001,
            data: b"hello",
        };
        let mut buf = vec![0u8; icmp_repr.buffer_len()];
        icmp_repr.emit(
            &mut Icmpv4Packet::new_unchecked(&mut buf),
            &ChecksumCapabilities::default(),
        );

        let (seq_no, data) = parse_icmpv4_echo_reply(&buf).unwrap();
        assert_eq!(seq_no, 0x0001);
        assert_eq!(data, b"hello");
    }

    #[test]
    fn parse_icmpv4_reply_accepts_ipv4_plus_icmp() {
        let frame = construct_icmpv4_echo_reply(
            Ipv4Addr::new(8, 8, 8, 8),
            Ipv4Addr::new(100, 96, 0, 2),
            0x1234,
            0x0001,
            b"hello",
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]),
        );
        let eth = EthernetFrame::new_checked(&frame).unwrap();

        let (seq_no, data) = parse_icmpv4_echo_reply(eth.payload()).unwrap();
        assert_eq!(seq_no, 0x0001);
        assert_eq!(data, b"hello");
    }

    #[test]
    fn parse_icmpv4_reply_accepts_macos_ping_socket_shape() {
        let buf = [
            0x45, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x01, 0x73, 0xef, 0x08, 0x08,
            0x08, 0x08, 0xc0, 0xa8, 0x01, 0x35, 0x00, 0x00, 0xa9, 0xf8, 0x12, 0x34, 0x00, 0x01,
            0x68, 0x65, 0x6c, 0x6c, 0x6f,
        ];

        let (seq_no, data) = parse_icmpv4_echo_reply(&buf).unwrap();
        assert_eq!(seq_no, 0x0001);
        assert_eq!(data, b"hello");
    }

    #[test]
    fn parse_icmpv6_reply_accepts_bare_icmpv6() {
        let src: Ipv6Addr = "2001:db8::1".parse().unwrap();
        let dst: Ipv6Addr = "fd42:6d73:62::2".parse().unwrap();
        let icmp_repr = Icmpv6Repr::EchoReply {
            ident: 0x1234,
            seq_no: 0x0002,
            data: b"hello6",
        };
        let mut buf = vec![0u8; icmp_repr.buffer_len()];
        icmp_repr.emit(
            &src.into(),
            &dst.into(),
            &mut Icmpv6Packet::new_unchecked(&mut buf),
            &ChecksumCapabilities::default(),
        );

        let (seq_no, data) = parse_icmpv6_echo_reply(&buf, src, dst).unwrap();
        assert_eq!(seq_no, 0x0002);
        assert_eq!(data, b"hello6");
    }

    #[test]
    fn parse_icmpv6_reply_accepts_ipv6_plus_icmpv6() {
        let src: Ipv6Addr = "2001:db8::1".parse().unwrap();
        let dst: Ipv6Addr = "fd42:6d73:62::2".parse().unwrap();
        let frame = construct_icmpv6_echo_reply(
            src,
            dst,
            0x5678,
            0x0002,
            b"v6ping",
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]),
            EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]),
        );
        let eth = EthernetFrame::new_checked(&frame).unwrap();

        let (seq_no, data) = parse_icmpv6_echo_reply(eth.payload(), src, dst).unwrap();
        assert_eq!(seq_no, 0x0002);
        assert_eq!(data, b"v6ping");
    }
}