Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/enterprise/firewall/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ impl FirewallManagementApi for FirewallApi {
) -> Result<(), FirewallError> {
debug!("Initializing firewall, VPN interface: {}", self.ifname);
if let Some(batch) = &mut self.batch {
drop_table(batch)?;
init_firewall(default_policy, priority, batch).expect("Failed to setup chains");
drop_table(batch, &self.ifname)?;
init_firewall(default_policy, priority, batch, &self.ifname)
.expect("Failed to setup chains");
debug!("Allowing all established traffic");
ignore_unrelated_traffic(batch, &self.ifname)?;
allow_established_traffic(batch)?;
allow_established_traffic(batch, &self.ifname)?;
debug!("Allowed all established traffic");
debug!("Initialized firewall");
Ok(())
Expand All @@ -96,7 +97,7 @@ impl FirewallManagementApi for FirewallApi {
fn cleanup(&mut self) -> Result<(), FirewallError> {
debug!("Cleaning up all previous firewall rules, if any");
if let Some(batch) = &mut self.batch {
drop_table(batch)?;
drop_table(batch, &self.ifname)?;
} else {
return Err(FirewallError::TransactionNotStarted);
}
Expand All @@ -108,7 +109,7 @@ impl FirewallManagementApi for FirewallApi {
fn set_firewall_default_policy(&mut self, policy: Policy) -> Result<(), FirewallError> {
debug!("Setting default firewall policy to: {policy:?}");
if let Some(batch) = &mut self.batch {
set_default_policy(policy, batch)?;
set_default_policy(policy, batch, &self.ifname)?;
} else {
return Err(FirewallError::TransactionNotStarted);
}
Expand Down Expand Up @@ -218,7 +219,7 @@ impl FirewallManagementApi for FirewallApi {
}
}

apply_filter_rules(rules, batch)?;
apply_filter_rules(rules, batch, &self.ifname)?;

debug!(
"Applied firewall rules for Defguard ACL rule ID: {}",
Expand Down
47 changes: 30 additions & 17 deletions src/enterprise/firewall/linux/netfilter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::enterprise::firewall::FirewallError;

const FILTER_TABLE: &str = "filter";
const NAT_TABLE: &str = "nat";
const DEFGUARD_TABLE: &str = "DEFGUARD";
const DEFGUARD_TABLE: &str = "DEFGUARD-{IFNAME}";
const POSTROUTING_CHAIN: &str = "POSTROUTING";
const FORWARD_CHAIN: &str = "FORWARD";
const ANON_SET_NAME: &str = "__set%d";
Expand Down Expand Up @@ -563,8 +563,9 @@ pub(crate) fn init_firewall(
initial_policy: Option<Policy>,
defguard_fwd_chain_priority: Option<i32>,
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);

batch.add(&table, nftnl::MsgType::Add);
batch.add(&table, nftnl::MsgType::Del);
Expand All @@ -582,16 +583,20 @@ pub(crate) fn init_firewall(
Ok(())
}

pub(crate) fn drop_table(batch: &mut Batch) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
pub(crate) fn drop_table(batch: &mut Batch, ifname: &str) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);
batch.add(&table, nftnl::MsgType::Del);

Ok(())
}

pub(crate) fn drop_chain(chain: &Chains, batch: &mut Batch) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
pub(crate) fn drop_chain(
chain: &Chains,
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
let chain = chain.to_chain(&table);
batch.add(&chain, nftnl::MsgType::Add);
batch.add(&chain, nftnl::MsgType::Del);
Expand All @@ -601,14 +606,14 @@ pub(crate) fn drop_chain(chain: &Chains, batch: &mut Batch) -> Result<(), Firewa

/// Applies masquerade on the specified interface for the outgoing packets
pub(crate) fn set_masq(
_ifname: &str,
ifname: &str,
enabled: bool,
batch: &mut Batch,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);

drop_chain(&Chains::Postrouting, batch)?;
drop_chain(&Chains::Postrouting, batch, ifname)?;

let mut nat_chain = Chains::Postrouting.to_chain(&table);
nat_chain.set_hook(nftnl::Hook::PostRouting, POSTROUTING_PRIORITY);
Expand All @@ -633,8 +638,12 @@ pub(crate) fn set_masq(
Ok(())
}

pub(crate) fn set_default_policy(policy: Policy, batch: &mut Batch) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
pub(crate) fn set_default_policy(
policy: Policy,
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);

let mut forward_chain = Chains::Forward.to_chain(&table);
Expand All @@ -648,8 +657,11 @@ pub(crate) fn set_default_policy(policy: Policy, batch: &mut Batch) -> Result<()
Ok(())
}

pub(crate) fn allow_established_traffic(batch: &mut Batch) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
pub(crate) fn allow_established_traffic(
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);

let forward_chain = Chains::Forward.to_chain(&table);
Expand All @@ -672,7 +684,7 @@ pub(crate) fn ignore_unrelated_traffic(
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);

let forward_chain = Chains::Forward.to_chain(&table);
Expand All @@ -699,7 +711,7 @@ pub enum Tables {
}

impl Tables {
fn to_table(&self) -> Table {
fn to_table(&self, ifname: &str) -> Table {
match self {
Self::Filter(family) => Table::new(
&CString::new(FILTER_TABLE)
Expand All @@ -712,7 +724,7 @@ impl Tables {
*family,
),
Self::Defguard(family) => Table::new(
&CString::new(DEFGUARD_TABLE)
&CString::new(DEFGUARD_TABLE.replace("{IFNAME}", ifname))
.expect("Failed to create CString from DEFGUARD_TABLE constant."),
*family,
),
Expand Down Expand Up @@ -745,8 +757,9 @@ impl Chains {
pub(crate) fn apply_filter_rules(
rules: Vec<FilterRule>,
batch: &mut Batch,
ifname: &str,
) -> Result<(), FirewallError> {
let table = Tables::Defguard(ProtoFamily::Inet).to_table();
let table = Tables::Defguard(ProtoFamily::Inet).to_table(ifname);
batch.add(&table, nftnl::MsgType::Add);

let forward_chain = Chains::Forward.to_chain(&table);
Expand Down