Skip to main content

fips_core/upper/
tun.rs

1//! FIPS TUN Interface
2//!
3//! Manages the TUN device for sending and receiving IPv6 packets.
4//! The TUN interface presents FIPS addresses to the local system,
5//! allowing standard socket applications to communicate over the mesh.
6//!
7//! Platform-specific implementations:
8//! - Linux: Uses the `tun` crate with `rtnetlink` for interface configuration
9//! - macOS: Uses the `tun` crate with `ifconfig`/`route` for interface configuration
10//! - Windows: Uses the `wintun` crate for TUN device support
11
12use crate::FipsAddress;
13#[cfg(any(
14    target_os = "linux",
15    target_os = "macos",
16    not(any(target_os = "linux", target_os = "macos", windows))
17))]
18use crate::TunConfig;
19use std::collections::HashMap;
20#[cfg(any(target_os = "linux", target_os = "macos"))]
21use std::fs::File;
22#[cfg(any(target_os = "linux", target_os = "macos"))]
23use std::io::Read;
24#[cfg(not(target_os = "macos"))]
25#[cfg(any(target_os = "linux", target_os = "macos"))]
26use std::io::Write;
27use std::net::Ipv6Addr;
28#[cfg(any(target_os = "linux", target_os = "macos"))]
29use std::os::unix::io::{AsRawFd, FromRawFd};
30use std::sync::{Arc, RwLock};
31use thiserror::Error;
32#[cfg(any(target_os = "linux", target_os = "macos"))]
33use tracing::error;
34use tracing::{debug, trace};
35#[cfg(windows)]
36use tracing::{error, warn};
37#[cfg(any(target_os = "linux", target_os = "macos"))]
38use tun::Layer;
39
40#[cfg(target_os = "linux")]
41use self::linux_vnet::{LinuxVnetTun, linux_vnet_tun_enabled};
42
43pub(crate) use super::tun_outbound::tun_outbound_channel;
44pub use super::tun_outbound::{TunOutboundRx, TunOutboundTx};
45#[cfg(any(test, target_os = "linux", target_os = "macos", windows))]
46pub(crate) use super::tun_write::TunRx;
47pub use super::tun_write::TunTx;
48pub(crate) use super::tun_write::write_channel;
49
50/// Read-only handle to the per-destination path MTU map. Populated by
51/// the discovery handler on `LookupResponse`; read by the TUN reader
52/// (outbound clamp) and writer (inbound clamp) at TCP MSS clamp time.
53/// Keyed by [`FipsAddress`] (16 bytes, the IPv6 form of a fips peer
54/// address).
55pub type PathMtuLookup = Arc<RwLock<HashMap<FipsAddress, u16>>>;
56
57#[cfg(any(test, target_os = "linux", target_os = "macos", windows))]
58const TUN_OUTBOUND_PACKET_TAIL_RESERVE: usize = 128;
59
60/// Compute the effective TCP MSS ceiling for a packet given its peer
61/// address bytes (a 16-byte IPv6 destination on outbound, source on
62/// inbound). Returns `min(global_max_mss, learned_path_max_mss)` when
63/// the per-destination path MTU is known via discovery; otherwise
64/// returns `min(global_max_mss, ipv6_minimum_safe_max_mss)`, the
65/// conservative IPv6-minimum-derived ceiling.
66///
67/// The conservative empty-lookup fallback exists because there is a
68/// race window between TCP-SYN-out and discovery-completes-with-path-
69/// MTU on cold flows. Without the floor, the first SYN exits at the
70/// kernel-natural MSS (TUN MTU minus IPv6/TCP headers), which can
71/// exceed what some downstream forwarder hop is willing to carry.
72/// The drop is silent (no PTB feedback through the userspace TUN to
73/// the kernel TCP stack), so TCP retransmits at the same too-large
74/// MSS and the application's first connection wedges before discovery
75/// completes for a corrected second SYN to fire.
76///
77/// RFC 8200 mandates every IPv6 path accepts at least 1280-byte
78/// packets, so a SYN clamped to the IPv6-minimum-derived MSS fits
79/// any compliant path. Subsequent flows pick up the actual learned
80/// per-destination value, which can be larger (when path supports
81/// it) or smaller (when path is observed-tighter than the IPv6 min).
82///
83/// Path MTU bytes-on-wire to TCP MSS: subtract 77 bytes of FIPS encap
84/// overhead, then 40 bytes IPv6 + 20 bytes TCP headers.
85#[cfg(any(test, target_os = "linux", target_os = "macos", windows))]
86pub(crate) fn per_flow_max_mss(
87    lookup: &PathMtuLookup,
88    addr_bytes: &[u8],
89    global_max_mss: u16,
90) -> u16 {
91    use super::icmp::effective_ipv6_mtu;
92
93    // RFC 8200 IPv6-minimum MTU (1280) → effective FIPS-encapsulated
94    // payload (1203) → TCP segment after IPv6+TCP headers (1143).
95    // Used as the conservative ceiling for empty-lookup destinations.
96    const IPV6_MIN_MTU: u16 = 1280;
97    let conservative_max_mss = effective_ipv6_mtu(IPV6_MIN_MTU)
98        .saturating_sub(40)
99        .saturating_sub(20);
100    let empty_lookup_ceiling = std::cmp::min(global_max_mss, conservative_max_mss);
101
102    if addr_bytes.len() != 16 {
103        trace!(
104            len = addr_bytes.len(),
105            global_max_mss,
106            empty_lookup_ceiling,
107            "per_flow_max_mss: addr_bytes wrong length, fall back to conservative ceiling"
108        );
109        return empty_lookup_ceiling;
110    }
111    let Ok(fips_addr) = FipsAddress::from_slice(addr_bytes) else {
112        trace!(
113            global_max_mss,
114            empty_lookup_ceiling,
115            "per_flow_max_mss: FipsAddress::from_slice rejected (non-fd::/8 prefix), fall back to conservative ceiling"
116        );
117        return empty_lookup_ceiling;
118    };
119    let Ok(map) = lookup.read() else {
120        trace!(
121            fips_addr = %fips_addr,
122            global_max_mss,
123            empty_lookup_ceiling,
124            "per_flow_max_mss: lookup read lock poisoned, fall back to conservative ceiling"
125        );
126        return empty_lookup_ceiling;
127    };
128    let Some(&path_mtu) = map.get(&fips_addr) else {
129        trace!(
130            fips_addr = %fips_addr,
131            global_max_mss,
132            empty_lookup_ceiling,
133            map_len = map.len(),
134            "per_flow_max_mss: no path_mtu_lookup entry for destination, fall back to conservative ceiling"
135        );
136        return empty_lookup_ceiling;
137    };
138    let path_max_mss = effective_ipv6_mtu(path_mtu)
139        .saturating_sub(40)
140        .saturating_sub(20);
141    let result = std::cmp::min(global_max_mss, path_max_mss);
142    trace!(
143        fips_addr = %fips_addr,
144        path_mtu,
145        path_max_mss,
146        global_max_mss,
147        result,
148        "per_flow_max_mss: per-destination clamp applied"
149    );
150    result
151}
152
153/// Errors that can occur with TUN operations.
154#[derive(Debug, Error)]
155pub enum TunError {
156    #[error("failed to create TUN device: {0}")]
157    Create(#[source] Box<dyn std::error::Error + Send + Sync>),
158
159    #[error("failed to configure TUN device: {0}")]
160    Configure(String),
161
162    #[cfg(target_os = "linux")]
163    #[error("netlink error: {0}")]
164    Netlink(#[from] rtnetlink::Error),
165
166    #[error("interface not found: {0}")]
167    InterfaceNotFound(String),
168
169    #[error("permission denied: {0}")]
170    PermissionDenied(String),
171
172    #[cfg(any(target_os = "linux", target_os = "macos"))]
173    #[error("IPv6 is disabled (set net.ipv6.conf.all.disable_ipv6=0)")]
174    Ipv6Disabled,
175
176    #[error("system TUN is not supported on this platform")]
177    UnsupportedPlatform,
178}
179
180#[cfg(any(target_os = "linux", target_os = "macos"))]
181impl From<tun::Error> for TunError {
182    fn from(e: tun::Error) -> Self {
183        TunError::Create(Box::new(e))
184    }
185}
186
187/// TUN device state.
188#[derive(Debug, Clone, Copy, PartialEq, Eq)]
189pub enum TunState {
190    /// TUN is disabled in configuration.
191    Disabled,
192    /// TUN is configured but not yet created.
193    Configured,
194    /// TUN device is active and ready.
195    Active,
196    /// TUN device failed to initialize.
197    Failed,
198}
199
200impl std::fmt::Display for TunState {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        match self {
203            TunState::Disabled => write!(f, "disabled"),
204            TunState::Configured => write!(f, "configured"),
205            TunState::Active => write!(f, "active"),
206            TunState::Failed => write!(f, "failed"),
207        }
208    }
209}
210
211// ============================================================================
212// Unix (Linux + macOS) TUN implementation
213// ============================================================================
214
215/// FIPS TUN device wrapper.
216#[cfg(any(target_os = "linux", target_os = "macos"))]
217pub struct TunDevice {
218    #[cfg(target_os = "linux")]
219    device: LinuxTunDevice,
220    #[cfg(target_os = "macos")]
221    device: tun::Device,
222    name: String,
223    mtu: u16,
224    address: FipsAddress,
225}
226
227#[cfg(target_os = "linux")]
228enum LinuxTunDevice {
229    Plain(tun::Device),
230    Vnet(LinuxVnetTun),
231}
232
233#[cfg(target_os = "linux")]
234impl LinuxTunDevice {
235    fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
236        match self {
237            Self::Plain(device) => device.as_raw_fd(),
238            Self::Vnet(device) => device.as_raw_fd(),
239        }
240    }
241
242    fn read_packet(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
243        match self {
244            Self::Plain(device) => {
245                let n = device.read(buf)?;
246                if n > 0 {
247                    crate::perf_profile::record_tun_read_frame(n);
248                }
249                Ok(n)
250            }
251            Self::Vnet(device) => device.read_packet(buf),
252        }
253    }
254
255    fn read_vnet_packets_into(
256        &mut self,
257        buf: &mut [u8],
258        packets: &mut Vec<Vec<u8>>,
259    ) -> Result<usize, std::io::Error> {
260        match self {
261            Self::Plain(_) => unreachable!("Linux vnet packet batching requires a vnet TUN"),
262            Self::Vnet(device) => device.read_packets_into(buf, packets),
263        }
264    }
265
266    fn read_buffer_len(&self, mtu: u16) -> usize {
267        match self {
268            Self::Plain(_) => default_tun_read_buffer_len(mtu),
269            Self::Vnet(device) => device.read_buffer_len(),
270        }
271    }
272
273    fn vnet_hdr(&self) -> bool {
274        matches!(self, Self::Vnet(_))
275    }
276}
277
278#[cfg(any(target_os = "linux", target_os = "macos"))]
279impl TunDevice {
280    /// Create or open a TUN device.
281    ///
282    /// If the interface already exists, opens it and reconfigures it.
283    /// Otherwise, creates a new TUN device.
284    ///
285    /// This requires CAP_NET_ADMIN capability (run with sudo or setcap).
286    pub async fn create(config: &TunConfig, address: FipsAddress) -> Result<Self, TunError> {
287        // Check if IPv6 is enabled
288        if platform::is_ipv6_disabled() {
289            return Err(TunError::Ipv6Disabled);
290        }
291
292        let name = config.name();
293        let mtu = config.mtu();
294
295        // Delete existing interface if present (TUN devices are exclusive)
296        if platform::interface_exists(name).await {
297            debug!(name, "Deleting existing TUN interface");
298            if let Err(e) = platform::delete_interface(name).await {
299                debug!(name, error = %e, "Failed to delete existing interface");
300            }
301        }
302
303        #[cfg(target_os = "linux")]
304        let (device, actual_name) = {
305            if linux_vnet_tun_enabled() {
306                let device =
307                    LinuxVnetTun::create(name).map_err(|e| TunError::Create(Box::new(e)))?;
308                let actual_name = device.name().to_string();
309                (LinuxTunDevice::Vnet(device), actual_name)
310            } else {
311                let mut tun_config = tun::Configuration::default();
312                tun_config.tun_name(name).layer(Layer::L3).mtu(mtu);
313                let device = tun::create(&tun_config)?;
314                let actual_name = {
315                    use tun::AbstractDevice;
316                    device.tun_name().map_err(|e| {
317                        TunError::Configure(format!("failed to get device name: {}", e))
318                    })?
319                };
320                (LinuxTunDevice::Plain(device), actual_name)
321            }
322        };
323
324        #[cfg(target_os = "macos")]
325        let (device, actual_name) = {
326            // On macOS, utun devices get kernel-assigned names (utun0, utun1, ...),
327            // so we skip setting the name and read it back after creation.
328            let mut tun_config = tun::Configuration::default();
329            tun_config.layer(Layer::L3).mtu(mtu);
330            let device = tun::create(&tun_config)?;
331            let actual_name = {
332                use tun::AbstractDevice;
333                device
334                    .tun_name()
335                    .map_err(|e| TunError::Configure(format!("failed to get device name: {}", e)))?
336            };
337            (device, actual_name)
338        };
339
340        // Configure address and bring up via platform-specific method
341        platform::configure_interface(&actual_name, address.to_ipv6(), mtu).await?;
342
343        Ok(Self {
344            device,
345            name: actual_name,
346            mtu,
347            address,
348        })
349    }
350
351    /// Get the device name.
352    pub fn name(&self) -> &str {
353        &self.name
354    }
355
356    /// Get the configured MTU.
357    pub fn mtu(&self) -> u16 {
358        self.mtu
359    }
360
361    /// Get the FIPS address assigned to this device.
362    pub fn address(&self) -> &FipsAddress {
363        &self.address
364    }
365
366    /// Get a reference to the underlying tun::Device.
367    #[cfg(target_os = "macos")]
368    pub fn device(&self) -> &tun::Device {
369        &self.device
370    }
371
372    /// Get a mutable reference to the underlying tun::Device.
373    #[cfg(target_os = "macos")]
374    pub fn device_mut(&mut self) -> &mut tun::Device {
375        &mut self.device
376    }
377
378    /// Read a packet from the TUN device.
379    ///
380    /// Returns the number of bytes read into the buffer, or an `io::Error`.
381    /// The buffer should be at least MTU + header size (typically 1500+ bytes).
382    ///
383    /// The tun crate's `Read` impl transparently strips the macOS utun
384    /// packet information header, so this returns a raw IP packet on all
385    /// platforms.
386    ///
387    /// The raw `io::Error` is returned so callers can inspect `ErrorKind`
388    /// (e.g. `WouldBlock`) or `raw_os_error()` without string matching.
389    pub fn read_packet(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
390        #[cfg(target_os = "linux")]
391        {
392            return self.device.read_packet(buf);
393        }
394
395        #[cfg(target_os = "macos")]
396        self.device.read(buf)
397    }
398
399    #[cfg(target_os = "linux")]
400    fn read_vnet_packets_into(
401        &mut self,
402        buf: &mut [u8],
403        packets: &mut Vec<Vec<u8>>,
404    ) -> Result<usize, std::io::Error> {
405        self.device.read_vnet_packets_into(buf, packets)
406    }
407
408    #[cfg(target_os = "linux")]
409    fn read_buffer_len(&self, mtu: u16) -> usize {
410        self.device.read_buffer_len(mtu)
411    }
412
413    #[cfg(target_os = "linux")]
414    fn vnet_hdr(&self) -> bool {
415        self.device.vnet_hdr()
416    }
417
418    #[cfg(any(target_os = "linux", target_os = "macos"))]
419    fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
420        #[cfg(target_os = "linux")]
421        {
422            return self.device.as_raw_fd();
423        }
424
425        #[cfg(target_os = "macos")]
426        self.device.as_raw_fd()
427    }
428
429    /// Shutdown and delete the TUN device.
430    ///
431    /// This deletes the interface entirely.
432    pub async fn shutdown(&self) -> Result<(), TunError> {
433        debug!(name = %self.name, "Deleting TUN device");
434        platform::delete_interface(&self.name).await
435    }
436
437    /// Create a TunWriter for this device.
438    ///
439    /// This duplicates the underlying file descriptor so that reads and writes
440    /// can happen independently on separate threads. Returns the writer and
441    /// a channel sender for submitting packets to be written.
442    ///
443    /// `max_mss` is the global TCP MSS ceiling derived from the local
444    /// `transport_mtu()` floor. `path_mtu_lookup` is a read-only handle to
445    /// the per-destination path MTU map populated by discovery; the writer
446    /// reads it on each inbound SYN-ACK to compute a per-flow ceiling that
447    /// honors learned narrow paths through the mesh.
448    pub fn create_writer(
449        &self,
450        max_mss: u16,
451        path_mtu_lookup: PathMtuLookup,
452    ) -> Result<(TunWriter, TunTx), TunError> {
453        let fd = self.as_raw_fd();
454
455        // Duplicate the file descriptor for writing
456        let write_fd = unsafe { libc::dup(fd) };
457        if write_fd < 0 {
458            return Err(TunError::Configure(format!(
459                "failed to dup fd: {}",
460                std::io::Error::last_os_error()
461            )));
462        }
463
464        let write_file = unsafe { File::from_raw_fd(write_fd) };
465        let (tx, rx) = write_channel();
466
467        Ok((
468            TunWriter {
469                file: write_file,
470                rx,
471                name: self.name.clone(),
472                max_mss,
473                path_mtu_lookup,
474                #[cfg(target_os = "linux")]
475                vnet_hdr: self.vnet_hdr(),
476            },
477            tx,
478        ))
479    }
480}
481
482/// macOS utun protocol family value for IPv6 (matches `<sys/socket.h>`
483/// `AF_INET6` on Darwin). Used as the 4-byte big-endian packet-info
484/// header prepended to every utun frame.
485#[cfg(target_os = "macos")]
486const UTUN_AF_INET6: u32 = 30;
487
488/// Build the 4-byte big-endian utun packet-info header for an IPv6 frame.
489///
490/// utun devices on macOS require a 4-byte address-family prefix on every
491/// frame: a single big-endian `u32` carrying the protocol family. For
492/// IPv6 traffic (the only family FIPS sends) this is `AF_INET6 = 30`,
493/// which serializes as `[0x00, 0x00, 0x00, 0x1e]`.
494#[cfg(target_os = "macos")]
495#[inline]
496fn utun_af_inet6_header() -> [u8; 4] {
497    UTUN_AF_INET6.to_be_bytes()
498}
499
500/// Parse the 4-byte big-endian utun packet-info header.
501///
502/// Returns the address-family value (`AF_INET6 = 30` for IPv6 frames),
503/// or `None` if the buffer is shorter than the 4-byte header. The `tun`
504/// crate's `Read` impl strips this transparently for us in the read
505/// path; this helper exists for round-trip testability with
506/// [`utun_af_inet6_header`] and for any future code path that reads
507/// from the dup'd fd directly.
508#[cfg(all(test, target_os = "macos"))]
509#[inline]
510fn parse_utun_af_prefix(buf: &[u8]) -> Option<u32> {
511    if buf.len() < 4 {
512        return None;
513    }
514    Some(u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]))
515}
516
517/// Writer thread for TUN device.
518///
519/// Services a queue of outbound packets and writes them to the TUN device.
520/// Multiple producers can send packets via the TunTx channel.
521///
522/// Also performs TCP MSS clamping on inbound SYN-ACK packets.
523#[cfg(any(target_os = "linux", target_os = "macos"))]
524pub struct TunWriter {
525    file: File,
526    rx: TunRx,
527    name: String,
528    max_mss: u16,
529    path_mtu_lookup: PathMtuLookup,
530    #[cfg(target_os = "linux")]
531    vnet_hdr: bool,
532}
533
534#[cfg(any(target_os = "linux", target_os = "macos"))]
535impl TunWriter {
536    fn clamp_inbound_packet(&self, packet: &mut super::tun_write::TunWritePacket) {
537        use super::tcp_mss::clamp_tcp_mss;
538
539        // Per-destination clamp: peer IPv6 source address (bytes 8..24)
540        // identifies the flow's remote end. If discovery has learned a
541        // smaller path MTU for that peer, tighten the ceiling.
542        let effective_max_mss = if packet.len() >= 24 {
543            per_flow_max_mss(
544                &self.path_mtu_lookup,
545                &packet.as_slice()[8..24],
546                self.max_mss,
547            )
548        } else {
549            self.max_mss
550        };
551        // Clamp TCP MSS on inbound SYN-ACK packets
552        if clamp_tcp_mss(packet.as_mut_slice(), effective_max_mss) {
553            trace!(
554                name = %self.name,
555                max_mss = effective_max_mss,
556                "Clamped TCP MSS in inbound SYN-ACK packet"
557            );
558        }
559    }
560
561    #[cfg(target_os = "linux")]
562    fn run_linux_vnet(mut self) {
563        use std::sync::mpsc::TryRecvError;
564
565        const LINUX_VNET_TUN_WRITE_BATCH_CAP: usize = 256;
566
567        debug!(name = %self.name, max_mss = self.max_mss, "Linux vnet TUN writer starting");
568
569        let mut batch = Vec::with_capacity(LINUX_VNET_TUN_WRITE_BATCH_CAP);
570        let mut write_preparer = linux_vnet::LinuxVnetWritePreparer::new();
571
572        while let Some(mut packet) = self.rx.recv() {
573            self.clamp_inbound_packet(&mut packet);
574            batch.push(packet);
575
576            while batch.len() < LINUX_VNET_TUN_WRITE_BATCH_CAP {
577                match self.rx.try_recv_packet() {
578                    Ok(mut packet) => {
579                        self.clamp_inbound_packet(&mut packet);
580                        batch.push(packet);
581                    }
582                    Err(TryRecvError::Empty | TryRecvError::Disconnected) => break,
583                }
584            }
585
586            let write_result = {
587                linux_vnet::write_packet_slices_to_tun(
588                    &mut self.file,
589                    batch.iter().map(|packet| packet.as_slice()),
590                    &mut write_preparer,
591                )
592            };
593
594            if let Err(e) = write_result {
595                let err_str = e.to_string();
596                if err_str.contains("Bad address") {
597                    break;
598                }
599                error!(name = %self.name, error = %e, "Linux vnet TUN write error");
600            } else {
601                for packet in &batch {
602                    crate::perf_profile::record_tun_write_packet(packet.len());
603                    trace!(name = %self.name, len = packet.len(), "TUN packet written");
604                }
605            }
606
607            batch.clear();
608        }
609    }
610
611    #[cfg(target_os = "macos")]
612    fn write_packet(&self, packet: &super::tun_write::TunWritePacket) -> std::io::Result<()> {
613        use std::os::unix::io::AsRawFd;
614
615        let af_header = utun_af_inet6_header();
616        let iov = [
617            libc::iovec {
618                iov_base: af_header.as_ptr() as *mut libc::c_void,
619                iov_len: 4,
620            },
621            libc::iovec {
622                iov_base: packet.as_slice().as_ptr() as *mut libc::c_void,
623                iov_len: packet.len(),
624            },
625        ];
626        let ret = unsafe { libc::writev(self.file.as_raw_fd(), iov.as_ptr(), 2) };
627        if ret < 0 {
628            return Err(std::io::Error::last_os_error());
629        }
630        let expected = 4 + packet.len();
631        if (ret as usize) < expected {
632            return Err(std::io::Error::new(
633                std::io::ErrorKind::WriteZero,
634                format!("short writev: {} of {} bytes", ret, expected),
635            ));
636        }
637        Ok(())
638    }
639
640    #[cfg(not(target_os = "macos"))]
641    fn write_packet(&mut self, packet: &super::tun_write::TunWritePacket) -> std::io::Result<()> {
642        self.file.write_all(packet.as_slice())
643    }
644
645    /// Run the writer loop.
646    ///
647    /// Blocks forever, reading packets from the channel and writing them
648    /// to the TUN device. Returns when the channel is closed (all senders dropped).
649    pub fn run(self) {
650        #[cfg(target_os = "linux")]
651        let mut writer = self;
652        #[cfg(target_os = "macos")]
653        let writer = self;
654
655        debug!(name = %writer.name, max_mss = writer.max_mss, "TUN writer starting");
656
657        #[cfg(target_os = "linux")]
658        if writer.vnet_hdr {
659            writer.run_linux_vnet();
660            return;
661        }
662
663        while let Some(mut packet) = writer.rx.recv() {
664            writer.clamp_inbound_packet(&mut packet);
665            let write_result = writer.write_packet(&packet);
666
667            if let Err(e) = write_result {
668                // "Bad address" is expected during shutdown when interface is deleted
669                let err_str = e.to_string();
670                if err_str.contains("Bad address") {
671                    break;
672                }
673                error!(name = %writer.name, error = %e, "TUN write error");
674            } else {
675                crate::perf_profile::record_tun_write_packet(packet.len());
676                trace!(name = %writer.name, len = packet.len(), "TUN packet written");
677            }
678        }
679    }
680}
681
682/// TUN packet reader loop (Linux).
683///
684/// Reads IPv6 packets from the TUN device. Packets destined for FIPS addresses
685/// (fd::/8) are forwarded to the Node via the outbound channel for session
686/// encapsulation and routing. Non-FIPS packets receive ICMPv6 Destination
687/// Unreachable responses.
688///
689/// Also performs TCP MSS clamping on SYN packets to prevent oversized segments.
690///
691/// This is designed to run in a dedicated thread since TUN reads are blocking.
692/// The loop exits when the TUN interface is deleted (EFAULT) or an unrecoverable
693/// error occurs.
694#[cfg(not(target_os = "macos"))]
695#[cfg(any(target_os = "linux", target_os = "macos"))]
696pub(crate) fn run_tun_reader(runtime: TunReaderRuntime) {
697    #[cfg(target_os = "linux")]
698    if runtime.device.vnet_hdr() {
699        run_linux_vnet_tun_reader(runtime);
700        return;
701    }
702
703    let TunReaderRuntime {
704        mut device,
705        mtu,
706        our_addr,
707        tun_tx,
708        outbound_tx,
709        transport_mtu,
710        path_mtu_lookup,
711    } = runtime;
712    let read_buffer_len = device.read_buffer_len(mtu);
713    let (name, mut buf, max_mss) =
714        tun_reader_setup_with_buffer_len(device.name(), mtu, transport_mtu, read_buffer_len);
715
716    loop {
717        match device.read_packet(&mut buf) {
718            Ok(n) if n > 0 => {
719                crate::perf_profile::record_tun_read_packet(n);
720                if !handle_tun_packet(
721                    &mut buf[..n],
722                    max_mss,
723                    &name,
724                    our_addr,
725                    &tun_tx,
726                    &outbound_tx,
727                    &path_mtu_lookup,
728                ) {
729                    break;
730                }
731            }
732            Ok(_) => {}
733            Err(e) => {
734                // EFAULT ("Bad address") is expected during shutdown when the interface is deleted
735                if e.raw_os_error() != Some(libc::EFAULT) {
736                    error!(name = %name, error = %e, "TUN read error");
737                }
738                break;
739            }
740        }
741    }
742}
743
744#[cfg(target_os = "linux")]
745fn run_linux_vnet_tun_reader(runtime: TunReaderRuntime) {
746    let TunReaderRuntime {
747        mut device,
748        mtu,
749        our_addr,
750        tun_tx,
751        outbound_tx,
752        transport_mtu,
753        path_mtu_lookup,
754    } = runtime;
755    let read_buffer_len = device.read_buffer_len(mtu);
756    let (name, mut buf, max_mss) =
757        tun_reader_setup_with_buffer_len(device.name(), mtu, transport_mtu, read_buffer_len);
758    let mut packets = Vec::with_capacity(64);
759
760    loop {
761        packets.clear();
762        match device.read_vnet_packets_into(&mut buf, &mut packets) {
763            Ok(n) if n > 0 => {
764                debug_assert_eq!(n, packets.len());
765                for packet in packets.drain(..) {
766                    crate::perf_profile::record_tun_read_packet(packet.len());
767                    if !handle_tun_packet_owned(
768                        packet,
769                        max_mss,
770                        &name,
771                        our_addr,
772                        &tun_tx,
773                        &outbound_tx,
774                        &path_mtu_lookup,
775                    ) {
776                        return;
777                    }
778                }
779            }
780            Ok(_) => {}
781            Err(e) => {
782                if e.raw_os_error() != Some(libc::EFAULT) {
783                    error!(name = %name, error = %e, "Linux vnet TUN read error");
784                }
785                break;
786            }
787        }
788    }
789}
790
791/// RAII wrapper that closes a raw fd on drop.
792///
793/// Used to ensure the shutdown pipe read-end is always closed when
794/// `run_tun_reader` returns, regardless of which exit path is taken.
795#[cfg(target_os = "macos")]
796struct ShutdownFd(std::os::unix::io::RawFd);
797
798#[cfg(target_os = "macos")]
799impl Drop for ShutdownFd {
800    fn drop(&mut self) {
801        unsafe {
802            libc::close(self.0);
803        }
804    }
805}
806
807/// TUN packet reader loop (macOS).
808///
809/// Uses `select()` to multiplex between the TUN fd and a shutdown pipe,
810/// avoiding the need to close the TUN fd externally (which would cause a
811/// double-close when `TunDevice` drops).
812#[cfg(target_os = "macos")]
813pub(crate) fn run_tun_reader(runtime: TunReaderRuntime, shutdown_fd: std::os::unix::io::RawFd) {
814    let TunReaderRuntime {
815        mut device,
816        mtu,
817        our_addr,
818        tun_tx,
819        outbound_tx,
820        transport_mtu,
821        path_mtu_lookup,
822    } = runtime;
823    let _shutdown_fd = ShutdownFd(shutdown_fd);
824    let tun_fd = device.device().as_raw_fd();
825    let (name, mut buf, max_mss) = tun_reader_setup(device.name(), mtu, transport_mtu);
826
827    // Set TUN fd to non-blocking so we can use select + read without blocking
828    // past the point where select returns readable.
829    unsafe {
830        let flags = libc::fcntl(tun_fd, libc::F_GETFL);
831        if flags >= 0 {
832            libc::fcntl(tun_fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
833        }
834    }
835
836    let nfds = tun_fd.max(shutdown_fd) + 1;
837
838    loop {
839        // Wait for either TUN data or shutdown signal
840        unsafe {
841            let mut read_fds: libc::fd_set = std::mem::zeroed();
842            libc::FD_ZERO(&mut read_fds);
843            libc::FD_SET(tun_fd, &mut read_fds);
844            libc::FD_SET(shutdown_fd, &mut read_fds);
845
846            let ret = libc::select(
847                nfds,
848                &mut read_fds,
849                std::ptr::null_mut(),
850                std::ptr::null_mut(),
851                std::ptr::null_mut(),
852            );
853            if ret < 0 {
854                let err = std::io::Error::last_os_error();
855                if err.kind() == std::io::ErrorKind::Interrupted {
856                    continue;
857                }
858                error!(name = %name, error = %err, "TUN select error");
859                break;
860            }
861
862            // Shutdown signal received
863            if libc::FD_ISSET(shutdown_fd, &read_fds) {
864                debug!(name = %name, "TUN reader received shutdown signal");
865                break;
866            }
867        }
868
869        // TUN fd is readable — drain all available packets
870        loop {
871            match device.read_packet(&mut buf) {
872                Ok(n) if n > 0 => {
873                    crate::perf_profile::record_tun_read_packet(n);
874                    if !handle_tun_packet(
875                        &mut buf[..n],
876                        max_mss,
877                        &name,
878                        our_addr,
879                        &tun_tx,
880                        &outbound_tx,
881                        &path_mtu_lookup,
882                    ) {
883                        return; // _shutdown_fd closes on drop
884                    }
885                }
886                Ok(_) => break, // No more data
887                Err(e) => {
888                    if e.kind() == std::io::ErrorKind::WouldBlock {
889                        break; // Done for this select round
890                    }
891                    // EBADF is expected during shutdown when the fd is closed
892                    if e.raw_os_error() != Some(libc::EBADF) {
893                        error!(name = %name, error = %e, "TUN read error");
894                    }
895                    return; // _shutdown_fd closes on drop
896                }
897            }
898        }
899    }
900    // _shutdown_fd closes on drop
901}
902
903/// Common setup for TUN reader: allocates buffer, computes max MSS.
904#[cfg(any(target_os = "macos", windows))]
905fn tun_reader_setup(device_name: &str, mtu: u16, transport_mtu: u16) -> (String, Vec<u8>, u16) {
906    tun_reader_setup_with_buffer_len(
907        device_name,
908        mtu,
909        transport_mtu,
910        default_tun_read_buffer_len(mtu),
911    )
912}
913
914#[cfg(any(target_os = "linux", target_os = "macos", windows))]
915fn tun_reader_setup_with_buffer_len(
916    device_name: &str,
917    mtu: u16,
918    transport_mtu: u16,
919    read_buffer_len: usize,
920) -> (String, Vec<u8>, u16) {
921    use super::icmp::effective_ipv6_mtu;
922
923    let name = device_name.to_string();
924    let buf = vec![0u8; read_buffer_len];
925
926    const IPV6_HEADER: u16 = 40;
927    const TCP_HEADER: u16 = 20;
928    let effective_mtu = effective_ipv6_mtu(transport_mtu);
929    let max_mss = effective_mtu
930        .saturating_sub(IPV6_HEADER)
931        .saturating_sub(TCP_HEADER);
932
933    debug!(
934        name = %name,
935        tun_mtu = mtu,
936        transport_mtu = transport_mtu,
937        effective_mtu = effective_mtu,
938        max_mss = max_mss,
939        "TUN reader starting"
940    );
941
942    (name, buf, max_mss)
943}
944
945#[cfg(any(target_os = "linux", target_os = "macos", windows))]
946fn default_tun_read_buffer_len(mtu: u16) -> usize {
947    mtu as usize + 100
948}
949
950/// Process a single TUN packet. Returns `false` if the reader should exit.
951#[cfg(any(target_os = "linux", target_os = "macos", windows))]
952fn handle_tun_packet(
953    packet: &mut [u8],
954    max_mss: u16,
955    name: &str,
956    our_addr: FipsAddress,
957    tun_tx: &TunTx,
958    outbound_tx: &TunOutboundTx,
959    path_mtu_lookup: &PathMtuLookup,
960) -> bool {
961    match prepare_tun_packet(packet, max_mss, name, our_addr, path_mtu_lookup) {
962        TunPacketAction::Forward => {
963            if outbound_tx
964                .admit_from_tun_reader(tun_outbound_packet(packet))
965                .is_err()
966            {
967                return false; // Channel closed, shutdown
968            }
969        }
970        TunPacketAction::Icmp(response) => {
971            if tun_tx.send(response).is_err() {
972                return false;
973            }
974        }
975        TunPacketAction::Ignore => {}
976    }
977    true
978}
979
980#[cfg(target_os = "linux")]
981fn handle_tun_packet_owned(
982    mut packet: Vec<u8>,
983    max_mss: u16,
984    name: &str,
985    our_addr: FipsAddress,
986    tun_tx: &TunTx,
987    outbound_tx: &TunOutboundTx,
988    path_mtu_lookup: &PathMtuLookup,
989) -> bool {
990    match prepare_tun_packet(&mut packet, max_mss, name, our_addr, path_mtu_lookup) {
991        TunPacketAction::Forward => {
992            if outbound_tx
993                .admit_from_tun_reader(tun_outbound_packet_owned(packet))
994                .is_err()
995            {
996                return false;
997            }
998        }
999        TunPacketAction::Icmp(response) => {
1000            if tun_tx.send(response).is_err() {
1001                return false;
1002            }
1003        }
1004        TunPacketAction::Ignore => {}
1005    }
1006    true
1007}
1008
1009#[cfg(any(target_os = "linux", target_os = "macos", windows))]
1010enum TunPacketAction {
1011    Forward,
1012    Icmp(Vec<u8>),
1013    Ignore,
1014}
1015
1016#[cfg(any(target_os = "linux", target_os = "macos", windows))]
1017fn prepare_tun_packet(
1018    packet: &mut [u8],
1019    max_mss: u16,
1020    name: &str,
1021    our_addr: FipsAddress,
1022    path_mtu_lookup: &PathMtuLookup,
1023) -> TunPacketAction {
1024    use super::icmp::{DestUnreachableCode, build_dest_unreachable, should_send_icmp_error};
1025    use super::tcp_mss::clamp_tcp_mss;
1026
1027    log_ipv6_packet(packet);
1028
1029    if packet.len() < 40 || packet[0] >> 4 != 6 {
1030        return TunPacketAction::Ignore;
1031    }
1032
1033    if packet[24] == crate::identity::FIPS_ADDRESS_PREFIX {
1034        let effective_max_mss = per_flow_max_mss(path_mtu_lookup, &packet[24..40], max_mss);
1035        if clamp_tcp_mss(packet, effective_max_mss) {
1036            trace!(name = %name, max_mss = effective_max_mss, "Clamped TCP MSS in SYN packet");
1037        }
1038        return TunPacketAction::Forward;
1039    }
1040
1041    if should_send_icmp_error(packet)
1042        && let Some(response) =
1043            build_dest_unreachable(packet, DestUnreachableCode::NoRoute, our_addr.to_ipv6())
1044    {
1045        trace!(name = %name, len = response.len(), "Sending ICMPv6 Destination Unreachable (non-FIPS destination)");
1046        return TunPacketAction::Icmp(response);
1047    }
1048
1049    TunPacketAction::Ignore
1050}
1051
1052#[cfg(any(test, target_os = "linux", target_os = "macos", windows))]
1053fn tun_outbound_packet(packet: &[u8]) -> Vec<u8> {
1054    let mut outbound = Vec::with_capacity(
1055        packet
1056            .len()
1057            .saturating_add(TUN_OUTBOUND_PACKET_TAIL_RESERVE),
1058    );
1059    outbound.extend_from_slice(packet);
1060    outbound
1061}
1062
1063#[cfg(any(test, target_os = "linux"))]
1064fn tun_outbound_packet_owned(mut packet: Vec<u8>) -> Vec<u8> {
1065    let needed = packet
1066        .len()
1067        .saturating_add(TUN_OUTBOUND_PACKET_TAIL_RESERVE);
1068    if packet.capacity() < needed {
1069        packet.reserve(needed - packet.capacity());
1070    }
1071    packet
1072}
1073
1074#[cfg(any(target_os = "linux", target_os = "macos"))]
1075impl std::fmt::Debug for TunDevice {
1076    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1077        f.debug_struct("TunDevice")
1078            .field("name", &self.name)
1079            .field("mtu", &self.mtu)
1080            .field("address", &self.address)
1081            .finish()
1082    }
1083}
1084
1085/// Log basic information about an IPv6 packet at TRACE level.
1086pub fn log_ipv6_packet(packet: &[u8]) {
1087    if packet.len() < 40 {
1088        debug!(len = packet.len(), "Received undersized packet");
1089        return;
1090    }
1091
1092    let version = packet[0] >> 4;
1093    if version != 6 {
1094        debug!(version, len = packet.len(), "Received non-IPv6 packet");
1095        return;
1096    }
1097
1098    let payload_len = u16::from_be_bytes([packet[4], packet[5]]);
1099    let next_header = packet[6];
1100    let hop_limit = packet[7];
1101
1102    let src = Ipv6Addr::from(<[u8; 16]>::try_from(&packet[8..24]).unwrap());
1103    let dst = Ipv6Addr::from(<[u8; 16]>::try_from(&packet[24..40]).unwrap());
1104
1105    let protocol = match next_header {
1106        6 => "TCP",
1107        17 => "UDP",
1108        58 => "ICMPv6",
1109        _ => "other",
1110    };
1111
1112    trace!("TUN packet received:");
1113    trace!("      src: {}", src);
1114    trace!("      dst: {}", dst);
1115    trace!(" protocol: {} ({})", protocol, next_header);
1116    trace!("  payload: {} bytes, hop_limit: {}", payload_len, hop_limit);
1117}
1118
1119/// Shutdown and delete a TUN interface by name.
1120///
1121/// This deletes the interface, which will cause any blocking reads
1122/// to return an error. Use this for graceful shutdown when the TUN device
1123/// has been moved to another thread.
1124#[cfg(any(target_os = "linux", target_os = "macos"))]
1125pub async fn shutdown_tun_interface(name: &str) -> Result<(), TunError> {
1126    debug!("Shutting down TUN interface {}", name);
1127    platform::delete_interface(name).await?;
1128    debug!("TUN interface {} stopped", name);
1129    Ok(())
1130}
1131
1132// ============================================================================
1133// Platform-specific system TUN modules
1134// ============================================================================
1135
1136#[cfg(windows)]
1137mod windows;
1138#[cfg(windows)]
1139pub(crate) use windows::run_tun_reader;
1140#[cfg(windows)]
1141pub use windows::{TunDevice, TunWriter, shutdown_tun_interface};
1142
1143#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
1144mod unsupported;
1145#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
1146pub(crate) use unsupported::run_tun_reader;
1147#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
1148pub use unsupported::{TunDevice, TunWriter, shutdown_tun_interface};
1149
1150pub(crate) struct TunReaderRuntime {
1151    pub(crate) device: TunDevice,
1152    pub(crate) mtu: u16,
1153    pub(crate) our_addr: FipsAddress,
1154    pub(crate) tun_tx: TunTx,
1155    pub(crate) outbound_tx: TunOutboundTx,
1156    pub(crate) transport_mtu: u16,
1157    pub(crate) path_mtu_lookup: PathMtuLookup,
1158}
1159
1160#[cfg(target_os = "linux")]
1161mod linux_vnet;
1162#[cfg(any(target_os = "linux", target_os = "macos"))]
1163mod platform;
1164
1165#[cfg(test)]
1166mod tests;