raw_socket.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use std::cell::RefCell;
  2. use std::io;
  3. use std::os::unix::io::{AsRawFd, RawFd};
  4. use std::rc::Rc;
  5. use std::vec::Vec;
  6. use crate::phy::{self, sys, Device, DeviceCapabilities, Medium};
  7. use crate::time::Instant;
  8. /// A socket that captures or transmits the complete frame.
  9. #[derive(Debug)]
  10. pub struct RawSocket {
  11. medium: Medium,
  12. lower: Rc<RefCell<sys::RawSocketDesc>>,
  13. mtu: usize,
  14. }
  15. impl AsRawFd for RawSocket {
  16. fn as_raw_fd(&self) -> RawFd {
  17. self.lower.borrow().as_raw_fd()
  18. }
  19. }
  20. impl RawSocket {
  21. /// Creates a raw socket, bound to the interface called `name`.
  22. ///
  23. /// This requires superuser privileges or a corresponding capability bit
  24. /// set on the executable.
  25. pub fn new(name: &str, medium: Medium) -> io::Result<RawSocket> {
  26. let mut lower = sys::RawSocketDesc::new(name, medium)?;
  27. lower.bind_interface()?;
  28. let mut mtu = lower.interface_mtu()?;
  29. #[cfg(feature = "medium-ieee802154")]
  30. if medium == Medium::Ieee802154 {
  31. // SIOCGIFMTU returns 127 - (ACK_PSDU - FCS - 1) - FCS.
  32. // 127 - (5 - 2 - 1) - 2 = 123
  33. // For IEEE802154, we want to add (ACK_PSDU - FCS - 1), since that is what SIOCGIFMTU
  34. // uses as the size of the link layer header.
  35. //
  36. // https://github.com/torvalds/linux/blob/7475e51b87969e01a6812eac713a1c8310372e8a/net/mac802154/iface.c#L541
  37. mtu += 2;
  38. }
  39. #[cfg(feature = "medium-ethernet")]
  40. if medium == Medium::Ethernet {
  41. // SIOCGIFMTU returns the IP MTU (typically 1500 bytes.)
  42. // smoltcp counts the entire Ethernet packet in the MTU, so add the Ethernet header size to it.
  43. mtu += crate::wire::EthernetFrame::<&[u8]>::header_len()
  44. }
  45. Ok(RawSocket {
  46. medium,
  47. lower: Rc::new(RefCell::new(lower)),
  48. mtu,
  49. })
  50. }
  51. }
  52. impl Device for RawSocket {
  53. type RxToken<'a> = RxToken
  54. where
  55. Self: 'a;
  56. type TxToken<'a> = TxToken
  57. where
  58. Self: 'a;
  59. fn capabilities(&self) -> DeviceCapabilities {
  60. DeviceCapabilities {
  61. max_transmission_unit: self.mtu,
  62. medium: self.medium,
  63. ..DeviceCapabilities::default()
  64. }
  65. }
  66. fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
  67. let mut lower = self.lower.borrow_mut();
  68. let mut buffer = vec![0; self.mtu];
  69. match lower.recv(&mut buffer[..]) {
  70. Ok(size) => {
  71. buffer.resize(size, 0);
  72. let rx = RxToken { buffer };
  73. let tx = TxToken {
  74. lower: self.lower.clone(),
  75. };
  76. Some((rx, tx))
  77. }
  78. Err(err) if err.kind() == io::ErrorKind::WouldBlock => None,
  79. Err(err) => panic!("{}", err),
  80. }
  81. }
  82. fn transmit(&mut self, _timestamp: Instant) -> Option<Self::TxToken<'_>> {
  83. Some(TxToken {
  84. lower: self.lower.clone(),
  85. })
  86. }
  87. }
  88. #[doc(hidden)]
  89. pub struct RxToken {
  90. buffer: Vec<u8>,
  91. }
  92. impl phy::RxToken for RxToken {
  93. fn consume<R, F>(self, f: F) -> R
  94. where
  95. F: FnOnce(&[u8]) -> R,
  96. {
  97. f(&self.buffer[..])
  98. }
  99. }
  100. #[doc(hidden)]
  101. pub struct TxToken {
  102. lower: Rc<RefCell<sys::RawSocketDesc>>,
  103. }
  104. impl phy::TxToken for TxToken {
  105. fn consume<R, F>(self, len: usize, f: F) -> R
  106. where
  107. F: FnOnce(&mut [u8]) -> R,
  108. {
  109. let mut lower = self.lower.borrow_mut();
  110. let mut buffer = vec![0; len];
  111. let result = f(&mut buffer);
  112. match lower.send(&buffer[..]) {
  113. Ok(_) => {}
  114. Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
  115. net_debug!("phy: tx failed due to WouldBlock")
  116. }
  117. Err(err) => panic!("{}", err),
  118. }
  119. result
  120. }
  121. }