1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! # Submodule: Fast I/O operations
//!
//! This submodule  offers methods to interact with the NDIS filter driver, allowing users to
//! initialize the fast I/O mechanism, add a secondary fast I/O shared memory sections, and forward
//! packets to the driver or to the target network interface or protocol layer. The methods in this
//! submodule are designed to be highly flexible, allowing for parameterization by the size of the shared
//! memory section or the number of packets to send. This submodule is part of the larger NDISAPI module,
//! which provides a high-level API for the Windows Packet Filter on Windows.
//!

use std::mem::size_of;

use windows::{core::Result, Win32::Foundation::GetLastError, Win32::System::IO::DeviceIoControl};

use super::Ndisapi;
use crate::driver::*;

impl Ndisapi {
    /// This function adds a secondary Fast I/O shared memory section to the NDIS filter driver,
    /// allowing faster communication between user-mode applications and the driver.
    ///
    /// # Type Parameters
    ///
    /// * `N`: The size of the Fast I/O shared memory section.
    ///
    /// # Arguments
    ///
    /// * `fast_io_section`: A mutable reference to a `FastIoSection<N>` object representing
    ///   the shared memory section to be added.
    ///
    /// # Returns
    ///
    /// * `Result<()>`: If successful, returns `Ok(())`. Otherwise, returns an error.
    pub fn add_secondary_fast_io<const N: usize>(
        &self,
        fast_io_section: &mut FastIoSection<N>,
    ) -> Result<()> {
        let params = InitializeFastIoParams::<N> {
            header_ptr: fast_io_section as *mut FastIoSection<N>,
            data_size: N as u32,
        };

        let result = unsafe {
            DeviceIoControl(
                self.driver_handle,
                IOCTL_NDISRD_ADD_SECOND_FAST_IO_SECTION,
                Some(&params as *const InitializeFastIoParams<N> as *const std::ffi::c_void),
                size_of::<InitializeFastIoParams<N>>() as u32,
                None,
                0,
                None,
                None,
            )
        };

        if !result.as_bool() {
            Err(unsafe { GetLastError() }.into())
        } else {
            Ok(())
        }
    }

    /// This function initializes the fast I/O mechanism for the NDIS filter driver and
    /// submits the initial shared memory section for faster communication between
    /// user-mode applications and the driver.
    ///
    /// # Type Parameters
    ///
    /// * `N`: The size of the Fast I/O shared memory section.
    ///
    /// # Arguments
    ///
    /// * `fast_io_section`: A mutable reference to a `FastIoSection<N>` object representing
    ///   the shared memory section to be submitted.
    ///
    /// # Returns
    ///
    /// * `Result<()>`: If successful, returns `Ok(())`. Otherwise, returns an error.
    pub fn initialize_fast_io<const N: usize>(
        &self,
        fast_io_section: &mut FastIoSection<N>,
    ) -> Result<()> {
        let params = InitializeFastIoParams::<N> {
            header_ptr: fast_io_section as *mut FastIoSection<N>,
            data_size: N as u32,
        };

        let result = unsafe {
            DeviceIoControl(
                self.driver_handle,
                IOCTL_NDISRD_INITIALIZE_FAST_IO,
                Some(&params as *const InitializeFastIoParams<N> as *const std::ffi::c_void),
                size_of::<InitializeFastIoParams<N>>() as u32,
                None,
                0,
                None,
                None,
            )
        };

        if !result.as_bool() {
            Err(unsafe { GetLastError() }.into())
        } else {
            Ok(())
        }
    }

