Commit Graph
73 Commits
Author SHA1 Message Date
Jasper Wiegratz 23dbeabf37 Merge pull request #166 from stevelr/main
fix: bitwise flags or with more than two operands
2025-08-15 15:49:28 +02:00
Paul Holzinger 54ba9a30e9 fix: accept single or multiple set/map flags
Like for fib and synproxy flags with nft 1.1.4 a single flag value is
printed as string not array so the parser need to account for that.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:34:35 +02:00
Paul Holzinger c7ce1b0c4a fix: accept single or multiple synproxy flags
Like for fib flags with nft 1.1.4 a single flag value is printed as
string not array so the parser need to account for that.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:34:35 +02:00
Paul Holzinger d234af6ea1 fix: accept fib flags as string or array
nft 1.1.4 changed the json output to return only a single string if
there is only one flag set.[1,2]

The same logic applies to some other types as well which I update in the
following commits.

[1] https://github.com/nftables-rs/nftables-rs/issues/183
[2] https://bugzilla.netfilter.org/show_bug.cgi?id=1806

Fixes #183

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:25:23 +02:00
Paul Holzinger 0294fb2601 fix: correctly parse log opts as array
The inffered type by Deserialize is Option<T> which is wrong and thus
the deserialize call fails as it is called on the wrong type. If we have
an array we need to directly use the HashSet type.

Switch one of the log statements in the tests to use two flags to cover
this path.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:25:22 +02:00
Paul Holzinger 4c6a943fef fix: do not panic when parsing an unsupported log flag
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2025-08-13 15:25:15 +02:00
jwhb 6bd0d2b61f fix(stmt): serialize anonymouscounter none as zero 2025-06-26 11:15:37 +02:00
stevelr 9024a91fc4 fix: bitwise flags or with more than two operands
Fixes #165
- changed BinaryOperation::OR to accept Vec<Expression> instead of exactly two.
- added regression test (tests/test_bit_flags)
- note: The fix was tested with deserialization only. I did not test adding an nftables
  rule with bit flags

Signed-off-by: stevelr <stevelr.git@pm.me>
2025-06-19 06:03:55 +00:00
jwhb 39b57961da fix: clippy string format lint 2025-05-08 17:16:07 +02:00
Jakub Gargul 11f9657f42 docs(expr): fix Payload docs 2025-05-07 13:55:34 +02:00
Joel Mühlena 79fe2f81ad feat(cli): add json schema export using schemars
Adds a feature to export the nftables rust types to json schema via cli
2025-02-06 16:12:59 +01:00
wiegratz 13c084968b feat(expr)!: add expr default impl, improve socket
Adds impls of Default for several expr types.
Adds doc comments throughout expr module.

BREAKING CHANGE: Adds SocketAttr enum type for Socket key.
2025-01-24 15:31:45 +01:00
Eric Long e55e6c26c4 feat(helper)!: add apply_and_return_ruleset* helpers
These correspond to the behaviour of `nft --echo`, which prints back applied rulesets along with more information like handles. They are especially useful when one needs a rule's handle for later positional insertion or removal.
2025-01-13 11:11:51 +08:00
wiegratz 07d062a8de fix(stmt): allow port-range for nat port 2025-01-09 22:48:36 +01:00
Eric Long 3a6be325a8 docs(helper): add docs for async helpers 2025-01-07 21:56:52 +08:00
Eric Long 81cd4f3738 feat(helper): add async helpers
Basically integrates functions from the nftables_async crate here, with both
tokio and async-process support behind feature gates.

Also corrected some of the behaviour differences in nftables_async, e.g.
get_current_ruleset_raw passes additional arguments (keeping "-j list ruleset"
intact) in nftables_async, but replaces the default arguments (except -j) here.
2025-01-07 21:30:59 +08:00
Eric Long 021668a923 feat(helper): generalize arguments
Use IntoIterator for arguments to generalize for all containers

