| 1 | use std::net::Ipv4Addr; |
| 2 | |
| 3 | use crate::{cerr, interface::InterfaceName}; |
| 4 | |
| 5 | use super::RawSocket; |
| 6 | |
| 7 | #[repr(C)] |
| 8 | struct so_timestamping { |
| 9 | flags: libc::c_int, |
| 10 | bind_phc: libc::c_int, |
| 11 | } |
| 12 | |
| 13 | impl RawSocket { |
| 14 | pub(crate) fn so_timestamping(&self, options: u32, bind_phc: u32) -> std::io::Result<()> { |
| 15 | // Documentation on the timestamping calls: |
| 16 | // |
| 17 | // - linux: https://www.kernel.org/doc/Documentation/networking/timestamping.txt |
| 18 | // - freebsd: https://man.freebsd.org/cgi/man.cgi?setsockopt |
| 19 | // |
| 20 | // SAFETY: |
| 21 | // |
| 22 | // - the socket is provided by (safe) rust, and will outlive the call |
| 23 | // - method is guaranteed to be a valid "name" argument |
| 24 | // - the options pointer outlives the call |
| 25 | // - the `option_len` corresponds with the options pointer |
| 26 | // |
| 27 | // Only some bits are valid to set in `options`, but setting invalid bits is |
| 28 | // perfectly safe |
| 29 | // |
| 30 | // > Setting other bit returns EINVAL and does not change the current state. |
| 31 | let tstamp_config = so_timestamping { |
| 32 | flags: options as libc::c_int, |
| 33 | bind_phc: bind_phc as libc::c_int, |
| 34 | }; |
| 35 | |
| 36 | unsafe { |
| 37 | cerr(libc::setsockopt( |
| 38 | self.fd, |
| 39 | libc::SOL_SOCKET, |
| 40 | libc::SO_TIMESTAMPING, |
| 41 | &tstamp_config as *const _ as *const libc::c_void, |
| 42 | std::mem::size_of_val(&tstamp_config) as libc::socklen_t, |
| 43 | )) |
| 44 | }?; |
| 45 | Ok(()) |
| 46 | } |
| 47 | |
| 48 | pub(crate) fn driver_enable_hardware_timestamping( |
| 49 | &self, |
| 50 | interface: InterfaceName, |
| 51 | rx_filter: libc::c_int, |
| 52 | ) -> std::io::Result<()> { |
| 53 | let mut tstamp_config = libc::hwtstamp_config { |
| 54 | flags: 0, |
| 55 | tx_type: libc::HWTSTAMP_TX_ON as _, |
| 56 | rx_filter, |
| 57 | }; |
| 58 | |
| 59 | let mut ifreq = libc::ifreq { |
| 60 | ifr_name: interface.to_ifr_name(), |
| 61 | ifr_ifru: libc::__c_anonymous_ifr_ifru { |
| 62 | ifru_data: (&mut tstamp_config as *mut _) as *mut libc::c_char, |
| 63 | }, |
| 64 | }; |
| 65 | |
| 66 | // Safety: |
| 67 | // ifreq lives for the duration of the call, ioctl is safe to call otherwise |
| 68 | cerr(unsafe { libc::ioctl(self.fd, libc::SIOCSHWTSTAMP as _, &mut ifreq) })?; |
| 69 | Ok(()) |
| 70 | } |
| 71 | |
| 72 | pub(crate) fn bind_to_device(&self, interface_name: InterfaceName) -> std::io::Result<()> { |
| 73 | let value = interface_name.as_str().as_bytes(); |
| 74 | let len = value.len(); |
| 75 | |
| 76 | // Safety: |
| 77 | // value lives for the duration of the call, and is of size len. |
| 78 | // setsockopt is safe to call in all other regards |
| 79 | unsafe { |
| 80 | cerr(libc::setsockopt( |
| 81 | self.fd, |
| 82 | libc::SOL_SOCKET, |
| 83 | libc::SO_BINDTODEVICE, |
| 84 | value.as_ptr().cast(), |
| 85 | len as libc::socklen_t, |
| 86 | ))?; |
| 87 | } |
| 88 | |
| 89 | Ok(()) |
| 90 | } |
| 91 | |
| 92 | pub(crate) fn ip_multicast_if(&self, interface_name: InterfaceName) -> std::io::Result<()> { |
| 93 | let request = libc::ip_mreqn { |
| 94 | imr_multiaddr: libc::in_addr { |
| 95 | s_addr: u32::from_ne_bytes(Ipv4Addr::UNSPECIFIED.octets()), |
| 96 | }, |
| 97 | imr_address: libc::in_addr { |
| 98 | s_addr: u32::from_ne_bytes(Ipv4Addr::UNSPECIFIED.octets()), |
| 99 | }, |
| 100 | imr_ifindex: interface_name |
| 101 | .get_index() |
| 102 | .ok_or(std::io::ErrorKind::InvalidInput)? as _, |
| 103 | }; |
| 104 | |
| 105 | // Safety: |
| 106 | // request lives for the duration of the call, and we pass its size |
| 107 | // as option_len. setsockopt is safe to call in all other regards |
| 108 | cerr(unsafe { |
| 109 | libc::setsockopt( |
| 110 | self.fd, |
| 111 | libc::IPPROTO_IP, |
| 112 | libc::IP_MULTICAST_IF, |
| 113 | &request as *const _ as *const _, |
| 114 | std::mem::size_of_val(&request) as _, |
| 115 | ) |
| 116 | })?; |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | pub(crate) fn ipv6_multicast_if(&self, interface_name: InterfaceName) -> std::io::Result<()> { |
| 121 | let index = interface_name |
| 122 | .get_index() |
| 123 | .ok_or(std::io::ErrorKind::InvalidInput)?; |
| 124 | |
| 125 | // Safety: |
| 126 | // index lives for the duration of the call, and we pass its size |
| 127 | // as option_len. setsockopt is safe to call in all other regards |
| 128 | cerr(unsafe { |
| 129 | libc::setsockopt( |
| 130 | self.fd, |
| 131 | libc::IPPROTO_IPV6, |
| 132 | libc::IPV6_MULTICAST_IF, |
| 133 | &index as *const _ as *const _, |
| 134 | std::mem::size_of_val(&index) as _, |
| 135 | ) |
| 136 | })?; |
| 137 | Ok(()) |
| 138 | } |
| 139 | |
| 140 | pub(crate) fn ip_multicast_loop(&self, enabled: bool) -> std::io::Result<()> { |
| 141 | let state: i32 = if enabled { 1 } else { 0 }; |
| 142 | |
| 143 | // Safety: |
| 144 | // state lives for the duration of the call, and we pass its size |
| 145 | // as option_len. setsockopt is safe to call in all other regards. |
| 146 | cerr(unsafe { |
| 147 | libc::setsockopt( |
| 148 | self.fd, |
| 149 | libc::IPPROTO_IP, |
| 150 | libc::IP_MULTICAST_LOOP, |
| 151 | &state as *const _ as *const _, |
| 152 | std::mem::size_of_val(&state) as _, |
| 153 | ) |
| 154 | })?; |
| 155 | Ok(()) |
| 156 | } |
| 157 | |
| 158 | pub(crate) fn ipv6_multicast_loop(&self, enabled: bool) -> std::io::Result<()> { |
| 159 | let state: i32 = if enabled { 1 } else { 0 }; |
| 160 | |
| 161 | // Safety: |
| 162 | // state lives for the duration of the call, and we pass its size |
| 163 | // as option_len. setsockopt is safe to call in all other regards. |
| 164 | cerr(unsafe { |
| 165 | libc::setsockopt( |
| 166 | self.fd, |
| 167 | libc::IPPROTO_IPV6, |
| 168 | libc::IPV6_MULTICAST_LOOP, |
| 169 | &state as *const _ as *const _, |
| 170 | std::mem::size_of_val(&state) as _, |
| 171 | ) |
| 172 | })?; |
| 173 | Ok(()) |
| 174 | } |
| 175 | |
| 176 | pub(crate) fn ipv6_v6only(&self, enabled: bool) -> std::io::Result<()> { |
| 177 | let state: i32 = if enabled { 1 } else { 0 }; |
| 178 | |
| 179 | // Safety: |
| 180 | // state lives for the duration of the call, and we pass its size |
| 181 | // as option_len. setsockopt is safe to call in all other regards. |
| 182 | cerr(unsafe { |
| 183 | libc::setsockopt( |
| 184 | self.fd, |
| 185 | libc::IPPROTO_IPV6, |
| 186 | libc::IPV6_V6ONLY, |
| 187 | &state as *const _ as *const _, |
| 188 | std::mem::size_of_val(&state) as _, |
| 189 | ) |
| 190 | })?; |
| 191 | Ok(()) |
| 192 | } |
| 193 | } |