Skip to main content

microsandbox_network/
stack.rs

1//! smoltcp interface setup, frame classification, and poll loop.
2//!
3//! This module contains the core networking event loop that runs on a
4//! dedicated OS thread. It bridges guest ethernet frames (via
5//! [`SmoltcpDevice`]) to smoltcp's TCP/IP stack and services connections
6//! through tokio proxy tasks.
7
8use std::collections::HashSet;
9use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
10use std::sync::Arc;
11use std::sync::atomic::Ordering;
12
13use smoltcp::iface::{Config, Interface, SocketSet};
14use smoltcp::time::Instant;
15
16use smoltcp::wire::{
17    EthernetAddress, EthernetFrame, EthernetProtocol, HardwareAddress, Icmpv4Packet, Icmpv4Repr,
18    Icmpv6Packet, Icmpv6Repr, IpAddress, IpCidr, IpProtocol, Ipv4Packet, Ipv4Repr, Ipv6Packet,
19    Ipv6Repr, TcpPacket, UdpPacket,
20};
21
22use crate::config::{DnsConfig, PublishedPort};
23use crate::conn::ConnectionTracker;
24use crate::device::SmoltcpDevice;
25use crate::dns::common::ports::DnsPortType;
26use crate::dns::{
27    interceptor::DnsInterceptor,
28    proxies::{dot::DotProxy, tcp::DnsTcpProxy},
29};
30use crate::icmp_relay::IcmpRelay;
31use crate::policy::{EgressEvaluation, HostnameSource, NetworkPolicy, Protocol};
32use crate::proxy;
33use crate::publisher::PortPublisher;
34use crate::secrets::config::SecretsConfig;
35use crate::shared::SharedState;
36use crate::tls::{proxy as tls_proxy, state::TlsState};
37use crate::udp_relay::UdpRelay;
38
39//--------------------------------------------------------------------------------------------------
40// Types
41//--------------------------------------------------------------------------------------------------
42
43/// Result of classifying a guest ethernet frame before smoltcp processes it.
44///
45/// Pre-inspection allows the poll loop to:
46/// - Create TCP sockets before smoltcp sees a SYN (preventing auto-RST).
47/// - Handle non-DNS UDP outside smoltcp (smoltcp lacks wildcard port binding).
48/// - Route DNS queries to the interception handler.
49pub enum FrameAction {
50    /// TCP SYN to a new destination — create a smoltcp socket before
51    /// letting smoltcp process the frame.
52    TcpSyn { src: SocketAddr, dst: SocketAddr },
53
54    /// Non-DNS UDP datagram — handle entirely outside smoltcp via the UDP
55    /// relay.
56    UdpRelay { src: SocketAddr, dst: SocketAddr },
57
58    /// DNS query (UDP to port 53) — let smoltcp's bound UDP socket handle it.
59    Dns,
60
61    /// Everything else (ARP, NDP, ICMP, TCP data/ACK/FIN, etc.) — let
62    /// smoltcp process normally.
63    Passthrough,
64}
65
66/// Resolved network parameters for the poll loop. Created by
67/// `SmoltcpNetwork::new()` from `NetworkConfig` + sandbox slot.
68pub struct PollLoopConfig {
69    /// Gateway MAC address (smoltcp's identity on the virtual LAN).
70    pub gateway_mac: [u8; 6],
71    /// Guest MAC address.
72    pub guest_mac: [u8; 6],
73    /// Gateway addresses owned by the smoltcp virtual stack. Each family
74    /// is `Some` when that family is active for this sandbox (host has a
75    /// route, or the user supplied an explicit address).
76    pub gateway: GatewayIps,
77    /// Guest IPv4 address. `None` when IPv4 is inactive for this sandbox.
78    pub guest_ipv4: Option<Ipv4Addr>,
79    /// Guest IPv6 address. `None` when IPv6 is inactive for this sandbox.
80    pub guest_ipv6: Option<Ipv6Addr>,
81    /// IP-level MTU (e.g. 1500).
82    pub mtu: usize,
83}
84
85/// Per-sandbox gateway addresses owned by the smoltcp virtual stack.
86///
87/// Each family is `Some` when active for this sandbox and `None` otherwise.
88/// `resolve_host_dst` rewrites gateway-bound connections to loopback at dial time.
89#[derive(Debug, Clone, Copy)]
90pub struct GatewayIps {
91    /// Gateway IPv4.
92    pub ipv4: Option<Ipv4Addr>,
93    /// Gateway IPv6.
94    pub ipv6: Option<Ipv6Addr>,
95}
96
97//--------------------------------------------------------------------------------------------------
98// Functions
99//--------------------------------------------------------------------------------------------------
100
101/// Classify a raw ethernet frame for pre-inspection.
102///
103/// Uses smoltcp's wire module for zero-copy parsing. Returns
104/// [`FrameAction::Passthrough`] for any frame that cannot be parsed or
105/// doesn't match a special case.
106pub fn classify_frame(frame: &[u8]) -> FrameAction {
107    let Ok(eth) = EthernetFrame::new_checked(frame) else {
108        return FrameAction::Passthrough;
109    };
110
111    match eth.ethertype() {
112        EthernetProtocol::Ipv4 => classify_ipv4(eth.payload()),
113        EthernetProtocol::Ipv6 => classify_ipv6(eth.payload()),
114        _ => FrameAction::Passthrough, // ARP, etc.
115    }
116}
117
118/// Create and configure the smoltcp [`Interface`].
119///
120/// The interface is configured as the **gateway**: it owns the gateway IP
121/// addresses and responds to ARP/NDP for them. `any_ip` mode is enabled so
122/// smoltcp accepts traffic destined for arbitrary remote IPs (not just the
123/// gateway), combined with default routes.
124pub fn create_interface(device: &mut SmoltcpDevice, config: &PollLoopConfig) -> Interface {
125    let hw_addr = HardwareAddress::Ethernet(EthernetAddress(config.gateway_mac));
126    let iface_config = Config::new(hw_addr);
127    let mut iface = Interface::new(iface_config, device, smoltcp_now());
128
129    // Configure gateway IP addresses for the active families.
130    iface.update_ip_addrs(|addrs| {
131        if let Some(ipv4) = config.gateway.ipv4 {
132            addrs
133                .push(IpCidr::new(IpAddress::Ipv4(ipv4), 30)) // 30 subnet: gateway + guest.
134                .expect("failed to add gateway IPv4 address");
135        }
136        if let Some(ipv6) = config.gateway.ipv6 {
137            addrs
138                .push(IpCidr::new(IpAddress::Ipv6(ipv6), 64))
139                .expect("failed to add gateway IPv6 address");
140        }
141    });
142
143    // Default routes so smoltcp accepts traffic for all destinations.
144    if let Some(ipv4) = config.gateway.ipv4 {
145        iface
146            .routes_mut()
147            .add_default_ipv4_route(ipv4)
148            .expect("failed to add default IPv4 route");
149    }
150    if let Some(ipv6) = config.gateway.ipv6 {
151        iface
152            .routes_mut()
153            .add_default_ipv6_route(ipv6)
154            .expect("failed to add default IPv6 route");
155    }
156
157    // Accept traffic destined for any IP, not just gateway addresses.
158    iface.set_any_ip(true);
159
160    iface
161}
162
163/// Main smoltcp poll loop. Runs on a dedicated OS thread.
164///
165/// Processes guest frames with pre-inspection, drives smoltcp's TCP/IP stack,
166/// and sleeps via `poll(2)` between events.
167///
168/// # Phases per iteration
169///
170/// 1. **Drain guest frames** — pop from `tx_ring`, classify, pre-inspect.
171/// 2. **smoltcp egress + maintenance** — transmit queued packets, run timers.
172/// 3. **Service connections** — relay data between smoltcp sockets and proxy
173///    tasks (added by later tasks).
174/// 4. **Sleep** — `poll(2)` on `tx_wake` + `proxy_wake` pipes with smoltcp's
175///    requested timeout.
176///
177/// # Arguments
178///
179/// * `shared` - Stack-wide shared state: `tx_ring` / `rx_ring` for the virtio-net boundary
180///   and the wake eventfds.
181/// * `config` - Resolved per-sandbox parameters (gateway / guest MAC + IPv4 + IPv6, MTU).
182/// * `network_policy` - User-provided egress policy. Evaluated against the sandbox's
183///   gateway IPs (stored on [`SharedState`]) so `DestinationGroup::Host` rules match.
184/// * `dns_config` - DNS interception settings (block lists, upstreams, timeout).
185/// * `tls_state` - Optional TLS MITM state; drives interception of intercepted ports and DoT
186///   when present.
187/// * `published_ports` - Host → guest port publishes; the publisher accepts inbound
188///   connections on the host-bind address and forwards into the guest.
189/// * `max_connections` - Optional cap on concurrent guest connections tracked by
190///   [`ConnectionTracker`]; `None` uses the default.
191/// * `tokio_handle` - Runtime handle used for proxy tasks, DNS forwarding, port publishing,
192///   and ICMP relays.
193#[allow(clippy::too_many_arguments)]
194pub fn smoltcp_poll_loop(
195    shared: Arc<SharedState>,
196    config: PollLoopConfig,
197    network_policy: NetworkPolicy,
198    dns_config: DnsConfig,
199    tls_state: Option<Arc<TlsState>>,
200    published_ports: Vec<PublishedPort>,
201    max_connections: Option<usize>,
202    tokio_handle: tokio::runtime::Handle,
203    secrets: Arc<SecretsConfig>,
204) {
205    let mut device = SmoltcpDevice::new(shared.clone(), config.mtu);
206    let mut iface = create_interface(&mut device, &config);
207    let mut sockets = SocketSet::new(vec![]);
208    let mut conn_tracker = ConnectionTracker::new(max_connections);
209
210    // The DNS forwarder needs to know which IPs count as "the gateway"
211    // (so it routes guest queries to those addresses through the
212    // configured upstream) and a policy evaluator (so guest-chosen
213    // `@target` resolvers are gated by egress rules just like any
214    // other outbound).
215    let gateway_ips: Arc<HashSet<IpAddr>> = Arc::new(
216        config
217            .gateway
218            .ipv4
219            .map(IpAddr::V4)
220            .into_iter()
221            .chain(config.gateway.ipv6.map(IpAddr::V6))
222            .collect(),
223    );
224    // Gateway IPs must be on SharedState before any egress evaluation runs,
225    // so `DestinationGroup::Host` rules can resolve to the right address.
226    shared.set_gateway_ips(config.gateway.ipv4, config.gateway.ipv6);
227    let network_policy = Arc::new(network_policy);
228
229    let (mut dns_interceptor, dns_forwarder_handle) = DnsInterceptor::new(
230        &mut sockets,
231        dns_config,
232        shared.clone(),
233        &tokio_handle,
234        gateway_ips,
235        network_policy.clone(),
236        config.gateway,
237        config.gateway_mac,
238        config.guest_mac,
239    );
240    let mut port_publisher = PortPublisher::new(
241        &published_ports,
242        config.guest_ipv4,
243        config.guest_ipv6,
244        config.gateway.ipv4,
245        config.gateway.ipv6,
246        config.gateway_mac,
247        config.guest_mac,
248        network_policy.clone(),
249        shared.clone(),
250        &tokio_handle,
251    );
252    let mut udp_relay = UdpRelay::new(
253        shared.clone(),
254        config.gateway_mac,
255        config.guest_mac,
256        tokio_handle.clone(),
257    );
258    let icmp_relay = IcmpRelay::new(
259        shared.clone(),
260        config.gateway_mac,
261        config.guest_mac,
262        tokio_handle.clone(),
263    );
264
265    // Rate-limit cleanup operations: run at most once per second.
266    let mut last_cleanup = std::time::Instant::now();
267
268    // poll(2) file descriptors for sleeping.
269    let mut poll_fds = [
270        libc::pollfd {
271            fd: shared.tx_wake.as_raw_fd(),
272            events: libc::POLLIN,
273            revents: 0,
274        },
275        libc::pollfd {
276            fd: shared.proxy_wake.as_raw_fd(),
277            events: libc::POLLIN,
278            revents: 0,
279        },
280    ];
281
282    loop {
283        let now = smoltcp_now();
284
285        // ── Phase 1: Drain all guest frames with pre-inspection ──────────
286        while let Some(frame) = device.stage_next_frame() {
287            if handle_gateway_icmp_echo(frame, &config, &shared) {
288                device.drop_staged_frame();
289                continue;
290            }
291
292            if icmp_relay.relay_outbound_if_echo(frame, &config, &network_policy) {
293                device.drop_staged_frame();
294                continue;
295            }
296
297            match classify_frame(frame) {
298                FrameAction::TcpSyn { src, dst } => {
299                    let allow = match DnsPortType::from_tcp(dst.port()) {
300                        // Plain DNS: the interceptor enforces policy at
301                        // the application layer (block list + rebind
302                        // protection); bypass the network egress check.
303                        DnsPortType::Dns => true,
304                        // DoT: intercept only when TLS MITM is
305                        // configured. Without it, the block list can't
306                        // apply (traffic is encrypted end-to-end), so
307                        // we refuse to force a fall-back to plain
308                        // TCP/53. When TLS MITM is configured, bypass
309                        // egress policy the same way plain DNS does —
310                        // policy for the upstream resolver is applied
311                        // per query by the forwarder.
312                        DnsPortType::EncryptedDns => {
313                            if tls_state.is_some() {
314                                true
315                            } else {
316                                tracing::debug!(%dst, "DoT port refused (TLS interception not configured); stub should fall back to TCP/53");
317                                false
318                            }
319                        }
320                        // Alternative DNS protocol we can't proxy:
321                        // refuse outright — no socket means smoltcp
322                        // emits RST, which the guest's stub treats as
323                        // "upstream unavailable" and falls back to
324                        // plain TCP/53.
325                        DnsPortType::AlternativeDns => {
326                            tracing::debug!(%dst, "alternative-DNS TCP port refused; stub should fall back to TCP/53");
327                            false
328                        }
329                        // Other: regular outbound — defer Domain rules to first-flight;
330                        // accept unless an IP-layer rule denies.
331                        DnsPortType::Other => match network_policy.evaluate_egress_with_source(
332                            dst,
333                            Protocol::Tcp,
334                            &shared,
335                            HostnameSource::Deferred,
336                        ) {
337                            EgressEvaluation::Allow | EgressEvaluation::DeferUntilHostname => true,
338                            EgressEvaluation::Deny => false,
339                        },
340                    };
341                    if allow && !conn_tracker.has_socket_for(&src, &dst) {
342                        conn_tracker.create_tcp_socket(src, dst, &mut sockets);
343                    }
344                    // Let smoltcp process — matching socket completes
345                    // handshake, no socket means auto-RST.
346                    iface.poll_ingress_single(now, &mut device, &mut sockets);
347                }
348
349                FrameAction::UdpRelay { src, dst } => {
350                    if port_publisher.relay_udp_outbound(frame, src, dst) {
351                        device.drop_staged_frame();
352                        continue;
353                    }
354
355                    // QUIC blocking: drop UDP to intercepted ports when
356                    // TLS interception is active.
357                    if let Some(ref tls) = tls_state
358                        && tls.config.intercepted_ports.contains(&dst.port())
359                        && tls.config.block_quic_on_intercept
360                    {
361                        device.drop_staged_frame();
362                        continue;
363                    }
364
365                    match DnsPortType::from_udp(dst.port()) {
366                        // Dns: unreachable here — classify_transport
367                        // routes UDP/53 to FrameAction::Dns, not
368                        // UdpRelay. Defensive drop covers regressions.
369                        DnsPortType::Dns => {
370                            device.drop_staged_frame();
371                            continue;
372                        }
373                        // EncryptedDns: unreachable here —
374                        // `DnsPortType::from_udp` never returns it
375                        // today (DoT is TCP-only; UDP/853 is DoQ and
376                        // returns AlternativeDns). Defensive drop.
377                        DnsPortType::EncryptedDns => {
378                            device.drop_staged_frame();
379                            continue;
380                        }
381                        // Alternative DNS protocols on well-known UDP
382                        // ports are dropped — forces fall-back to UDP/53.
383                        DnsPortType::AlternativeDns => {
384                            tracing::debug!(%dst, "alternative-DNS UDP port dropped; stub should fall back to UDP/53");
385                            device.drop_staged_frame();
386                            continue;
387                        }
388                        DnsPortType::Other => {}
389                    }
390
391                    // Policy check.
392                    if network_policy
393                        .evaluate_egress(dst, Protocol::Udp, &shared)
394                        .is_deny()
395                    {
396                        device.drop_staged_frame();
397                        continue;
398                    }
399
400                    // Resolve the host-side destination for the dial.
401                    // `dst` stays unchanged so reply frames are stamped
402                    // with the IP the guest expects.
403                    let host_dst = resolve_host_dst(dst, config.gateway);
404                    udp_relay.relay_outbound(frame, src, dst, host_dst);
405                    device.drop_staged_frame();
406                }
407
408                FrameAction::Dns | FrameAction::Passthrough => {
409                    // ARP, ICMP, DNS (port 53), TCP data — smoltcp handles.
410                    iface.poll_ingress_single(now, &mut device, &mut sockets);
411                }
412            }
413        }
414
415        // ── Phase 2: Ingress egress + maintenance ─────────────────────────
416        // Flush frames generated by Phase 1 ingress (ACKs, SYN-ACKs, etc.)
417        // before relaying data so smoltcp has up-to-date state.
418        loop {
419            let result = iface.poll_egress(now, &mut device, &mut sockets);
420            if matches!(result, smoltcp::iface::PollResult::None) {
421                break;
422            }
423        }
424        iface.poll_maintenance(now);
425
426        // Coalesced wake: if Phase 1/2 emitted any frames, wake the
427        // NetWorker once instead of per-frame.
428        if device.frames_emitted.swap(false, Ordering::Relaxed) {
429            shared.rx_wake.wake();
430        }
431
432        // ── Phase 3: Service connections + relay data ────────────────────
433        // Relay proxy data INTO smoltcp sockets first, then a single egress
434        // pass flushes everything. This eliminates the former "Phase 2b"
435        // double-egress pattern.
436        conn_tracker.relay_data(&mut sockets);
437        dns_interceptor.process(&mut sockets);
438
439        // Accept queued inbound connections from published port listeners.
440        port_publisher.accept_inbound(&mut iface, &mut sockets, &shared, &tokio_handle);
441        port_publisher.relay_data(&mut sockets);
442
443        // Detect newly-established connections and spawn proxy tasks.
444        let new_conns = conn_tracker.take_new_connections(&mut sockets);
445        for conn in new_conns {
446            if let Some(ref tls_state) = tls_state
447                && tls_state
448                    .config
449                    .intercepted_ports
450                    .contains(&conn.dst.port())
451            {
452                // TLS-intercepted port — spawn TLS MITM proxy.
453                let connect_dst = resolve_host_dst(conn.dst, config.gateway);
454                tls_proxy::spawn_tls_proxy(
455                    &tokio_handle,
456                    conn.dst,
457                    connect_dst,
458                    conn.from_smoltcp,
459                    conn.to_smoltcp,
460                    shared.clone(),
461                    tls_state.clone(),
462                    network_policy.clone(),
463                    conn.proxy_connect,
464                );
465                continue;
466            }
467            if conn.dst.port() == 53 {
468                // DNS proxies have no guest-visible
469                // "upstream-unreachable" failure mode — even an
470                // upstream DNS failure yields SERVFAIL responses
471                // rather than a silently-closed connection. Mark the
472                // connection as connected so normal task exit
473                // produces FIN, not RST.
474                conn.proxy_connect.mark_connected();
475
476                // DNS over TCP: route through the same forwarder the UDP
477                // path uses. The forwarder applies the domain block list
478                // and rebind protection to every query and routes
479                // upstream based on `conn.dst.ip()` — the configured
480                // upstream for queries to the gateway, direct forward
481                // to the chosen `@target` (subject to egress policy)
482                // otherwise. No gateway→loopback rewrite here: the
483                // forwarder dials the configured upstream, not the
484                // gateway.
485                DnsTcpProxy::spawn(
486                    &tokio_handle,
487                    conn.dst,
488                    conn.from_smoltcp,
489                    conn.to_smoltcp,
490                    dns_forwarder_handle.clone(),
491                    shared.clone(),
492                );
493                continue;
494            }
495            if conn.dst.port() == 853
496                && let Some(ref tls_state) = tls_state
497            {
498                // Same "always upstream-connected" reasoning as plain DNS over TCP.
499                conn.proxy_connect.mark_connected();
500
501                // DNS over TLS: terminate TLS at the gateway with a
502                // per-domain cert, hand the inner DNS frames to the
503                // same forwarder plain DNS uses. Policy for the
504                // chosen `@target` resolver is applied per-query by
505                // the forwarder (block list + rebind + egress).
506                DotProxy::spawn(
507                    &tokio_handle,
508                    conn.dst,
509                    conn.from_smoltcp,
510                    conn.to_smoltcp,
511                    dns_forwarder_handle.clone(),
512                    tls_state.clone(),
513                    shared.clone(),
514                );
515                continue;
516            }
517            // Plain TCP proxy.
518            let connect_dst = resolve_host_dst(conn.dst, config.gateway);
519            proxy::spawn_tcp_proxy(
520                &tokio_handle,
521                conn.dst,
522                connect_dst,
523                conn.from_smoltcp,
524                conn.to_smoltcp,
525                shared.clone(),
526                network_policy.clone(),
527                secrets.clone(),
528                conn.proxy_connect,
529            );
530        }
531
532        // Rate-limited cleanup: TIME_WAIT is 60s, session timeout is 60s,
533        // so checking once per second is more than sufficient.
534        if last_cleanup.elapsed() >= std::time::Duration::from_secs(1) {
535            conn_tracker.cleanup_closed(&mut sockets);
536            port_publisher.cleanup_closed(&mut sockets);
537            udp_relay.cleanup_expired();
538            shared.cleanup_resolved_hostnames();
539            last_cleanup = std::time::Instant::now();
540        }
541
542        // ── Phase 4: Flush relay data + sleep ────────────────────────────
543        // Single egress pass flushes all data written by Phase 3.
544        loop {
545            let result = iface.poll_egress(now, &mut device, &mut sockets);
546            if matches!(result, smoltcp::iface::PollResult::None) {
547                break;
548            }
549        }
550
551        // Coalesced wake: if Phase 3/4 emitted any frames, wake once.
552        if device.frames_emitted.swap(false, Ordering::Relaxed) {
553            shared.rx_wake.wake();
554        }
555
556        let timeout_ms = iface
557            .poll_delay(now, &sockets)
558            .map(|d| d.total_millis().min(i32::MAX as u64) as i32)
559            .unwrap_or(100); // 100ms fallback when no timers pending.
560
561        // SAFETY: poll_fds is a valid array of pollfd structs with valid fds.
562        unsafe {
563            libc::poll(
564                poll_fds.as_mut_ptr(),
565                poll_fds.len() as libc::nfds_t,
566                timeout_ms,
567            );
568        }
569
570        // Conditional drain: only drain pipes that actually have data.
571        if poll_fds[0].revents & libc::POLLIN != 0 {
572            shared.tx_wake.drain();
573        }
574        if poll_fds[1].revents & libc::POLLIN != 0 {
575            shared.proxy_wake.drain();
576        }
577    }
578}
579
580//--------------------------------------------------------------------------------------------------
581// Functions: Helpers
582//--------------------------------------------------------------------------------------------------
583
584/// Map a guest-wire destination to its host-socket equivalent.
585///
586/// Gateway IPs rewrite to loopback (`127.0.0.1` / `::1`); everything else
587/// passes through. Shared by the TCP proxy dispatch and the UDP relay.
588///
589/// # Arguments
590///
591/// * `dst` - Destination from the guest's packet.
592/// * `gateway` - Per-sandbox gateway IPs that trigger the loopback rewrite.
593pub(crate) fn resolve_host_dst(dst: SocketAddr, gateway: GatewayIps) -> SocketAddr {
594    match dst.ip() {
595        IpAddr::V4(v4) if gateway.ipv4 == Some(v4) => {
596            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), dst.port())
597        }
598        IpAddr::V6(v6) if gateway.ipv6 == Some(v6) => {
599            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), dst.port())
600        }
601        _ => dst,
602    }
603}
604
605/// Get the current time as a smoltcp [`Instant`] using a monotonic clock.
606///
607/// Uses `std::time::Instant` (monotonic) instead of `SystemTime` (wall
608/// clock) to avoid issues with NTP clock step corrections that could
609/// cause smoltcp timers to misbehave.
610fn smoltcp_now() -> Instant {
611    static EPOCH: std::sync::OnceLock<std::time::Instant> = std::sync::OnceLock::new();
612    let epoch = EPOCH.get_or_init(std::time::Instant::now);
613    let elapsed = epoch.elapsed();
614    Instant::from_millis(elapsed.as_millis() as i64)
615}
616
617/// Reply locally to ICMP echo requests aimed at the sandbox gateway.
618///
619/// `any_ip` is required so smoltcp accepts guest traffic for arbitrary remote
620/// destinations, but that would make smoltcp's automatic ICMP echo replies
621/// spoof remote hosts. Handle only the real gateway IPs here and leave all
622/// other ICMP traffic untouched.
623fn handle_gateway_icmp_echo(frame: &[u8], config: &PollLoopConfig, shared: &SharedState) -> bool {
624    let Ok(eth) = EthernetFrame::new_checked(frame) else {
625        return false;
626    };
627
628    let reply = match eth.ethertype() {
629        EthernetProtocol::Ipv4 => gateway_icmpv4_echo_reply(&eth, config),
630        EthernetProtocol::Ipv6 => gateway_icmpv6_echo_reply(&eth, config),
631        _ => None,
632    };
633    let Some(reply) = reply else {
634        return false;
635    };
636
637    shared.push_rx_frame_and_wake(reply);
638
639    true
640}
641
642/// Build an IPv4 ICMP echo reply when the guest pings the gateway IPv4.
643fn gateway_icmpv4_echo_reply(
644    eth: &EthernetFrame<&[u8]>,
645    config: &PollLoopConfig,
646) -> Option<Vec<u8>> {
647    let gateway_ipv4 = config.gateway.ipv4?;
648    let ipv4 = Ipv4Packet::new_checked(eth.payload()).ok()?;
649    if ipv4.dst_addr() != gateway_ipv4 || ipv4.next_header() != IpProtocol::Icmp {
650        return None;
651    }
652
653    let icmp = Icmpv4Packet::new_checked(ipv4.payload()).ok()?;
654    let Icmpv4Repr::EchoRequest {
655        ident,
656        seq_no,
657        data,
658    } = Icmpv4Repr::parse(&icmp, &smoltcp::phy::ChecksumCapabilities::default()).ok()?
659    else {
660        return None;
661    };
662
663    let ipv4_repr = Ipv4Repr {
664        src_addr: gateway_ipv4,
665        dst_addr: ipv4.src_addr(),
666        next_header: IpProtocol::Icmp,
667        payload_len: 8 + data.len(),
668        hop_limit: 64,
669    };
670    let icmp_repr = Icmpv4Repr::EchoReply {
671        ident,
672        seq_no,
673        data,
674    };
675    let mut reply = vec![0u8; 14 + ipv4_repr.buffer_len() + icmp_repr.buffer_len()];
676
677    let mut reply_eth = EthernetFrame::new_unchecked(&mut reply);
678    reply_eth.set_src_addr(EthernetAddress(config.gateway_mac));
679    reply_eth.set_dst_addr(eth.src_addr());
680    reply_eth.set_ethertype(EthernetProtocol::Ipv4);
681
682    ipv4_repr.emit(
683        &mut Ipv4Packet::new_unchecked(&mut reply[14..34]),
684        &smoltcp::phy::ChecksumCapabilities::default(),
685    );
686    icmp_repr.emit(
687        &mut Icmpv4Packet::new_unchecked(&mut reply[34..]),
688        &smoltcp::phy::ChecksumCapabilities::default(),
689    );
690
691    Some(reply)
692}
693
694/// Build an IPv6 ICMP echo reply when the guest pings the gateway IPv6.
695fn gateway_icmpv6_echo_reply(
696    eth: &EthernetFrame<&[u8]>,
697    config: &PollLoopConfig,
698) -> Option<Vec<u8>> {
699    let gateway_ipv6 = config.gateway.ipv6?;
700    let ipv6 = Ipv6Packet::new_checked(eth.payload()).ok()?;
701    if ipv6.dst_addr() != gateway_ipv6 || ipv6.next_header() != IpProtocol::Icmpv6 {
702        return None;
703    }
704
705    let icmp = Icmpv6Packet::new_checked(ipv6.payload()).ok()?;
706    let Icmpv6Repr::EchoRequest {
707        ident,
708        seq_no,
709        data,
710    } = Icmpv6Repr::parse(
711        &ipv6.src_addr(),
712        &ipv6.dst_addr(),
713        &icmp,
714        &smoltcp::phy::ChecksumCapabilities::default(),
715    )
716    .ok()?
717    else {
718        return None;
719    };
720
721    let ipv6_repr = Ipv6Repr {
722        src_addr: gateway_ipv6,
723        dst_addr: ipv6.src_addr(),
724        next_header: IpProtocol::Icmpv6,
725        payload_len: icmp_repr_buffer_len_v6(data),
726        hop_limit: 64,
727    };
728    let icmp_repr = Icmpv6Repr::EchoReply {
729        ident,
730        seq_no,
731        data,
732    };
733    let ipv6_hdr_len = 40;
734    let mut reply = vec![0u8; 14 + ipv6_hdr_len + icmp_repr.buffer_len()];
735
736    let mut reply_eth = EthernetFrame::new_unchecked(&mut reply);
737    reply_eth.set_src_addr(EthernetAddress(config.gateway_mac));
738    reply_eth.set_dst_addr(eth.src_addr());
739    reply_eth.set_ethertype(EthernetProtocol::Ipv6);
740
741    ipv6_repr.emit(&mut Ipv6Packet::new_unchecked(&mut reply[14..54]));
742    icmp_repr.emit(
743        &gateway_ipv6,
744        &ipv6.src_addr(),
745        &mut Icmpv6Packet::new_unchecked(&mut reply[54..]),
746        &smoltcp::phy::ChecksumCapabilities::default(),
747    );
748
749    Some(reply)
750}
751
752fn icmp_repr_buffer_len_v6(data: &[u8]) -> usize {
753    Icmpv6Repr::EchoReply {
754        ident: 0,
755        seq_no: 0,
756        data,
757    }
758    .buffer_len()
759}
760
761/// Classify an IPv4 packet payload (after stripping the Ethernet header).
762fn classify_ipv4(payload: &[u8]) -> FrameAction {
763    let Ok(ipv4) = Ipv4Packet::new_checked(payload) else {
764        return FrameAction::Passthrough;
765    };
766    classify_transport(
767        ipv4.next_header(),
768        ipv4.src_addr().into(),
769        ipv4.dst_addr().into(),
770        ipv4.payload(),
771    )
772}
773
774/// Classify an IPv6 packet payload (after stripping the Ethernet header).
775fn classify_ipv6(payload: &[u8]) -> FrameAction {
776    let Ok(ipv6) = Ipv6Packet::new_checked(payload) else {
777        return FrameAction::Passthrough;
778    };
779    classify_transport(
780        ipv6.next_header(),
781        ipv6.src_addr().into(),
782        ipv6.dst_addr().into(),
783        ipv6.payload(),
784    )
785}
786
787/// Classify the transport-layer protocol (shared by IPv4 and IPv6).
788fn classify_transport(
789    protocol: IpProtocol,
790    src_ip: std::net::IpAddr,
791    dst_ip: std::net::IpAddr,
792    transport_payload: &[u8],
793) -> FrameAction {
794    match protocol {
795        IpProtocol::Tcp => {
796            let Ok(tcp) = TcpPacket::new_checked(transport_payload) else {
797                return FrameAction::Passthrough;
798            };
799            if tcp.syn() && !tcp.ack() {
800                FrameAction::TcpSyn {
801                    src: SocketAddr::new(src_ip, tcp.src_port()),
802                    dst: SocketAddr::new(dst_ip, tcp.dst_port()),
803                }
804            } else {
805                FrameAction::Passthrough
806            }
807        }
808        IpProtocol::Udp => {
809            let Ok(udp) = UdpPacket::new_checked(transport_payload) else {
810                return FrameAction::Passthrough;
811            };
812            // The plain-DNS port (UDP/53) lives in dns::common::ports so
813            // the alternative-DNS refusal logic and this dispatcher
814            // share one source of truth for "which UDP ports are DNS".
815            if DnsPortType::from_udp(udp.dst_port()) == DnsPortType::Dns {
816                FrameAction::Dns
817            } else {
818                FrameAction::UdpRelay {
819                    src: SocketAddr::new(src_ip, udp.src_port()),
820                    dst: SocketAddr::new(dst_ip, udp.dst_port()),
821                }
822            }
823        }
824        _ => FrameAction::Passthrough, // ICMP, etc.
825    }
826}
827
828//--------------------------------------------------------------------------------------------------
829// Tests
830//--------------------------------------------------------------------------------------------------
831
832#[cfg(test)]
833mod tests {
834    use super::*;
835    use std::sync::Arc;
836
837    use smoltcp::phy::ChecksumCapabilities;
838    use smoltcp::wire::{
839        ArpOperation, ArpPacket, ArpRepr, EthernetRepr, Icmpv4Packet, Icmpv4Repr, Ipv4Repr,
840    };
841
842    use crate::device::SmoltcpDevice;
843    use crate::shared::SharedState;
844
845    /// Build a minimal Ethernet + IPv4 + TCP SYN frame.
846    fn build_tcp_syn_frame(
847        src_ip: [u8; 4],
848        dst_ip: [u8; 4],
849        src_port: u16,
850        dst_port: u16,
851    ) -> Vec<u8> {
852        let mut frame = vec![0u8; 14 + 20 + 20]; // eth + ipv4 + tcp
853
854        // Ethernet header.
855        frame[12] = 0x08; // EtherType: IPv4
856        frame[13] = 0x00;
857
858        // IPv4 header.
859        let ip = &mut frame[14..34];
860        ip[0] = 0x45; // Version + IHL
861        let total_len = 40u16; // 20 (IP) + 20 (TCP)
862        ip[2..4].copy_from_slice(&total_len.to_be_bytes());
863        ip[6] = 0x40; // Don't Fragment
864        ip[8] = 64; // TTL
865        ip[9] = 6; // Protocol: TCP
866        ip[12..16].copy_from_slice(&src_ip);
867        ip[16..20].copy_from_slice(&dst_ip);
868
869        // TCP header.
870        let tcp = &mut frame[34..54];
871        tcp[0..2].copy_from_slice(&src_port.to_be_bytes());
872        tcp[2..4].copy_from_slice(&dst_port.to_be_bytes());
873        tcp[12] = 0x50; // Data offset: 5 words
874        tcp[13] = 0x02; // SYN flag
875
876        frame
877    }
878
879    /// Build a minimal Ethernet + IPv4 + UDP frame.
880    fn build_udp_frame(src_ip: [u8; 4], dst_ip: [u8; 4], src_port: u16, dst_port: u16) -> Vec<u8> {
881        let mut frame = vec![0u8; 14 + 20 + 8]; // eth + ipv4 + udp
882
883        // Ethernet header.
884        frame[12] = 0x08;
885        frame[13] = 0x00;
886
887        // IPv4 header.
888        let ip = &mut frame[14..34];
889        ip[0] = 0x45;
890        let total_len = 28u16; // 20 (IP) + 8 (UDP)
891        ip[2..4].copy_from_slice(&total_len.to_be_bytes());
892        ip[8] = 64;
893        ip[9] = 17; // Protocol: UDP
894        ip[12..16].copy_from_slice(&src_ip);
895        ip[16..20].copy_from_slice(&dst_ip);
896
897        // UDP header.
898        let udp = &mut frame[34..42];
899        udp[0..2].copy_from_slice(&src_port.to_be_bytes());
900        udp[2..4].copy_from_slice(&dst_port.to_be_bytes());
901        let udp_len = 8u16;
902        udp[4..6].copy_from_slice(&udp_len.to_be_bytes());
903
904        frame
905    }
906
907    /// Build a minimal Ethernet + IPv4 + ICMP echo request frame.
908    fn build_icmpv4_echo_frame(
909        src_mac: [u8; 6],
910        dst_mac: [u8; 6],
911        src_ip: [u8; 4],
912        dst_ip: [u8; 4],
913        ident: u16,
914        seq_no: u16,
915        data: &[u8],
916    ) -> Vec<u8> {
917        let ipv4_repr = Ipv4Repr {
918            src_addr: Ipv4Addr::from(src_ip),
919            dst_addr: Ipv4Addr::from(dst_ip),
920            next_header: IpProtocol::Icmp,
921            payload_len: 8 + data.len(),
922            hop_limit: 64,
923        };
924        let icmp_repr = Icmpv4Repr::EchoRequest {
925            ident,
926            seq_no,
927            data,
928        };
929        let frame_len = 14 + ipv4_repr.buffer_len() + icmp_repr.buffer_len();
930        let mut frame = vec![0u8; frame_len];
931
932        let mut eth_frame = EthernetFrame::new_unchecked(&mut frame);
933        EthernetRepr {
934            src_addr: EthernetAddress(src_mac),
935            dst_addr: EthernetAddress(dst_mac),
936            ethertype: EthernetProtocol::Ipv4,
937        }
938        .emit(&mut eth_frame);
939
940        ipv4_repr.emit(
941            &mut Ipv4Packet::new_unchecked(&mut frame[14..34]),
942            &ChecksumCapabilities::default(),
943        );
944        icmp_repr.emit(
945            &mut Icmpv4Packet::new_unchecked(&mut frame[34..]),
946            &ChecksumCapabilities::default(),
947        );
948
949        frame
950    }
951
952    /// Build a minimal Ethernet + ARP request frame.
953    fn build_arp_request_frame(src_mac: [u8; 6], src_ip: [u8; 4], target_ip: [u8; 4]) -> Vec<u8> {
954        let mut frame = vec![0u8; 14 + 28];
955
956        let mut eth_frame = EthernetFrame::new_unchecked(&mut frame);
957        EthernetRepr {
958            src_addr: EthernetAddress(src_mac),
959            dst_addr: EthernetAddress([0xff; 6]),
960            ethertype: EthernetProtocol::Arp,
961        }
962        .emit(&mut eth_frame);
963
964        ArpRepr::EthernetIpv4 {
965            operation: ArpOperation::Request,
966            source_hardware_addr: EthernetAddress(src_mac),
967            source_protocol_addr: Ipv4Addr::from(src_ip),
968            target_hardware_addr: EthernetAddress([0x00; 6]),
969            target_protocol_addr: Ipv4Addr::from(target_ip),
970        }
971        .emit(&mut ArpPacket::new_unchecked(&mut frame[14..]));
972
973        frame
974    }
975
976    #[test]
977    fn classify_tcp_syn() {
978        let frame = build_tcp_syn_frame([10, 0, 0, 2], [93, 184, 216, 34], 54321, 443);
979        match classify_frame(&frame) {
980            FrameAction::TcpSyn { src, dst } => {
981                assert_eq!(
982                    src,
983                    SocketAddr::new(Ipv4Addr::new(10, 0, 0, 2).into(), 54321)
984                );
985                assert_eq!(
986                    dst,
987                    SocketAddr::new(Ipv4Addr::new(93, 184, 216, 34).into(), 443)
988                );
989            }
990            _ => panic!("expected TcpSyn"),
991        }
992    }
993
994    #[test]
995    fn classify_tcp_ack_is_passthrough() {
996        let mut frame = build_tcp_syn_frame([10, 0, 0, 2], [93, 184, 216, 34], 54321, 443);
997        // Change flags to ACK only (not SYN).
998        frame[34 + 13] = 0x10; // ACK flag
999        assert!(matches!(classify_frame(&frame), FrameAction::Passthrough));
1000    }
1001
1002    #[test]
1003    fn classify_udp_dns() {
1004        let frame = build_udp_frame([10, 0, 0, 2], [10, 0, 0, 1], 12345, 53);
1005        assert!(matches!(classify_frame(&frame), FrameAction::Dns));
1006    }
1007
1008    #[test]
1009    fn classify_udp_non_dns() {
1010        let frame = build_udp_frame([10, 0, 0, 2], [8, 8, 8, 8], 12345, 443);
1011        match classify_frame(&frame) {
1012            FrameAction::UdpRelay { src, dst } => {
1013                assert_eq!(src.port(), 12345);
1014                assert_eq!(dst.port(), 443);
1015            }
1016            _ => panic!("expected UdpRelay"),
1017        }
1018    }
1019
1020    #[test]
1021    fn classify_arp_is_passthrough() {
1022        let mut frame = vec![0u8; 42]; // ARP frame
1023        frame[12] = 0x08;
1024        frame[13] = 0x06; // EtherType: ARP
1025        assert!(matches!(classify_frame(&frame), FrameAction::Passthrough));
1026    }
1027
1028    #[test]
1029    fn classify_garbage_is_passthrough() {
1030        assert!(matches!(classify_frame(&[]), FrameAction::Passthrough));
1031        assert!(matches!(classify_frame(&[0; 5]), FrameAction::Passthrough));
1032    }
1033
1034    #[test]
1035    fn gateway_replies_to_icmp_echo_requests() {
1036        fn drive_one_frame(
1037            device: &mut SmoltcpDevice,
1038            iface: &mut Interface,
1039            sockets: &mut SocketSet<'_>,
1040            shared: &Arc<SharedState>,
1041            poll_config: &PollLoopConfig,
1042            now: Instant,
1043        ) {
1044            let frame = device.stage_next_frame().expect("expected staged frame");
1045            if handle_gateway_icmp_echo(frame, poll_config, shared) {
1046                device.drop_staged_frame();
1047                return;
1048            }
1049            let _ = iface.poll_ingress_single(now, device, sockets);
1050            let _ = iface.poll_egress(now, device, sockets);
1051        }
1052
1053        let shared = Arc::new(SharedState::new(4));
1054        let poll_config = PollLoopConfig {
1055            gateway_mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x01],
1056            guest_mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x02],
1057            gateway: GatewayIps {
1058                ipv4: Some(Ipv4Addr::new(100, 96, 0, 1)),
1059                ipv6: Some(Ipv6Addr::LOCALHOST),
1060            },
1061            guest_ipv4: Some(Ipv4Addr::new(100, 96, 0, 2)),
1062            guest_ipv6: None,
1063            mtu: 1500,
1064        };
1065        let guest_ipv4 = poll_config.guest_ipv4.unwrap();
1066        let gateway_ipv4 = poll_config.gateway.ipv4.unwrap();
1067        let mut device = SmoltcpDevice::new(shared.clone(), poll_config.mtu);
1068        let mut iface = create_interface(&mut device, &poll_config);
1069        let mut sockets = SocketSet::new(vec![]);
1070        let now = smoltcp_now();
1071
1072        // Mirror the real guest flow: resolve the gateway MAC before sending
1073        // the ICMP echo request.
1074        shared
1075            .tx_ring
1076            .push(build_arp_request_frame(
1077                poll_config.guest_mac,
1078                guest_ipv4.octets(),
1079                gateway_ipv4.octets(),
1080            ))
1081            .unwrap();
1082        shared
1083            .tx_ring
1084            .push(build_icmpv4_echo_frame(
1085                poll_config.guest_mac,
1086                poll_config.gateway_mac,
1087                guest_ipv4.octets(),
1088                gateway_ipv4.octets(),
1089                0x1234,
1090                0xABCD,
1091                b"ping",
1092            ))
1093            .unwrap();
1094
1095        drive_one_frame(
1096            &mut device,
1097            &mut iface,
1098            &mut sockets,
1099            &shared,
1100            &poll_config,
1101            now,
1102        );
1103        let _ = shared.rx_ring.pop().expect("expected ARP reply");
1104
1105        drive_one_frame(
1106            &mut device,
1107            &mut iface,
1108            &mut sockets,
1109            &shared,
1110            &poll_config,
1111            now,
1112        );
1113
1114        let reply = shared.rx_ring.pop().expect("expected ICMP echo reply");
1115        let eth = EthernetFrame::new_checked(&reply).expect("valid ethernet frame");
1116        assert_eq!(eth.src_addr(), EthernetAddress(poll_config.gateway_mac));
1117        assert_eq!(eth.dst_addr(), EthernetAddress(poll_config.guest_mac));
1118        assert_eq!(eth.ethertype(), EthernetProtocol::Ipv4);
1119
1120        let ipv4 = Ipv4Packet::new_checked(eth.payload()).expect("valid IPv4 packet");
1121        assert_eq!(ipv4.src_addr(), gateway_ipv4);
1122        assert_eq!(ipv4.dst_addr(), guest_ipv4);
1123        assert_eq!(ipv4.next_header(), IpProtocol::Icmp);
1124
1125        let icmp = Icmpv4Packet::new_checked(ipv4.payload()).expect("valid ICMP packet");
1126        let icmp_repr = Icmpv4Repr::parse(&icmp, &ChecksumCapabilities::default())
1127            .expect("valid ICMP echo reply");
1128        assert_eq!(
1129            icmp_repr,
1130            Icmpv4Repr::EchoReply {
1131                ident: 0x1234,
1132                seq_no: 0xABCD,
1133                data: b"ping",
1134            }
1135        );
1136    }
1137
1138    fn test_gateway() -> GatewayIps {
1139        GatewayIps {
1140            ipv4: Some(Ipv4Addr::new(100, 96, 0, 1)),
1141            ipv6: Some("fd42:6d73:62::1".parse().unwrap()),
1142        }
1143    }
1144
1145    #[test]
1146    fn resolve_host_dst_matches_ipv4() {
1147        let gw = test_gateway();
1148        let dst = SocketAddr::new(IpAddr::V4(gw.ipv4.unwrap()), 8080);
1149        assert_eq!(
1150            resolve_host_dst(dst, gw),
1151            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 8080)
1152        );
1153    }
1154
1155    #[test]
1156    fn resolve_host_dst_matches_ipv6() {
1157        let gw = test_gateway();
1158        let dst = SocketAddr::new(IpAddr::V6(gw.ipv6.unwrap()), 8080);
1159        assert_eq!(
1160            resolve_host_dst(dst, gw),
1161            SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 8080)
1162        );
1163    }
1164
1165    #[test]
1166    fn resolve_host_dst_passes_through_when_family_absent() {
1167        let gw = GatewayIps {
1168            ipv4: None,
1169            ipv6: Some("fd42:6d73:62::1".parse().unwrap()),
1170        };
1171        // IPv4 dst with no IPv4 gateway must not be rewritten to loopback.
1172        let dst = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(100, 96, 0, 1)), 8080);
1173        assert_eq!(resolve_host_dst(dst, gw), dst);
1174    }
1175
1176    #[test]
1177    fn resolve_host_dst_passes_through_non_gateway() {
1178        let gw = test_gateway();
1179        let dst = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)), 443);
1180        assert_eq!(resolve_host_dst(dst, gw), dst);
1181    }
1182
1183    #[test]
1184    fn external_icmp_echo_requests_are_not_answered_locally() {
1185        fn drive_one_frame(
1186            device: &mut SmoltcpDevice,
1187            iface: &mut Interface,
1188            sockets: &mut SocketSet<'_>,
1189            shared: &Arc<SharedState>,
1190            poll_config: &PollLoopConfig,
1191            now: Instant,
1192        ) {
1193            let frame = device.stage_next_frame().expect("expected staged frame");
1194            if handle_gateway_icmp_echo(frame, poll_config, shared) {
1195                device.drop_staged_frame();
1196                return;
1197            }
1198            let _ = iface.poll_ingress_single(now, device, sockets);
1199            let _ = iface.poll_egress(now, device, sockets);
1200        }
1201
1202        let shared = Arc::new(SharedState::new(4));
1203        let poll_config = PollLoopConfig {
1204            gateway_mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x01],
1205            guest_mac: [0x02, 0x00, 0x00, 0x00, 0x00, 0x02],
1206            gateway: GatewayIps {
1207                ipv4: Some(Ipv4Addr::new(100, 96, 0, 1)),
1208                ipv6: Some(Ipv6Addr::LOCALHOST),
1209            },
1210            guest_ipv4: Some(Ipv4Addr::new(100, 96, 0, 2)),
1211            guest_ipv6: None,
1212            mtu: 1500,
1213        };
1214        let guest_ipv4 = poll_config.guest_ipv4.unwrap();
1215        let gateway_ipv4 = poll_config.gateway.ipv4.unwrap();
1216        let mut device = SmoltcpDevice::new(shared.clone(), poll_config.mtu);
1217        let mut iface = create_interface(&mut device, &poll_config);
1218        let mut sockets = SocketSet::new(vec![]);
1219        let now = smoltcp_now();
1220
1221        shared
1222            .tx_ring
1223            .push(build_arp_request_frame(
1224                poll_config.guest_mac,
1225                guest_ipv4.octets(),
1226                gateway_ipv4.octets(),
1227            ))
1228            .unwrap();
1229        shared
1230            .tx_ring
1231            .push(build_icmpv4_echo_frame(
1232                poll_config.guest_mac,
1233                poll_config.gateway_mac,
1234                guest_ipv4.octets(),
1235                [142, 251, 216, 46],
1236                0x1234,
1237                0xABCD,
1238                b"ping",
1239            ))
1240            .unwrap();
1241
1242        drive_one_frame(
1243            &mut device,
1244            &mut iface,
1245            &mut sockets,
1246            &shared,
1247            &poll_config,
1248            now,
1249        );
1250        let _ = shared.rx_ring.pop().expect("expected ARP reply");
1251
1252        drive_one_frame(
1253            &mut device,
1254            &mut iface,
1255            &mut sockets,
1256            &shared,
1257            &poll_config,
1258            now,
1259        );
1260        assert!(
1261            shared.rx_ring.pop().is_none(),
1262            "external ICMP should not be answered locally"
1263        );
1264    }
1265}