#! /bin/bash -x
ip -all netns del
rmmod veth
ip netns add rf_r
ip netns add rf_c

ip link add veth_rf_s1 type veth peer name eth1 netns rf_r
ip link set veth_rf_s1 up
ip netns exec rf_r ip link set eth1 up
ip link add eth1 type veth peer name eth2 netns rf_r
ip link set eth1 netns rf_c
ip netns exec rf_r ip link set eth2 up
ip netns exec rf_c ip link set eth1 up

ip addr add 10.167.41.1/24 dev veth_rf_s1
ip netns exec rf_c ip -4 addr add 10.167.31.2/24 dev eth1
ip netns exec rf_r ip -4 addr add 10.167.41.254/24 dev eth1
ip netns exec rf_r ip -4 addr add 10.167.31.254/24 dev eth2
ip netns exec rf_r sysctl -w net.ipv4.ip_forward=1
ip netns exec rf_c ip route add 10.167.41.0/24 via 10.167.31.254 dev eth1
ip route add 10.167.31.0/24 via 10.167.41.254 dev veth_rf_s1

ip netns exec rf_c ping 10.167.41.1 -c1
ping 10.167.31.2 -c1


ip netns exec rf_r nft add table ip filter
ip netns exec rf_r nft add chain ip filter prerouting { type filter hook prerouting priority 0 \; }
ip netns exec rf_r nft add rule ip filter prerouting icmp type echo-request fib saddr . iif oif missing counter
ip netns exec rf_r nft add rule ip filter prerouting icmp type echo-request fib saddr . iif oif exists counter

ip netns exec rf_c ping 10.167.41.1 -c1
ip netns exec rf_r nft list ruleset
