Skip to content

Commit b93394a

Browse files
committed
pf: fix 'natpass'
If an rdr (or nat) rule specifies 'pass' we don't run the filter rules, we just pass the traffic. Or at least, we did until that got unintentionally broken. Restore that behaviour and add a test case. While here also fix nat:dummynet_mask, which relied on the broken behaviour. MFC after: 3 days Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D52838
1 parent ad38f6a commit b93394a

File tree

3 files changed

+91
-28
lines changed

3 files changed

+91
-28
lines changed

sys/netpfil/pf/pf.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5965,37 +5965,42 @@ pf_test_rule(struct pf_krule **rm, struct pf_kstate **sm,
59655965
ctx.nat_pool = &(ctx.nr->rdr);
59665966
}
59675967

5968-
ruleset = &pf_main_ruleset;
5969-
rv = pf_match_rule(&ctx, ruleset, match_rules);
5970-
if (rv == PF_TEST_FAIL) {
5971-
/*
5972-
* Reason has been set in pf_match_rule() already.
5973-
*/
5974-
goto cleanup;
5975-
}
5976-
5977-
r = *ctx.rm; /* matching rule */
5978-
ctx.a = *ctx.am; /* rule that defines an anchor containing 'r' */
5979-
ruleset = *ctx.rsm; /* ruleset of the anchor defined by the rule 'a' */
5980-
ctx.aruleset = ctx.arsm; /* ruleset of the 'a' rule itself */
5968+
if (ctx.nr && ctx.nr->natpass) {
5969+
r = ctx.nr;
5970+
ruleset = *ctx.rsm;
5971+
} else {
5972+
ruleset = &pf_main_ruleset;
5973+
rv = pf_match_rule(&ctx, ruleset, match_rules);
5974+
if (rv == PF_TEST_FAIL) {
5975+
/*
5976+
* Reason has been set in pf_match_rule() already.
5977+
*/
5978+
goto cleanup;
5979+
}
59815980

5982-
REASON_SET(&ctx.reason, PFRES_MATCH);
5981+
r = *ctx.rm; /* matching rule */
5982+
ctx.a = *ctx.am; /* rule that defines an anchor containing 'r' */
5983+
ruleset = *ctx.rsm; /* ruleset of the anchor defined by the rule 'a' */
5984+
ctx.aruleset = ctx.arsm; /* ruleset of the 'a' rule itself */
59835985

5984-
/* apply actions for last matching pass/block rule */
5985-
pf_rule_to_actions(r, &pd->act);
5986-
transerror = pf_rule_apply_nat(&ctx, r);
5987-
switch (transerror) {
5988-
case PFRES_MATCH:
5989-
/* Translation action found in rule and applied successfully */
5990-
case PFRES_MAX:
5991-
/* No translation action found in rule */
5992-
break;
5993-
default:
5994-
/* Translation action found in rule but failed to apply */
5995-
REASON_SET(&ctx.reason, transerror);
5996-
goto cleanup;
5986+
/* apply actions for last matching pass/block rule */
5987+
pf_rule_to_actions(r, &pd->act);
5988+
transerror = pf_rule_apply_nat(&ctx, r);
5989+
switch (transerror) {
5990+
case PFRES_MATCH:
5991+
/* Translation action found in rule and applied successfully */
5992+
case PFRES_MAX:
5993+
/* No translation action found in rule */
5994+
break;
5995+
default:
5996+
/* Translation action found in rule but failed to apply */
5997+
REASON_SET(&ctx.reason, transerror);
5998+
goto cleanup;
5999+
}
59976000
}
59986001

6002+
REASON_SET(&ctx.reason, PFRES_MATCH);
6003+
59996004
if (r->log) {
60006005
if (ctx.rewrite)
60016006
m_copyback(pd->m, pd->off, pd->hdrlen, pd->hdr.any);

tests/sys/netpfil/pf/nat.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ dummynet_mask_body()
838838
jexec gw dnctl pipe 1 config delay 100 mask src-ip 0xffffff00
839839
jexec gw pfctl -e
840840
pft_set_rules gw \
841-
"nat pass on ${epair_srv}b inet from 192.0.2.0/24 to any -> (${epair_srv}b)" \
841+
"nat on ${epair_srv}b inet from 192.0.2.0/24 to any -> (${epair_srv}b)" \
842842
"pass out dnpipe 1"
843843

844844
atf_check -s exit:0 -o ignore \

tests/sys/netpfil/pf/rdr.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,66 @@ srcport_pass_cleanup()
281281
pft_cleanup
282282
}
283283

284+
atf_test_case "natpass" "cleanup"
285+
natpass_head()
286+
{
287+
atf_set descr 'Test rdr pass'
288+
atf_set require.user root
289+
}
290+
291+
natpass_body()
292+
{
293+
pft_init
294+
295+
epair=$(vnet_mkepair)
296+
epair_link=$(vnet_mkepair)
297+
298+
ifconfig ${epair}a 192.0.2.2/24 up
299+
300+
vnet_mkjail alcatraz ${epair}b ${epair_link}a
301+
jexec alcatraz ifconfig lo0 inet 127.0.0.1/8 up
302+
jexec alcatraz ifconfig ${epair}b inet 192.0.2.1/24 up
303+
jexec alcatraz ifconfig ${epair_link}a 198.51.100.1/24 up
304+
jexec alcatraz sysctl net.inet.ip.forwarding=1
305+
306+
vnet_mkjail srv ${epair_link}b
307+
jexec srv ifconfig ${epair_link}b inet 198.51.100.2/24 up
308+
jexec srv route add default 198.51.100.1
309+
310+
# Sanity check
311+
atf_check -s exit:0 -o ignore \
312+
ping -c 1 192.0.2.1
313+
atf_check -s exit:0 -o ignore \
314+
jexec alcatraz ping -c 1 198.51.100.2
315+
316+
jexec alcatraz pfctl -e
317+
pft_set_rules alcatraz \
318+
"rdr pass on ${epair}b proto udp from any to 192.0.2.1 port 80 -> 198.51.100.2" \
319+
"nat on ${epair}b inet from 198.51.100.0/24 to any -> 192.0.2.1" \
320+
"block in proto udp from any to any port 80" \
321+
"pass in proto icmp"
322+
323+
echo "foo" | jexec srv nc -u -l 80 &
324+
sleep 1 # Give the above a moment to start
325+
326+
out=$(echo 1 | nc -u -w 1 192.0.2.1 80)
327+
echo "out ${out}"
328+
if [ "${out}" != "foo" ];
329+
then
330+
jexec alcatraz pfctl -sn -vv
331+
jexec alcatraz pfctl -ss -vv
332+
atf_fail "rdr failed"
333+
fi
334+
}
335+
336+
natpass_cleanup()
337+
{
338+
pft_cleanup
339+
}
340+
284341
atf_init_test_cases()
285342
{
343+
atf_add_test_case "natpass"
286344
atf_add_test_case "tcp_v6_compat"
287345
atf_add_test_case "tcp_v6_pass"
288346
atf_add_test_case "srcport_compat"

0 commit comments

Comments
 (0)