    /// This function retrieves queued packets from the NDIS filter driver without considering
    /// the network interface. It reads packets in an unsorted manner and stores them in the
    /// provided buffer.
    ///
    /// # Type Parameters
    ///
    /// * `N`: The number of packets to read.
    ///
    /// # Arguments
    ///
    /// * `packets`: A mutable reference to an array of `IntermediateBuffer` objects, where the
    ///   read packets will be stored.
    ///
    /// # Returns
    ///
    /// * `Result<usize>`: If successful, returns `Ok(usize)` with the number of packets read.
    ///   Otherwise, returns an error.
    pub fn read_packets_unsorted<const N: usize>(
        &self,
        packets: &mut [IntermediateBuffer; N],
    ) -> Result<usize> {
        let mut request = UnsortedReadSendRequest::<N> {
            packets: packets as *mut [IntermediateBuffer; N],
            packets_num: N as u32,
        };

        let result = unsafe {
            DeviceIoControl(
                self.driver_handle,
                IOCTL_NDISRD_READ_PACKETS_UNSORTED,
                Some(&request as *const UnsortedReadSendRequest<N> as *const std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                Some(&mut request as *mut UnsortedReadSendRequest<N> as *mut std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                None,
                None,
            )
        };

        if !result.as_bool() {
            Err(unsafe { GetLastError() }.into())
        } else {
            Ok(request.packets_num as usize)
        }
    }

    /// This function forwards packets to the NDIS filter driver in an unsorted manner, which then
    /// sends them to the target network interface. The target adapter handle should be set in the
    /// `IntermediateBuffer.header.adapter_handle` field.
    ///
    /// # Type Parameters
    ///
    /// * `N`: The number of packets to send.
    ///
    /// # Arguments
    ///
    /// * `packets`: A mutable reference to an array of `IntermediateBuffer` objects, which contain
    ///   the packets to be sent.
    /// * `packets_num`: The number of packets to send from the array.
    ///
    /// # Returns
    ///
    /// * `Result<usize>`: If successful, returns `Ok(usize)` with the number of packets sent.
    ///   Otherwise, returns an error.
    pub fn send_packets_to_adapters_unsorted<const N: usize>(
        &self,
        packets: &mut [IntermediateBuffer; N],
        packets_num: usize,
    ) -> Result<usize> {
        let mut request = UnsortedReadSendRequest::<N> {
            packets: packets as *mut [IntermediateBuffer; N],
            packets_num: packets_num as u32,
        };

        let result = unsafe {
            DeviceIoControl(
                self.driver_handle,
                IOCTL_NDISRD_SEND_PACKET_TO_ADAPTER_UNSORTED,
                Some(&request as *const UnsortedReadSendRequest<N> as *const std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                Some(&mut request as *mut UnsortedReadSendRequest<N> as *mut std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                None,
                None,
            )
        };

        if !result.as_bool() {
            Err(unsafe { GetLastError() }.into())
        } else {
            Ok(request.packets_num as usize)
        }
    }

    /// This function forwards packets to the NDIS filter driver in an unsorted manner, which then
    /// sends them to the target protocols layer (MSTCP). The target adapter handle (to be indicated
    /// from) should be set in the `IntermediateBuffer.header.adapter_handle` field.
    ///
    /// # Type Parameters
    ///
    /// * `N`: The number of packets to send.
    ///
    /// # Arguments
    ///
    /// * `packets`: A mutable reference to an array of `IntermediateBuffer` objects, which contain
    ///   the packets to be sent.
    /// * `packets_num`: The number of packets to send from the array.
    ///
    /// # Returns
    ///
    /// * `Result<usize>`: If successful, returns `Ok(usize)` with the number of packets sent.
    ///   Otherwise, returns an error.
    pub fn send_packets_to_mstcp_unsorted<const N: usize>(
        &self,
        packets: &mut [IntermediateBuffer; N],
        packets_num: usize,
    ) -> Result<usize> {
        let mut request = UnsortedReadSendRequest::<N> {
            packets: packets as *mut [IntermediateBuffer; N],
            packets_num: packets_num as u32,
        };

        let result = unsafe {
            DeviceIoControl(
                self.driver_handle,
                IOCTL_NDISRD_SEND_PACKET_TO_MSTCP_UNSORTED,
                Some(&request as *const UnsortedReadSendRequest<N> as *const std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                Some(&mut request as *mut UnsortedReadSendRequest<N> as *mut std::ffi::c_void),
                size_of::<UnsortedReadSendRequest<N>>() as u32,
                None,
                None,
            )
        };

        if !result.as_bool() {
            Err(unsafe { GetLastError() }.into())
        } else {
            Ok(request.packets_num as usize)
        }
    }
}