Есть скрипт:
ip netns add pr1;
ip link add pr1if0 type veth peer name pr1if1;
ip link set dev pr1if1 netns pr1;
ip link set pr1if0 up;
ip route add local 172.20.1.0 dev pr1if0;
ip route add 172.20.1.0/31 dev pr1if0 src 172.20.1.0;
ip netns exec pr1 ip link set lo up;
ip netns exec pr1 ip link set dev pr1if1 up;
ip netns exec pr1 ip address add 172.20.1.1/31 dev pr1if1;
ip netns exec pr1 ip route add default via 172.20.1.0 dev pr1if1 src 172.20.1.1;
#test
ping 172.20.1.1 -c1;
ip netns exec pr1 ping 172.20.1.0 -c1;
nc -lp 9040 -v -w 2 &
ip netns exec pr1 nc -v 172.20.1.0 9040 -w 2;
#clear
iptables -tnat -F && iptables -tfilter -F && ip netns del pr1;
exit;
А теперь добавим правило iptables REDIRECT:
ip netns add pr1;
ip link add pr1if0 type veth peer name pr1if1;
ip link set dev pr1if1 netns pr1;
ip link set pr1if0 up;
ip route add local 172.20.1.0 dev pr1if0;
ip route add 172.20.1.0/31 dev pr1if0 src 172.20.1.0;
ip netns exec pr1 ip link set lo up;
ip netns exec pr1 ip link set dev pr1if1 up;
ip netns exec pr1 ip address add 172.20.1.1/31 dev pr1if1;
ip netns exec pr1 ip route add default via 172.20.1.0 dev pr1if1 src 172.20.1.1;
iptables -t nat -A PREROUTING -p tcp -i pr1if0 -j REDIRECT --to-port 9040; #!!!! То, что добавили !!!!!
#test
ping 172.20.1.1 -c1;
ip netns exec pr1 ping 172.20.1.0 -c1;
nc -lp 9040 -v -w 2 &
ip netns exec pr1 nc -v 172.20.1.0 9040 -w 2;
#clear
iptables -tnat -F && iptables -tfilter -F && ip netns del pr1;
exit;
Но если заменить «ip route add» на «ip address»:
ip netns add pr1;
ip link add pr1if0 type veth peer name pr1if1;
ip link set dev pr1if1 netns pr1;
ip link set pr1if0 up;
ip address add 172.20.1.0/31 dev pr1if0; #!!!! ВМЕСТО ip route add !!!!!
ip netns exec pr1 ip link set lo up;
ip netns exec pr1 ip link set dev pr1if1 up;
ip netns exec pr1 ip address add 172.20.1.1/31 dev pr1if1;
ip netns exec pr1 ip route add default via 172.20.1.0 dev pr1if1 src 172.20.1.1;
iptables -t nat -A PREROUTING -p tcp -i pr1if0 -j REDIRECT --to-port 9040;
#test
ping 172.20.1.1 -c1;
ip netns exec pr1 ping 172.20.1.0 -c1;
nc -lp 9040 -v -w 2 &
ip netns exec pr1 nc -v 172.20.1.0 9040 -w 2;
#clear
iptables -tnat -F && iptables -tfilter -F && ip netns del pr1;
exit;
Как так? Что произошло? Что творит ip address???