Skip to content

Commit

Permalink
Update the tunnel state on lockdown mode change.
Browse files Browse the repository at this point in the history
The printing of lockdown mode by `mullvad status` does not reflect the
current setting unless the tunnel state has also been updated.
  • Loading branch information
Serock3 committed Jan 8, 2024
1 parent 84cf8c8 commit 3b638f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mullvad-daemon/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ impl TunnelStateChangeHandler {
}
TunnelStateTransition::Error(_)
| TunnelStateTransition::Connected(_)
| TunnelStateTransition::Disconnected => 0,
| TunnelStateTransition::Disconnected { .. } => 0,
_ => retry_attempt,
}
}
Expand Down
5 changes: 2 additions & 3 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,9 @@ where
.handle_state_transition(&tunnel_state_transition);

let tunnel_state = match tunnel_state_transition {
TunnelStateTransition::Disconnected => TunnelState::Disconnected {
TunnelStateTransition::Disconnected { locked_down } => TunnelState::Disconnected {
location: None,
// If lockdown mode is enabled and state is disconnected
locked_down: self.settings.block_when_disconnected,
locked_down,
},
TunnelStateTransition::Connecting(endpoint) => TunnelState::Connecting {
endpoint,
Expand Down
24 changes: 20 additions & 4 deletions talpid-core/src/tunnel_state_machine/disconnected_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ impl DisconnectedState {
error.display_chain_with_msg("Unable to disable filtering resolver")
);
}

#[cfg(windows)]
Self::register_split_tunnel_addresses(shared_values, should_reset_firewall);
Self::set_firewall_policy(shared_values, should_reset_firewall);
Expand All @@ -43,9 +42,17 @@ impl DisconnectedState {
#[cfg(target_os = "android")]
shared_values.tun_provider.lock().unwrap().close_tun();

Self::construct_state_transition(shared_values)
}

fn construct_state_transition(
shared_values: &mut SharedTunnelStateValues,
) -> (Box<dyn TunnelState>, TunnelStateTransition) {
(
Box::new(DisconnectedState(())),
TunnelStateTransition::Disconnected,
TunnelStateTransition::Disconnected {
locked_down: shared_values.block_when_disconnected,
},
)
}

Expand Down Expand Up @@ -160,6 +167,12 @@ impl TunnelState for DisconnectedState {
Some(TunnelCommand::BlockWhenDisconnected(block_when_disconnected, complete_tx)) => {
if shared_values.block_when_disconnected != block_when_disconnected {
shared_values.block_when_disconnected = block_when_disconnected;

// TODO: Investigate if we can simply return
// `NewState(Self::enter(shared_values, true))`.
// The logic for updating the firewall in `DisconnectedState::enter` is
// identical but it does not enter the error state if setting the local DNS
// fails.
Self::set_firewall_policy(shared_values, true);
#[cfg(windows)]
Self::register_split_tunnel_addresses(shared_values, true);
Expand All @@ -178,9 +191,12 @@ impl TunnelState for DisconnectedState {
} else {
Self::reset_dns(shared_values);
}
let _ = complete_tx.send(());
NewState(Self::construct_state_transition(shared_values))
} else {
let _ = complete_tx.send(());
SameState(self)
}
let _ = complete_tx.send(());
SameState(self)
}
Some(TunnelCommand::IsOffline(is_offline)) => {
shared_values.is_offline = is_offline;
Expand Down

0 comments on commit 3b638f9

Please sign in to comment.