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-raw / src / protocol / network / tcp4.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! TCPv4 Protocol |
| 4 | //! |
| 5 | //! This module provides the TCPv4 Protocol interface definitions. The |
| 6 | //! TCPv4 Protocol provides services to send and receive data streams |
| 7 | //! over IPv4 networks. |
| 8 | //! |
| 9 | //! The protocol is defined in the [UEFI Specification, Section 28.1](https://uefi.org/specs/UEFI/2.11/28_Network_Protocols_TCP_IP_and_Configuration.html#efi-tcpv4-protocol). |
| 10 | |
| 11 | use crate::protocol::network::ip4::Ip4ModeData; |
| 12 | use crate::protocol::network::snp::NetworkMode; |
| 13 | use crate::{Boolean, Event, Guid, Handle, Ipv4Address, Status, guid, newtype_enum}; |
| 14 | use core::ffi::c_void; |
| 15 | use core::fmt; |
| 16 | use core::fmt::{Debug, Formatter}; |
| 17 | |
| 18 | #[derive(Debug)] |
| 19 | #[repr(C)] |
| 20 | pub struct Tcp4Protocol { |
| 21 | /// Returns the current operational status. |
| 22 | /// |
| 23 | /// `get_mode_data` copies the current operational settings of |
| 24 | /// this instance into user-supplied structs. This function can |
| 25 | /// also be used to retrieve the operational setting of underlying |
| 26 | /// drivers such as IPv4, MNP, or SNP. |
| 27 | pub get_mode_data: unsafe extern "efiapi" fn( |
| 28 | this: *mut Self, |
| 29 | connection_state: *mut Tcp4ConnectionState, |
| 30 | config_data: *mut Tcp4ConfigData, |
| 31 | ip4_mode_data: *mut Ip4ModeData, |
| 32 | managed_network_config_data: *mut c_void, |
| 33 | simple_network_mode: *mut NetworkMode, |
| 34 | ) -> Status, |
| 35 | |
| 36 | /// Initialize or brutally reset the operational parameters for |
| 37 | /// this instance. |
| 38 | /// |
| 39 | /// No other [`Tcp4Protocol`] operation can be executed by this |
| 40 | /// instance until it is configured properly. For an active |
| 41 | /// [`Tcp4Protocol`] instance, after a proper configuration it may |
| 42 | /// call [`Tcp4Protocol::connect`] to initiates the three-way |
| 43 | /// handshake. For a passive [`Tcp4Protocol`] instance, its state |
| 44 | /// will transition to [`Tcp4ConnectionState::LISTEN`] after |
| 45 | /// configuration, and [`accept`][Self::accept] may be called to |
| 46 | /// listen for incoming TCP connection requests. If `config_data` |
| 47 | /// is set to `NULL`, the instance is reset. Resetting process |
| 48 | /// will be done brutally; the state machine will be set to |
| 49 | /// [`Tcp4ConnectionState::CLOSED`] directly, the receive queue |
| 50 | /// and transmit queue will be flushed, and no traffic will be |
| 51 | /// allowed through this instance. |
| 52 | pub configure: |
| 53 | unsafe extern "efiapi" fn(this: *mut Self, config_data: *const Tcp4ConfigData) -> Status, |
| 54 | |
| 55 | /// Add or delete routing entries. |
| 56 | /// |
| 57 | /// The most specific route is selected by comparing the |
| 58 | /// `subnet_address` with the destination IP address |
| 59 | /// arithmetically ANDed with the `subnet_mask`. |
| 60 | /// |
| 61 | /// The default route is added with both `subnet_address` and |
| 62 | /// `subnet_mask` set to `0.0.0.0`. The default route matches all |
| 63 | /// destination IP addresses if there is no more specific route. |
| 64 | /// |
| 65 | /// A direct route is added with `gateway_address` set to |
| 66 | /// `0.0.0.0`. Packets are sent to the destination host if its |
| 67 | /// address can be found in the Address Resolution Protocol (ARP) |
| 68 | /// cache or it is on the local subnet. If the instance is |
| 69 | /// configured to use default address, a direct route to the local |
| 70 | /// network will be added automatically. |
| 71 | /// |
| 72 | /// Each TCP instance has its own independent routing table. An |
| 73 | /// instance that uses the default IP address will have a copy of |
| 74 | /// the |
| 75 | /// [`Ipv4Config2Protocol`][super::ip4_config2::Ip4Config2Protocol]'s |
| 76 | /// routing table. The copy will be updated automatically whenever |
| 77 | /// the IP driver reconfigures its instance. As a result, the |
| 78 | /// previous modification to the instance's local copy will be |
| 79 | /// lost. |
| 80 | /// |
| 81 | /// The priority of checking the route table is specific to the IP |
| 82 | /// implementation, and every IP implementation must comply with |
| 83 | /// RFC 1122. |
| 84 | /// |
| 85 | /// Note: There is no way to set up routes to other network |
| 86 | /// interface cards (NICs) because each NIC has its own |
| 87 | /// independent network stack that shares information only through |
| 88 | /// EFI TCPv4 variable. |
| 89 | pub routes: unsafe extern "efiapi" fn( |
| 90 | this: *mut Self, |
| 91 | delete_route: Boolean, |
| 92 | subnet_address: *const Ipv4Address, |
| 93 | subnet_mask: *const Ipv4Address, |
| 94 | gateway_address: *const Ipv4Address, |
| 95 | ) -> Status, |
| 96 | |
| 97 | /// Initiate a nonblocking TCP connection request for an active |
| 98 | /// TCP instance. |
| 99 | /// |
| 100 | /// `connect` initiates an active open to the remote peer |
| 101 | /// configured in the current TCP instance if it is configured as |
| 102 | /// active. If the connection succeeds or fails due to any error, |
| 103 | /// the [`token.completion_token.event`][Tcp4CompletionToken::event] |
| 104 | /// will be signaled and `token.completion_token.status` will be |
| 105 | /// updated accordingly. This function can only be called for the |
| 106 | /// TCP instance in the [`Tcp4ConnectionState::CLOSED`] state. The |
| 107 | /// instance will transition to [`Tcp4ConnectionState::SYN_SENT`] |
| 108 | /// if the function returns [`Status::SUCCESS`]. If the TCP |
| 109 | /// three-way handshake succeeds, its state will become |
| 110 | /// [`Tcp4ConnectionState::ESTABLISHED`], otherwise, the state |
| 111 | /// will return to [`Tcp4ConnectionState::CLOSED`]. |
| 112 | pub connect: |
| 113 | unsafe extern "efiapi" fn(this: *mut Self, token: *mut Tcp4ConnectionToken) -> Status, |
| 114 | |
| 115 | /// Listen on the passive instance to accept an incoming |
| 116 | /// connection request. This is a nonblocking operation. |
| 117 | /// |
| 118 | /// The `accept` function initiates an asynchronous accept request |
| 119 | /// to wait for an incoming connection on the passive TCP |
| 120 | /// instance. If a remote peer successfully establishes a |
| 121 | /// connection with this instance, a new TCP instance will be |
| 122 | /// created and its handle will be returned in |
| 123 | /// [`new_child_handle`][Tcp4ListenToken::new_child_handle]. The |
| 124 | /// newly created instance is configured by inheriting the passive |
| 125 | /// instance's configuration and is ready for use upon return. The |
| 126 | /// instance is in the [`Tcp4ConnectionState::ESTABLISHED`] |
| 127 | /// state. |
| 128 | /// |
| 129 | /// The event of the [`completion_token`][Tcp4ListenToken::completion_token] |
| 130 | /// is signaled when a new connection is accepted, the user aborts |
| 131 | /// the listen, or the connection is reset. On success, |
| 132 | /// [`new_child_handle`][Tcp4ListenToken::new_child_handle] holds |
| 133 | /// the instance of the accepted connection. |
| 134 | /// |
| 135 | /// This function can only be called when the current TCP instance |
| 136 | /// is in [`Tcp4ConnectionState::LISTEN`] state. |
| 137 | pub accept: |
| 138 | unsafe extern "efiapi" fn(this: *mut Self, listen_token: *mut Tcp4ListenToken) -> Status, |
| 139 | |
| 140 | /// Queues outgoing data into the transmit queue. |
| 141 | /// |
| 142 | /// The `transmit` function queues a sending request to this |
| 143 | /// instance along with the user data. The status of the token is |
| 144 | /// updated and the event in the token will be signaled once the |
| 145 | /// data is sent out or some error occurs. |
| 146 | pub transmit: unsafe extern "efiapi" fn(this: *mut Self, token: *mut Tcp4IoToken) -> Status, |
| 147 | |
| 148 | /// Places an asynchronous receive request into the receiving |
| 149 | /// queue. |
| 150 | /// |
| 151 | /// `receive` places a completion token into the receive packet |
| 152 | /// queue. This function is always asynchronous. The caller must |
| 153 | /// allocate the |
| 154 | /// [`token.completion_token.event`][Tcp4CompletionToken::event] |
| 155 | /// and the `fragment_buffer` used to receive data. They also must |
| 156 | /// fill [`data_length`][Tcp4ReceiveData::data_length] which |
| 157 | /// represents the whole length of all `fragment_buffer`. When the |
| 158 | /// receive operation completes, the driver updates the |
| 159 | /// [`token.completion_token.status`][Tcp4CompletionToken::status] |
| 160 | /// and [`token.packet.rx_data`][Tcp4Packet::rx_data] fields and |
| 161 | /// and the `token.completion_token.event` is signaled. If data |
| 162 | /// was received, the data and its length will be copied into the |
| 163 | /// `fragment_table`. At the same time, the full length of |
| 164 | /// received data will be recorded in the `data_length` |
| 165 | /// fields. Providing a proper notification function and context |
| 166 | /// for the event will enable the user to receive the notification |
| 167 | /// and receiving status. That notification function is guaranteed |
| 168 | /// to not be re-entered. |
| 169 | pub receive: unsafe extern "efiapi" fn(this: *mut Self, token: *mut Tcp4IoToken) -> Status, |
| 170 | |
| 171 | /// Disconnect a TCP connection gracefully or reset a TCP |
| 172 | /// connection. This function is a nonblocking operation. |
| 173 | /// |
| 174 | /// Initiate an asynchronous close token to TCP driver. After |
| 175 | /// `close` is called, any buffered transmission data will be sent |
| 176 | /// by TCP driver and the current instance will have a graceful |
| 177 | /// close working flow described as RFC 793 if `abort_on_close` is |
| 178 | /// set to `false`, otherwise, a reset packet will be sent by the |
| 179 | /// TCP driver to quickly disconnect this connection. When the |
| 180 | /// close operation completes successfully the TCP instance is in |
| 181 | /// [`Tcp4ConnectionState::CLOSED`] state, all pending |
| 182 | /// asynchronous operations are signaled, and any buffers used for |
| 183 | /// TCP network traffic are flushed. |
| 184 | pub close: |
| 185 | unsafe extern "efiapi" fn(this: *mut Self, close_token: *mut Tcp4CloseToken) -> Status, |
| 186 | |
| 187 | /// Abort an asynchronous connection, listen, transmission or |
| 188 | /// receive request. |
| 189 | /// |
| 190 | /// The `cancel` function aborts a pending connection, listen, |
| 191 | /// transmit or receive request. If `completion_token` is not |
| 192 | /// `NULL` and the token is in the connection, listen, |
| 193 | /// transmission or receive queue when it is being cancelled, its |
| 194 | /// `status` will be set to [`Status::ABORTED`] and then `event` |
| 195 | /// will be signaled. If the token is not in one of the queues, |
| 196 | /// which usually means that the asynchronous operation has |
| 197 | /// completed, [`Status::NOT_FOUND`] is returned. If |
| 198 | /// `completion_token` is `NULL`, all asynchronous tokens issued |
| 199 | /// by [`Tcp4Protocol::connect`], [`Tcp4Protocol::accept`], |
| 200 | /// [`Tcp4Protocol::transmit`] and [`Tcp4Protocol::receive`] |
| 201 | /// will be aborted. |
| 202 | pub cancel: unsafe extern "efiapi" fn( |
| 203 | this: *mut Self, |
| 204 | completion_token: *mut Tcp4CompletionToken, |
| 205 | ) -> Status, |
| 206 | |
| 207 | /// Poll to receive incoming data and transmit outgoing segments. |
| 208 | /// |
| 209 | /// The `poll` function increases the rate that data is moved |
| 210 | /// between the network and application and can be called when the |
| 211 | /// TCP instance is created successfully. Its use is optional. |
| 212 | /// |
| 213 | /// In some implementations, the periodical timer in the MNP |
| 214 | /// driver may not poll the underlying communications device fast |
| 215 | /// enough to avoid dropping packets. Drivers and applications |
| 216 | /// that are experiencing packet loss should try calling the |
| 217 | /// `poll` function at a high frequency. |
| 218 | pub poll: unsafe extern "efiapi" fn(this: *mut Self) -> Status, |
| 219 | } |
| 220 | |
| 221 | impl Tcp4Protocol { |
| 222 | /// The GUID for the TCPv4 protocol. |
| 223 | /// |
| 224 | /// Defined in the [UEFI Specification, Section 28.1.4](https://uefi.org/specs/UEFI/2.11/28_Network_Protocols_TCP_IP_and_Configuration.html#efi-tcp4-protocol). |
| 225 | pub const GUID: Guid = guid!("65530BC7-A359-410F-B010-5AADC7EC2B62"); |
| 226 | |
| 227 | /// The GUID for the TCPv4 service binding protocol. |
| 228 | /// |
| 229 | /// Defined in the [UEFI Specification, Section 28.1.2](https://uefi.org/specs/UEFI/2.11/28_Network_Protocols_TCP_IP_and_Configuration.html#efi-tcp4-service-binding-protocol). |
| 230 | pub const SERVICE_BINDING_GUID: Guid = guid!("00720665-67EB-4a99-BAF7-D3C33A1C7CC9"); |
| 231 | } |
| 232 | |
| 233 | newtype_enum! { |
| 234 | pub enum Tcp4ConnectionState: i32 => { |
| 235 | CLOSED = 0, |
| 236 | LISTEN = 1, |
| 237 | SYN_SENT = 2, |
| 238 | SYN_RECEIVED = 3, |
| 239 | ESTABLISHED = 4, |
| 240 | FIN_WAIT1 = 5, |
| 241 | FIN_WAIT2 = 6, |
| 242 | CLOSING = 7, |
| 243 | TIME_WAIT = 8, |
| 244 | CLOSE_WAIT = 9, |
| 245 | LAST_ACK = 10, |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | #[derive(Debug)] |
| 250 | #[repr(C)] |
| 251 | pub struct Tcp4ConfigData { |
| 252 | /// Type of service field in transmitted IPv4 packets. |
| 253 | pub type_of_service: u8, |
| 254 | /// Time to live field in transmitted IPv4 packets. |
| 255 | pub time_to_live: u8, |
| 256 | /// Access point configuration. |
| 257 | pub access_point: Tcp4AccessPoint, |
| 258 | /// Optional TCP configuration parameters. |
| 259 | pub control_option: *mut Tcp4Option, |
| 260 | } |
| 261 | |
| 262 | #[derive(Debug, Clone)] |
| 263 | #[repr(C)] |
| 264 | pub struct Tcp4AccessPoint { |
| 265 | /// Set to `TRUE` to use the default IP address. |
| 266 | pub use_default_address: Boolean, |
| 267 | /// The local IP address assigned to this TCP instance. |
| 268 | pub station_address: Ipv4Address, |
| 269 | /// The subnet mask associated with the station address. |
| 270 | pub subnet_mask: Ipv4Address, |
| 271 | /// The local port number. |
| 272 | pub station_port: u16, |
| 273 | /// The remote IP address. |
| 274 | pub remote_address: Ipv4Address, |
| 275 | /// The remote port number. |
| 276 | pub remote_port: u16, |
| 277 | /// Set to `TRUE` for active open, `FALSE` for passive open. |
| 278 | pub active_flag: Boolean, |
| 279 | } |
| 280 | |
| 281 | #[derive(Debug)] |
| 282 | #[repr(C)] |
| 283 | pub struct Tcp4Option { |
| 284 | /// Size of the TCP receive buffer. |
| 285 | pub receive_buffer_size: u32, |
| 286 | /// Size of the TCP send buffer. |
| 287 | pub send_buffer_size: u32, |
| 288 | /// Maximum number of pending connections for passive instances. |
| 289 | pub max_syn_back_log: u32, |
| 290 | /// Connection timeout in seconds. |
| 291 | pub connection_timeout: u32, |
| 292 | /// Number of data retransmission attempts. |
| 293 | pub data_retries: u32, |
| 294 | /// FIN timeout in seconds. |
| 295 | pub fin_timeout: u32, |
| 296 | /// TIME_WAIT timeout in seconds. |
| 297 | pub time_wait_timeout: u32, |
| 298 | /// Number of keep-alive probes. |
| 299 | pub keep_alive_probes: u32, |
| 300 | /// Time before sending keep-alive probes in seconds. |
| 301 | pub keep_alive_time: u32, |
| 302 | /// Interval between keep-alive probes in seconds. |
| 303 | pub keep_alive_interval: u32, |
| 304 | /// Set to `TRUE` to enable Nagle algorithm. |
| 305 | pub enable_nagle: Boolean, |
| 306 | /// Set to `TRUE` to enable TCP timestamps. |
| 307 | pub enable_time_stamp: Boolean, |
| 308 | /// Set to `TRUE` to enable window scaling. |
| 309 | pub enable_window_scaling: Boolean, |
| 310 | /// Set to `TRUE` to enable selective acknowledgment. |
| 311 | pub enable_selective_ack: Boolean, |
| 312 | /// Set to `TRUE` to enable path MTU discovery. |
| 313 | pub enable_path_mtu_discovery: Boolean, |
| 314 | } |
| 315 | |
| 316 | #[derive(Debug)] |
| 317 | #[repr(C)] |
| 318 | pub struct Tcp4CompletionToken { |
| 319 | /// Event to signal when the operation completes. |
| 320 | pub event: Event, |
| 321 | /// Status of the completed operation. |
| 322 | pub status: Status, |
| 323 | } |
| 324 | |
| 325 | #[derive(Debug)] |
| 326 | #[repr(C)] |
| 327 | pub struct Tcp4ConnectionToken { |
| 328 | pub completion_token: Tcp4CompletionToken, |
| 329 | } |
| 330 | |
| 331 | #[derive(Debug)] |
| 332 | #[repr(C)] |
| 333 | pub struct Tcp4ListenToken { |
| 334 | /// Completion token for the listen operation. |
| 335 | pub completion_token: Tcp4CompletionToken, |
| 336 | /// The new TCP instance handle created for the established |
| 337 | /// connection. |
| 338 | pub new_child_handle: Handle, |
| 339 | } |
| 340 | |
| 341 | #[derive(Debug)] |
| 342 | #[repr(C)] |
| 343 | pub struct Tcp4IoToken { |
| 344 | /// Completion token for the I/O operation. |
| 345 | pub completion_token: Tcp4CompletionToken, |
| 346 | /// Packet data for the I/O operation. |
| 347 | pub packet: Tcp4Packet, |
| 348 | } |
| 349 | |
| 350 | #[derive(Debug)] |
| 351 | #[repr(C)] |
| 352 | pub struct Tcp4CloseToken { |
| 353 | /// Completion token for the close operation. |
| 354 | pub completion_token: Tcp4CompletionToken, |
| 355 | /// Abort the TCP connection on close instead of the standard TCP |
| 356 | /// close process when it is set to TRUE. This option can be used |
| 357 | /// to satisfy a fast disconnect. |
| 358 | pub abort_on_close: Boolean, |
| 359 | } |
| 360 | |
| 361 | #[repr(C)] |
| 362 | pub union Tcp4Packet { |
| 363 | /// Pointer to receive data structure. |
| 364 | pub rx_data: *mut Tcp4ReceiveData, |
| 365 | /// Pointer to transmit data structure. |
| 366 | pub tx_data: *mut Tcp4TransmitData, |
| 367 | } |
| 368 | |
| 369 | impl Debug for Tcp4Packet { |
| 370 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 371 | f.debug_struct("Tcp4Packet").finish() |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | #[derive(Debug)] |
| 376 | #[repr(C)] |
| 377 | pub struct Tcp4FragmentData { |
| 378 | /// Length of the fragment in bytes. |
| 379 | pub fragment_length: u32, |
| 380 | /// Pointer to an array of contiguous bytes, at least |
| 381 | /// `fragment_length` in length. |
| 382 | pub fragment_buf: *mut u8, |
| 383 | } |
| 384 | |
| 385 | #[derive(Debug)] |
| 386 | #[repr(C)] |
| 387 | pub struct Tcp4ReceiveData { |
| 388 | /// When `TRUE`, the instance is in urgent mode. The |
| 389 | /// implementations of this specification should follow RFC793 to |
| 390 | /// process urgent data, and should NOT mix the data across the |
| 391 | /// urgent point in one token. |
| 392 | pub urgent: Boolean, |
| 393 | /// When calling [`receive`][Tcp4Protocol::receive], the caller |
| 394 | /// is responsible for setting it to the byte counts of all |
| 395 | /// fragment buffers. When the token is signaled by the driver |
| 396 | /// it is the length of received data in the fragments. |
| 397 | pub data_length: u32, |
| 398 | /// Number of fragments in the following fragment table. |
| 399 | pub fragment_count: u32, |
| 400 | /// Variable-length array of fragment descriptors. |
| 401 | /// |
| 402 | /// NOTE: this is a flexible array member. |
| 403 | pub fragment_table: [Tcp4FragmentData; 0], |
| 404 | } |
| 405 | |
| 406 | #[derive(Debug)] |
| 407 | #[repr(C)] |
| 408 | pub struct Tcp4TransmitData { |
| 409 | /// If `TRUE`, data must be transmitted promptly, and the PUSH bit |
| 410 | /// in the last TCP segment created will be set. If `FALSE`, data |
| 411 | /// transmission may be delayed to combine with data from |
| 412 | /// subsequent [`transmit`][Tcp4Protocol::transmit] for |
| 413 | /// efficiency. |
| 414 | pub push: Boolean, |
| 415 | /// The data in the fragment table are urgent and urgent point is |
| 416 | /// in effect if `TRUE`. Otherwise those data are NOT considered |
| 417 | /// urgent. |
| 418 | pub urgent: Boolean, |
| 419 | /// Total length of data to transmit. |
| 420 | pub data_length: u32, |
| 421 | /// Number of fragments in the following fragment table. |
| 422 | pub fragment_count: u32, |
| 423 | /// Variable-length array of fragment descriptors. |
| 424 | /// |
| 425 | /// NOTE: this is a flexible array member. |
| 426 | pub fragment_table: [Tcp4FragmentData; 0], |
| 427 | } |
| 428 | |
| 429 | #[derive(Debug)] |
| 430 | #[repr(C)] |
| 431 | pub struct Tcp4ClientConnectionModeParams { |
| 432 | /// Remote IP address for the connection. |
| 433 | pub remote_ip: Ipv4Address, |
| 434 | /// Remote port for the connection. |
| 435 | pub remote_port: u16, |
| 436 | } |