-
Notifications
You must be signed in to change notification settings - Fork 335
/
mod.rs
919 lines (811 loc) · 33.2 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
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
mod driver;
mod path_monitor;
mod service;
mod volume_monitor;
mod windows;
use crate::{
tunnel::TunnelMetadata,
tunnel_state_machine::TunnelCommand,
window::{PowerManagementEvent, PowerManagementListener},
};
use futures::channel::{mpsc, oneshot};
use std::{
collections::HashMap,
convert::TryFrom,
ffi::{OsStr, OsString},
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
mpsc as sync_mpsc, Arc, Mutex, RwLock, Weak,
},
time::Duration,
};
use talpid_routing::{get_best_default_route, CallbackHandle, EventType, RouteManagerHandle};
use talpid_types::{tunnel::ErrorStateCause, ErrorExt};
use talpid_windows_net::{get_ip_address_for_interface, AddressFamily};
use windows_sys::Win32::Foundation::ERROR_OPERATION_ABORTED;
const DRIVER_EVENT_BUFFER_SIZE: usize = 2048;
const RESERVED_IP_V4: Ipv4Addr = Ipv4Addr::new(192, 0, 2, 123);
/// Errors that may occur in [`SplitTunnel`].
#[derive(err_derive::Error, Debug)]
#[error(no_from)]
pub enum Error {
/// Failed to install or start driver service
#[error(display = "Failed to start driver service")]
ServiceError(#[error(source)] service::Error),
/// Failed to initialize the driver
#[error(display = "Failed to initialize driver")]
InitializationError(#[error(source)] driver::DeviceHandleError),
/// Failed to reset the driver
#[error(display = "Failed to reset driver")]
ResetError(#[error(source)] io::Error),
/// Failed to set paths to excluded applications
#[error(display = "Failed to set list of excluded applications")]
SetConfiguration(#[error(source)] io::Error),
/// Failed to obtain the current driver state
#[error(display = "Failed to obtain the driver state")]
GetState(#[error(source)] io::Error),
/// Failed to register interface IP addresses
#[error(display = "Failed to register IP addresses for exclusions")]
RegisterIps(#[error(source)] io::Error),
/// Failed to clear interface IP addresses
#[error(display = "Failed to clear registered IP addresses")]
ClearIps(#[error(source)] io::Error),
/// Failed to set up the driver event loop
#[error(display = "Failed to set up the driver event loop")]
EventThreadError(#[error(source)] io::Error),
/// Failed to obtain default route
#[error(display = "Failed to obtain the default route")]
ObtainDefaultRoute(#[error(source)] talpid_routing::Error),
/// Failed to obtain an IP address given a network interface LUID
#[error(display = "Failed to obtain IP address for interface LUID")]
LuidToIp(#[error(source)] talpid_windows_net::Error),
/// Failed to set up callback for monitoring default route changes
#[error(display = "Failed to register default route change callback")]
RegisterRouteChangeCallback,
/// Unexpected IP parsing error
#[error(display = "Failed to parse IP address")]
IpParseError,
/// The request handling thread is stuck
#[error(display = "The ST request thread is stuck")]
RequestThreadStuck,
/// The request handling thread is down
#[error(display = "The split tunnel monitor is down")]
SplitTunnelDown,
/// Failed to start the NTFS reparse point monitor
#[error(display = "Failed to start path monitor")]
StartPathMonitor(#[error(source)] io::Error),
/// A previous path update has not yet completed
#[error(display = "A previous update is not yet complete")]
AlreadySettingPaths,
/// Resetting in the engaged state risks leaking into the tunnel
#[error(display = "Failed to reset driver because it is engaged")]
CannotResetEngaged,
}
/// Manages applications whose traffic to exclude from the tunnel.
pub struct SplitTunnel {
runtime: tokio::runtime::Handle,
request_tx: RequestTx,
event_thread: Option<std::thread::JoinHandle<()>>,
quit_event: Arc<windows::Event>,
excluded_processes: Arc<RwLock<HashMap<usize, ExcludedProcess>>>,
_route_change_callback: Option<CallbackHandle>,
daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
async_path_update_in_progress: Arc<AtomicBool>,
power_mgmt_handle: tokio::task::JoinHandle<()>,
route_manager: RouteManagerHandle,
}
enum Request {
SetPaths(Vec<OsString>),
RegisterIps(InterfaceAddresses),
Restart,
Stop,
}
type RequestResponseTx = sync_mpsc::Sender<Result<(), Error>>;
type RequestTx = sync_mpsc::Sender<(Request, RequestResponseTx)>;
const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Default, PartialEq, Clone)]
struct InterfaceAddresses {
tunnel_ipv4: Option<Ipv4Addr>,
tunnel_ipv6: Option<Ipv6Addr>,
internet_ipv4: Option<Ipv4Addr>,
internet_ipv6: Option<Ipv6Addr>,
}
/// Represents a process that is being excluded from the tunnel.
#[derive(Debug, Clone)]
pub struct ExcludedProcess {
/// Process identifier.
pub pid: u32,
/// Path to the image that this process is an instance of.
pub image: PathBuf,
/// If true, then the process is split because its parent was split,
/// not due to its path being in the config.
pub inherited: bool,
}
/// Cloneable handle for interacting with the split tunnel module.
#[derive(Debug, Clone)]
pub struct SplitTunnelHandle {
excluded_processes: Weak<RwLock<HashMap<usize, ExcludedProcess>>>,
}
impl SplitTunnelHandle {
/// Return processes that are currently being excluded, including
/// their pids, paths, and reason for being excluded.
pub fn get_processes(&self) -> Result<Vec<ExcludedProcess>, Error> {
let processes = self
.excluded_processes
.upgrade()
.ok_or(Error::SplitTunnelDown)?;
let processes = processes.read().unwrap();
Ok(processes.values().cloned().collect())
}
}
enum EventResult {
/// Result containing the next event.
Event(driver::EventId, driver::EventBody),
/// Quit event was signaled.
Quit,
}
impl SplitTunnel {
/// Initialize the split tunnel device.
pub fn new(
runtime: tokio::runtime::Handle,
resource_dir: PathBuf,
daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
volume_update_rx: mpsc::UnboundedReceiver<()>,
power_mgmt_rx: PowerManagementListener,
route_manager: RouteManagerHandle,
) -> Result<Self, Error> {
let excluded_processes = Arc::new(RwLock::new(HashMap::new()));
let (request_tx, handle) =
Self::spawn_request_thread(resource_dir, volume_update_rx, excluded_processes.clone())?;
let (event_thread, quit_event) =
Self::spawn_event_listener(handle, excluded_processes.clone())?;
let power_mgmt_handle =
Self::spawn_power_management_monitor(request_tx.clone(), power_mgmt_rx);
Ok(SplitTunnel {
runtime,
request_tx,
event_thread: Some(event_thread),
quit_event,
_route_change_callback: None,
daemon_tx,
async_path_update_in_progress: Arc::new(AtomicBool::new(false)),
excluded_processes,
power_mgmt_handle,
route_manager,
})
}
/// Spawns an event loop thread that processes events from the driver service.
fn spawn_event_listener(
handle: Arc<driver::DeviceHandle>,
excluded_processes: Arc<RwLock<HashMap<usize, ExcludedProcess>>>,
) -> Result<(std::thread::JoinHandle<()>, Arc<windows::Event>), Error> {
let mut event_overlapped = windows::Overlapped::new(Some(
windows::Event::new(true, false).map_err(Error::EventThreadError)?,
))
.map_err(Error::EventThreadError)?;
let quit_event =
Arc::new(windows::Event::new(true, false).map_err(Error::EventThreadError)?);
let quit_event_copy = quit_event.clone();
let event_thread = std::thread::spawn(move || {
log::debug!("Starting split tunnel event thread");
let mut data_buffer = vec![];
loop {
// Wait until either the next event is received or the quit event is signaled.
let (event_id, event_body) = match Self::fetch_next_event(
&handle,
&quit_event,
&mut event_overlapped,
&mut data_buffer,
) {
Ok(EventResult::Event(event_id, event_body)) => (event_id, event_body),
Ok(EventResult::Quit) => break,
Err(error) => {
if error.raw_os_error() == Some(ERROR_OPERATION_ABORTED as i32) {
// The driver will normally abort the request if the driver state
// is reset. Give the driver service some time to recover before
// retrying.
std::thread::sleep(Duration::from_millis(500));
}
continue;
}
};
Self::handle_event(event_id, event_body, &excluded_processes);
}
log::debug!("Stopping split tunnel event thread");
});
Ok((event_thread, quit_event_copy))
}
fn fetch_next_event(
device: &Arc<driver::DeviceHandle>,
quit_event: &windows::Event,
overlapped: &mut windows::Overlapped,
data_buffer: &mut Vec<u8>,
) -> io::Result<EventResult> {
if unsafe { driver::wait_for_single_object(quit_event.as_handle(), Some(Duration::ZERO)) }
.is_ok()
{
return Ok(EventResult::Quit);
}
data_buffer.resize(DRIVER_EVENT_BUFFER_SIZE, 0u8);
unsafe {
driver::device_io_control_buffer_async(
device,
driver::DriverIoctlCode::DequeEvent as u32,
None,
data_buffer.as_mut_ptr(),
u32::try_from(data_buffer.len()).expect("buffer must be smaller than u32"),
overlapped.as_mut_ptr(),
)
}
.map_err(|error| {
log::error!(
"{}",
error.display_chain_with_msg("DeviceIoControl failed to deque event")
);
error
})?;
let event_objects = [
overlapped.get_event().unwrap().as_handle(),
quit_event.as_handle(),
];
let signaled_object =
unsafe { driver::wait_for_multiple_objects(&event_objects[..], false) }.map_err(
|error| {
log::error!(
"{}",
error.display_chain_with_msg("wait_for_multiple_objects failed")
);
error
},
)?;
if signaled_object == quit_event.as_handle() {
// Quit event was signaled
return Ok(EventResult::Quit);
}
let returned_bytes =
driver::get_overlapped_result(device, overlapped).map_err(|error| {
if error.raw_os_error() != Some(ERROR_OPERATION_ABORTED as i32) {
log::error!(
"{}",
error.display_chain_with_msg(
"get_overlapped_result failed for dequed event"
),
);
}
error
})?;
data_buffer
.truncate(usize::try_from(returned_bytes).expect("usize must be no smaller than u32"));
driver::parse_event_buffer(&data_buffer)
.map(|(id, body)| EventResult::Event(id, body))
.map_err(|error| {
log::error!(
"{}",
error.display_chain_with_msg("Failed to parse ST event buffer")
);
io::Error::new(io::ErrorKind::Other, "Failed to parse ST event buffer")
})
}
fn handle_event(
event_id: driver::EventId,
event_body: driver::EventBody,
excluded_processes: &Arc<RwLock<HashMap<usize, ExcludedProcess>>>,
) {
use driver::{EventBody, EventId};
let event_str = match &event_id {
EventId::StartSplittingProcess | EventId::ErrorStartSplittingProcess => {
"Start splitting process"
}
EventId::StopSplittingProcess | EventId::ErrorStopSplittingProcess => {
"Stop splitting process"
}
EventId::ErrorMessage => "ErrorMessage",
};
match event_body {
EventBody::SplittingEvent {
process_id,
reason,
image,
} => {
let mut pids = excluded_processes.write().unwrap();
match event_id {
EventId::StartSplittingProcess => {
if let Some(prev_entry) = pids.get(&process_id) {
log::error!("PID collision: {process_id} is already in the list of excluded processes. New image: {:?}. Current image: {:?}", image, prev_entry);
}
pids.insert(
process_id,
ExcludedProcess {
pid: u32::try_from(process_id)
.expect("PID should be containable in a DWORD"),
image: Path::new(&image).to_path_buf(),
inherited: reason
.contains(driver::SplittingChangeReason::BY_INHERITANCE),
},
);
}
EventId::StopSplittingProcess => {
if pids.remove(&process_id).is_none() {
log::error!("Inconsistent process tree: {process_id} was not found");
}
}
_ => (),
}
log::trace!(
"{}:\n\tpid: {}\n\treason: {:?}\n\timage: {:?}",
event_str,
process_id,
reason,
image,
);
}
EventBody::SplittingError { process_id, image } => {
log::error!(
"FAILED: {}:\n\tpid: {}\n\timage: {:?}",
event_str,
process_id,
image,
);
}
EventBody::ErrorMessage { status, message } => {
log::error!("NTSTATUS {:#x}: {}", status, message.to_string_lossy())
}
}
}
fn spawn_request_thread(
resource_dir: PathBuf,
volume_update_rx: mpsc::UnboundedReceiver<()>,
excluded_processes: Arc<RwLock<HashMap<usize, ExcludedProcess>>>,
) -> Result<(RequestTx, Arc<driver::DeviceHandle>), Error> {
let (tx, rx): (RequestTx, _) = sync_mpsc::channel();
let (init_tx, init_rx) = sync_mpsc::channel();
let monitored_paths = Arc::new(Mutex::new(vec![]));
let monitored_paths_copy = monitored_paths.clone();
let (monitor_tx, monitor_rx) = sync_mpsc::channel();
let path_monitor = path_monitor::PathMonitor::spawn(monitor_tx.clone())
.map_err(Error::StartPathMonitor)?;
let volume_monitor = volume_monitor::VolumeMonitor::spawn(
path_monitor.clone(),
monitor_tx,
monitored_paths.clone(),
volume_update_rx,
);
std::thread::spawn(move || {
let init_fn = || {
service::install_driver_if_required(&resource_dir).map_err(Error::ServiceError)?;
driver::DeviceHandle::new()
.map(Arc::new)
.map_err(Error::InitializationError)
};
let handle = match init_fn() {
Ok(handle) => {
let _ = init_tx.send(Ok(handle.clone()));
handle
}
Err(error) => {
let _ = init_tx.send(Err(error));
return;
}
};
let mut previous_addresses = InterfaceAddresses::default();
while let Ok((request, response_tx)) = rx.recv() {
let response = match request {
Request::SetPaths(paths) => {
let mut monitored_paths_guard = monitored_paths.lock().unwrap();
let result = if paths.len() > 0 {
handle.set_config(&paths).map_err(Error::SetConfiguration)
} else {
handle.clear_config().map_err(Error::SetConfiguration)
};
if result.is_ok() {
if let Err(error) = path_monitor.set_paths(&paths) {
log::error!(
"{}",
error.display_chain_with_msg("Failed to update path monitor")
);
}
*monitored_paths_guard = paths.to_vec();
}
result
}
Request::RegisterIps(mut ips) => {
if ips.internet_ipv4.is_none() && ips.internet_ipv6.is_none() {
ips.tunnel_ipv4 = None;
ips.tunnel_ipv6 = None;
}
if previous_addresses == ips {
Ok(())
} else {
let result = handle
.register_ips(
ips.tunnel_ipv4,
ips.tunnel_ipv6,
ips.internet_ipv4,
ips.internet_ipv6,
)
.map_err(Error::RegisterIps);
if result.is_ok() {
previous_addresses = ips;
}
result
}
}
Request::Restart => {
let monitored_paths_guard = monitored_paths.lock().unwrap();
(|| {
let state = handle.get_driver_state().map_err(Error::GetState)?;
if state == driver::DriverState::Engaged {
// Leaving the engaged state risks leaking traffic into the tunnel,
// so err on the safe side.
log::warn!(
"Not resetting driver state because it is currently engaged"
);
return Err(Error::CannotResetEngaged);
}
handle.reinitialize().map_err(Error::InitializationError)?;
{
excluded_processes.write().unwrap().clear();
}
handle
.register_ips(
previous_addresses.tunnel_ipv4,
previous_addresses.tunnel_ipv6,
previous_addresses.internet_ipv4,
previous_addresses.internet_ipv6,
)
.map_err(Error::RegisterIps)?;
if monitored_paths_guard.len() > 0 {
handle
.set_config(&*monitored_paths_guard)
.map_err(Error::SetConfiguration)?;
}
Ok(())
})()
}
Request::Stop => {
if let Err(error) = handle.reset().map_err(Error::ResetError) {
let _ = response_tx.send(Err(error));
continue;
}
monitored_paths.lock().unwrap().clear();
excluded_processes.write().unwrap().clear();
let _ = response_tx.send(Ok(()));
// Stop listening to commands
break;
}
};
if response_tx.send(response).is_err() {
log::error!("A response could not be sent for a completed request");
}
}
drop(volume_monitor);
if let Err(error) = path_monitor.shutdown() {
log::error!(
"{}",
error.display_chain_with_msg("Failed to shut down path monitor")
);
}
drop(handle);
log::debug!("Stopping ST service");
if let Err(error) = service::stop_driver_service() {
log::error!(
"{}",
error.display_chain_with_msg("Failed to stop ST service")
);
}
});
let handle = init_rx
.recv_timeout(REQUEST_TIMEOUT)
.map_err(|_| Error::RequestThreadStuck)??;
let handle_copy = handle.clone();
std::thread::spawn(move || {
while let Ok(()) = monitor_rx.recv() {
let paths = monitored_paths_copy.lock().unwrap();
let result = if paths.len() > 0 {
log::debug!("Re-resolving excluded paths");
handle_copy.set_config(&*paths)
} else {
continue;
};
if let Err(error) = result {
log::error!(
"{}",
error.display_chain_with_msg("Failed to update excluded paths")
);
}
}
});
Ok((tx, handle))
}
fn send_request(&self, request: Request) -> Result<(), Error> {
Self::send_request_inner(&self.request_tx, request)
}
fn send_request_inner(request_tx: &RequestTx, request: Request) -> Result<(), Error> {
let (response_tx, response_rx) = sync_mpsc::channel();
request_tx
.send((request, response_tx))
.map_err(|_| Error::SplitTunnelDown)?;
response_rx
.recv_timeout(REQUEST_TIMEOUT)
.map_err(|_| Error::RequestThreadStuck)?
}
fn spawn_power_management_monitor(
request_tx: RequestTx,
mut power_mgmt_rx: PowerManagementListener,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
while let Some(event) = power_mgmt_rx.next().await {
match event {
PowerManagementEvent::ResumeAutomatic => {
let tx = request_tx.clone();
tokio::task::spawn_blocking(move || {
std::thread::sleep(Duration::from_secs(1));
if let Err(error) =
SplitTunnel::send_request_inner(&tx, Request::Restart)
{
log::error!(
"{}",
error
.display_chain_with_msg("Failed to reinitialize ST module")
);
}
});
}
_ => (),
}
}
})
}
/// Set a list of applications to exclude from the tunnel.
pub fn set_paths_sync<T: AsRef<OsStr>>(&self, paths: &[T]) -> Result<(), Error> {
self.send_request(Request::SetPaths(
paths
.iter()
.map(|path| path.as_ref().to_os_string())
.collect(),
))
}
/// Set a list of applications to exclude from the tunnel.
pub fn set_paths<T: AsRef<OsStr>>(
&self,
paths: &[T],
result_tx: oneshot::Sender<Result<(), Error>>,
) {
let busy = self
.async_path_update_in_progress
.swap(true, Ordering::SeqCst);
if busy {
let _ = result_tx.send(Err(Error::AlreadySettingPaths));
return;
}
let (response_tx, response_rx) = sync_mpsc::channel();
let request = Request::SetPaths(
paths
.iter()
.map(|path| path.as_ref().to_os_string())
.collect(),
);
let request_tx = self.request_tx.clone();
let wait_task = move || {
request_tx
.send((request, response_tx))
.map_err(|_| Error::SplitTunnelDown)?;
response_rx.recv().map_err(|_| Error::SplitTunnelDown)?
};
let in_progress = self.async_path_update_in_progress.clone();
self.runtime.spawn_blocking(move || {
let _ = result_tx.send(wait_task());
in_progress.store(false, Ordering::SeqCst);
});
}
/// Instructs the driver to redirect traffic from sockets bound to 0.0.0.0, ::, or the
/// tunnel addresses (if any) to the default route.
pub fn set_tunnel_addresses(&mut self, metadata: Option<&TunnelMetadata>) -> Result<(), Error> {
let mut tunnel_ipv4 = None;
let mut tunnel_ipv6 = None;
if let Some(metadata) = metadata {
for ip in &metadata.ips {
match ip {
IpAddr::V4(address) => tunnel_ipv4 = Some(address.clone()),
IpAddr::V6(address) => tunnel_ipv6 = Some(address.clone()),
}
}
}
let tunnel_ipv4 = Some(tunnel_ipv4.unwrap_or(RESERVED_IP_V4));
let context_mutex = Arc::new(Mutex::new(
SplitTunnelDefaultRouteChangeHandlerContext::new(
self.request_tx.clone(),
self.daemon_tx.clone(),
tunnel_ipv4,
tunnel_ipv6,
),
));
self._route_change_callback = None;
let moved_context_mutex = context_mutex.clone();
let mut context = context_mutex.lock().unwrap();
let callback = self
.runtime
.block_on(
self.route_manager
.add_default_route_change_callback(Box::new(move |event, addr_family| {
split_tunnel_default_route_change_handler(
event,
addr_family,
&moved_context_mutex,
)
})),
)
.map(Some)
.map_err(|_| Error::RegisterRouteChangeCallback)?;
context.initialize_internet_addresses()?;
context.register_ips()?;
self._route_change_callback = callback;
Ok(())
}
/// Instructs the driver to stop redirecting tunnel traffic and INADDR_ANY.
pub fn clear_tunnel_addresses(&mut self) -> Result<(), Error> {
self._route_change_callback = None;
self.send_request(Request::RegisterIps(InterfaceAddresses::default()))
}
/// Returns a handle used for interacting with the split tunnel module.
pub fn handle(&self) -> SplitTunnelHandle {
SplitTunnelHandle {
excluded_processes: Arc::downgrade(&self.excluded_processes),
}
}
}
impl Drop for SplitTunnel {
fn drop(&mut self) {
self.power_mgmt_handle.abort();
if let Some(_event_thread) = self.event_thread.take() {
if let Err(error) = self.quit_event.set() {
log::error!(
"{}",
error.display_chain_with_msg("Failed to close ST event thread")
);
}
// Not joining `event_thread`: It may be unresponsive.
}
if let Err(error) = self.send_request(Request::Stop) {
log::error!(
"{}",
error.display_chain_with_msg("Failed to stop ST driver service")
);
}
}
}
struct SplitTunnelDefaultRouteChangeHandlerContext {
request_tx: RequestTx,
pub daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
pub addresses: InterfaceAddresses,
}
impl SplitTunnelDefaultRouteChangeHandlerContext {
pub fn new(
request_tx: RequestTx,
daemon_tx: Weak<mpsc::UnboundedSender<TunnelCommand>>,
tunnel_ipv4: Option<Ipv4Addr>,
tunnel_ipv6: Option<Ipv6Addr>,
) -> Self {
SplitTunnelDefaultRouteChangeHandlerContext {
request_tx,
daemon_tx,
addresses: InterfaceAddresses {
tunnel_ipv4,
tunnel_ipv6,
internet_ipv4: None,
internet_ipv6: None,
},
}
}
pub fn register_ips(&self) -> Result<(), Error> {
SplitTunnel::send_request_inner(
&self.request_tx,
Request::RegisterIps(self.addresses.clone()),
)
}
pub fn initialize_internet_addresses(&mut self) -> Result<(), Error> {
// Identify IP address that gives us Internet access
let internet_ipv4 = get_best_default_route(AddressFamily::Ipv4)
.map_err(Error::ObtainDefaultRoute)?
.map(|route| {
get_ip_address_for_interface(AddressFamily::Ipv4, route.iface).map(|ip| match ip {
Some(IpAddr::V4(addr)) => Some(addr),
Some(_) => unreachable!("wrong address family (expected IPv4)"),
None => {
log::warn!("No IPv4 address was found for the default route interface");
None
}
})
})
.transpose()
.map_err(Error::LuidToIp)?
.flatten();
let internet_ipv6 = get_best_default_route(AddressFamily::Ipv6)
.map_err(Error::ObtainDefaultRoute)?
.map(|route| {
get_ip_address_for_interface(AddressFamily::Ipv6, route.iface).map(|ip| match ip {
Some(IpAddr::V6(addr)) => Some(addr),
Some(_) => unreachable!("wrong address family (expected IPv6)"),
None => {
log::warn!("No IPv6 address was found for the default route interface");
None
}
})
})
.transpose()
.map_err(Error::LuidToIp)?
.flatten();
self.addresses.internet_ipv4 = internet_ipv4;
self.addresses.internet_ipv6 = internet_ipv6;
Ok(())
}
}
fn split_tunnel_default_route_change_handler<'a>(
event_type: EventType<'a>,
address_family: AddressFamily,
ctx_mutex: &Arc<Mutex<SplitTunnelDefaultRouteChangeHandlerContext>>,
) {
use talpid_routing::EventType::*;
// Update the "internet interface" IP when best default route changes
let mut ctx = ctx_mutex.lock().expect("ST route handler mutex poisoned");
let daemon_tx = ctx.daemon_tx.upgrade();
let maybe_send = move |content| {
if let Some(tx) = daemon_tx {
let _ = tx.unbounded_send(content);
}
};
let result = match event_type {
Updated(default_route) | UpdatedDetails(default_route) => {
match get_ip_address_for_interface(address_family, default_route.iface) {
Ok(Some(ip)) => match IpAddr::from(ip) {
IpAddr::V4(addr) => ctx.addresses.internet_ipv4 = Some(addr),
IpAddr::V6(addr) => ctx.addresses.internet_ipv6 = Some(addr),
},
Ok(None) => {
log::warn!("Failed to obtain default route interface address");
match address_family {
AddressFamily::Ipv4 => {
ctx.addresses.internet_ipv4 = None;
}
AddressFamily::Ipv6 => {
ctx.addresses.internet_ipv6 = None;
}
}
}
Err(error) => {
log::error!(
"{}",
error.display_chain_with_msg(
"Failed to obtain default route interface address"
)
);
maybe_send(TunnelCommand::Block(ErrorStateCause::SplitTunnelError));
return;
}
};
ctx.register_ips()
}
// no default route
Removed => {
match address_family {
AddressFamily::Ipv4 => {
ctx.addresses.internet_ipv4 = None;
}
AddressFamily::Ipv6 => {
ctx.addresses.internet_ipv6 = None;
}
}
ctx.register_ips()
}
};
if let Err(error) = result {
log::error!(
"{}",
error.display_chain_with_msg("Failed to register new addresses in split tunnel driver")
);
maybe_send(TunnelCommand::Block(ErrorStateCause::SplitTunnelError));
}
}