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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! # Submodule: Basic NDISAPI static filter definitions
//!
//! This submodule contains various structures used for static filters in the NDISAPI Rust library.
//!
//! The `filters` submodule provides a Rust interface for configuring static filters for the Windows Packet
//! Filter driver. It contains structures for specifying filter conditions and actions for various protocols,
//! including Ethernet 802.3, IPv4, IPv6, TCP, UDP, and ICMP. These structures allow users to define complex
//! filtering rules based on multiple packet fields and layers.
//!
//! # Structures
//!
//! * [`Eth8023Filter`] - Represents a static filter for Ethernet 802.3 frames.
//! * [`IpV4Filter`] - Represents a static filter for IPv4 packets.
//! * [`IpV6Filter`] - Represents a static filter for IPv6 packets.
//! * [`TcpUdpFilter`] - Represents a static filter for TCP and UDP packets.
//! * [`IcmpFilter`] - Represents a static filter for ICMP packets.
//! * [`StaticFilter`] - Represents a single static filter entry that combines filter conditions for various
//! layers and the filter action to be taken.
//! * [`StaticFilterTable`] - Represents a table of static filters, used for managing multiple static filter entries.
// Import required external crates and types
use ;
use *;
/// This structure is used to define an Ethernet 802.3 filter based on various fields like source and destination addresses, and protocol.
///
/// A Rust equivalent for the [_ETH_802_3_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_eth_802_3_filter/) structure.
/// Creates a new `Eth8023Filter` instance.
///
/// # Arguments
///
/// * `valid_fields` - A bitmask indicating which fields in the filter are valid.
/// * `src_address` - The source address to filter on.
/// * `dest_address` - The destination address to filter on.
/// * `protocol` - The protocol (Ethertype) to filter on.
///
/// # Returns
///
/// * A new `Eth8023Filter` instance.
/// This structure is used to represent an IPv4 subnet based on an IP address and a subnet mask.
///
/// A Rust equivalent for the [_IP_SUBNET_V4](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_subnet_v4/) structure.
/// Creates a new `IpSubnetV4` instance.
///
/// # Arguments
///
/// * `ip` - The IPv4 address.
/// * `ip_mask` - The subnet mask.
///
/// # Returns
///
/// * A new `IpSubnetV4` instance.
/// This structure is used to represent an IPv4 address range based on a start and end IP address.
///
/// A Rust equivalent for the [_IP_RANGE_V4](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_range_v4/) structure.
/// Creates a new `IpRangeV4` instance.
///
/// # Arguments
///
/// * `start_ip` - The start of the IPv4 address range.
/// * `end_ip` - The end of the IPv4 address range.
///
/// # Returns
///
/// * A new `IpRangeV4` instance.
/// A Rust union representing either an IPv4 subnet (IpSubnetV4) or an IPv4 address range (IpRangeV4).
pub union IpAddressV4Union
/// Represents an IPv4 address in a format used by the packet filtering mechanism.
///
/// A Rust equivalent for [_IP_ADDRESS_V4](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_address_v4/).
///
/// The `address_type` field indicates whether the address is a subnet or a range.
/// The `address` field contains the actual IPv4 address information in a union format.
/// Creates a new `IpAddressV4` instance.
///
/// # Arguments
///
/// * `address_type` - Indicates whether the address is a subnet or a range.
/// * `address` - Contains the actual IPv4 address information in a union format.
///
/// # Returns
///
/// * A new `IpAddressV4` instance.
/// Represents an IPv4 filter used by the packet filtering mechanism.
///
/// A Rust equivalent for [_IP_V4_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_v4_filter/).
///
/// The `valid_fields` field specifies which fields in the filter structure are used for filtering.
/// The `src_address` field contains the source IPv4 address information.
/// The `dest_address` field contains the destination IPv4 address information.
/// The `protocol` field represents the IP protocol number.
/// The `padding` field is used for alignment purposes.
/// Creates a new `IpV4Filter` instance.
///
/// # Arguments
///
/// * `valid_fields` - Specifies which fields in the filter structure are used for filtering.
/// * `src_address` - Contains the source IPv4 address information.
/// * `dest_address` - Contains the destination IPv4 address information.
/// * `protocol` - Represents the IP protocol number.
///
/// # Returns
///
/// * A new `IpV4Filter` instance.
/// Represents an IPv6 subnet used by the packet filtering mechanism.
///
/// A Rust equivalent for [_IP_SUBNET_V6](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_subnet_v6/).
///
/// The `ip` field contains the IPv6 address.
/// The `ip_mask` field contains the subnet mask.
/// Creates a new `IpSubnetV6` instance.
///
/// # Arguments
///
/// * `ip` - The IPv6 address.
/// * `ip_mask` - The subnet mask.
///
/// # Returns
///
/// * A new `IpSubnetV6` instance.
/// Represents an IPv6 address range used by the packet filtering mechanism.
///
/// A Rust equivalent for [_IP_RANGE_V6](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_range_v6/).
///
/// The `start_ip` field contains the starting IPv6 address of the range.
/// The `end_ip` field contains the ending IPv6 address of the range.
/// Creates a new `IpRangeV6` instance.
///
/// # Arguments
///
/// * `start_ip` - The start of the IPv6 address range.
/// * `end_ip` - The end of the IPv6 address range.
///
/// # Returns
///
/// * A new `IpRangeV6` instance.
/// This structure is used to store information about a particular address space
/// for packet filtering purposes.
///
/// A Rust union that represents either an IPv6 subnet or an IPv6 address range.
///
/// The `ip_subnet` field contains the IPv6 subnet if the address space is a subnet.
/// The `ip_range` field contains the IPv6 address range if the address space is a range.
pub union IpAddressV6Union
/// This structure is used to store information about an IPv6 address for packet filtering purposes.
///
/// Rust equivalent for [_IP_ADDRESS_V6](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_address_v6/).
///
/// The `address_type` field indicates whether the address is a subnet (IP_SUBNET_V6_TYPE) or a range (IP_RANGE_V6_TYPE).
/// The `address` field contains the specific IPv6 address data, either a subnet or an address range, depending on the `address_type`.
/// Creates a new `IpAddressV6` instance.
///
/// # Arguments
///
/// * `address_type` - Indicates whether the address is a subnet or a range.
/// * `address` - Contains the specific IPv6 address data, either a subnet or an address range, depending on the `address_type`.
///
/// # Returns
///
/// * A new `IpAddressV6` instance.
/// This structure is used to define packet filtering rules for IPv6 packets.
///
/// Rust equivalent for [_IP_V6_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_ip_v6_filter/).
///
/// The `valid_fields` field contains flags that specify which fields of the filter are active.
/// The `src_address` and `dest_address` fields store information about the source and destination IPv6 addresses respectively.
/// The `protocol` field represents the protocol used in the packet (e.g., TCP, UDP).
/// The `padding` field is reserved for padding to ensure the correct alignment of the structure.
/// Creates a new `IpV6Filter` instance.
///
/// # Arguments
///
/// * `valid_fields` - Specifies which fields in the filter structure are used for filtering.
/// * `src_address` - Contains the source IPv6 address information.
/// * `dest_address` - Contains the destination IPv6 address information.
/// * `protocol` - Represents the IP protocol number.
///
/// # Returns
///
/// * A new `IpV6Filter` instance.
/// This structure is used to define a range of port numbers for packet filtering rules.
///
/// Rust equivalent for [_PORT_RANGE](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_port_range/).
///
/// The `start_range` field represents the starting port number in the range.
/// The `end_range` field represents the ending port number in the range.
/// Creates a new `PortRange` instance.
///
/// # Arguments
///
/// * `start_range` - The start of the port range.
/// * `end_range` - The end of the port range.
///
/// # Returns
///
/// * A new `PortRange` instance.
/// This structure is used to define filtering rules for TCP and UDP packets.
///
/// Rust equivalent for [_TCPUDP_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_tcpudp_filter/).
///
/// The `valid_fields` field specifies which fields in the structure are valid for filtering.
/// The `source_port` field represents the range of source port numbers to filter.
/// The `dest_port` field represents the range of destination port numbers to filter.
/// The `tcp_flags` field is used to filter TCP packets based on their flags.
/// The `padding` field ensures proper alignment of the structure.
/// Creates a new `TcpUdpFilter` instance.
///
/// # Arguments
///
/// * `valid_fields` - Specifies which fields in the filter structure are valid for filtering.
/// * `source_port` - Represents the range of source port numbers to filter.
/// * `dest_port` - Represents the range of destination port numbers to filter.
/// * `tcp_flags` - Used to filter TCP packets based on their flags.
///
/// # Returns
///
/// * A new `TcpUdpFilter` instance.
/// A Rust struct that represents a range of byte values.
///
/// Rust equivalent for _BYTE_RANGE. This structure can be used to define
/// filtering rules based on byte ranges, such as ICMP type or code ranges.
///
/// The `start_range` field represents the start of the byte range.
/// The `end_range` field represents the end of the byte range.
/// Creates a new `ByteRange` instance.
///
/// # Arguments
///
/// * `start_range` - The start of the byte range.
/// * `end_range` - The end of the byte range.
///
/// # Returns
///
/// * A new `ByteRange` instance.
/// A Rust struct that represents an ICMP filter.
///
/// Rust equivalent for _ICMP_FILTER. This structure can be used to define
/// filtering rules for ICMP packets based on ICMP type and code ranges.
///
/// The `valid_fields` field specifies which fields in the filter are valid for filtering.
/// The `type_range` field represents a range of ICMP types for filtering.
/// The `code_range` field represents a range of ICMP codes for filtering.
/// Creates a new `IcmpFilter` instance.
///
/// # Arguments
///
/// * `valid_fields` - Specifies which fields in the filter are valid for filtering.
/// * `type_range` - Represents a range of ICMP types for filtering.
/// * `code_range` - Represents a range of ICMP codes for filtering.
///
/// # Returns
///
/// * A new `IcmpFilter` instance.
/// A Rust union that holds an `Eth8023Filter`.
///
/// This union can be extended to include other data link layer filters if needed.
/// Currently, it only contains an `Eth8023Filter` for filtering Ethernet/802.3 packets.
pub union DataLinkLayerFilterUnion
/// A Rust struct that represents a data link layer filter.
///
/// Rust equivalent for [_DATA_LINK_LAYER_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/data_link_layer_filter/)
/// This struct can be used to filter packets at the data link layer (e.g., Ethernet/802.3) by specifying the filter type in `union_selector`.
/// Creates a new `DataLinkLayerFilter` instance.
///
/// # Arguments
///
/// * `union_selector` - Specifies the filter type (e.g., ETH_802_3 for Eth8023Filter).
/// * `data_link_layer` - Contains the actual data link layer filter information in a union format.
///
/// # Returns
///
/// * A new `DataLinkLayerFilter` instance.
/// A Rust union that holds either an `IpV4Filter` or an `IpV6Filter`.
///
/// This union can be used to filter packets at the network layer by specifying the appropriate filter type (IPv4 or IPv6).
pub union NetworkLayerFilterUnion
/// A Rust struct that represents a network layer filter.
///
/// Rust equivalent for [_NETWORK_LAYER_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_network_layer_filter/).
/// Creates a new `NetworkLayerFilter` instance.
///
/// # Arguments
///
/// * `union_selector` - A field that determines the type of the network layer filter.
/// * `network_layer` - A union that holds either an IpV4Filter or an IpV6Filter.
///
/// # Returns
///
/// * A new `NetworkLayerFilter` instance.
/// A Rust union that represents a transport layer filter.
///
/// Holds either a `TcpUdpFilter` or an `IcmpFilter`.
pub union TransportLayerFilterUnion
/// A Rust struct that represents a transport layer filter.
///
/// Rust equivalent for [_TRANSPORT_LAYER_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_transport_layer_filter/)
/// Creates a new `TransportLayerFilter` instance.
///
/// # Arguments
///
/// * `union_selector` - A u32 flag that selects the appropriate filter. Use TCPUDP for TcpUdpFilter and ICMP for IcmpFilter.
/// * `transport_layer` - A TransportLayerFilterUnion that holds either a `TcpUdpFilter` or an `IcmpFilter`.
///
/// # Returns
///
/// * A new `TransportLayerFilter` instance.
/// This structure is used to define a single static filter rule for packet filtering. Each rule can specify filtering criteria at
/// the data link, network, and transport layers. The structure also includes counters for incoming and outgoing packets and bytes
/// that match the filter rule.
///
/// * Rust equivalent for [_STATIC_FILTER](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_static_filter/)
/// Creates a new `StaticFilter` instance.
///
/// # Arguments
///
/// * `adapter_handle` - Adapter handle extended to 64 bit size for structure compatibility across x64 and x86 architectures.
/// * `direction_flags` - PACKET_FLAG_ON_SEND or/and PACKET_FLAG_ON_RECEIVE to specify the direction of packets to match.
/// * `filter_action` - FILTER_PACKET_XXX to define the action to take when a packet matches the filter.
/// * `valid_fields` - Specifies which of the fields below contain valid values and should be matched against the packet.
/// * `data_link_filter` - Filter criteria for the data link layer (e.g., Ethernet).
/// * `network_filter` - Filter criteria for the network layer (e.g., IPv4, IPv6).
/// * `transport_filter` - Filter criteria for the transport layer (e.g., TCP, UDP, ICMP).
///
/// # Returns
///
/// * A new `StaticFilter` instance.
/// This structure represents an array of static filter rules, each of which is defined by a `StaticFilter` structure.
/// It is used to manage multiple filter rules for packet filtering in a table format.
///
/// * Rust equivalent to the [_STATIC_FILTER_TABLE](https://www.ntkernel.com/docs/windows-packet-filter-documentation/structures/_static_filter_table/)