Skip to content

Add support for FIB rule messages#310

Merged
jbaublitz merged 1 commit intojbaublitz:mainfrom
alexsikomas:main
Feb 5, 2026
Merged

Add support for FIB rule messages#310
jbaublitz merged 1 commit intojbaublitz:mainfrom
alexsikomas:main

Conversation

@alexsikomas
Copy link
Contributor

This PR adds support for managing the routing policy database through FIB rule messages, enabling the creation, deletion, and querying of ip rules.

Changes:

  • Fibmsg struct: A message struct that corresponds to the fib_rule_hdr in the Linux kernel.
  • Frattr enum: Netlink attributes for a FIB rule.
  • FrAct enum: Possible actions for a FIB rule.
  • Frf flags: Possible flags associated with a FIB rule
  • fwmark.rs example: An example is included to demonstrate how to create and delete a firewall mark-based routing rule. It shows how to build a Fibmsg, send it to the kernel, handle the acknowledgment, and then send a corresponding delete request.

The new functionality can be tested by running the example:

# Requires network capabilities or root privileges
sudo cargo run --example fwmark

or

cargo build --example fwmark
sudo ./target/debug/examples/fwmark

When run, the example will:

  1. Create a new rule: [PRIORITY]: not from all fwmark 0xca6c lookup 246813579
  2. Print a success message.
  3. Delete the rule.

You can comment out the deletion part in examples/fwmark.rs and verify the rule's creation with ip rule show.

@jbaublitz
Copy link
Owner

Hi @alexsikomas, thanks for the contribution. Can you provide more information on where these constants are defined?

@alexsikomas
Copy link
Contributor Author

Hi, the definitions for these constants can be found here: include/uapi/linux/fib_rules.h

@jbaublitz
Copy link
Owner

@alexsikomas Thanks for providing the link to the constants. I wanted to check if it's covered in linux-sys-raw. It appears that it is not currently. If you could submit a PR to linux-sys-raw or libc to have the constants automatically generated from there, I would be willing to accept this PR. As it stands, hardcoding constants often causes breakage, so I'd prefer to have it generated from C headers by another crate.

@alexsikomas
Copy link
Contributor Author

Sounds good, @jbaublitz I'll make a PR to libc.

@jbaublitz
Copy link
Owner

@alexsikomas Just be aware that if libc doesn't want to add it, linux-raw-sys may be willing to. There have been some constants that libc has avoided adding related to netlink.

@alexsikomas
Copy link
Contributor Author

I've changed the hardcoded constants to rely on linux-raw-sys, which has support for fib-rules.h as of 0.12.1.

Copy link
Owner

@jbaublitz jbaublitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one request and then I'll merge this. Also please rebase onto the current branch.

@@ -1,3 +1,4 @@
use linux_raw_sys::netlink::*;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this file, I think it would be better to use the namespace in the path for the constants to avoid collisions as we're ultimately using two separate libraries now to supply constants.

Introduced linux-raw-sys to pull constants for supporting
FIB rules. Added relavant constants and structures from the
Linux UAPI.

Added an example fwmark.
@alexsikomas
Copy link
Contributor Author

I've made the requested changes, removing the wildcard import, and rebased onto the current branch.

@alexsikomas alexsikomas requested a review from jbaublitz January 28, 2026 19:44
Copy link
Owner

@jbaublitz jbaublitz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexsikomas I've fully reviewed the code and it all looks good and consistent with the style of neli. Thanks for that! The one concern is that I can't find the definition of fibmsg anywhere in the kernel or kernel headers based on a search. Can you link me to the place you're seeing this data structure defined?

@jbaublitz
Copy link
Owner

Okay I found the definition. The naming was a little bit off so it was not easy to immediately find but I believe this is fine. I'm going to merge this now, but if you can follow up and explain your usage of libc::u_char instead of u8 as it is specified in the definition, that would be helpful. If I don't hear from you, I may change the type, but based on the C spec, they're equivalent anyway so it shouldn't be a problem.

@jbaublitz jbaublitz merged commit 22f8d01 into jbaublitz:main Feb 5, 2026
45 checks passed
@alexsikomas
Copy link
Contributor Author

You're right, u8 is the better type, I originally used u_char because I was just starting out with FFI and thought that it was the 'more correct' type to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants