1use 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
50pub 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#[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 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#[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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
189pub enum TunState {
190 Disabled,
192 Configured,
194 Active,
196 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#[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 pub async fn create(config: &TunConfig, address: FipsAddress) -> Result<Self, TunError> {
287 if platform::is_ipv6_disabled() {
289 return Err(TunError::Ipv6Disabled);
290 }
291
292 let name = config.name();
293 let mtu = config.mtu();
294
295 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 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 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 pub fn name(&self) -> &str {
353 &self.name
354 }
355
356 pub fn mtu(&self) -> u16 {
358 self.mtu
359 }
360
361 pub fn address(&self) -> &FipsAddress {
363 &self.address
364 }
365
366 #[cfg(target_os = "macos")]
368 pub fn device(&self) -> &tun::Device {
369 &self.device
370 }
371
372 #[cfg(target_os = "macos")]
374 pub fn device_mut(&mut self) -> &mut tun::Device {
375 &mut self.device
376 }
377
378 pub fn read_packet(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
390 #[cfg(target_os = "linux")]
391 {
392 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 self.device.as_raw_fd()
423 }
424
425 #[cfg(target_os = "macos")]
426 self.device.as_raw_fd()
427 }
428
429 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 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 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#[cfg(target_os = "macos")]
486const UTUN_AF_INET6: u32 = 30;
487
488#[cfg(target_os = "macos")]
495#[inline]
496fn utun_af_inet6_header() -> [u8; 4] {
497 UTUN_AF_INET6.to_be_bytes()
498}
499
500#[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#[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 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 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 debug_ipv4_icmp_packet("Linux vnet TUN packet written", packet.as_slice());
604 trace!(name = %self.name, len = packet.len(), "TUN packet written");
605 }
606 }
607
608 batch.clear();
609 }
610 }
611
612 #[cfg(target_os = "macos")]
613 fn write_packet(&self, packet: &super::tun_write::TunWritePacket) -> std::io::Result<()> {
614 use std::os::unix::io::AsRawFd;
615
616 let af_header = utun_af_inet6_header();
617 let iov = [
618 libc::iovec {
619 iov_base: af_header.as_ptr() as *mut libc::c_void,
620 iov_len: 4,
621 },
622 libc::iovec {
623 iov_base: packet.as_slice().as_ptr() as *mut libc::c_void,
624 iov_len: packet.len(),
625 },
626 ];
627 let ret = unsafe { libc::writev(self.file.as_raw_fd(), iov.as_ptr(), 2) };
628 if ret < 0 {
629 return Err(std::io::Error::last_os_error());
630 }
631 let expected = 4 + packet.len();
632 if (ret as usize) < expected {
633 return Err(std::io::Error::new(
634 std::io::ErrorKind::WriteZero,
635 format!("short writev: {} of {} bytes", ret, expected),
636 ));
637 }
638 Ok(())
639 }
640
641 #[cfg(not(target_os = "macos"))]
642 fn write_packet(&mut self, packet: &super::tun_write::TunWritePacket) -> std::io::Result<()> {
643 self.file.write_all(packet.as_slice())
644 }
645
646 pub fn run(self) {
651 #[cfg(target_os = "linux")]
652 let mut writer = self;
653 #[cfg(target_os = "macos")]
654 let writer = self;
655
656 debug!(name = %writer.name, max_mss = writer.max_mss, "TUN writer starting");
657
658 #[cfg(target_os = "linux")]
659 if writer.vnet_hdr {
660 writer.run_linux_vnet();
661 return;
662 }
663
664 while let Some(mut packet) = writer.rx.recv() {
665 writer.clamp_inbound_packet(&mut packet);
666 let write_result = writer.write_packet(&packet);
667
668 if let Err(e) = write_result {
669 let err_str = e.to_string();
671 if err_str.contains("Bad address") {
672 break;
673 }
674 error!(name = %writer.name, error = %e, "TUN write error");
675 } else {
676 crate::perf_profile::record_tun_write_packet(packet.len());
677 debug_ipv4_icmp_packet("TUN packet written", packet.as_slice());
678 trace!(name = %writer.name, len = packet.len(), "TUN packet written");
679 }
680 }
681 }
682}
683
684#[cfg(any(target_os = "linux", target_os = "macos"))]
685fn debug_ipv4_icmp_packet(message: &'static str, packet: &[u8]) {
686 let Some((src, dst, icmp_type, icmp_id, icmp_seq)) = ipv4_icmp_echo(packet) else {
687 return;
688 };
689 debug!(
690 src = %src,
691 dst = %dst,
692 icmp_type,
693 icmp_id,
694 icmp_seq,
695 message
696 );
697}
698
699#[cfg(any(target_os = "linux", target_os = "macos"))]
700fn ipv4_icmp_echo(packet: &[u8]) -> Option<(std::net::Ipv4Addr, std::net::Ipv4Addr, u8, u16, u16)> {
701 if packet.len() < 28 || packet[0] >> 4 != 4 || packet[9] != 1 {
702 return None;
703 }
704 let header_len = usize::from(packet[0] & 0x0f).checked_mul(4)?;
705 if header_len < 20 || packet.len() < header_len.saturating_add(8) {
706 return None;
707 }
708 let icmp_type = packet[header_len];
709 if !matches!(icmp_type, 0 | 8) {
710 return None;
711 }
712 let src = std::net::Ipv4Addr::new(packet[12], packet[13], packet[14], packet[15]);
713 let dst = std::net::Ipv4Addr::new(packet[16], packet[17], packet[18], packet[19]);
714 let icmp_id = u16::from_be_bytes([packet[header_len + 4], packet[header_len + 5]]);
715 let icmp_seq = u16::from_be_bytes([packet[header_len + 6], packet[header_len + 7]]);
716 Some((src, dst, icmp_type, icmp_id, icmp_seq))
717}
718
719#[cfg(any(target_os = "linux", target_os = "macos"))]
720mod platform;
721
722#[cfg(windows)]
723mod windows;
724
725#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
726mod unsupported;
727
728#[cfg(target_os = "linux")]
729mod linux_vnet;
730
731#[cfg(test)]
732mod tests;
733
734include!("tun_runtime.rs");