07.-Nftables Examples
07.-Nftables Examples
org/wiki/Nftables/Examples
(/) Wiki
Nftables/Examples
De Gentoo Wiki
< Nftables (/wiki/Nftables)
Saltar a:navegación Saltar a:buscar
On this page several example nftable configurations can be found. The first two examples are
skeletons to illustrate how nftables works. The third and fourth exmaple show how, using nftables,
rules can be simplified by combining IPv4 and IPv6 in the generic IP table 'inet'. The fifth example
shows how nftables can be combined with bash scripting.
Sumario
For forwarding between WAN and LAN to work, it needs to be enabled with:
1 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
#!/sbin/nft -f
flush ruleset
table ip filter {
# allow all packets sent by the firewall machine itself
chain output {
type filter hook output priority 100; policy accept;
}
# allow packets from LAN to WAN, and WAN to LAN if LAN initiated the connection
chain forward {
type filter hook forward priority 0; policy drop;
iifname "lan0" oifname "wan0" accept
iifname "wan0" oifname "lan0" ct state related,established accept
}
}
Basic NAT
The following is an example of nftables rules for setting up basic Network Address Translation (NAT)
using masquerade. If you have a static IP, it would be slightly faster to use source nat (SNAT) instead of
masquerade. This way the router would replace the source with a predefined IP, instead of looking up
the outgoing IP for every packet.
Note
masquerade is available in kernel 3.18 and up. When using NAT on kernels before 4.18, be sure to
unload or disable iptables NAT, as it will take precedence over nftables NAT.
FILE /etc/nftables/nftables_nat
2 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
#!/sbin/nft -f
flush ruleset
table ip nat {
chain prerouting {
type nat hook prerouting priority 0; policy accept;
}
# for all packets to WAN, after routing, replace source address with primary IP of
WAN interface
chain postrouting {
type nat hook postrouting priority 100; policy accept;
oifname "wan0" masquerade
}
}
Note
Generally, you should accept all ICMP. It is fundamental to various networking operations and
eases trouble shooting. Dropping them altogether can result in undesirable behavior.[1]
FILE /etc/nftables/nftables.rules
3 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
#!/sbin/nft -f
flush ruleset
chain forward {
type filter hook forward priority 0; policy drop;
counter comment "count dropped packets"
}
chain forward {
type filter hook forward priority 0; policy drop;
counter comment "count dropped packets"
}
4 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
Note
FILE /etc/nftables/nftables.rules
#!/sbin/nft -f
flush ruleset
chain forward {
type filter hook forward priority 0; policy drop;
counter comment "count dropped packets"
}
5 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
Note
Shell scripts break atomicity when applying the ruleset unless using nftables native scripting
environment. See Nftables Scripting (https://wiki.nftables.org/wiki-nftables/index.php/Scripting).
FILE /home/rt/scripts/nft.sh
6 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
#!/bin/bash
nft="/sbin/nft";
# ruleset, masquerade and full reject support are available starting with Linux Kernel
3.18
${nft} flush ruleset;
export LAN_IN=enp3s6
export LAN_ML=enp2s0
export WAN=ppp0
LAN_INLOCALNET=192.168.1.0/24
LAN_MLNET=10.52.0.0/14
MLIP=10.54.1.101
TORRENT_PORT_WAN=55414
TRACKER_TORRENT_PORT_WAN=4949
TORRENT_PORT_LAN=55413
MAC[2]=00:23:45:67:89:ab
...
MAC[20]=00:fe:dc:ba:98:76
${nft} -f /etc/nftables/ipv4-filter;
${nft} -f /etc/nftables/ipv4-nat;
# BANNED
${nft} add rule filter input meta iifname ${WAN} ip saddr 121.12.242.43 drop;
# Drop invalid
${nft} add rule filter input ct state invalid drop;
${nft} add rule filter input meta iif lo ct state new accept;
${nft} add rule filter input meta iif ${LAN_ML} ip saddr ${LAN_MLNET} ct state new acce
pt;
${nft} add rule filter input meta iif ${LAN_IN} ip saddr ${LAN_INLOCALNET} ct state new
accept;
${nft} add rule filter input meta iifname ${WAN} ip protocol tcp ct state new tcp dport
80 accept;
7 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
dnat 192.168.1.10:${TORRENT_PORT_LAN}
${nft} add rule filter forward meta iifname ${WAN} meta oif ${LAN_IN} ip daddr 192.168.
1.10 \
tcp dport ${TORRENT_PORT_LAN} ct state new accept;
${nft} add rule filter input ip saddr != ${LAN_INLOCALNET} ct state new drop;
${nft} add rule filter forward meta iif ${LAN_ML} ct state new drop;
${nft} add rule filter forward meta iifname ${WAN} ct state new drop;
${nft} add rule nat postrouting oif ${LAN_ML} ip saddr ${LAN_INLOCALNET} snat ${MLIP};
${nft} add rule nat postrouting oifname ${WAN} ip saddr ${LAN_INLOCALNET} masquerade;
# Policies
${nft} add rule filter input drop;
${nft} add rule filter forward drop;
${nft} add rule filter output accept;
/etc/init.d/nftables save;
References
1. http://shouldiblockicmp.com/ (http://shouldiblockicmp.com/)
Obtenido de «https://wiki.gentoo.org/index.php?title=Nftables/Examples&oldid=927825
(https://wiki.gentoo.org/index.php?title=Nftables/Examples&oldid=927825)»
▪ Esta página se editó por última vez el 4 mar 2021 a las 19:33.
8 de 9 15/03/2023, 10:28
Nftables/Examples - Gentoo Wiki https://wiki.gentoo.org/wiki/Nftables/Examples
9 de 9 15/03/2023, 10:28