-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathtunnel.rs
772 lines (643 loc) · 21.8 KB
/
tunnel.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
use super::helpers::{
self, connect_and_wait, disconnect_and_wait, set_bridge_settings, set_relay_settings,
};
use super::{config::TEST_CONFIG, Error, TestContext};
use crate::network_monitor::{start_packet_monitor, MonitorOptions};
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::relay_constraints::{
self, BridgeConstraints, BridgeSettings, BridgeType, Constraint, OpenVpnConstraints,
RelayConstraints, RelaySettings, SelectedObfuscation, TransportPort,
Udp2TcpObfuscationSettings, WireguardConstraints,
};
use mullvad_types::wireguard;
use std::net::SocketAddr;
use talpid_types::net::{
proxy::{CustomProxy, Socks5Local, Socks5Remote},
TransportProtocol, TunnelType,
};
use test_macro::test_function;
use test_rpc::meta::Os;
use test_rpc::mullvad_daemon::ServiceStatus;
use test_rpc::ServiceClient;
use pnet_packet::ip::IpNextHeaderProtocols;
/// Set up an OpenVPN tunnel, UDP as well as TCP.
/// This test fails if a working tunnel cannot be set up.
#[test_function]
pub async fn test_openvpn_tunnel(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
// TODO: observe traffic on the expected destination/port (only)
const CONSTRAINTS: [(&str, Constraint<TransportPort>); 3] = [
("any", Constraint::Any),
(
"UDP",
Constraint::Only(TransportPort {
protocol: TransportProtocol::Udp,
port: Constraint::Any,
}),
),
(
"TCP",
Constraint::Only(TransportPort {
protocol: TransportProtocol::Tcp,
port: Constraint::Any,
}),
),
];
for (protocol, constraint) in CONSTRAINTS {
log::info!("Connect to {protocol} OpenVPN endpoint");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::OpenVpn),
openvpn_constraints: OpenVpnConstraints { port: constraint },
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
connect_and_wait(&mut mullvad_client).await?;
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
disconnect_and_wait(&mut mullvad_client).await?;
}
Ok(())
}
/// Set up a WireGuard tunnel.
/// This test fails if a working tunnel cannot be set up.
/// WARNING: This test will fail if host has something bound to port 53 such as a connected Mullvad
#[test_function]
pub async fn test_wireguard_tunnel(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
// TODO: observe UDP traffic on the expected destination/port (only)
// TODO: IPv6
const PORTS: [(u16, bool); 3] = [(53, true), (51820, true), (1, false)];
for (port, should_succeed) in PORTS {
log::info!("Connect to WireGuard endpoint on port {port}");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::Wireguard),
wireguard_constraints: WireguardConstraints {
port: Constraint::Only(port),
..Default::default()
},
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
let connection_result = connect_and_wait(&mut mullvad_client).await;
assert_eq!(
connection_result.is_ok(),
should_succeed,
"unexpected result for port {port}: {connection_result:?}",
);
if should_succeed {
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
}
disconnect_and_wait(&mut mullvad_client).await?;
}
Ok(())
}
/// Use udp2tcp obfuscation. This test connects to a WireGuard relay over TCP. It fails if no
/// outgoing TCP traffic to the relay is observed on the expected port.
#[test_function]
pub async fn test_udp2tcp_tunnel(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
mullvad_client
.set_obfuscation_settings(relay_constraints::ObfuscationSettings {
selected_obfuscation: SelectedObfuscation::Udp2Tcp,
udp2tcp: Udp2TcpObfuscationSettings {
port: Constraint::Any,
},
})
.await
.expect("failed to enable udp2tcp");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::Wireguard),
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
log::info!("Connect to WireGuard via tcp2udp endpoint");
connect_and_wait(&mut mullvad_client).await?;
let endpoint = match mullvad_client.get_tunnel_state().await? {
mullvad_types::states::TunnelState::Connected { endpoint, .. } => endpoint.endpoint,
_ => panic!("unexpected tunnel state"),
};
//
// Set up packet monitor
//
let monitor = start_packet_monitor(
move |packet| {
packet.destination.ip() == endpoint.address.ip()
&& packet.protocol == IpNextHeaderProtocols::Tcp
},
MonitorOptions::default(),
)
.await;
//
// Verify that we can reach stuff
//
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
let monitor_result = monitor.into_result().await.unwrap();
assert!(
!monitor_result.packets.is_empty(),
"detected no tcp traffic",
);
Ok(())
}
/// Test whether bridge mode works. This fails if:
/// * No outgoing traffic to the bridge/entry relay is
/// observed from the SUT.
/// * The conncheck reports an unexpected exit relay.
#[test_function]
pub async fn test_bridge(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
//
// Enable bridge mode
//
log::info!("Updating bridge settings");
mullvad_client
.set_bridge_state(relay_constraints::BridgeState::On)
.await
.expect("failed to enable bridge mode");
set_bridge_settings(&mut mullvad_client, BridgeSettings::default())
.await
.expect("failed to update bridge settings");
set_relay_settings(
&mut mullvad_client,
RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::OpenVpn),
..Default::default()
}),
)
.await
.expect("failed to update relay settings");
//
// Connect to VPN
//
log::info!("Connect to OpenVPN relay via bridge");
connect_and_wait(&mut mullvad_client).await?;
let (entry, exit) = match mullvad_client.get_tunnel_state().await? {
mullvad_types::states::TunnelState::Connected { endpoint, .. } => {
(endpoint.proxy.unwrap().endpoint, endpoint.endpoint)
}
actual => {
panic!("unexpected tunnel state. Expected `TunnelState::Connected` but got {actual:?}")
}
};
log::info!(
"Selected entry bridge {entry_addr} & exit relay {exit_addr}",
entry_addr = entry.address,
exit_addr = exit.address
);
// Start recording outgoing packets. Their destination will be verified
// against the bridge's IP address later.
let monitor = start_packet_monitor(
move |packet| packet.destination.ip() == entry.address.ip(),
MonitorOptions::default(),
)
.await;
//
// Verify exit IP
//
log::info!("Verifying exit server");
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
//
// Verify entry IP
//
log::info!("Verifying entry server");
let monitor_result = monitor.into_result().await.unwrap();
assert!(
!monitor_result.packets.is_empty(),
"detected no traffic to entry server",
);
Ok(())
}
/// Test whether WireGuard multihop works. This fails if:
/// * No outgoing traffic to the entry relay is observed from the SUT.
/// * The conncheck reports an unexpected exit relay.
#[test_function]
pub async fn test_multihop(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
let wireguard_constraints = WireguardConstraints {
use_multihop: true,
..Default::default()
};
let relay_settings = RelaySettings::Normal(RelayConstraints {
wireguard_constraints,
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
//
// Connect
//
log::info!("Connect using WG multihop");
connect_and_wait(&mut mullvad_client).await?;
let (entry, exit) = match mullvad_client.get_tunnel_state().await? {
mullvad_types::states::TunnelState::Connected { endpoint, .. } => {
(endpoint.entry_endpoint.unwrap(), endpoint.endpoint)
}
actual => {
panic!("unexpected tunnel state. Expected `TunnelState::Connected` but got {actual:?}")
}
};
log::info!(
"Selected entry {entry_addr} & exit relay {exit_addr}",
entry_addr = entry.address,
exit_addr = exit.address
);
//
// Record outgoing packets to the entry relay
//
let monitor = start_packet_monitor(
move |packet| packet.destination.ip() == entry.address.ip(),
MonitorOptions::default(),
)
.await;
//
// Verify exit IP
//
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
//
// Verify entry IP
//
log::info!("Verifying entry server");
let monitor_result = monitor.into_result().await.unwrap();
assert!(!monitor_result.packets.is_empty(), "no matching packets",);
Ok(())
}
/// Test whether the daemon automatically connects on reboot when using
/// WireGuard.
///
/// # Limitations
///
/// This test does not guarantee that nothing leaks during boot or shutdown.
#[test_function]
pub async fn test_wireguard_autoconnect(
_: TestContext,
mut rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
log::info!("Setting tunnel protocol to WireGuard");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::Wireguard),
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
mullvad_client
.set_auto_connect(true)
.await
.expect("failed to enable auto-connect");
helpers::reboot(&mut rpc).await?;
rpc.mullvad_daemon_wait_for_state(|state| state == ServiceStatus::Running)
.await?;
log::info!("Waiting for daemon to connect");
helpers::wait_for_tunnel_state(mullvad_client, |state| {
matches!(state, mullvad_types::states::TunnelState::Connected { .. })
})
.await?;
Ok(())
}
/// Test whether the daemon automatically connects on reboot when using
/// OpenVPN.
///
/// # Limitations
///
/// This test does not guarantee that nothing leaks during boot or shutdown.
#[test_function]
pub async fn test_openvpn_autoconnect(
_: TestContext,
mut rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
log::info!("Setting tunnel protocol to OpenVPN");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::OpenVpn),
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("failed to update relay settings");
mullvad_client
.set_auto_connect(true)
.await
.expect("failed to enable auto-connect");
helpers::reboot(&mut rpc).await?;
rpc.mullvad_daemon_wait_for_state(|state| state == ServiceStatus::Running)
.await?;
log::info!("Waiting for daemon to connect");
helpers::wait_for_tunnel_state(mullvad_client, |state| {
matches!(state, mullvad_types::states::TunnelState::Connected { .. })
})
.await?;
Ok(())
}
/// Test whether quantum-resistant tunnels can be set up.
///
/// # Limitations
///
/// This only checks whether we have a working tunnel and a PSK. It does not determine whether the
/// exchange part is correct.
///
/// We only check whether there is a PSK on Linux.
#[test_function]
pub async fn test_quantum_resistant_tunnel(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
mullvad_client
.set_quantum_resistant_tunnel(wireguard::QuantumResistantState::Off)
.await
.expect("Failed to disable PQ tunnels");
//
// PQ disabled: Find no "preshared key"
//
connect_and_wait(&mut mullvad_client).await?;
check_tunnel_psk(&rpc, &mullvad_client, false).await;
log::info!("Setting tunnel protocol to WireGuard");
let relay_settings = RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::Wireguard),
..Default::default()
});
set_relay_settings(&mut mullvad_client, relay_settings)
.await
.expect("Failed to update relay settings");
mullvad_client
.set_quantum_resistant_tunnel(wireguard::QuantumResistantState::On)
.await
.expect("Failed to enable PQ tunnels");
//
// PQ enabled: Find "preshared key"
//
connect_and_wait(&mut mullvad_client).await?;
check_tunnel_psk(&rpc, &mullvad_client, true).await;
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
Ok(())
}
async fn check_tunnel_psk(
rpc: &ServiceClient,
mullvad_client: &MullvadProxyClient,
should_have_psk: bool,
) {
match TEST_CONFIG.os {
Os::Linux => {
let name = helpers::get_tunnel_interface(&mut mullvad_client.clone())
.await
.expect("failed to get tun name");
let output = rpc
.exec("wg", vec!["show", &name].into_iter())
.await
.expect("failed to run wg");
let parsed_output = std::str::from_utf8(&output.stdout).expect("non-utf8 output");
assert!(
parsed_output.contains("preshared key: ") == should_have_psk,
"expected to NOT find preshared key"
);
}
os => {
log::warn!("Not checking if there is a PSK on {os}");
}
}
}
/// Test whether a PQ tunnel can be set up with multihop and UDP-over-TCP enabled.
///
/// # Limitations
///
/// This is not testing any of the individual components, just whether the daemon can connect when
/// all of these features are combined.
#[test_function]
pub async fn test_quantum_resistant_multihop_udp2tcp_tunnel(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
mullvad_client
.set_quantum_resistant_tunnel(wireguard::QuantumResistantState::On)
.await
.expect("Failed to enable PQ tunnels");
mullvad_client
.set_obfuscation_settings(relay_constraints::ObfuscationSettings {
selected_obfuscation: SelectedObfuscation::Udp2Tcp,
udp2tcp: Udp2TcpObfuscationSettings {
port: Constraint::Any,
},
})
.await
.expect("Failed to enable obfuscation");
mullvad_client
.set_relay_settings(relay_constraints::RelaySettings::Normal(RelayConstraints {
wireguard_constraints: WireguardConstraints {
use_multihop: true,
..Default::default()
},
tunnel_protocol: Constraint::Only(TunnelType::Wireguard),
..Default::default()
}))
.await
.expect("Failed to update relay settings");
connect_and_wait(&mut mullvad_client).await?;
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
Ok(())
}
/// Try to connect to an OpenVPN relay via a remote, passwordless SOCKS5 server.
/// * No outgoing traffic to the bridge/entry relay is observed from the SUT.
/// * The conncheck reports an unexpected exit relay.
#[test_function]
pub async fn test_remote_socks_bridge(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
mullvad_client
.set_bridge_state(relay_constraints::BridgeState::On)
.await
.expect("failed to enable bridge mode");
mullvad_client
.set_bridge_settings(BridgeSettings {
bridge_type: BridgeType::Custom,
normal: BridgeConstraints::default(),
custom: Some(CustomProxy::Socks5Remote(Socks5Remote::new((
crate::vm::network::NON_TUN_GATEWAY,
crate::vm::network::SOCKS5_PORT,
)))),
})
.await
.expect("failed to update bridge settings");
set_relay_settings(
&mut mullvad_client,
RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::OpenVpn),
..Default::default()
}),
)
.await
.expect("failed to update relay settings");
//
// Connect to VPN
//
connect_and_wait(&mut mullvad_client).await?;
let (entry, exit) = match mullvad_client.get_tunnel_state().await? {
mullvad_types::states::TunnelState::Connected { endpoint, .. } => {
(endpoint.proxy.unwrap().endpoint, endpoint.endpoint)
}
actual => {
panic!("unexpected tunnel state. Expected `TunnelState::Connected` but got {actual:?}")
}
};
log::info!(
"Selected entry bridge {entry_addr} & exit relay {exit_addr}",
entry_addr = entry.address,
exit_addr = exit.address
);
// Start recording outgoing packets. Their destination will be verified
// against the bridge's IP address later.
let monitor = start_packet_monitor(
move |packet| packet.destination.ip() == entry.address.ip(),
MonitorOptions::default(),
)
.await;
//
// Verify exit IP
//
log::info!("Verifying exit server");
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
//
// Verify entry IP
//
log::info!("Verifying entry server");
let monitor_result = monitor.into_result().await.unwrap();
assert!(
!monitor_result.packets.is_empty(),
"detected no traffic to entry server",
);
Ok(())
}
/// Try to connect to an OpenVPN relay via a local, passwordless SOCKS5 server.
/// * No outgoing traffic to the bridge/entry relay is observed from the SUT.
/// * The conncheck reports an unexpected exit relay.
#[test_function]
pub async fn test_local_socks_bridge(
_: TestContext,
rpc: ServiceClient,
mut mullvad_client: MullvadProxyClient,
) -> Result<(), Error> {
let remote_addr = SocketAddr::from((
crate::vm::network::NON_TUN_GATEWAY,
crate::vm::network::SOCKS5_PORT,
));
let socks_server = rpc
.start_tcp_forward("127.0.0.1:0".parse().unwrap(), remote_addr)
.await
.expect("failed to start TCP forward");
mullvad_client
.set_bridge_state(relay_constraints::BridgeState::On)
.await
.expect("failed to enable bridge mode");
mullvad_client
.set_bridge_settings(BridgeSettings {
bridge_type: BridgeType::Custom,
normal: BridgeConstraints::default(),
custom: Some(CustomProxy::Socks5Local(
Socks5Local::new_with_transport_protocol(
remote_addr,
socks_server.bind_addr().port(),
TransportProtocol::Tcp,
),
)),
})
.await
.expect("failed to update bridge settings");
set_relay_settings(
&mut mullvad_client,
RelaySettings::Normal(RelayConstraints {
tunnel_protocol: Constraint::Only(TunnelType::OpenVpn),
..Default::default()
}),
)
.await
.expect("failed to update relay settings");
//
// Connect to VPN
//
connect_and_wait(&mut mullvad_client).await?;
let (entry, exit) = match mullvad_client.get_tunnel_state().await? {
mullvad_types::states::TunnelState::Connected { endpoint, .. } => {
(endpoint.proxy.unwrap().endpoint, endpoint.endpoint)
}
actual => {
panic!("unexpected tunnel state. Expected `TunnelState::Connected` but got {actual:?}")
}
};
log::info!(
"Selected entry bridge {entry_addr} & exit relay {exit_addr}",
entry_addr = entry.address,
exit_addr = exit.address
);
// Start recording outgoing packets. Their destination will be verified
// against the bridge's IP address later.
let monitor = start_packet_monitor(
move |packet| packet.destination.ip() == entry.address.ip(),
MonitorOptions::default(),
)
.await;
//
// Verify exit IP
//
log::info!("Verifying exit server");
assert!(
helpers::using_mullvad_exit(&rpc).await,
"expected Mullvad exit IP"
);
//
// Verify entry IP
//
log::info!("Verifying entry server");
let monitor_result = monitor.into_result().await.unwrap();
assert!(
!monitor_result.packets.is_empty(),
"detected no traffic to entry server",
);
Ok(())
}