aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / network / snp.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Simple Network Protocol |
| 4 | //! |
| 5 | //! Provides a packet level interface to a network adapter. |
| 6 | //! Once the adapter is initialized, the protocol provides services that allows |
| 7 | //! packets to be transmitted and received. |
| 8 | //! |
| 9 | //! No interface function must be called until `SimpleNetwork.start` is successfully |
| 10 | //! called first. |
| 11 | |
| 12 | use crate::data_types::Event; |
| 13 | use crate::proto::unsafe_protocol; |
| 14 | use crate::{Result, StatusExt}; |
| 15 | use core::ffi::c_void; |
| 16 | use core::net::IpAddr; |
| 17 | use core::ptr; |
| 18 | use core::ptr::NonNull; |
| 19 | use uefi::Error; |
| 20 | use uefi_raw::protocol::network::snp::SimpleNetworkProtocol; |
| 21 | use uefi_raw::{Boolean, IpAddress as EfiIpAddr, MacAddress as EfiMacAddr, Status}; |
| 22 | |
| 23 | pub use uefi_raw::protocol::network::snp::{ |
| 24 | InterruptStatus, NetworkMode, NetworkState, NetworkStatistics, ReceiveFlags, |
| 25 | }; |
| 26 | |
| 27 | /// Simple Network [`Protocol`]. |
| 28 | /// |
| 29 | /// [`Protocol`]: uefi::proto::Protocol |
| 30 | #[derive(Debug)] |
| 31 | #[repr(transparent)] |
| 32 | #[unsafe_protocol(SimpleNetworkProtocol::GUID)] |
| 33 | pub struct SimpleNetwork(SimpleNetworkProtocol); |
| 34 | |
| 35 | impl SimpleNetwork { |
| 36 | /// Change the state of a network from "Stopped" to "Started". |
| 37 | pub fn start(&mut self) -> Result { |
| 38 | // SAFETY: The memory is valid. |
| 39 | unsafe { (self.0.start)(&self.0) }.to_result() |
| 40 | } |
| 41 | |
| 42 | /// Change the state of a network interface from "Started" to "Stopped". |
| 43 | pub fn stop(&mut self) -> Result { |
| 44 | // SAFETY: The memory is valid. |
| 45 | unsafe { (self.0.stop)(&self.0) }.to_result() |
| 46 | } |
| 47 | |
| 48 | /// Reset a network adapter and allocate the transmit and receive buffers |
| 49 | /// required by the network interface; optionally, also request allocation of |
| 50 | /// additional transmit and receive buffers. |
| 51 | pub fn initialize( |
| 52 | &mut self, |
| 53 | extra_rx_buffer_size: usize, |
| 54 | extra_tx_buffer_size: usize, |
| 55 | ) -> Result { |
| 56 | // SAFETY: The memory is valid. |
| 57 | unsafe { (self.0.initialize)(&self.0, extra_rx_buffer_size, extra_tx_buffer_size) } |
| 58 | .to_result() |
| 59 | } |
| 60 | |
| 61 | /// Reset a network adapter and reinitialize it with the parameters that were |
| 62 | /// provided in the previous call to `initialize`. |
| 63 | pub fn reset(&mut self, extended_verification: bool) -> Result { |
| 64 | // SAFETY: The memory is valid. |
| 65 | unsafe { (self.0.reset)(&self.0, Boolean::from(extended_verification)) }.to_result() |
| 66 | } |
| 67 | |
| 68 | /// Reset a network adapter, leaving it in a state that is safe |
| 69 | /// for another driver to initialize |
| 70 | pub fn shutdown(&mut self) -> Result { |
| 71 | // SAFETY: The memory is valid. |
| 72 | unsafe { (self.0.shutdown)(&self.0) }.to_result() |
| 73 | } |
| 74 | |
| 75 | /// Manage the multicast receive filters of a network. |
| 76 | pub fn receive_filters( |
| 77 | &mut self, |
| 78 | enable: ReceiveFlags, |
| 79 | disable: ReceiveFlags, |
| 80 | reset_mcast_filter: bool, |
| 81 | mcast_filter: Option<&[EfiMacAddr]>, |
| 82 | ) -> Result { |
| 83 | let filter_count = mcast_filter.map(|filters| filters.len()).unwrap_or(0); |
| 84 | let filters = mcast_filter |
| 85 | .map(|filters| filters.as_ptr()) |
| 86 | .unwrap_or(ptr::null_mut()); |
| 87 | |
| 88 | // SAFETY: The memory is valid. |
| 89 | unsafe { |
| 90 | (self.0.receive_filters)( |
| 91 | &self.0, |
| 92 | enable, |
| 93 | disable, |
| 94 | Boolean::from(reset_mcast_filter), |
| 95 | filter_count, |
| 96 | filters, |
| 97 | ) |
| 98 | } |
| 99 | .to_result() |
| 100 | } |
| 101 | |
| 102 | /// Modify or reset the current station address, if supported. |
| 103 | pub fn station_address(&mut self, reset: bool, new: Option<&EfiMacAddr>) -> Result { |
| 104 | // SAFETY: The memory is valid. |
| 105 | unsafe { |
| 106 | (self.0.station_address)( |
| 107 | &self.0, |
| 108 | Boolean::from(reset), |
| 109 | new.map(ptr::from_ref).unwrap_or(ptr::null()), |
| 110 | ) |
| 111 | } |
| 112 | .to_result() |
| 113 | } |
| 114 | |
| 115 | /// Reset statistics on a network interface. |
| 116 | pub fn reset_statistics(&self) -> Result { |
| 117 | // SAFETY: The memory is valid. |
| 118 | unsafe { |
| 119 | (self.0.statistics)( |
| 120 | &self.0, |
| 121 | Boolean::from(true), |
| 122 | ptr::null_mut(), |
| 123 | ptr::null_mut(), |
| 124 | ) |
| 125 | } |
| 126 | .to_result() |
| 127 | } |
| 128 | |
| 129 | /// Collect statistics on a network interface. |
| 130 | pub fn collect_statistics(&self) -> Result<NetworkStatistics> { |
| 131 | let mut stats_table: NetworkStatistics = Default::default(); |
| 132 | let mut stats_size = size_of::<NetworkStatistics>(); |
| 133 | // SAFETY: The memory is valid. |
| 134 | let status = unsafe { |
| 135 | (self.0.statistics)( |
| 136 | &self.0, |
| 137 | Boolean::from(false), |
| 138 | &mut stats_size, |
| 139 | &mut stats_table, |
| 140 | ) |
| 141 | }; |
| 142 | status.to_result_with_val(|| stats_table) |
| 143 | } |
| 144 | |
| 145 | /// Convert a multicast IP address to a multicast HW MAC Address. |
| 146 | pub fn mcast_ip_to_mac(&self, ipv6: bool, ip: IpAddr) -> Result<EfiMacAddr> { |
| 147 | let mut mac_address = EfiMacAddr([0; 32]); |
| 148 | let ip = EfiIpAddr::from(ip); |
| 149 | // SAFETY: The memory is valid. |
| 150 | let status = unsafe { |
| 151 | (self.0.multicast_ip_to_mac)( |
| 152 | &self.0, |
| 153 | Boolean::from(ipv6), |
| 154 | &raw const ip, |
| 155 | &mut mac_address, |
| 156 | ) |
| 157 | }; |
| 158 | status.to_result_with_val(|| mac_address) |
| 159 | } |
| 160 | |
| 161 | /// Reads data from the NVRAM device attached to the network interface into |
| 162 | /// the provided `dst_buffer`. |
| 163 | pub fn read_nv_data(&self, offset: usize, dst_buffer: &mut [u8]) -> Result { |
| 164 | // SAFETY: The memory is valid. |
| 165 | unsafe { |
| 166 | (self.0.non_volatile_data)( |
| 167 | &self.0, |
| 168 | Boolean::from(true), |
| 169 | offset, |
| 170 | dst_buffer.len(), |
| 171 | dst_buffer.as_mut_ptr().cast(), |
| 172 | ) |
| 173 | } |
| 174 | .to_result() |
| 175 | } |
| 176 | |
| 177 | /// Writes data into the NVRAM device attached to the network interface from |
| 178 | /// the provided `src_buffer`. |
| 179 | pub fn write_nv_data(&self, offset: usize, src_buffer: &[u8]) -> Result { |
| 180 | // SAFETY: The memory is valid. |
| 181 | unsafe { |
| 182 | (self.0.non_volatile_data)( |
| 183 | &self.0, |
| 184 | Boolean::from(false), |
| 185 | offset, |
| 186 | src_buffer.len(), |
| 187 | // SAFETY: The buffer is only used for reading. |
| 188 | src_buffer.as_ptr().cast::<c_void>().cast_mut(), |
| 189 | ) |
| 190 | } |
| 191 | .to_result() |
| 192 | } |
| 193 | |
| 194 | /// Read the current interrupt status and recycled transmit buffer |
| 195 | /// status from a network interface. |
| 196 | pub fn get_interrupt_status(&mut self) -> Result<InterruptStatus> { |
| 197 | let mut interrupt_status = InterruptStatus::empty(); |
| 198 | let status = |
| 199 | // SAFETY: The memory is valid. |
| 200 | unsafe { (self.0.get_status)(&self.0, &mut interrupt_status, ptr::null_mut()) }; |
| 201 | status.to_result_with_val(|| interrupt_status) |
| 202 | } |
| 203 | |
| 204 | /// Read the current recycled transmit buffer status from a |
| 205 | /// network interface. |
| 206 | pub fn get_recycled_transmit_buffer_status(&mut self) -> Result<Option<NonNull<u8>>> { |
| 207 | let mut tx_buf: *mut c_void = ptr::null_mut(); |
| 208 | // SAFETY: The memory is valid. |
| 209 | let status = unsafe { (self.0.get_status)(&self.0, ptr::null_mut(), &mut tx_buf) }; |
| 210 | status.to_result_with_val(|| NonNull::new(tx_buf.cast())) |
| 211 | } |
| 212 | |
| 213 | /// Place a packet in the transmit queue of the network interface. |
| 214 | /// |
| 215 | /// The packet structure varies based on the type of network interface. In |
| 216 | /// typical scenarios, the protocol is implemented for Ethernet devices, |
| 217 | /// meaning this function transmits Ethernet frames. |
| 218 | /// |
| 219 | /// The header of the packet can be filled by the function with the given |
| 220 | /// parameters, but the buffer must already reserve the space for the |
| 221 | /// header. |
| 222 | /// |
| 223 | /// # Arguments |
| 224 | /// - `header_size`: The size in bytes of the media header to be filled by |
| 225 | /// the `transmit()` function. If this is `0`, the (ethernet frame) header |
| 226 | /// will not be filled by the function and taken as-is from the buffer. |
| 227 | /// If it is nonzero, then it must be equal to `media_header_size` of |
| 228 | /// the corresponding [`NetworkMode`] and the `dst_addr` and `protocol` |
| 229 | /// parameters must not be `None`. |
| 230 | /// - `buffer`: The buffer containing the whole network packet with all |
| 231 | /// its payload including the header for the medium. If `header_size` |
| 232 | /// is nonzero, the function writes the media header into the start of |
| 233 | /// the buffer. |
| 234 | /// - `src_addr`: The optional source address. |
| 235 | /// - `dst_addr`: The optional destination address. |
| 236 | /// - `protocol`: Ether Type as of RFC 3232. See |
| 237 | /// [IANA IEEE 802 Numbers][ethertype] for examples. Typically, this is |
| 238 | /// `0x0800` (IPv4) or `0x0806` (ARP). |
| 239 | /// |
| 240 | /// [ethertype]: https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml#ieee-802-numbers-1 |
| 241 | pub fn transmit( |
| 242 | &self, |
| 243 | header_size: usize, |
| 244 | buffer: &mut [u8], |
| 245 | src_addr: Option<EfiMacAddr>, |
| 246 | dst_addr: Option<EfiMacAddr>, |
| 247 | protocol: Option<u16>, |
| 248 | ) -> Result { |
| 249 | // SAFETY: The memory is valid. |
| 250 | unsafe { |
| 251 | (self.0.transmit)( |
| 252 | &self.0, |
| 253 | header_size, |
| 254 | buffer.len(), |
| 255 | buffer.as_mut_ptr().cast(), |
| 256 | src_addr.as_ref().map(ptr::from_ref).unwrap_or(ptr::null()), |
| 257 | dst_addr.as_ref().map(ptr::from_ref).unwrap_or(ptr::null()), |
| 258 | protocol.as_ref().map(ptr::from_ref).unwrap_or(ptr::null()), |
| 259 | ) |
| 260 | } |
| 261 | .to_result() |
| 262 | } |
| 263 | |
| 264 | /// Receive a packet from a network interface. |
| 265 | /// |
| 266 | /// On success, returns the size of bytes of the received packet. |
| 267 | pub fn receive( |
| 268 | &self, |
| 269 | buffer: &mut [u8], |
| 270 | header_size: Option<&mut usize>, |
| 271 | src_addr: Option<&mut EfiMacAddr>, |
| 272 | dest_addr: Option<&mut EfiMacAddr>, |
| 273 | protocol: Option<&mut u16>, |
| 274 | ) -> Result<usize> { |
| 275 | let mut buffer_size = buffer.len(); |
| 276 | // SAFETY: The memory is valid. |
| 277 | let status = unsafe { |
| 278 | (self.0.receive)( |
| 279 | &self.0, |
| 280 | header_size.map(ptr::from_mut).unwrap_or(ptr::null_mut()), |
| 281 | &mut buffer_size, |
| 282 | buffer.as_mut_ptr().cast(), |
| 283 | src_addr.map(ptr::from_mut).unwrap_or(ptr::null_mut()), |
| 284 | dest_addr.map(ptr::from_mut).unwrap_or(ptr::null_mut()), |
| 285 | protocol.map(ptr::from_mut).unwrap_or(ptr::null_mut()), |
| 286 | ) |
| 287 | }; |
| 288 | status.to_result_with_val(|| buffer_size) |
| 289 | } |
| 290 | |
| 291 | /// Event that fires once a packet is available to be received. |
| 292 | /// |
| 293 | /// On QEMU, this event seems to never fire; it is suggested to verify that your implementation |
| 294 | /// of UEFI properly implements this event before using it. |
| 295 | pub fn wait_for_packet_event(&self) -> Result<Event> { |
| 296 | // SAFETY: The memory is valid. |
| 297 | unsafe { Event::from_ptr(self.0.wait_for_packet) }.ok_or(Error::from(Status::UNSUPPORTED)) |
| 298 | } |
| 299 | |
| 300 | /// Returns a reference to the Simple Network mode. |
| 301 | /// |
| 302 | /// The firmware updates the mode from the methods that take `&mut self`, |
| 303 | /// so the returned reference cannot be held across those calls. |
| 304 | #[must_use] |
| 305 | pub const fn mode(&self) -> &NetworkMode { |
| 306 | // SAFETY: The memory is valid. |
| 307 | unsafe { &*self.0.mode } |
| 308 | } |
| 309 | } |