-
Notifications
You must be signed in to change notification settings - Fork 386
/
Copy pathmod.rs
778 lines (682 loc) · 24.8 KB
/
mod.rs
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
#![allow(clippy::undocumented_unsafe_blocks)] // Remove me if you dare.
use crate::{dns::ResolvedDnsConfig, tunnel::TunnelMetadata};
use std::{ffi::CStr, io, net::IpAddr, ptr, sync::LazyLock};
use self::winfw::*;
use super::{FirewallArguments, FirewallPolicy, InitialFirewallState};
use talpid_types::{
net::{AllowedEndpoint, AllowedTunnelTraffic},
tunnel::FirewallPolicyError,
ErrorExt,
};
use widestring::WideCString;
use windows_sys::Win32::Globalization::{MultiByteToWideChar, CP_ACP};
mod hyperv;
const HYPERV_LEAK_WARNING_MSG: &str = "Hyper-V (e.g. WSL machines) may leak in blocked states.";
// `COMLibrary` must be initialized for per thread, so use TLS
thread_local! {
static WMI: Option<wmi::WMIConnection> = {
let result = hyperv::init_wmi();
if matches!(&result, Err(hyperv::Error::ObtainHyperVClass(_))) {
log::warn!("The Hyper-V firewall is not available. {HYPERV_LEAK_WARNING_MSG}");
return None;
}
consume_and_log_hyperv_err(
"Initialize COM and WMI",
result,
)
};
}
/// Enable or disable blocking Hyper-V rule
static BLOCK_HYPERV: LazyLock<bool> = LazyLock::new(|| {
let enable = std::env::var("TALPID_FIREWALL_BLOCK_HYPERV")
.map(|v| v != "0")
.unwrap_or(true);
if !enable {
log::debug!("Hyper-V block rule disabled by TALPID_FIREWALL_BLOCK_HYPERV");
}
enable
});
/// Errors that can happen when configuring the Windows firewall.
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// Failure to initialize windows firewall module
#[error("Failed to initialize windows firewall module")]
Initialization,
/// Failure to deinitialize windows firewall module
#[error("Failed to deinitialize windows firewall module")]
Deinitialization,
/// Failure to apply a firewall _connecting_ policy
#[error("Failed to apply connecting firewall policy")]
ApplyingConnectingPolicy(#[source] FirewallPolicyError),
/// Failure to apply a firewall _connected_ policy
#[error("Failed to apply connected firewall policy")]
ApplyingConnectedPolicy(#[source] FirewallPolicyError),
/// Failure to apply firewall _blocked_ policy
#[error("Failed to apply blocked firewall policy")]
ApplyingBlockedPolicy(#[source] FirewallPolicyError),
/// Failure to reset firewall policies
#[error("Failed to reset firewall policies")]
ResettingPolicy(#[source] FirewallPolicyError),
}
/// Timeout for acquiring the WFP transaction lock
const WINFW_TIMEOUT_SECONDS: u32 = 5;
const LOGGING_CONTEXT: &[u8] = b"WinFw\0";
/// The Windows implementation for the firewall.
pub struct Firewall(());
impl Firewall {
pub fn from_args(args: FirewallArguments) -> Result<Self, Error> {
if let InitialFirewallState::Blocked(allowed_endpoint) = args.initial_state {
Self::initialize_blocked(allowed_endpoint, args.allow_lan)
} else {
Self::new()
}
}
pub fn new() -> Result<Self, Error> {
unsafe {
WinFw_Initialize(
WINFW_TIMEOUT_SECONDS,
Some(log_sink),
LOGGING_CONTEXT.as_ptr(),
)
.into_result()?
};
log::trace!("Successfully initialized windows firewall module");
Ok(Firewall(()))
}
fn initialize_blocked(
allowed_endpoint: AllowedEndpoint,
allow_lan: bool,
) -> Result<Self, Error> {
let cfg = &WinFwSettings::new(allow_lan);
let allowed_endpoint = WinFwAllowedEndpointContainer::from(allowed_endpoint);
unsafe {
WinFw_InitializeBlocked(
WINFW_TIMEOUT_SECONDS,
cfg,
&allowed_endpoint.as_endpoint(),
Some(log_sink),
LOGGING_CONTEXT.as_ptr(),
)
.into_result()?
};
log::trace!("Successfully initialized windows firewall module to a blocking state");
with_wmi_if_enabled(|wmi| {
let result = hyperv::add_blocking_hyperv_firewall_rules(wmi);
consume_and_log_hyperv_err("Add block-all Hyper-V filter", result);
});
Ok(Firewall(()))
}
pub fn apply_policy(&mut self, policy: FirewallPolicy) -> Result<(), Error> {
let should_block_hyperv = matches!(
policy,
FirewallPolicy::Connecting { .. } | FirewallPolicy::Blocked { .. }
);
let apply_result = match policy {
FirewallPolicy::Connecting {
peer_endpoint,
tunnel,
allow_lan,
allowed_endpoint,
allowed_tunnel_traffic,
} => {
let cfg = &WinFwSettings::new(allow_lan);
self.set_connecting_state(
&peer_endpoint,
cfg,
&tunnel,
&WinFwAllowedEndpointContainer::from(allowed_endpoint).as_endpoint(),
&allowed_tunnel_traffic,
)
}
FirewallPolicy::Connected {
peer_endpoint,
tunnel,
allow_lan,
dns_config,
} => {
let cfg = &WinFwSettings::new(allow_lan);
self.set_connected_state(&peer_endpoint, cfg, &tunnel, &dns_config)
}
FirewallPolicy::Blocked {
allow_lan,
allowed_endpoint,
} => {
let cfg = &WinFwSettings::new(allow_lan);
self.set_blocked_state(
cfg,
allowed_endpoint.map(WinFwAllowedEndpointContainer::from),
)
}
};
with_wmi_if_enabled(|wmi| {
if should_block_hyperv {
let result = hyperv::add_blocking_hyperv_firewall_rules(wmi);
consume_and_log_hyperv_err("Add block-all Hyper-V filter", result);
} else {
let result = hyperv::remove_blocking_hyperv_firewall_rules(wmi);
consume_and_log_hyperv_err("Remove block-all Hyper-V filter", result);
}
});
apply_result
}
pub fn reset_policy(&mut self) -> Result<(), Error> {
unsafe { WinFw_Reset().into_result().map_err(Error::ResettingPolicy) }?;
with_wmi_if_enabled(|wmi| {
let result = hyperv::remove_blocking_hyperv_firewall_rules(wmi);
consume_and_log_hyperv_err("Remove block-all Hyper-V filter", result);
});
Ok(())
}
fn set_connecting_state(
&mut self,
endpoint: &AllowedEndpoint,
winfw_settings: &WinFwSettings,
tunnel_metadata: &Option<TunnelMetadata>,
allowed_endpoint: &WinFwAllowedEndpoint<'_>,
allowed_tunnel_traffic: &AllowedTunnelTraffic,
) -> Result<(), Error> {
log::trace!("Applying 'connecting' firewall policy");
let ip_str = widestring_ip(endpoint.endpoint.address.ip());
let winfw_relay = WinFwEndpoint {
ip: ip_str.as_ptr(),
port: endpoint.endpoint.address.port(),
protocol: WinFwProt::from(endpoint.endpoint.protocol),
};
// SAFETY: `endpoint1_ip`, `endpoint2_ip`, `endpoint1`, `endpoint2`, `relay_client_wstrs`
// must not be dropped until `WinFw_ApplyPolicyConnecting` has returned.
let relay_client_wstrs: Vec<_> = endpoint
.clients
.iter()
.map(WideCString::from_os_str_truncate)
.collect();
let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs
.iter()
.map(|wstr| wstr.as_ptr())
.collect();
let relay_client_wstr_ptrs_len = relay_client_wstr_ptrs.len();
let interface_wstr = tunnel_metadata
.as_ref()
.map(|metadata| WideCString::from_str_truncate(&metadata.interface));
let interface_wstr_ptr = if let Some(ref wstr) = interface_wstr {
wstr.as_ptr()
} else {
ptr::null()
};
let mut endpoint1_ip = WideCString::new();
let mut endpoint2_ip = WideCString::new();
let (endpoint1, endpoint2) = match allowed_tunnel_traffic {
AllowedTunnelTraffic::One(endpoint) => {
endpoint1_ip = widestring_ip(endpoint.address.ip());
(
Some(WinFwEndpoint {
ip: endpoint1_ip.as_ptr(),
port: endpoint.address.port(),
protocol: WinFwProt::from(endpoint.protocol),
}),
None,
)
}
AllowedTunnelTraffic::Two(endpoint1, endpoint2) => {
endpoint1_ip = widestring_ip(endpoint1.address.ip());
let endpoint1 = Some(WinFwEndpoint {
ip: endpoint1_ip.as_ptr(),
port: endpoint1.address.port(),
protocol: WinFwProt::from(endpoint1.protocol),
});
endpoint2_ip = widestring_ip(endpoint2.address.ip());
let endpoint2 = Some(WinFwEndpoint {
ip: endpoint2_ip.as_ptr(),
port: endpoint2.address.port(),
protocol: WinFwProt::from(endpoint2.protocol),
});
(endpoint1, endpoint2)
}
AllowedTunnelTraffic::None | AllowedTunnelTraffic::All => (None, None),
};
let allowed_tunnel_traffic = WinFwAllowedTunnelTraffic {
type_: WinFwAllowedTunnelTrafficType::from(allowed_tunnel_traffic),
endpoint1: endpoint1
.as_ref()
.map(|ep| ep as *const _)
.unwrap_or(ptr::null()),
endpoint2: endpoint2
.as_ref()
.map(|ep| ep as *const _)
.unwrap_or(ptr::null()),
};
let res = unsafe {
WinFw_ApplyPolicyConnecting(
winfw_settings,
&winfw_relay,
relay_client_wstr_ptrs.as_ptr(),
relay_client_wstr_ptrs_len,
interface_wstr_ptr,
allowed_endpoint,
&allowed_tunnel_traffic,
)
.into_result()
.map_err(Error::ApplyingConnectingPolicy)
};
// SAFETY: All of these hold stack allocated memory which is pointed to by
// `allowed_tunnel_traffic` and must remain allocated until `WinFw_ApplyPolicyConnecting`
// has returned.
drop(endpoint1_ip);
drop(endpoint2_ip);
#[allow(clippy::drop_non_drop)]
drop(endpoint1);
#[allow(clippy::drop_non_drop)]
drop(endpoint2);
drop(relay_client_wstrs);
res
}
fn set_connected_state(
&mut self,
endpoint: &AllowedEndpoint,
winfw_settings: &WinFwSettings,
tunnel_metadata: &TunnelMetadata,
dns_config: &ResolvedDnsConfig,
) -> Result<(), Error> {
log::trace!("Applying 'connected' firewall policy");
let ip_str = widestring_ip(endpoint.endpoint.address.ip());
let tunnel_alias = WideCString::from_str_truncate(&tunnel_metadata.interface);
// ip_str, gateway_str and tunnel_alias have to outlive winfw_relay
let winfw_relay = WinFwEndpoint {
ip: ip_str.as_ptr(),
port: endpoint.endpoint.address.port(),
protocol: WinFwProt::from(endpoint.endpoint.protocol),
};
// SAFETY: `relay_client_wstrs` must not be dropped until `WinFw_ApplyPolicyConnected` has
// returned.
let relay_client_wstrs: Vec<_> = endpoint
.clients
.iter()
.map(WideCString::from_os_str_truncate)
.collect();
let relay_client_wstr_ptrs: Vec<*const u16> = relay_client_wstrs
.iter()
.map(|wstr| wstr.as_ptr())
.collect();
let relay_client_wstr_ptrs_len = relay_client_wstr_ptrs.len();
let tunnel_dns_servers: Vec<WideCString> = dns_config
.tunnel_config()
.iter()
.cloned()
.map(widestring_ip)
.collect();
let tunnel_dns_servers: Vec<*const u16> =
tunnel_dns_servers.iter().map(|ip| ip.as_ptr()).collect();
let non_tunnel_dns_servers: Vec<WideCString> = dns_config
.non_tunnel_config()
.iter()
.cloned()
.map(widestring_ip)
.collect();
let non_tunnel_dns_servers: Vec<*const u16> = non_tunnel_dns_servers
.iter()
.map(|ip| ip.as_ptr())
.collect();
let result = unsafe {
WinFw_ApplyPolicyConnected(
winfw_settings,
&winfw_relay,
relay_client_wstr_ptrs.as_ptr(),
relay_client_wstr_ptrs_len,
tunnel_alias.as_ptr(),
tunnel_dns_servers.as_ptr(),
tunnel_dns_servers.len(),
non_tunnel_dns_servers.as_ptr(),
non_tunnel_dns_servers.len(),
)
.into_result()
.map_err(Error::ApplyingConnectedPolicy)
};
// SAFETY: `relay_client_wstrs` holds memory pointed to by pointers used in C++ and must
// not be dropped until after `WinFw_ApplyPolicyConnected` has returned.
drop(relay_client_wstrs);
result
}
fn set_blocked_state(
&mut self,
winfw_settings: &WinFwSettings,
allowed_endpoint: Option<WinFwAllowedEndpointContainer>,
) -> Result<(), Error> {
log::trace!("Applying 'blocked' firewall policy");
let endpoint = allowed_endpoint
.as_ref()
.map(WinFwAllowedEndpointContainer::as_endpoint);
unsafe {
WinFw_ApplyPolicyBlocked(
winfw_settings,
endpoint
.as_ref()
.map(|container| container as *const _)
.unwrap_or(ptr::null()),
)
.into_result()
.map_err(Error::ApplyingBlockedPolicy)
}
}
}
impl Drop for Firewall {
fn drop(&mut self) {
if unsafe {
WinFw_Deinitialize(WinFwCleanupPolicy::ContinueBlocking)
.into_result()
.is_ok()
} {
log::trace!("Successfully deinitialized windows firewall module");
} else {
log::error!("Failed to deinitialize windows firewall module");
};
}
}
fn widestring_ip(ip: IpAddr) -> WideCString {
WideCString::from_str_truncate(ip.to_string())
}
/// Logging callback implementation.
pub extern "system" fn log_sink(
level: log::Level,
msg: *const std::ffi::c_char,
context: *mut std::ffi::c_void,
) {
if msg.is_null() {
log::error!("Log message from FFI boundary is NULL");
} else {
let target = if context.is_null() {
"UNKNOWN".into()
} else {
unsafe { CStr::from_ptr(context as *const _).to_string_lossy() }
};
let mb_string = unsafe { CStr::from_ptr(msg) };
let managed_msg = match multibyte_to_wide(mb_string, CP_ACP) {
Ok(wide_str) => String::from_utf16_lossy(&wide_str),
// Best effort:
Err(_) => mb_string.to_string_lossy().into_owned(),
};
log::logger().log(
&log::Record::builder()
.level(level)
.target(&target)
.args(format_args!("{}", managed_msg))
.build(),
);
}
}
/// Convert `mb_string`, with the given character encoding `codepage`, to a UTF-16 string.
fn multibyte_to_wide(mb_string: &CStr, codepage: u32) -> Result<Vec<u16>, io::Error> {
if mb_string.is_empty() {
return Ok(vec![]);
}
// SAFETY: `mb_string` is null-terminated and valid.
let wc_size = unsafe {
MultiByteToWideChar(
codepage,
0,
mb_string.as_ptr() as *const u8,
-1,
ptr::null_mut(),
0,
)
};
if wc_size == 0 {
return Err(io::Error::last_os_error());
}
let mut wc_buffer = vec![0u16; usize::try_from(wc_size).unwrap()];
// SAFETY: `wc_buffer` can contain up to `wc_size` characters, including a null
// terminator.
let chars_written = unsafe {
MultiByteToWideChar(
codepage,
0,
mb_string.as_ptr() as *const u8,
-1,
wc_buffer.as_mut_ptr(),
wc_size,
)
};
if chars_written == 0 {
return Err(io::Error::last_os_error());
}
wc_buffer.truncate(usize::try_from(chars_written - 1).unwrap());
Ok(wc_buffer)
}
#[cfg(test)]
mod test {
use super::multibyte_to_wide;
use windows_sys::Win32::Globalization::CP_UTF8;
#[test]
fn test_multibyte_to_wide() {
// € = 0x20AC in UTF-16
let converted = multibyte_to_wide(c"€€", CP_UTF8);
const EXPECTED: &[u16] = &[0x20AC, 0x20AC];
assert!(
matches!(converted.as_deref(), Ok(EXPECTED)),
"expected Ok({EXPECTED:?}), got {converted:?}",
);
// boundary case
let converted = multibyte_to_wide(c"", CP_UTF8);
assert!(
matches!(converted.as_deref(), Ok([])),
"unexpected result {converted:?}"
);
}
}
// Convert `result` into an option and log the error, if any.
fn consume_and_log_hyperv_err<T>(
action: &'static str,
result: Result<T, hyperv::Error>,
) -> Option<T> {
result
.inspect_err(|error| {
log::error!(
"{}",
error.display_chain_with_msg(&format!("{action}. {HYPERV_LEAK_WARNING_MSG}"))
);
})
.ok()
}
// Run a closure with the current thread's WMI connection, if available
fn with_wmi_if_enabled(f: impl FnOnce(&wmi::WMIConnection)) {
if !*BLOCK_HYPERV {
return;
}
WMI.with(|wmi| {
if let Some(con) = wmi {
f(con)
}
})
}
#[allow(non_snake_case)]
mod winfw {
use super::{widestring_ip, AllowedEndpoint, AllowedTunnelTraffic, Error, WideCString};
use std::ffi::{c_char, c_void};
use talpid_types::net::TransportProtocol;
type LogSink = extern "system" fn(level: log::Level, msg: *const c_char, context: *mut c_void);
pub struct WinFwAllowedEndpointContainer {
_clients: Box<[WideCString]>,
clients_ptrs: Box<[*const u16]>,
ip: WideCString,
port: u16,
protocol: WinFwProt,
}
impl From<AllowedEndpoint> for WinFwAllowedEndpointContainer {
fn from(endpoint: AllowedEndpoint) -> Self {
let clients = endpoint
.clients
.iter()
.map(WideCString::from_os_str_truncate)
.collect::<Box<_>>();
let clients_ptrs = clients
.iter()
.map(|client| client.as_ptr())
.collect::<Box<_>>();
let ip = widestring_ip(endpoint.endpoint.address.ip());
WinFwAllowedEndpointContainer {
_clients: clients,
clients_ptrs,
ip,
port: endpoint.endpoint.address.port(),
protocol: WinFwProt::from(endpoint.endpoint.protocol),
}
}
}
impl WinFwAllowedEndpointContainer {
pub fn as_endpoint(&self) -> WinFwAllowedEndpoint<'_> {
WinFwAllowedEndpoint {
num_clients: self.clients_ptrs.len() as u32,
clients: self.clients_ptrs.as_ptr(),
endpoint: WinFwEndpoint {
ip: self.ip.as_ptr(),
port: self.port,
protocol: self.protocol,
},
_phantom: std::marker::PhantomData,
}
}
}
#[repr(C)]
pub struct WinFwAllowedEndpoint<'a> {
num_clients: u32,
clients: *const *const libc::wchar_t,
endpoint: WinFwEndpoint,
_phantom: std::marker::PhantomData<&'a WinFwAllowedEndpointContainer>,
}
#[repr(C)]
pub struct WinFwAllowedTunnelTraffic {
pub type_: WinFwAllowedTunnelTrafficType,
pub endpoint1: *const WinFwEndpoint,
pub endpoint2: *const WinFwEndpoint,
}
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum WinFwAllowedTunnelTrafficType {
None,
All,
One,
Two,
}
impl From<&AllowedTunnelTraffic> for WinFwAllowedTunnelTrafficType {
fn from(traffic: &AllowedTunnelTraffic) -> Self {
match traffic {
AllowedTunnelTraffic::None => WinFwAllowedTunnelTrafficType::None,
AllowedTunnelTraffic::All => WinFwAllowedTunnelTrafficType::All,
AllowedTunnelTraffic::One(..) => WinFwAllowedTunnelTrafficType::One,
AllowedTunnelTraffic::Two(..) => WinFwAllowedTunnelTrafficType::Two,
}
}
}
#[repr(C)]
pub struct WinFwEndpoint {
pub ip: *const libc::wchar_t,
pub port: u16,
pub protocol: WinFwProt,
}
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum WinFwProt {
Tcp = 0u8,
Udp = 1u8,
}
impl From<TransportProtocol> for WinFwProt {
fn from(prot: TransportProtocol) -> WinFwProt {
match prot {
TransportProtocol::Tcp => WinFwProt::Tcp,
TransportProtocol::Udp => WinFwProt::Udp,
}
}
}
#[repr(C)]
pub struct WinFwSettings {
permitDhcp: bool,
permitLan: bool,
}
impl WinFwSettings {
pub fn new(permit_lan: bool) -> WinFwSettings {
WinFwSettings {
permitDhcp: true,
permitLan: permit_lan,
}
}
}
#[allow(dead_code)]
#[repr(u32)]
#[derive(Clone, Copy)]
pub enum WinFwCleanupPolicy {
ContinueBlocking = 0,
ResetFirewall = 1,
}
ffi_error!(InitializationResult, Error::Initialization);
ffi_error!(DeinitializationResult, Error::Deinitialization);
#[derive(Debug)]
#[allow(dead_code)]
#[repr(u32)]
pub enum WinFwPolicyStatus {
Success = 0,
GeneralFailure = 1,
LockTimeout = 2,
}
impl WinFwPolicyStatus {
pub fn into_result(self) -> Result<(), super::FirewallPolicyError> {
match self {
WinFwPolicyStatus::Success => Ok(()),
WinFwPolicyStatus::GeneralFailure => Err(super::FirewallPolicyError::Generic),
WinFwPolicyStatus::LockTimeout => {
// TODO: Obtain application name and string from WinFw
Err(super::FirewallPolicyError::Locked(None))
}
}
}
}
impl From<WinFwPolicyStatus> for Result<(), super::FirewallPolicyError> {
fn from(val: WinFwPolicyStatus) -> Self {
val.into_result()
}
}
unsafe extern "system" {
#[link_name = "WinFw_Initialize"]
pub fn WinFw_Initialize(
timeout: libc::c_uint,
sink: Option<LogSink>,
sink_context: *const u8,
) -> InitializationResult;
#[link_name = "WinFw_InitializeBlocked"]
pub fn WinFw_InitializeBlocked(
timeout: libc::c_uint,
settings: &WinFwSettings,
allowed_endpoint: *const WinFwAllowedEndpoint<'_>,
sink: Option<LogSink>,
sink_context: *const u8,
) -> InitializationResult;
#[link_name = "WinFw_Deinitialize"]
pub fn WinFw_Deinitialize(cleanupPolicy: WinFwCleanupPolicy) -> DeinitializationResult;
#[link_name = "WinFw_ApplyPolicyConnecting"]
pub fn WinFw_ApplyPolicyConnecting(
settings: &WinFwSettings,
relay: &WinFwEndpoint,
relayClient: *const *const libc::wchar_t,
relayClientLen: usize,
tunnelIfaceAlias: *const libc::wchar_t,
allowedEndpoint: *const WinFwAllowedEndpoint<'_>,
allowedTunnelTraffic: &WinFwAllowedTunnelTraffic,
) -> WinFwPolicyStatus;
#[link_name = "WinFw_ApplyPolicyConnected"]
pub fn WinFw_ApplyPolicyConnected(
settings: &WinFwSettings,
relay: &WinFwEndpoint,
relayClient: *const *const libc::wchar_t,
relayClientLen: usize,
tunnelIfaceAlias: *const libc::wchar_t,
tunnelDnsServers: *const *const libc::wchar_t,
numTunnelDnsServers: usize,
nonTunnelDnsServers: *const *const libc::wchar_t,
numNonTunnelDnsServers: usize,
) -> WinFwPolicyStatus;
#[link_name = "WinFw_ApplyPolicyBlocked"]
pub fn WinFw_ApplyPolicyBlocked(
settings: &WinFwSettings,
allowed_endpoint: *const WinFwAllowedEndpoint<'_>,
) -> WinFwPolicyStatus;
#[link_name = "WinFw_Reset"]
pub fn WinFw_Reset() -> WinFwPolicyStatus;
}
}