Nftables

Nftables is the framework for the packet filtering inside the linux kernel.

Tables

Tables are the top-level object inside the nft domain model, they belong to a specific family that defines what typologies of packets are targeted from the rules inside the table (for example ip family targets ipv4 packets)

To list all tables run:

nft list ruleset

To add a new table run:

nft add table [family] [name]

Chains

Chains are containers for rules, they are defined by a name, a type and a hook. A type describe witch operation on packet can be performed by the rule of a specific chain, possible values are:

The hook describe at which point of the packet processing pipeline the rule is applied, possible values are:

Each table family and each chain type implements a specific set of hooks, see this for reference

Rules

Rules are the effective operation done on packets based on filter and parameters, for a list of filters see this

mark packets for routing policy with different ip route tables

In order to route packets based on a specific mark configure ip as follows

ehco '[NUMBER] [NAME]'>> /etc/iproute2/rt_tables
ip rule add fwmark [MARK NUMBER] table [NAME]
nft add table [FAMILY] [NAME]
nft 'add chain [FAMILY] [TABLE] [CHAIN NAME] {type filter hook prerouting priority 0;} '

🔷 Note

In this case the type filter is used because the interested packets comes from another host and are not originated from this local machine, otherwise the type route is preferable

nft add rule [TABLE NAME] [CHAIN NAME] ip saddr 169.254.254.2/32 tcp dport 123 mark set 230

🔷 Note

In this case the filter targets ntp packets from 169.254.254.2

Table of Contents