Based on a suggestion from Eric Long in #108
2025-01-07 14:16:41 +01:00
Michael Buesch efa8d00078 helper: Add documentation 2025-01-07 14:05:58 +01:00
Michael Buesch ccee3c243f helper: Change API to AsRef<OsStr> based API
This improves the versatility of the API.
With the new API there are more ideomatic options for the user like
&Path, PathBuf and also still &str and more.

This is an API incompatible change.
I think it's not really possible to make this API compatible,
because the None-values need explicit type annotations due to the trait based API.
The default None-based API has been replaced by functions without arguments
to make the common case easier to use.

The user code changes are probably minimal in most cases, because None arguments
can just be removed and string arguments keep working as-is due to the std
implementations of AsRef.
Convenience constants are provided to make use of the None-arguments
to the _with_args() functions more readable and easier to use.

As a side effect this change also removes an unnecessary allocation of the program
name string in case of a successful execution.
2025-01-07 14:05:58 +01:00
Eric Long c22a2a47d6 feat!: replace Cow<'static, _> with 'a
This adds a bunch of lifetimes, but it works with `.into()` when a borrowed value's array container is stored in another place, without the need of `Cow::Borrowed`:

```
let objects = [
    NfObject::ListObject(NfListObject::Table(Table {
        family: NfFamily::INet,
        name: "some_inet_table".into(), // here
        handle: None,
    })),
    // ...
];
let expected = Nftables {
    objects: objects.into(); // and here
};
```

However, `Cow::{Borrowed, Owned}` still needs to be used when you want to write the above in one statement.
2024-12-21 21:19:10 +01:00
Eric Long 75b7f48795 fix(expr)!: revert recursive Cow<[Expression]> back to Vec
This fails to build on 1.65:

```
error[E0277]: the trait bound `[SetItem]: ToOwned` is not satisfied in `Expression`
   --> src/stmt.rs:245:14
    |
245 |     pub dev: Option<Expression>,
    |              ^^^^^^^^^^^^^^^^^^ within `Expression`, the trait `ToOwned` is not implemented for `[SetItem]`, which is required by `Expression: Sized`
    |
    = help: the trait `ToOwned` is implemented for `[T]`
note: required because it appears within the type `Expression`
   --> src/expr.rs:10:10
    |
10  | pub enum Expression {
    |          ^^^^^^^^^^
note: required by an implicit `Sized` bound in `std::option::Option`
   --> /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/option.rs:572:1

...and many, many similar errors
```

It compiles successfully on Rust >=1.79, so this commit can be reverted once our MSRV reaches it.
2024-12-21 21:19:10 +01:00
Eric Long 1ce80215bd feat(expr)!: make range fixed-sized array, not slice
Since we only expect two elements from Range, we can just use array instead.
2024-12-21 21:19:10 +01:00
Eric Long 583b2d58cb feat!: reduce stack usage by selectively wrapping large values in Box
This evens out some of the large enums' sizes (e.g. Expression and Statement's)
from over 200 bytes to <=144. Further reduction could be done, as this commit
only changes the obvious ones for now.

Also moves BinaryOperation's Box up one level so it only uses one Box.
2024-12-21 21:19:10 +01:00
Eric Long 8ddb5ff132 feat!: use Cow whenever possible instead of owned values
A lot of strings and arrays used in nftables structures are static, but
currently we need to do allocations everywhere using owned String and Vec.
Replace them with Cow so we can pass &'static str or &'static [_] whenever
possible, while returning owned value when deserializing.
2024-12-21 21:19:10 +01:00
Eric Long 091adb4313 feat(helper)!: make helper APIs accept borrowed values 2024-12-21 21:19:10 +01:00
wiegratz 61ba8eaec6 fix(stmt)!: match anonymous and named quotas 2024-11-12 11:45:02 +01:00
wiegratz 2e98483b74 docs(lib): add library description 2024-10-24 20:51:06 +02:00
wiegratz abd3156e84 feat(schema)!: add default impl, add doc comments
added default impl for table, chain, rule, set, map, element, flowtable, counter, quota, cthelper, limit, ruleset, metainfoobject, cttimeout.
Default impls replace ::new() functions.

replace family: String by family: NfFamily in flowtable, counter, quota, cthelper, limit
2024-10-24 20:51:06 +02:00
wiegratz 42c399d2d2 feat(schema): qualify limit's per-attribute as time unit enum 2024-10-24 20:09:01 +02:00
wiegratz d8e0c68391 docs(visitor): fix doc comment syntax 2024-10-24 20:09:01 +02:00
Jasper d1cfeddd2d Merge pull request #71 from namib-project/non-exhaustive-statement
Mark Statement as non-exhaustive enum
2024-10-21 12:44:03 +02:00
wiegratz 0108fbfc9e feat: add synproxy statement and list object
also mark some other statements that are missing.
compare with nftables json_parse_stmt:  https://git.netfilter.org/nftables/tree/src/parser_json.c?id=c4c740117f6fbf39dd67dd87635ea8b497718ad7#n2884
2024-10-21 12:19:07 +02:00
wiegratz 87512028d9 feat(stmt)!: mark statement as non-exhaustive
non-exhaustive avoids future breaking changes when statements are added
by forcing users to add a wildcard arm to match on statement.
2024-10-21 12:14:13 +02:00
wiegratz a3209cb2c2 feat(stmt): implement flow statement 2024-10-21 10:55:09 +02:00
wiegratz 617b071330 fix: add doc comment and trait derive to counters 2024-10-19 16:38:47 +02:00
wiegratz 9f109c51e4 fix(stmt): fix named counter
BREAKING CHANGE: Use new enum variants of `Counter`: `Named` and `Anonymous`.
2024-10-06 18:14:32 +02:00
wiegratz 51ccf106da fix: store NfListObjects in heap
BREAKING CHANGE: With enum `NfObject::ListObject`, use `Box::new(NfListObject...)`.
2024-10-06 16:52:49 +02:00
wiegratz 290c5bbb0c docs: fix long doc comments in expr, stmt 2024-10-06 16:46:22 +02:00
wiegratz e13a5ed90d docs: add doc comments for tproxy 2024-07-08 18:51:50 +02:00
Zoruk a557c49936 feat: add support for tproxy 2024-06-25 13:39:58 +02:00
agarof 890098f034 Add common traits to basic enums 2024-04-26 22:09:51 +02:00
wiegratz 16d97fe736 add ruleset flush type 2024-03-16 15:05:41 +00:00
wiegratz 6fe77ae6c7 Merge branch 'args' 2024-03-16 14:59:56 +00:00
Paul Holzinger 6b135baa82 fix get_current_ruleset_raw args parsing
If users supply their own args do not append the list ruleset args.

Fixes #14

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-11 17:35:16 +01:00
Paul Holzinger 312a279fbb add inner header as payload base
nft can also use "ih" for inner header in the raw payload base, see
nft(8) for more info.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-11 16:40:28 +01:00
Paul Holzinger 0deaf7d864 fix payload raw expression
The current code cannot handle the ParloadRaw type, serde is unable to
deserialize the json with a raw payload fields.

This is because serde cannot handle two fields with the same name and I
haven't found a way to make this work like that. Instead we have to use
a untagged enum that share both possible payload values.

Therefore this is an API break: NamedExpression now only contains one
Payload field which contains the Payload enum, the old Payload type is
renamed to PayloadField.

Lastly, export the PayloadRaw fields so they can be used externally.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-11 16:35:25 +01:00
wiegratz 471e9fab77 schema: fix ct helper key name 2024-01-22 12:34:42 +01:00
wiegratz e5fb6d4e48 expr: fix sctp chunks
also adds space-keys test for all keys with spaces
2024-01-22 12:26:50 +01:00
wiegratz fd2bd09ad1 chore: fix clippy warnings 2024-01-22 11:56:31 +01:00
wiegratz 04e29d85e8 Merge branch 'add_missing_things' 2024-01-22 11:36:00 +01:00