Skip to content

Commit 282e5f8

Browse files
Florian Westphalummakynes
Florian Westphal
authored andcommitted
netfilter: nat: really support inet nat without l3 address
When no l3 address is given, priv->family is set to NFPROTO_INET and the evaluation function isn't called. Call it too so l4-only rewrite can work. Also add a test case for this. Fixes: a33f387 ("netfilter: nft_nat: allow to specify layer 4 protocol NAT only") Reported-by: Yi Chen <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 0a375c8 commit 282e5f8

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

net/netfilter/nft_nat.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ static void nft_nat_inet_eval(const struct nft_expr *expr,
335335
{
336336
const struct nft_nat *priv = nft_expr_priv(expr);
337337

338-
if (priv->family == nft_pf(pkt))
338+
if (priv->family == nft_pf(pkt) ||
339+
priv->family == NFPROTO_INET)
339340
nft_nat_eval(expr, regs, pkt);
340341
}
341342

tools/testing/selftests/netfilter/nft_nat.sh

+43
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,45 @@ EOF
374374
return $lret
375375
}
376376

377+
test_local_dnat_portonly()
378+
{
379+
local family=$1
380+
local daddr=$2
381+
local lret=0
382+
local sr_s
383+
local sr_r
384+
385+
ip netns exec "$ns0" nft -f /dev/stdin <<EOF
386+
table $family nat {
387+
chain output {
388+
type nat hook output priority 0; policy accept;
389+
meta l4proto tcp dnat to :2000
390+
391+
}
392+
}
393+
EOF
394+
if [ $? -ne 0 ]; then
395+
if [ $family = "inet" ];then
396+
echo "SKIP: inet port test"
397+
test_inet_nat=false
398+
return
399+
fi
400+
echo "SKIP: Could not add $family dnat hook"
401+
return
402+
fi
403+
404+
echo SERVER-$family | ip netns exec "$ns1" timeout 5 socat -u STDIN TCP-LISTEN:2000 &
405+
sc_s=$!
406+
407+
result=$(ip netns exec "$ns0" timeout 1 socat TCP:$daddr:2000 STDOUT)
408+
409+
if [ "$result" = "SERVER-inet" ];then
410+
echo "PASS: inet port rewrite without l3 address"
411+
else
412+
echo "ERROR: inet port rewrite"
413+
ret=1
414+
fi
415+
}
377416

378417
test_masquerade6()
379418
{
@@ -1148,6 +1187,10 @@ fi
11481187
reset_counters
11491188
test_local_dnat ip
11501189
test_local_dnat6 ip6
1190+
1191+
reset_counters
1192+
test_local_dnat_portonly inet 10.0.1.99
1193+
11511194
reset_counters
11521195
$test_inet_nat && test_local_dnat inet
11531196
$test_inet_nat && test_local_dnat6 inet

0 commit comments

Comments
 (0)