-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Miscelaneous fixes and features: Set bugfixes, Conntrack zone support, FIB support #55
Open
brizental
wants to merge
4
commits into
mullvad:main
Choose a base branch
from
brizental:misc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use super::{Expression, Rule}; | ||
use nftnl_sys::{self as sys, libc}; | ||
use std::os::raw::c_char; | ||
use std::str::FromStr; | ||
|
||
#[non_exhaustive] | ||
pub enum FibResult { | ||
Oif, | ||
OifName, | ||
AddrType, | ||
} | ||
|
||
impl FromStr for FibResult { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.trim().to_lowercase().as_str() { | ||
"oif" => Ok(FibResult::Oif), | ||
"oifname" => Ok(FibResult::OifName), | ||
"type" => Ok(FibResult::AddrType), | ||
_ => Err("Invalid FibResult variant"), | ||
} | ||
} | ||
} | ||
|
||
impl FibResult { | ||
pub fn raw_result_type(&self) -> u32 { | ||
use FibResult::*; | ||
|
||
// From: linux/netfilter/nf_tables.h | ||
match *self { | ||
Oif => 1, | ||
OifName => 2, | ||
AddrType => 3, | ||
} | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
pub enum Fib { | ||
SAddr { result: &'static str }, | ||
DAddr { result: &'static str }, | ||
Mark { result: &'static str }, | ||
Iif { result: &'static str }, | ||
Oif { result: &'static str }, | ||
Present { result: &'static str }, | ||
} | ||
|
||
impl Fib { | ||
pub fn flags(&self) -> u32 { | ||
use Fib::*; | ||
|
||
// From: linux/netfilter/nf_tables.h | ||
match *self { | ||
SAddr { .. } => 1 << 0, | ||
DAddr { .. } => 1 << 1, | ||
Mark { .. } => 1 << 2, | ||
Iif { .. } => 1 << 3, | ||
Oif { .. } => 1 << 4, | ||
Present { .. } => 1 << 5, | ||
} | ||
} | ||
|
||
pub fn result(&self) -> u32 { | ||
use Fib::*; | ||
|
||
let result: FibResult = match self { | ||
SAddr { result } | ||
| DAddr { result } | ||
| Mark { result } | ||
| Iif { result } | ||
| Oif { result } | ||
| Present { result } => result | ||
.parse() | ||
.expect("Unexpected fib result. Must be type, oif or oifname."), | ||
}; | ||
|
||
result.raw_result_type() | ||
} | ||
} | ||
|
||
impl Expression for Fib { | ||
fn to_expr(&self, _rule: &Rule) -> *mut sys::nftnl_expr { | ||
unsafe { | ||
let expr = try_alloc!(sys::nftnl_expr_alloc(b"fib\0" as *const _ as *const c_char)); | ||
|
||
sys::nftnl_expr_set_u32( | ||
expr, | ||
sys::NFTNL_EXPR_FIB_DREG as u16, | ||
libc::NFT_REG_1 as u32, | ||
); | ||
|
||
sys::nftnl_expr_set_u32(expr, sys::NFTNL_EXPR_FIB_RESULT as u16, self.result()); | ||
sys::nftnl_expr_set_u32(expr, sys::NFTNL_EXPR_FIB_FLAGS as u16, self.flags()); | ||
|
||
expr | ||
} | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! nft_expr_fib { | ||
(saddr $result:expr) => { | ||
$crate::expr::Fib::SAddr { result: $result } | ||
}; | ||
(daddr $result:expr) => { | ||
$crate::expr::Fib::DAddr { result: $result } | ||
}; | ||
(mark $result:expr) => { | ||
$crate::expr::Fib::Mark { result: $result } | ||
}; | ||
(iif $result:expr) => { | ||
$crate::expr::Fib::Iif { result: $result } | ||
}; | ||
(oif $result:expr) => { | ||
$crate::expr::Fib::Oif { result: $result } | ||
}; | ||
(present $result:expr) => { | ||
$crate::expr::Fib::Present { result: $result } | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Can you submit a PR to
libc
? We won't merge this with a hardcoded constant.