tuntap_interface.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use super::*;
  2. use crate::{phy::Medium, wire::EthernetFrame};
  3. use std::io;
  4. use std::os::unix::io::{AsRawFd, RawFd};
  5. #[derive(Debug)]
  6. pub struct TunTapInterfaceDesc {
  7. lower: libc::c_int,
  8. mtu: usize,
  9. }
  10. impl AsRawFd for TunTapInterfaceDesc {
  11. fn as_raw_fd(&self) -> RawFd {
  12. self.lower
  13. }
  14. }
  15. impl TunTapInterfaceDesc {
  16. pub fn new(name: &str, medium: Medium) -> io::Result<TunTapInterfaceDesc> {
  17. let lower = unsafe {
  18. let lower = libc::open(
  19. "/dev/net/tun\0".as_ptr() as *const libc::c_char,
  20. libc::O_RDWR | libc::O_NONBLOCK,
  21. );
  22. if lower == -1 {
  23. return Err(io::Error::last_os_error());
  24. }
  25. lower
  26. };
  27. let mut ifreq = ifreq_for(name);
  28. Self::attach_interface_ifreq(lower, medium, &mut ifreq)?;
  29. let mtu = Self::mtu_ifreq(medium, &mut ifreq)?;
  30. Ok(TunTapInterfaceDesc { lower, mtu })
  31. }
  32. pub fn from_fd(fd: RawFd, mtu: usize) -> io::Result<TunTapInterfaceDesc> {
  33. Ok(TunTapInterfaceDesc { lower: fd, mtu })
  34. }
  35. fn attach_interface_ifreq(
  36. lower: libc::c_int,
  37. medium: Medium,
  38. ifr: &mut ifreq,
  39. ) -> io::Result<()> {
  40. let mode = match medium {
  41. #[cfg(feature = "medium-ip")]
  42. Medium::Ip => imp::IFF_TUN,
  43. #[cfg(feature = "medium-ethernet")]
  44. Medium::Ethernet => imp::IFF_TAP,
  45. #[cfg(feature = "medium-ieee802154")]
  46. Medium::Ieee802154 => todo!(),
  47. };
  48. ifr.ifr_data = mode | imp::IFF_NO_PI;
  49. ifreq_ioctl(lower, ifr, imp::TUNSETIFF).map(|_| ())
  50. }
  51. fn mtu_ifreq(medium: Medium, ifr: &mut ifreq) -> io::Result<usize> {
  52. let lower = unsafe {
  53. let lower = libc::socket(libc::AF_INET, libc::SOCK_DGRAM, libc::IPPROTO_IP);
  54. if lower == -1 {
  55. return Err(io::Error::last_os_error());
  56. }
  57. lower
  58. };
  59. let ip_mtu = ifreq_ioctl(lower, ifr, imp::SIOCGIFMTU).map(|mtu| mtu as usize);
  60. unsafe {
  61. libc::close(lower);
  62. }
  63. // Propagate error after close, to ensure we always close.
  64. let ip_mtu = ip_mtu?;
  65. // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
  66. // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
  67. let mtu = match medium {
  68. #[cfg(feature = "medium-ip")]
  69. Medium::Ip => ip_mtu,
  70. #[cfg(feature = "medium-ethernet")]
  71. Medium::Ethernet => ip_mtu + EthernetFrame::<&[u8]>::header_len(),
  72. #[cfg(feature = "medium-ieee802154")]
  73. Medium::Ieee802154 => todo!(),
  74. };
  75. Ok(mtu)
  76. }
  77. pub fn interface_mtu(&self) -> io::Result<usize> {
  78. Ok(self.mtu)
  79. }
  80. pub fn recv(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
  81. unsafe {
  82. let len = libc::read(
  83. self.lower,
  84. buffer.as_mut_ptr() as *mut libc::c_void,
  85. buffer.len(),
  86. );
  87. if len == -1 {
  88. return Err(io::Error::last_os_error());
  89. }
  90. Ok(len as usize)
  91. }
  92. }
  93. pub fn send(&mut self, buffer: &[u8]) -> io::Result<usize> {
  94. unsafe {
  95. let len = libc::write(
  96. self.lower,
  97. buffer.as_ptr() as *const libc::c_void,
  98. buffer.len(),
  99. );
  100. if len == -1 {
  101. return Err(io::Error::last_os_error());
  102. }
  103. Ok(len as usize)
  104. }
  105. }
  106. }
  107. impl Drop for TunTapInterfaceDesc {
  108. fn drop(&mut self) {
  109. unsafe {
  110. libc::close(self.lower);
  111. }
  112. }
  113. }