66# Kselftest framework requirement - SKIP code is 4.
77ksft_skip=4
88ret=0
9+ test_inet_nat=true
910
1011nft --version > /dev/null 2>&1
1112if [ $? -ne 0 ]; then
@@ -141,17 +142,24 @@ reset_counters()
141142
142143test_local_dnat6 ()
143144{
145+ local family=$1
144146 local lret=0
147+ local IPF=" "
148+
149+ if [ $family = " inet" ]; then
150+ IPF=" ip6"
151+ fi
152+
145153ip netns exec ns0 nft -f - << EOF
146- table ip6 nat {
154+ table $family nat {
147155 chain output {
148156 type nat hook output priority 0; policy accept;
149- ip6 daddr dead:1::99 dnat to dead:2::99
157+ ip6 daddr dead:1::99 dnat $IPF to dead:2::99
150158 }
151159}
152160EOF
153161 if [ $? -ne 0 ]; then
154- echo " SKIP: Could not add add ip6 dnat hook"
162+ echo " SKIP: Could not add add $family dnat hook"
155163 return $ksft_skip
156164 fi
157165
@@ -201,23 +209,40 @@ EOF
201209 fi
202210 done
203211
204- test $lret -eq 0 && echo " PASS: ipv6 ping to ns1 was NATted to ns2"
212+ test $lret -eq 0 && echo " PASS: ipv6 ping to ns1 was $family NATted to ns2"
205213 ip netns exec ns0 nft flush chain ip6 nat output
206214
207215 return $lret
208216}
209217
210218test_local_dnat ()
211219{
220+ local family=$1
212221 local lret=0
213- ip netns exec ns0 nft -f - << EOF
214- table ip nat {
222+ local IPF=" "
223+
224+ if [ $family = " inet" ]; then
225+ IPF=" ip"
226+ fi
227+
228+ ip netns exec ns0 nft -f - << EOF 2>/dev/null
229+ table $family nat {
215230 chain output {
216231 type nat hook output priority 0; policy accept;
217- ip daddr 10.0.1.99 dnat to 10.0.2.99
232+ ip daddr 10.0.1.99 dnat $IPF to 10.0.2.99
218233 }
219234}
220235EOF
236+ if [ $? -ne 0 ]; then
237+ if [ $family = " inet" ]; then
238+ echo " SKIP: inet nat tests"
239+ test_inet_nat=false
240+ return $ksft_skip
241+ fi
242+ echo " SKIP: Could not add add $family dnat hook"
243+ return $ksft_skip
244+ fi
245+
221246 # ping netns1, expect rewrite to netns2
222247 ip netns exec ns0 ping -q -c 1 10.0.1.99 > /dev/null
223248 if [ $? -ne 0 ]; then
264289 fi
265290 done
266291
267- test $lret -eq 0 && echo " PASS: ping to ns1 was NATted to ns2"
292+ test $lret -eq 0 && echo " PASS: ping to ns1 was $family NATted to ns2"
268293
269- ip netns exec ns0 nft flush chain ip nat output
294+ ip netns exec ns0 nft flush chain $family nat output
270295
271296 reset_counters
272297 ip netns exec ns0 ping -q -c 1 10.0.1.99 > /dev/null
@@ -313,14 +338,15 @@ EOF
313338 fi
314339 done
315340
316- test $lret -eq 0 && echo " PASS: ping to ns1 OK after nat output chain flush"
341+ test $lret -eq 0 && echo " PASS: ping to ns1 OK after $family nat output chain flush"
317342
318343 return $lret
319344}
320345
321346
322347test_masquerade6 ()
323348{
349+ local family=$1
324350 local lret=0
325351
326352 ip netns exec ns0 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
@@ -351,16 +377,21 @@ test_masquerade6()
351377
352378# add masquerading rule
353379ip netns exec ns0 nft -f - << EOF
354- table ip6 nat {
380+ table $family nat {
355381 chain postrouting {
356382 type nat hook postrouting priority 0; policy accept;
357383 meta oif veth0 masquerade
358384 }
359385}
360386EOF
387+ if [ $? -ne 0 ]; then
388+ echo " SKIP: Could not add add $family masquerade hook"
389+ return $ksft_skip
390+ fi
391+
361392 ip netns exec ns2 ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
362393 if [ $? -ne 0 ] ; then
363- echo " ERROR: cannot ping ns1 from ns2 with active ipv6 masquerading"
394+ echo " ERROR: cannot ping ns1 from ns2 with active $family masquerading"
364395 lret=1
365396 fi
366397
@@ -397,19 +428,20 @@ EOF
397428 fi
398429 done
399430
400- ip netns exec ns0 nft flush chain ip6 nat postrouting
431+ ip netns exec ns0 nft flush chain $family nat postrouting
401432 if [ $? -ne 0 ]; then
402- echo " ERROR: Could not flush ip6 nat postrouting" 1>&2
433+ echo " ERROR: Could not flush $family nat postrouting" 1>&2
403434 lret=1
404435 fi
405436
406- test $lret -eq 0 && echo " PASS: IPv6 masquerade for ns2"
437+ test $lret -eq 0 && echo " PASS: $family IPv6 masquerade for ns2"
407438
408439 return $lret
409440}
410441
411442test_masquerade ()
412443{
444+ local family=$1
413445 local lret=0
414446
415447 ip netns exec ns0 sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
@@ -440,16 +472,21 @@ test_masquerade()
440472
441473# add masquerading rule
442474ip netns exec ns0 nft -f - << EOF
443- table ip nat {
475+ table $family nat {
444476 chain postrouting {
445477 type nat hook postrouting priority 0; policy accept;
446478 meta oif veth0 masquerade
447479 }
448480}
449481EOF
482+ if [ $? -ne 0 ]; then
483+ echo " SKIP: Could not add add $family masquerade hook"
484+ return $ksft_skip
485+ fi
486+
450487 ip netns exec ns2 ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
451488 if [ $? -ne 0 ] ; then
452- echo " ERROR: cannot ping ns1 from ns2 with active ip masquerading"
489+ echo " ERROR: cannot ping ns1 from ns2 with active $family masquerading"
453490 lret=1
454491 fi
455492
@@ -485,19 +522,20 @@ EOF
485522 fi
486523 done
487524
488- ip netns exec ns0 nft flush chain ip nat postrouting
525+ ip netns exec ns0 nft flush chain $family nat postrouting
489526 if [ $? -ne 0 ]; then
490- echo " ERROR: Could not flush nat postrouting" 1>&2
527+ echo " ERROR: Could not flush $family nat postrouting" 1>&2
491528 lret=1
492529 fi
493530
494- test $lret -eq 0 && echo " PASS: IP masquerade for ns2"
531+ test $lret -eq 0 && echo " PASS: $family IP masquerade for ns2"
495532
496533 return $lret
497534}
498535
499536test_redirect6 ()
500537{
538+ local family=$1
501539 local lret=0
502540
503541 ip netns exec ns0 sysctl net.ipv6.conf.all.forwarding=1 > /dev/null
@@ -527,16 +565,21 @@ test_redirect6()
527565
528566# add redirect rule
529567ip netns exec ns0 nft -f - << EOF
530- table ip6 nat {
568+ table $family nat {
531569 chain prerouting {
532570 type nat hook prerouting priority 0; policy accept;
533571 meta iif veth1 meta l4proto icmpv6 ip6 saddr dead:2::99 ip6 daddr dead:1::99 redirect
534572 }
535573}
536574EOF
575+ if [ $? -ne 0 ]; then
576+ echo " SKIP: Could not add add $family redirect hook"
577+ return $ksft_skip
578+ fi
579+
537580 ip netns exec ns2 ping -q -c 1 dead:1::99 > /dev/null # ping ns2->ns1
538581 if [ $? -ne 0 ] ; then
539- echo " ERROR: cannot ping ns1 from ns2 with active ip6 redirect"
582+ echo " ERROR: cannot ping ns1 from ns2 via ipv6 with active $family redirect"
540583 lret=1
541584 fi
542585
@@ -560,19 +603,20 @@ EOF
560603 fi
561604 done
562605
563- ip netns exec ns0 nft delete table ip6 nat
606+ ip netns exec ns0 nft delete table $family nat
564607 if [ $? -ne 0 ]; then
565- echo " ERROR: Could not delete ip6 nat table" 1>&2
608+ echo " ERROR: Could not delete $family nat table" 1>&2
566609 lret=1
567610 fi
568611
569- test $lret -eq 0 && echo " PASS: IPv6 redirection for ns2"
612+ test $lret -eq 0 && echo " PASS: $family IPv6 redirection for ns2"
570613
571614 return $lret
572615}
573616
574617test_redirect ()
575618{
619+ local family=$1
576620 local lret=0
577621
578622 ip netns exec ns0 sysctl net.ipv4.conf.veth0.forwarding=1 > /dev/null
@@ -603,16 +647,21 @@ test_redirect()
603647
604648# add redirect rule
605649ip netns exec ns0 nft -f - << EOF
606- table ip nat {
650+ table $family nat {
607651 chain prerouting {
608652 type nat hook prerouting priority 0; policy accept;
609653 meta iif veth1 ip protocol icmp ip saddr 10.0.2.99 ip daddr 10.0.1.99 redirect
610654 }
611655}
612656EOF
657+ if [ $? -ne 0 ]; then
658+ echo " SKIP: Could not add add $family redirect hook"
659+ return $ksft_skip
660+ fi
661+
613662 ip netns exec ns2 ping -q -c 1 10.0.1.99 > /dev/null # ping ns2->ns1
614663 if [ $? -ne 0 ] ; then
615- echo " ERROR: cannot ping ns1 from ns2 with active ip redirect"
664+ echo " ERROR: cannot ping ns1 from ns2 with active $family ip redirect"
616665 lret=1
617666 fi
618667
@@ -637,13 +686,13 @@ EOF
637686 fi
638687 done
639688
640- ip netns exec ns0 nft delete table ip nat
689+ ip netns exec ns0 nft delete table $family nat
641690 if [ $? -ne 0 ]; then
642- echo " ERROR: Could not delete nat table" 1>&2
691+ echo " ERROR: Could not delete $family nat table" 1>&2
643692 lret=1
644693 fi
645694
646- test $lret -eq 0 && echo " PASS: IP redirection for ns2"
695+ test $lret -eq 0 && echo " PASS: $family IP redirection for ns2"
647696
648697 return $lret
649698}
@@ -746,16 +795,25 @@ if [ $ret -eq 0 ];then
746795fi
747796
748797reset_counters
749- test_local_dnat
750- test_local_dnat6
798+ test_local_dnat ip
799+ test_local_dnat6 ip6
800+ reset_counters
801+ $test_inet_nat && test_local_dnat inet
802+ $test_inet_nat && test_local_dnat6 inet
751803
752804reset_counters
753- test_masquerade
754- test_masquerade6
805+ test_masquerade ip
806+ test_masquerade6 ip6
807+ reset_counters
808+ $test_inet_nat && test_masquerade inet
809+ $test_inet_nat && test_masquerade6 inet
755810
756811reset_counters
757- test_redirect
758- test_redirect6
812+ test_redirect ip
813+ test_redirect6 ip6
814+ reset_counters
815+ $test_inet_nat && test_redirect inet
816+ $test_inet_nat && test_redirect6 inet
759817
760818for i in 0 1 2; do ip netns del ns$i ; done
761819
0 commit comments