3 # This test is for basic NAT functionality: snat, dnat, redirect, masquerade.
6 # Kselftest framework requirement - SKIP code is 4.
11 sfx=$(mktemp -u "XXXXXXXX")
18 for i in 0 1 2; do ip netns del ns$i-"$sfx";done
21 nft --version > /dev/null 2>&1
23 echo "SKIP: Could not run test without nft tool"
27 ip -Version > /dev/null 2>&1
29 echo "SKIP: Could not run test without ip tool"
35 echo "SKIP: Could not create net namespace $ns0"
43 echo "SKIP: Could not create net namespace $ns1"
49 echo "SKIP: Could not create net namespace $ns2"
53 ip link add veth0 netns "$ns0" type veth peer name eth0 netns "$ns1" > /dev/null 2>&1
55 echo "SKIP: No virtual ethernet pair device support in kernel"
58 ip link add veth1 netns "$ns0" type veth peer name eth0 netns "$ns2"
60 ip -net "$ns0" link set lo up
61 ip -net "$ns0" link set veth0 up
62 ip -net "$ns0" addr add 10.0.1.1/24 dev veth0
63 ip -net "$ns0" addr add dead:1::1/64 dev veth0
65 ip -net "$ns0" link set veth1 up
66 ip -net "$ns0" addr add 10.0.2.1/24 dev veth1
67 ip -net "$ns0" addr add dead:2::1/64 dev veth1
70 ip -net ns$i-$sfx link set lo up
71 ip -net ns$i-$sfx link set eth0 up
72 ip -net ns$i-$sfx addr add 10.0.$i.99/24 dev eth0
73 ip -net ns$i-$sfx route add default via 10.0.$i.1
74 ip -net ns$i-$sfx addr add dead:$i::99/64 dev eth0
75 ip -net ns$i-$sfx route add default via dead:$i::1
85 echo "ERROR: $counter counter in $ns has unexpected value (expected $expect) at $tag" 1>&2
86 ip netns exec $ns nft list counter inet filter $counter 1>&2
94 cnt=$(ip netns exec $ns nft list counter inet filter ns0in | grep -q "packets 1 bytes 84")
96 bad_counter $ns ns0in "packets 1 bytes 84" "check_counters 1"
99 cnt=$(ip netns exec $ns nft list counter inet filter ns0out | grep -q "packets 1 bytes 84")
100 if [ $? -ne 0 ]; then
101 bad_counter $ns ns0out "packets 1 bytes 84" "check_counters 2"
105 expect="packets 1 bytes 104"
106 cnt=$(ip netns exec $ns nft list counter inet filter ns0in6 | grep -q "$expect")
107 if [ $? -ne 0 ]; then
108 bad_counter $ns ns0in6 "$expect" "check_counters 3"
111 cnt=$(ip netns exec $ns nft list counter inet filter ns0out6 | grep -q "$expect")
112 if [ $? -ne 0 ]; then
113 bad_counter $ns ns0out6 "$expect" "check_counters 4"
125 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns0in | grep -q "packets 0 bytes 0")
126 if [ $? -ne 0 ]; then
127 bad_counter "$ns0" ns0in "packets 0 bytes 0" "check_ns0_counters 1"
131 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns0in6 | grep -q "packets 0 bytes 0")
132 if [ $? -ne 0 ]; then
133 bad_counter "$ns0" ns0in6 "packets 0 bytes 0"
137 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns0out | grep -q "packets 0 bytes 0")
138 if [ $? -ne 0 ]; then
139 bad_counter "$ns0" ns0out "packets 0 bytes 0" "check_ns0_counters 2"
142 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns0out6 | grep -q "packets 0 bytes 0")
143 if [ $? -ne 0 ]; then
144 bad_counter "$ns0" ns0out6 "packets 0 bytes 0" "check_ns0_counters3 "
148 for dir in "in" "out" ; do
149 expect="packets 1 bytes 84"
150 cnt=$(ip netns exec "$ns0" nft list counter inet filter ${ns}${dir} | grep -q "$expect")
151 if [ $? -ne 0 ]; then
152 bad_counter "$ns0" $ns$dir "$expect" "check_ns0_counters 4"
156 expect="packets 1 bytes 104"
157 cnt=$(ip netns exec "$ns0" nft list counter inet filter ${ns}${dir}6 | grep -q "$expect")
158 if [ $? -ne 0 ]; then
159 bad_counter "$ns0" $ns$dir6 "$expect" "check_ns0_counters 5"
170 ip netns exec ns$i-$sfx nft reset counters inet > /dev/null
180 if [ $family = "inet" ];then
184 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
187 type nat hook output priority 0; policy accept;
188 ip6 daddr dead:1::99 dnat $IPF to dead:2::99
192 if [ $? -ne 0 ]; then
193 echo "SKIP: Could not add add $family dnat hook"
197 # ping netns1, expect rewrite to netns2
198 ip netns exec "$ns0" ping -q -c 1 dead:1::99 > /dev/null
199 if [ $? -ne 0 ]; then
201 echo "ERROR: ping6 failed"
205 expect="packets 0 bytes 0"
206 for dir in "in6" "out6" ; do
207 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns1${dir} | grep -q "$expect")
208 if [ $? -ne 0 ]; then
209 bad_counter "$ns0" ns1$dir "$expect" "test_local_dnat6 1"
214 expect="packets 1 bytes 104"
215 for dir in "in6" "out6" ; do
216 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns2${dir} | grep -q "$expect")
217 if [ $? -ne 0 ]; then
218 bad_counter "$ns0" ns2$dir "$expect" "test_local_dnat6 2"
223 # expect 0 count in ns1
224 expect="packets 0 bytes 0"
225 for dir in "in6" "out6" ; do
226 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns0${dir} | grep -q "$expect")
227 if [ $? -ne 0 ]; then
228 bad_counter "$ns1" ns0$dir "$expect" "test_local_dnat6 3"
233 # expect 1 packet in ns2
234 expect="packets 1 bytes 104"
235 for dir in "in6" "out6" ; do
236 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns0${dir} | grep -q "$expect")
237 if [ $? -ne 0 ]; then
238 bad_counter "$ns2" ns0$dir "$expect" "test_local_dnat6 4"
243 test $lret -eq 0 && echo "PASS: ipv6 ping to $ns1 was $family NATted to $ns2"
244 ip netns exec "$ns0" nft flush chain ip6 nat output
255 if [ $family = "inet" ];then
259 ip netns exec "$ns0" nft -f /dev/stdin <<EOF 2>/dev/null
262 type nat hook output priority 0; policy accept;
263 ip daddr 10.0.1.99 dnat $IPF to 10.0.2.99
267 if [ $? -ne 0 ]; then
268 if [ $family = "inet" ];then
269 echo "SKIP: inet nat tests"
273 echo "SKIP: Could not add add $family dnat hook"
277 # ping netns1, expect rewrite to netns2
278 ip netns exec "$ns0" ping -q -c 1 10.0.1.99 > /dev/null
279 if [ $? -ne 0 ]; then
281 echo "ERROR: ping failed"
285 expect="packets 0 bytes 0"
286 for dir in "in" "out" ; do
287 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns1${dir} | grep -q "$expect")
288 if [ $? -ne 0 ]; then
289 bad_counter "$ns0" ns1$dir "$expect" "test_local_dnat 1"
294 expect="packets 1 bytes 84"
295 for dir in "in" "out" ; do
296 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns2${dir} | grep -q "$expect")
297 if [ $? -ne 0 ]; then
298 bad_counter "$ns0" ns2$dir "$expect" "test_local_dnat 2"
303 # expect 0 count in ns1
304 expect="packets 0 bytes 0"
305 for dir in "in" "out" ; do
306 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns0${dir} | grep -q "$expect")
307 if [ $? -ne 0 ]; then
308 bad_counter "$ns1" ns0$dir "$expect" "test_local_dnat 3"
313 # expect 1 packet in ns2
314 expect="packets 1 bytes 84"
315 for dir in "in" "out" ; do
316 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns0${dir} | grep -q "$expect")
317 if [ $? -ne 0 ]; then
318 bad_counter "$ns2" ns0$dir "$expect" "test_local_dnat 4"
323 test $lret -eq 0 && echo "PASS: ping to $ns1 was $family NATted to $ns2"
325 ip netns exec "$ns0" nft flush chain $family nat output
328 ip netns exec "$ns0" ping -q -c 1 10.0.1.99 > /dev/null
329 if [ $? -ne 0 ]; then
331 echo "ERROR: ping failed"
335 expect="packets 1 bytes 84"
336 for dir in "in" "out" ; do
337 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns1${dir} | grep -q "$expect")
338 if [ $? -ne 0 ]; then
339 bad_counter "$ns1" ns1$dir "$expect" "test_local_dnat 5"
343 expect="packets 0 bytes 0"
344 for dir in "in" "out" ; do
345 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns2${dir} | grep -q "$expect")
346 if [ $? -ne 0 ]; then
347 bad_counter "$ns0" ns2$dir "$expect" "test_local_dnat 6"
352 # expect 1 count in ns1
353 expect="packets 1 bytes 84"
354 for dir in "in" "out" ; do
355 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns0${dir} | grep -q "$expect")
356 if [ $? -ne 0 ]; then
357 bad_counter "$ns0" ns0$dir "$expect" "test_local_dnat 7"
362 # expect 0 packet in ns2
363 expect="packets 0 bytes 0"
364 for dir in "in" "out" ; do
365 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns0${dir} | grep -q "$expect")
366 if [ $? -ne 0 ]; then
367 bad_counter "$ns2" ns0$dir "$expect" "test_local_dnat 8"
372 test $lret -eq 0 && echo "PASS: ping to $ns1 OK after $family nat output chain flush"
384 ip netns exec "$ns0" sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
386 ip netns exec "$ns2" ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
387 if [ $? -ne 0 ] ; then
388 echo "ERROR: cannot ping $ns1 from $ns2 via ipv6"
393 expect="packets 1 bytes 104"
394 for dir in "in6" "out6" ; do
395 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
396 if [ $? -ne 0 ]; then
397 bad_counter "$ns1" ns2$dir "$expect" "test_masquerade6 1"
401 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
402 if [ $? -ne 0 ]; then
403 bad_counter "$ns2" ns1$dir "$expect" "test_masquerade6 2"
410 # add masquerading rule
411 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
414 type nat hook postrouting priority 0; policy accept;
415 meta oif veth0 masquerade $natflags
419 if [ $? -ne 0 ]; then
420 echo "SKIP: Could not add add $family masquerade hook"
424 ip netns exec "$ns2" ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
425 if [ $? -ne 0 ] ; then
426 echo "ERROR: cannot ping $ns1 from $ns2 with active $family masquerade $natflags"
430 # ns1 should have seen packets from ns0, due to masquerade
431 expect="packets 1 bytes 104"
432 for dir in "in6" "out6" ; do
433 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns0${dir} | grep -q "$expect")
434 if [ $? -ne 0 ]; then
435 bad_counter "$ns1" ns0$dir "$expect" "test_masquerade6 3"
439 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
440 if [ $? -ne 0 ]; then
441 bad_counter "$ns2" ns1$dir "$expect" "test_masquerade6 4"
446 # ns1 should not have seen packets from ns2, due to masquerade
447 expect="packets 0 bytes 0"
448 for dir in "in6" "out6" ; do
449 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
450 if [ $? -ne 0 ]; then
451 bad_counter "$ns1" ns0$dir "$expect" "test_masquerade6 5"
455 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns1${dir} | grep -q "$expect")
456 if [ $? -ne 0 ]; then
457 bad_counter "$ns0" ns1$dir "$expect" "test_masquerade6 6"
462 ip netns exec "$ns2" ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
463 if [ $? -ne 0 ] ; then
464 echo "ERROR: cannot ping $ns1 from $ns2 with active ipv6 masquerade $natflags (attempt 2)"
468 ip netns exec "$ns0" nft flush chain $family nat postrouting
469 if [ $? -ne 0 ]; then
470 echo "ERROR: Could not flush $family nat postrouting" 1>&2
474 test $lret -eq 0 && echo "PASS: $family IPv6 masquerade $natflags for $ns2"
485 ip netns exec "$ns0" sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
486 ip netns exec "$ns0" sysctl net.ipv4.conf.veth1.forwarding=1 > /dev/null
488 ip netns exec "$ns2" ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
489 if [ $? -ne 0 ] ; then
490 echo "ERROR: cannot ping $ns1 from "$ns2" $natflags"
494 expect="packets 1 bytes 84"
495 for dir in "in" "out" ; do
496 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
497 if [ $? -ne 0 ]; then
498 bad_counter "$ns1" ns2$dir "$expect" "test_masquerade 1"
502 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
503 if [ $? -ne 0 ]; then
504 bad_counter "$ns2" ns1$dir "$expect" "test_masquerade 2"
511 # add masquerading rule
512 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
515 type nat hook postrouting priority 0; policy accept;
516 meta oif veth0 masquerade $natflags
520 if [ $? -ne 0 ]; then
521 echo "SKIP: Could not add add $family masquerade hook"
525 ip netns exec "$ns2" ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
526 if [ $? -ne 0 ] ; then
527 echo "ERROR: cannot ping $ns1 from $ns2 with active $family masquerade $natflags"
531 # ns1 should have seen packets from ns0, due to masquerade
532 expect="packets 1 bytes 84"
533 for dir in "in" "out" ; do
534 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns0${dir} | grep -q "$expect")
535 if [ $? -ne 0 ]; then
536 bad_counter "$ns1" ns0$dir "$expect" "test_masquerade 3"
540 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
541 if [ $? -ne 0 ]; then
542 bad_counter "$ns2" ns1$dir "$expect" "test_masquerade 4"
547 # ns1 should not have seen packets from ns2, due to masquerade
548 expect="packets 0 bytes 0"
549 for dir in "in" "out" ; do
550 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
551 if [ $? -ne 0 ]; then
552 bad_counter "$ns1" ns0$dir "$expect" "test_masquerade 5"
556 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns1${dir} | grep -q "$expect")
557 if [ $? -ne 0 ]; then
558 bad_counter "$ns0" ns1$dir "$expect" "test_masquerade 6"
563 ip netns exec "$ns2" ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
564 if [ $? -ne 0 ] ; then
565 echo "ERROR: cannot ping $ns1 from $ns2 with active ip masquerade $natflags (attempt 2)"
569 ip netns exec "$ns0" nft flush chain $family nat postrouting
570 if [ $? -ne 0 ]; then
571 echo "ERROR: Could not flush $family nat postrouting" 1>&2
575 test $lret -eq 0 && echo "PASS: $family IP masquerade $natflags for $ns2"
585 ip netns exec "$ns0" sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
587 ip netns exec "$ns2" ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
588 if [ $? -ne 0 ] ; then
589 echo "ERROR: cannnot ping $ns1 from $ns2 via ipv6"
593 expect="packets 1 bytes 104"
594 for dir in "in6" "out6" ; do
595 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
596 if [ $? -ne 0 ]; then
597 bad_counter "$ns1" ns2$dir "$expect" "test_redirect6 1"
601 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
602 if [ $? -ne 0 ]; then
603 bad_counter "$ns2" ns1$dir "$expect" "test_redirect6 2"
611 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
614 type nat hook prerouting priority 0; policy accept;
615 meta iif veth1 meta l4proto icmpv6 ip6 saddr dead:2::99 ip6 daddr dead:1::99 redirect
619 if [ $? -ne 0 ]; then
620 echo "SKIP: Could not add add $family redirect hook"
624 ip netns exec "$ns2" ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
625 if [ $? -ne 0 ] ; then
626 echo "ERROR: cannot ping $ns1 from $ns2 via ipv6 with active $family redirect"
630 # ns1 should have seen no packets from ns2, due to redirection
631 expect="packets 0 bytes 0"
632 for dir in "in6" "out6" ; do
633 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
634 if [ $? -ne 0 ]; then
635 bad_counter "$ns1" ns0$dir "$expect" "test_redirect6 3"
640 # ns0 should have seen packets from ns2, due to masquerade
641 expect="packets 1 bytes 104"
642 for dir in "in6" "out6" ; do
643 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns2${dir} | grep -q "$expect")
644 if [ $? -ne 0 ]; then
645 bad_counter "$ns1" ns0$dir "$expect" "test_redirect6 4"
650 ip netns exec "$ns0" nft delete table $family nat
651 if [ $? -ne 0 ]; then
652 echo "ERROR: Could not delete $family nat table" 1>&2
656 test $lret -eq 0 && echo "PASS: $family IPv6 redirection for $ns2"
666 ip netns exec "$ns0" sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
667 ip netns exec "$ns0" sysctl net.ipv4.conf.veth1.forwarding=1 > /dev/null
669 ip netns exec "$ns2" ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
670 if [ $? -ne 0 ] ; then
671 echo "ERROR: cannot ping $ns1 from $ns2"
675 expect="packets 1 bytes 84"
676 for dir in "in" "out" ; do
677 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
678 if [ $? -ne 0 ]; then
679 bad_counter "$ns1" $ns2$dir "$expect" "test_redirect 1"
683 cnt=$(ip netns exec "$ns2" nft list counter inet filter ns1${dir} | grep -q "$expect")
684 if [ $? -ne 0 ]; then
685 bad_counter "$ns2" ns1$dir "$expect" "test_redirect 2"
693 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
696 type nat hook prerouting priority 0; policy accept;
697 meta iif veth1 ip protocol icmp ip saddr 10.0.2.99 ip daddr 10.0.1.99 redirect
701 if [ $? -ne 0 ]; then
702 echo "SKIP: Could not add add $family redirect hook"
706 ip netns exec "$ns2" ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
707 if [ $? -ne 0 ] ; then
708 echo "ERROR: cannot ping $ns1 from $ns2 with active $family ip redirect"
712 # ns1 should have seen no packets from ns2, due to redirection
713 expect="packets 0 bytes 0"
714 for dir in "in" "out" ; do
716 cnt=$(ip netns exec "$ns1" nft list counter inet filter ns2${dir} | grep -q "$expect")
717 if [ $? -ne 0 ]; then
718 bad_counter "$ns1" ns0$dir "$expect" "test_redirect 3"
723 # ns0 should have seen packets from ns2, due to masquerade
724 expect="packets 1 bytes 84"
725 for dir in "in" "out" ; do
726 cnt=$(ip netns exec "$ns0" nft list counter inet filter ns2${dir} | grep -q "$expect")
727 if [ $? -ne 0 ]; then
728 bad_counter "$ns0" ns0$dir "$expect" "test_redirect 4"
733 ip netns exec "$ns0" nft delete table $family nat
734 if [ $? -ne 0 ]; then
735 echo "ERROR: Could not delete $family nat table" 1>&2
739 test $lret -eq 0 && echo "PASS: $family IP redirection for $ns2"
744 # test port shadowing.
745 # create two listening services, one on router (ns0), one
746 # on client (ns2), which is masqueraded from ns1 point of view.
747 # ns2 sends udp packet coming from service port to ns1, on a highport.
748 # Later, if n1 uses same highport to connect to ns0:service, packet
749 # might be port-forwarded to ns2 instead.
751 # second argument tells if we expect the 'fake-entry' to take effect
752 # (CLIENT) or not (ROUTER).
757 local daddrc="10.0.1.99"
758 local daddrs="10.0.1.1"
762 echo ROUTER | ip netns exec "$ns0" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
765 echo CLIENT | ip netns exec "$ns2" nc -w 5 -u -l -p 1405 >/dev/null 2>&1 &
768 # make shadow entry, from client (ns2), going to (ns1), port 41404, sport 1405.
769 echo "fake-entry" | ip netns exec "$ns2" nc -w 1 -p 1405 -u "$daddrc" 41404 > /dev/null
771 # ns1 tries to connect to ns0:1405. With default settings this should connect
772 # to client, it matches the conntrack entry created above.
774 result=$(echo "" | ip netns exec "$ns1" nc -w 1 -p 41404 -u "$daddrs" 1405)
776 if [ "$result" = "$expect" ] ;then
777 echo "PASS: portshadow test $test: got reply from ${expect}${logmsg}"
779 echo "ERROR: portshadow test $test: got reply from \"$result\", not $expect as intended"
783 kill $nc_r $nc_c 2>/dev/null
785 # flush udp entries for next test round, if any
786 ip netns exec "$ns0" conntrack -F >/dev/null 2>&1
789 # This prevents port shadow of router service via packet filter,
790 # packets claiming to originate from service port from internal
791 # network are dropped.
792 test_port_shadow_filter()
796 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
797 table $family filter {
799 type filter hook forward priority 0; policy accept;
800 meta iif veth1 udp sport 1405 drop
804 test_port_shadow "port-filter" "ROUTER"
806 ip netns exec "$ns0" nft delete table $family filter
809 # This prevents port shadow of router service via notrack.
810 test_port_shadow_notrack()
814 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
817 type filter hook prerouting priority -300; policy accept;
818 meta iif veth0 udp dport 1405 notrack
819 udp dport 1405 notrack
822 type filter hook output priority -300; policy accept;
823 udp sport 1405 notrack
827 test_port_shadow "port-notrack" "ROUTER"
829 ip netns exec "$ns0" nft delete table $family raw
832 # This prevents port shadow of router service via sport remap.
833 test_port_shadow_pat()
837 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
840 type nat hook postrouting priority -1; policy accept;
841 meta iif veth1 udp sport <= 1405 masquerade to : 1406-65535 random
845 test_port_shadow "pat" "ROUTER"
847 ip netns exec "$ns0" nft delete table $family pat
850 test_port_shadowing()
854 ip netns exec "$ns0" sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
855 ip netns exec "$ns0" sysctl net.ipv4.conf.veth1.forwarding=1 > /dev/null
857 ip netns exec "$ns0" nft -f /dev/stdin <<EOF
860 type nat hook postrouting priority 0; policy accept;
861 meta oif veth0 masquerade
865 if [ $? -ne 0 ]; then
866 echo "SKIP: Could not add add $family masquerade hook"
870 # test default behaviour. Packet from ns1 to ns0 is redirected to ns2.
871 test_port_shadow "default" "CLIENT"
873 # test packet filter based mitigation: prevent forwarding of
874 # packets claiming to come from the service port.
875 test_port_shadow_filter "$family"
877 # test conntrack based mitigation: connections going or coming
878 # from router:service bypass connection tracking.
879 test_port_shadow_notrack "$family"
881 # test nat based mitigation: fowarded packets coming from service port
882 # are masqueraded with random highport.
883 test_port_shadow_pat "$family"
885 ip netns exec "$ns0" nft delete table $family nat
888 # ip netns exec "$ns0" ping -c 1 -q 10.0.$i.99
890 ip netns exec ns$i-$sfx nft -f /dev/stdin <<EOF
909 type ipv4_addr : counter
910 elements = { 10.0.1.1 : "ns0in",
913 10.0.2.99 : "ns2in" }
917 type ipv6_addr : counter
918 elements = { dead:1::1 : "ns0in6",
919 dead:2::1 : "ns0in6",
920 dead:1::99 : "ns1in6",
921 dead:2::99 : "ns2in6" }
925 type ipv4_addr : counter
926 elements = { 10.0.1.1 : "ns0out",
929 10.0.2.99: "ns2out" }
933 type ipv6_addr : counter
934 elements = { dead:1::1 : "ns0out6",
935 dead:2::1 : "ns0out6",
936 dead:1::99 : "ns1out6",
937 dead:2::99 : "ns2out6" }
941 type filter hook input priority 0; policy accept;
942 counter name ip saddr map @nsincounter
943 icmpv6 type { "echo-request", "echo-reply" } counter name ip6 saddr map @nsincounter6
946 type filter hook output priority 0; policy accept;
947 counter name ip daddr map @nsoutcounter
948 icmpv6 type { "echo-request", "echo-reply" } counter name ip6 daddr map @nsoutcounter6
955 # test basic connectivity
957 ip netns exec "$ns0" ping -c 1 -q 10.0.$i.99 > /dev/null
959 echo "ERROR: Could not reach other namespace(s)" 1>&2
963 ip netns exec "$ns0" ping -c 1 -q dead:$i::99 > /dev/null
965 echo "ERROR: Could not reach other namespace(s) via ipv6" 1>&2
968 check_counters ns$i-$sfx
969 if [ $? -ne 0 ]; then
973 check_ns0_counters ns$i
974 if [ $? -ne 0 ]; then
980 if [ $ret -eq 0 ];then
981 echo "PASS: netns routing/connectivity: $ns0 can reach $ns1 and $ns2"
988 $test_inet_nat && test_local_dnat inet
989 $test_inet_nat && test_local_dnat6 inet
991 for flags in "" "fully-random"; do
993 test_masquerade ip $flags
994 test_masquerade6 ip6 $flags
996 $test_inet_nat && test_masquerade inet $flags
997 $test_inet_nat && test_masquerade6 inet $flags
1004 $test_inet_nat && test_redirect inet
1005 $test_inet_nat && test_redirect6 inet
1009 if [ $ret -ne 0 ];then