From nobody Fri Mar 01 12:19:50 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TmRx23NJcz5CHMm; Fri, 1 Mar 2024 12:19:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TmRx22nrRz58QN; Fri, 1 Mar 2024 12:19:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1709295590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=br5RSMkgumcsOsJLLGNO40Ip6GzI1U02gai3z5DquEI=; b=Kh3vgs7R9wYGAUu51w+atH0sMTdJ61/0p8qfT5nHuLKxjpLn5Jes/ohe6vmhEV9fXTEUoI eIJ5Ms3+kumlTnZH2VmxIfam67RTwhafw2+0uD2x12D92cmC8NKOLeMvRPd91u5z7Y2Y6I gqITxaaOCAQceKXtuJPbyLQK/LJIkD+caXqbLdyTXUhjAQ6tvof9CrnDDxuXurAMStbMcS 8hY47IN9pYaQ711aURM/joqQHPuHQwpcV9ImRjxrgTDjFUBUeL8CY8BghIq0tYlfdiwMRi uWZB74TwcQrVNkK9ZqaKmKgBV9UCAl0PiDTTZcy5bY84TyBIMRmLsdaIjC0L8Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1709295590; a=rsa-sha256; cv=none; b=WByBZqeN02JeUqUXIzGpWYs11Aw7nBwhe061outvQd+Ixg8sdC3bCs/SehJqFxzKSaD9Vt T00rguq2j3nB8+wL61iO4y/MzfA2KiLgvZ7h6TklxFh3ivwskji0SLoPtHJ7ybkBkZLyeQ YPthtE4PHwyCN4/E023JtcYYKN9FjsnKD9978/EmFpc97dcYxT7orRtEH6zwqepXk3uJn+ oqM3ox+EoTWL4k/0v3PSig1xnrWnexzypi1NIihfRuvKneubJCgrBPRIeHz3fB5qKn5Hj8 5rkStulY9oY+Fe+I7JMsLXbJDFk7J3kBI9PefYu0/n+yPqJN6h9DEAMXKigCtA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1709295590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=br5RSMkgumcsOsJLLGNO40Ip6GzI1U02gai3z5DquEI=; b=uFhd9diOASa5JseCjMRtfc96QfnYGwgvDRd8m24XF1jLOTjh0H61pRLYqsCmYLcAza+ftj dN3YCsqfshD9IpkAIS2gUu1MsAYO6J6R+svcODONZRsh25bRBUZg2O/6rqSkW1tj07tT9T BBAwKqK/UXXrT2bDuXu+b43o9nUQ+MNL4SIgxxPnF2JDDyg1Y+WpTjqSDv4lra8PGOq/g0 RIK/jq+e17/B74Dq87S9mg0NszDv8DBHc+En9GuF05e5ngbD5N1R4Kp0VomCYU9jOzM1DF S0XN/qh4+usrqwqNjMX+Mo9HD+G0ccV/Lrn1Aqm+B60Gk7jHUIcX1yFOR6O8Tw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TmRx22GnszQL1; Fri, 1 Mar 2024 12:19:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 421CJoNB038943; Fri, 1 Mar 2024 12:19:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 421CJoHP038940; Fri, 1 Mar 2024 12:19:50 GMT (envelope-from git) Date: Fri, 1 Mar 2024 12:19:50 GMT Message-Id: <202403011219.421CJoHP038940@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: 6460322a0a51 - main - pf: support if-bound with reply-to List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 6460322a0a512f4e2c263bee54fc8bf46091f4cd Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=6460322a0a512f4e2c263bee54fc8bf46091f4cd commit 6460322a0a512f4e2c263bee54fc8bf46091f4cd Author: Kristof Provost AuthorDate: 2024-02-02 20:56:55 +0000 Commit: Kristof Provost CommitDate: 2024-03-01 08:39:43 +0000 pf: support if-bound with reply-to On reply-to we don't know what interface to bind to when we create the state. Create any reply-to state as floating, but bind to the appropriate interface once we're handling the reply. See also: https://redmine.pfsense.org/issues/15220 Sponsored by: Rubicon Communications, LLC ("Netgate") --- sys/netpfil/pf/pf.c | 33 ++++++++++++++++++++++- tests/sys/netpfil/pf/route_to.sh | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index 46f01f986d05..b1f93f605b4f 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -423,6 +423,13 @@ BOUND_IFACE(struct pf_kstate *st, struct pfi_kkif *k) if (! (st->rule.ptr->rule_flag & PFRULE_IFBOUND)) return (V_pfi_all); + /* + * Initially set to all, because we don't know what interface we'll be + * sending this out when we create the state. + */ + if (st->rule.ptr->rt == PF_REPLYTO) + return (V_pfi_all); + /* Don't overrule the interface for states created on incoming packets. */ if (st->direction == PF_IN) return (k); @@ -7317,15 +7324,27 @@ pf_route(struct mbuf **m, struct pf_krule *r, struct ifnet *oifp, dst.sin_addr.s_addr = naddr.v4.s_addr; ifp = nkif ? nkif->pfik_ifp : NULL; } else { + struct pfi_kkif *kif; + if (!PF_AZERO(&s->rt_addr, AF_INET)) dst.sin_addr.s_addr = s->rt_addr.v4.s_addr; ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; + kif = s->rt_kif; /* If pfsync'd */ if (ifp == NULL && r->rpool.cur != NULL) { ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; + kif = r->rpool.cur->kif; + } + if (ifp != NULL && kif != NULL && + r->rule_flag & PFRULE_IFBOUND && + r->rt == PF_REPLYTO && + s->kif == V_pfi_all) { + s->kif = kif; + s->orig_kif = oifp->if_pf_kif; } + PF_STATE_UNLOCK(s); } @@ -7538,14 +7557,26 @@ pf_route6(struct mbuf **m, struct pf_krule *r, struct ifnet *oifp, &naddr, AF_INET6); ifp = nkif ? nkif->pfik_ifp : NULL; } else { + struct pfi_kkif *kif; + if (!PF_AZERO(&s->rt_addr, AF_INET6)) PF_ACPY((struct pf_addr *)&dst.sin6_addr, &s->rt_addr, AF_INET6); ifp = s->rt_kif ? s->rt_kif->pfik_ifp : NULL; + kif = s->rt_kif; /* If pfsync'd */ - if (ifp == NULL && r->rpool.cur != NULL) + if (ifp == NULL && r->rpool.cur != NULL) { ifp = r->rpool.cur->kif ? r->rpool.cur->kif->pfik_ifp : NULL; + kif = r->rpool.cur->kif; + } + if (ifp != NULL && kif != NULL && + r->rule_flag & PFRULE_IFBOUND && + r->rt == PF_REPLYTO && + s->kif == V_pfi_all) { + s->kif = kif; + s->orig_kif = oifp->if_pf_kif; + } } if (s) diff --git a/tests/sys/netpfil/pf/route_to.sh b/tests/sys/netpfil/pf/route_to.sh index 31a47e75c82e..5223381d9c24 100644 --- a/tests/sys/netpfil/pf/route_to.sh +++ b/tests/sys/netpfil/pf/route_to.sh @@ -407,6 +407,61 @@ ifbound_cleanup() pft_cleanup } +atf_test_case "ifbound_reply_to" "cleanup" +ifbound_reply_to_head() +{ + atf_set descr 'Test that reply-to states bind to the expected interface' + atf_set require.user root +} + +ifbound_reply_to_body() +{ + pft_init + + j="route_to:ifbound_reply_to" + + epair_one=$(vnet_mkepair) + epair_two=$(vnet_mkepair) + ifconfig ${epair_one}b inet 192.0.2.2/24 up + ifconfig ${epair_two}b up + + vnet_mkjail $j ${epair_one}a ${epair_two}a + jexec $j ifconfig ${epair_one}a 192.0.2.1/24 up + jexec $j ifconfig ${epair_two}a 198.51.100.1/24 up + jexec $j route add default 198.51.100.254 + + jexec $j pfctl -e + pft_set_rules $j \ + "set state-policy if-bound" \ + "block" \ + "pass in on ${epair_one}a reply-to (${epair_one}a 192.0.2.2) inet from any to 192.0.2.0/24 keep state" + + atf_check -s exit:0 -o ignore \ + ping -c 3 192.0.2.1 + + atf_check -s exit:0 \ + ${common_dir}/pft_ping.py \ + --to 192.0.2.1 \ + --from 203.0.113.2 \ + --sendif ${epair_one}b \ + --replyif ${epair_one}b + + # pft_ping uses the same ID every time, so this will look like more traffic in the same state + atf_check -s exit:0 \ + ${common_dir}/pft_ping.py \ + --to 192.0.2.1 \ + --from 203.0.113.2 \ + --sendif ${epair_one}b \ + --replyif ${epair_one}b + + jexec $j pfctl -ss -vv +} + +ifbound_reply_to_cleanup() +{ + pft_cleanup +} + atf_test_case "dummynet_frag" "cleanup" dummynet_frag_head() { @@ -465,5 +520,6 @@ atf_init_test_cases() atf_add_test_case "icmp_nat" atf_add_test_case "dummynet" atf_add_test_case "ifbound" + atf_add_test_case "ifbound_reply_to" atf_add_test_case "dummynet_frag" }