I am testing NFtables and am attempting to set up a basic routing firewall on a linux machine with 2 interfaces, ens37 and ens38. Here is the ifconfig output for these 2 interfaces.
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.3 netmask 255.255.255.0 broadcast 192.168.0.255
ether 00:0c:29:74:33:e7 txqueuelen 1000 (Ethernet)
RX packets 20 bytes 2524 (2.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 156 bytes 9952 (9.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.4 netmask 255.255.255.0 broadcast 192.168.0.255
ether 00:0c:29:74:33:f1 txqueuelen 1000 (Ethernet)
RX packets 147 bytes 9340 (9.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 18 bytes 1672 (1.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
I am wanting to emulate ens38 being a WAN port, and block all non-lan-initiated traffic that is inbound, but allow LAN traffic outbound.
I have these rules set up in /etc/nftables.conf
:
#!/usr/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 LAN to firewall, disallow WAN to firewall
chain input {
type filter hook input priority 0; policy accept;
iifname "ens37" accept
iifname "ens38" drop
}
# 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 "ens37" oifname "ens38" accept
iifname "ens38" oifname "ens37" ct state related,established accept
}
}
To test if the rules are successful, I am setting up a listener with netcat:
nc -lp 80 -s 192.168.0.3
Then I connect from the other interface using netcat:
nc 192.168.0.3 80 -s 192.168.0.4
My issue is that these nftables rules are not blocking traffic from the emulated WAN port. The netcat connections work perfectly fine bidirectionally, which is not what I am looking for.
If I run nft list table filter
, I get the rules I am expecting to see as output.
I am new to nftables, how can I get these rules to run against these two interfaces correctly? What is wrong with my current approach?