Page MenuHomeFreeBSD

D58021.id181302.diff
No OneTemporary

D58021.id181302.diff

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -9980,6 +9980,21 @@
pd->act.dnpipe = tmp;
}
+ /*
+ * If the output interface does not accept unmapped mbufs, convert
+ * them to mapped mbufs.
+ */
+ if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
+ error = mb_unmapped_to_ext(m0, &md);
+ if (error)
+ goto done;
+ /*
+ * The first mbuf should not be reallocated because it is
+ * always mapped.
+ */
+ MPASS(m0 == md);
+ }
+
/*
* If small enough for interface, or the interface will take
* care of the fragmentation for us, we can just send directly.
@@ -10321,6 +10336,11 @@
}
if ((u_long)m0->m_pkthdr.len <= ifp->if_mtu) {
+ if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
+ if (mb_unmapped_to_ext(m0, &md) != 0)
+ goto done;
+ MPASS(m0 == md);
+ }
md = m0;
pf_dummynet_route(pd, s, r, ifp, sintosa(&dst), &md);
if (md != NULL) {
@@ -11585,6 +11605,7 @@
{
struct pfi_kkif *kif;
u_short action, reason = 0;
+ struct mbuf *m;
struct m_tag *mtag;
struct pf_krule *a = NULL, *r = &V_pf_default_rule;
struct pf_kstate *s = NULL;
@@ -11620,6 +11641,12 @@
}
if (__predict_false(! M_WRITABLE(*m0))) {
+ /* Need to convert unmapped mbufs before calling m_unshare(). */
+ if (mb_unmapped_to_ext(*m0, &m) != 0) {
+ *m0 = NULL;
+ return (PF_DROP);
+ }
+ MPASS(*m0 == m);
*m0 = m_unshare(*m0, M_NOWAIT);
if (*m0 == NULL) {
return (PF_DROP);
@@ -11639,6 +11666,13 @@
*m0 = NULL;
return (PF_PASS);
}
+
+ /*
+ * No need to call mb_unmapped_to_ext() here because it had
+ * already been called in pf_route()/pf_route6() and dummynet
+ * re-injected this packet.
+ */
+
(ifp->if_output)(ifp, *m0, sintosa(&pd.pf_mtag->dst), NULL);
*m0 = NULL;
return (PF_PASS);
diff --git a/tests/sys/netpfil/pf/Makefile b/tests/sys/netpfil/pf/Makefile
--- a/tests/sys/netpfil/pf/Makefile
+++ b/tests/sys/netpfil/pf/Makefile
@@ -56,7 +56,8 @@
synproxy \
table \
tcp \
- tos
+ tos \
+ unmapped_mbuf
ATF_TESTS_PYTEST+= frag4.py
ATF_TESTS_PYTEST+= frag6.py
diff --git a/tests/sys/netpfil/pf/unmapped_mbuf.sh b/tests/sys/netpfil/pf/unmapped_mbuf.sh
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/pf/unmapped_mbuf.sh
@@ -0,0 +1,216 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 KUROSAWA Takahiro <takahiro.kurosawa@gmail.com>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+
+. $(atf_get_srcdir)/utils.subr
+
+HELPER=$(atf_get_srcdir)/../../common/sendfile_helper
+ovpn_dir=$(atf_get_srcdir)/../../net/if_ovpn
+. ${ovpn_dir}/utils.subr
+
+ifa_v4() {
+ local j=$1
+ local iface=$2
+
+ jexec $j ifconfig $iface | awk '/^[[:space:]]inet /{print $2}'
+}
+
+ifa_v6() {
+ local j=$1
+ local iface=$2
+
+ jexec $j ifconfig $iface | awk '/^[[:space:]]inet6 2001/{print $2}'
+}
+
+# pf must call mb_unmapped_to_ext() before passing an output mbuf to a network
+# interface if it does not accept unmapped mbufs as ip_output() does. These tests
+# make sure that unmapped mbufs are correctly converted to mapped ones before pf
+# passes mbufs to interfaces.
+# Use ovpn(4) because it does not accept unmapped mbufs and actually crashes if
+# unmapped mbufs are passed to ovpn_output().
+setup_ovpn_peers() {
+ pft_init
+ ovpn_init
+
+ l=$(vnet_mkepair)
+
+ vnet_mkjail a ${l}a
+ jexec a ifconfig ${l}a 192.0.2.1/24 up
+ vnet_mkjail b ${l}b
+ jexec b ifconfig ${l}b 192.0.2.2/24 up
+
+ # Sanity check
+ atf_check -s exit:0 -o ignore jexec a ping -c 1 192.0.2.2
+
+ ovpn_start a "
+ dev ovpn0
+ dev-type tun
+ proto udp4
+
+ cipher AES-256-GCM
+ auth SHA256
+
+ local 192.0.2.1
+ server 198.51.100.0 255.255.255.0
+ server-ipv6 2001:db8::/64
+ ca ${ovpn_dir}/ca.crt
+ cert ${ovpn_dir}/server.crt
+ key ${ovpn_dir}/server.key
+ dh ${ovpn_dir}/dh.pem
+
+ mode server
+ script-security 2
+ auth-user-pass-verify /usr/bin/true via-env
+ topology subnet
+
+ keepalive 2 10
+
+ management 192.0.2.1 1234
+ "
+ ovpn_start b "
+ dev tun0
+ dev-type tun
+
+ client
+
+ remote 192.0.2.1
+ auth-user-pass ${ovpn_dir}/user.pass
+
+ ca ${ovpn_dir}/ca.crt
+ cert ${ovpn_dir}/client.crt
+ key ${ovpn_dir}/client.key
+ dh ${ovpn_dir}/dh.pem
+
+ keepalive 2 10
+ "
+
+ # Give the tunnel time to come up
+ sleep 10
+
+ atf_check -s exit:0 -o ignore jexec b ping -c 1 198.51.100.1
+ atf_check -s exit:0 -o ignore jexec a ping -c 1 $(ifa_v6 b tun0)
+}
+
+atf_test_case "v4" "cleanup"
+v4_head() {
+ atf_set descr 'unmapped mbuf IPv4 test'
+ atf_set require.user root
+ atf_set require.kmods if_ovpn
+ atf_set require.progs openvpn
+}
+
+v4_body() {
+ pft_init
+ setup_ovpn_peers
+
+ ovpn_a=$(jexec a ifconfig -g openvpn)
+ b_v4=$(ifa_v4 b tun0)
+ jexec a pfctl -e
+ pft_set_rules a "pass out on $ovpn_a route-to ($ovpn_a $b_v4) all"
+ jexec b timeout 30s nc -4 -d -l 2345 > out &
+ sleep 1
+ atf_check -s exit:0 -o ignore \
+ jexec a ${HELPER} -c $b_v4 -p 2345 ${HELPER} 0 512 0
+ wait
+ atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \
+ $(sha256 -q out)
+}
+
+v4_cleanup() {
+ pft_cleanup
+ ovpn_cleanup
+ rm -f out
+}
+
+atf_test_case "v6" "cleanup"
+v6_head() {
+ atf_set descr 'unmapped mbuf IPv6 test'
+ atf_set require.user root
+ atf_set require.kmods if_ovpn
+ atf_set require.progs openvpn
+}
+
+v6_body() {
+ pft_init
+ setup_ovpn_peers
+
+ ovpn_a=$(jexec a ifconfig -g openvpn)
+ b_v6=$(ifa_v6 b tun0)
+ jexec a pfctl -e
+ pft_set_rules a "pass out on $ovpn_a route-to ($ovpn_a $b_v6) all"
+ jexec b timeout 30s nc -6 -d -l 2345 > out &
+ sleep 1
+ atf_check -s exit:0 -o ignore \
+ jexec a ${HELPER} -c $b_v6 -p 2345 ${HELPER} 0 512 0
+ wait
+ atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \
+ $(sha256 -q out)
+}
+
+v6_cleanup() {
+ pft_cleanup
+ ovpn_cleanup
+ rm -f out
+}
+
+atf_test_case "dummynet" "cleanup"
+dummynet_head() {
+ atf_set descr 'unmapped mbuf dummynet test'
+ atf_set require.user root
+ atf_set require.kmods if_ovpn dummynet
+ atf_set require.progs openvpn
+}
+
+dummynet_body() {
+ pft_init
+ setup_ovpn_peers
+
+ ovpn_a=$(jexec a ifconfig -g openvpn)
+ b_v4=$(ifa_v4 b tun0)
+ atf_check -s exit:0 -o ignore \
+ jexec a dnctl pipe 1 config delay 100
+ jexec a pfctl -e
+ pft_set_rules a "pass out on $ovpn_a route-to ($ovpn_a $b_v4) all dnpipe 1"
+ jexec b timeout 30s nc -4 -d -l 2345 > out &
+ sleep 1
+ atf_check -s exit:0 -o ignore \
+ jexec a ${HELPER} -c $b_v4 -p 2345 ${HELPER} 0 512 0
+ wait
+ atf_check_equal $(dd bs=512 count=1 if=${HELPER} | sha256 -q) \
+ $(sha256 -q out)
+}
+
+dummynet_cleanup() {
+ pft_cleanup
+ ovpn_cleanup
+ rm -f out
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case "v4"
+ atf_add_test_case "v6"
+ atf_add_test_case "dummynet"
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Aug 28, 8:14 PM (13 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
37470331
Default Alt Text
D58021.id181302.diff (7 KB)

Event Timeline