diff --git a/doc/scapy/_static/vethrelay.sh b/doc/scapy/_static/vethrelay.sh index 0e62f7b4903..f9c6406b794 100755 --- a/doc/scapy/_static/vethrelay.sh +++ b/doc/scapy/_static/vethrelay.sh @@ -1,7 +1,8 @@ #!/bin/bash -# Setup iptables for IP relay by creating an interface configured -# to be the destination of TPROXY rules. +# Setup nftables for IP relay by creating an interface configured +# to be the destination of TPROXY rules. All nft rules live in a +# dedicated 'scapy-tproxy' table. if [ "$EUID" -ne 0 ] then echo "Please run as root" @@ -15,18 +16,19 @@ fi IFACE="vethrelay" IP="2.2.2.2" +NFT_TABLE="scapy-tproxy" # Linux doc about TPROXY and example regarding this: # https://www.kernel.org/doc/Documentation/networking/tproxy.txt # https://powerdns.org/tproxydoc/tproxy.md.html function checkSetup() { - iptables -t mangle -n --list "DIVERT" >/dev/null 2>&1 + nft list table ip "$NFT_TABLE" >/dev/null 2>&1 return $? } if [ "$1" == "setup" ]; then - # Add "DIVERT" chain if it doesn't exist + # Add the scapy-tproxy table if it doesn't exist checkSetup if [ $? -eq 0 ]; then echo "vethrelay already setup !" @@ -37,13 +39,18 @@ if [ "$1" == "setup" ]; then sysctl net.ipv6.conf.$IFACE.disable_ipv6=1 >/dev/null ip link set dev $IFACE up ip addr add dev $IFACE $IP/32 - # Create mangle "DIVERT" chain as an optimisation. -m socket matches - # packets from already established sockets. Those are marked as 1 then - # accepted directly. - iptables -t mangle -N DIVERT - iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT - iptables -t mangle -A DIVERT -j MARK --set-mark 1 - iptables -t mangle -A DIVERT -j ACCEPT + # All TPROXY rules live in a dedicated nftables table so that + # iptables-nft (which manages the ip filter/nat/mangle tables) + # is left untouched. + # The DIVERT chain is an optimisation. The socket match catches + # packets from already established sockets. Those are marked as 1 + # then accepted directly so that TPROXY does not run again. + nft "add table ip "$NFT_TABLE"" + nft "add chain ip "$NFT_TABLE" DIVERT" + nft "add chain ip "$NFT_TABLE" PREROUTING { type filter hook prerouting priority mangle; policy accept; }" + nft "add rule ip "$NFT_TABLE" PREROUTING ip protocol tcp socket wildcard 0 jump DIVERT" + nft "add rule ip "$NFT_TABLE" DIVERT meta mark set 0x1" + nft "add rule ip "$NFT_TABLE" DIVERT accept" # Packets marked with 1 are routed through table 100 instead of the # default routing table ip rule add fwmark 1 lookup 100 @@ -52,9 +59,12 @@ if [ "$1" == "setup" ]; then echo -e "\x1b[32mInterface $IFACE is now setup with IPv4: $IP !\x1b[0m\n" echo -e "Add listening rules as follow:\n" echo "# TPROXY incoming TCP packets on port 80 to $IFACE on port 8080" - echo "iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 8080 --on-ip $IP" + echo "nft add rule ip "$NFT_TABLE" PREROUTING tcp dport 80 meta mark set 0x1 tproxy ip to $IP:8080 accept" echo - echo "# Listen on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from)" + echo "# Note: you need to allow INPUT on the port that you are adding a listening rule on. For instance, to listen" + echo "# on wlp4s0 for incoming packets on port 80 (on the interface where it really comes from), one can do" + echo "nft add rule ip INPUT iifname