Page MenuHomeFreeBSD

D62125.1795990032.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D62125.1795990032.diff

diff --git a/sbin/ipfw/ipfw.8 b/sbin/ipfw/ipfw.8
--- a/sbin/ipfw/ipfw.8
+++ b/sbin/ipfw/ipfw.8
@@ -1,5 +1,5 @@
.\"
-.Dd November 24, 2026
+.Dd November 26, 2026
.Dt IPFW 8
.Os
.Sh NAME
@@ -2228,11 +2228,16 @@
.Cm frag
option for details on matching fragmented packets.
.It Cm tcpmss Ar tcpmss-list
-Matches TCP packets whose MSS (maximum segment size) value is set to
+Matches TCP packets with the SYN flag set whose MSS (maximum segment size)
+option is set to
.Ar tcpmss-list ,
which is either a single value or a list of values or ranges
specified in the same way as
.Ar ports .
+A rule which contains a
+.Cm tcpmss
+specification can never match a fragmented packet which has
+a non-zero offset.
.It Cm tcpseq Ar seq
TCP packets only.
Match if the TCP header sequence number field is set to
diff --git a/sys/netpfil/ipfw/ip_fw2.c b/sys/netpfil/ipfw/ip_fw2.c
--- a/sys/netpfil/ipfw/ip_fw2.c
+++ b/sys/netpfil/ipfw/ip_fw2.c
@@ -2554,7 +2554,7 @@
break;
case O_TCPMSS:
- if (proto == IPPROTO_TCP &&
+ if (proto == IPPROTO_TCP && offset == 0 &&
(args->f_id._flags & TH_SYN) != 0) {
struct tcphdr *tcp = ulp;
uint16_t mss, *p;
diff --git a/tests/sys/netpfil/ipfw/Makefile b/tests/sys/netpfil/ipfw/Makefile
--- a/tests/sys/netpfil/ipfw/Makefile
+++ b/tests/sys/netpfil/ipfw/Makefile
@@ -9,6 +9,7 @@
lookup \
nat \
table \
+ tcpmss \
unmapped
ATF_TESTS_C+= nat_getlog
diff --git a/tests/sys/netpfil/ipfw/tcpmss.sh b/tests/sys/netpfil/ipfw/tcpmss.sh
new file mode 100644
--- /dev/null
+++ b/tests/sys/netpfil/ipfw/tcpmss.sh
@@ -0,0 +1,144 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Maxim Konovalov <maxim@FreeBSD.org>
+#
+
+. $(atf_get_srcdir)/../common/utils.subr
+
+# The tcpmss option matches the MSS option of TCP SYN segments. A
+# non-first fragment of a TCP datagram has no TCP header, and ipfw does
+# not record TCP flags for it. Send SYNs through a tcpmss rule, each
+# followed by such a fragment: the SYNs with a small MSS must match the
+# rule, and the fragments must pass it.
+
+# Send count SYNs with the given MSS from src to dst on iface. If frag
+# is 1, follow each SYN with a 16-byte TCP fragment at offset 16.
+tcpmss_send()
+{
+ python3 - "$@" <<'EOF'
+import logging
+import sys
+
+logging.getLogger("scapy").setLevel(logging.CRITICAL)
+import scapy.all as sp
+
+af, iface, dmac, src, dst = sys.argv[1:6]
+mss, count, frag = (int(x) for x in sys.argv[6:9])
+
+if af == "inet":
+ ip = sp.IP(src=src, dst=dst)
+ fip = sp.IP(src=src, dst=dst, proto=6, frag=2)
+else:
+ ip = sp.IPv6(src=src, dst=dst)
+ fip = sp.IPv6(src=src, dst=dst) / sp.IPv6ExtHdrFragment(nh=6, offset=2)
+eth = sp.Ether(dst=dmac)
+syn = eth / ip / sp.TCP(dport=9, flags="S", options=[("MSS", mss)])
+fragment = eth / fip / sp.Raw(b"\0" * 16)
+pkts = []
+for i in range(count):
+ pkts.append(syn)
+ if frag:
+ pkts.append(fragment)
+sp.sendp(pkts, iface=iface, verbose=False)
+EOF
+}
+
+# Check that the packet counter of the given rule in the rcv jail
+# reaches the expected value. The epair delivers packets
+# asynchronously, so wait for it for up to five seconds.
+tcpmss_check()
+{
+ rule=$1
+ expect=$2
+
+ for i in $(seq 50); do
+ count=$(jexec rcv ipfw show ${rule} | awk '{ print $2 }')
+ [ "${count}" = "${expect}" ] && break
+ sleep 0.1
+ done
+ atf_check_equal "${expect}" "${count}"
+}
+
+tcpmss_test()
+{
+ af=$1
+ src=$2
+ dst=$3
+ plen=$4
+
+ firewall_init ipfw
+
+ epair=$(vnet_mkepair)
+ vnet_mkjail rcv ${epair}b
+ if [ ${af} = "inet" ]; then
+ ifconfig ${epair}a inet ${src}/${plen} up
+ jexec rcv ifconfig ${epair}b inet ${dst}/${plen} up
+ else
+ ifconfig ${epair}a inet6 ${src}/${plen} up no_dad -ifdisabled
+ jexec rcv ifconfig ${epair}b inet6 ${dst}/${plen} up no_dad \
+ -ifdisabled
+ fi
+ dmac=$(jexec rcv ifconfig ${epair}b ether | awk '/ether/ { print $2 }')
+
+ firewall_config rcv ipfw ipfw \
+ "ipfw -q add 100 deny tcp from any to any tcpmss 1-500 in" \
+ "ipfw -q add 200 count tcp from any to any in"
+
+ # A SYN with a large MSS does not match.
+ tcpmss_send ${af} ${epair}a ${dmac} ${src} ${dst} 1460 10 0
+ tcpmss_check 100 0
+ tcpmss_check 200 10
+
+ # A SYN with a small MSS matches.
+ tcpmss_send ${af} ${epair}a ${dmac} ${src} ${dst} 400 10 0
+ tcpmss_check 100 10
+ tcpmss_check 200 10
+
+ # A fragment after such a SYN does not match, and does not panic.
+ tcpmss_send ${af} ${epair}a ${dmac} ${src} ${dst} 400 1000 1
+ tcpmss_check 100 1010
+ tcpmss_check 200 1010
+}
+
+atf_test_case "frag4" "cleanup"
+frag4_head()
+{
+ atf_set descr 'tcpmss rule and non-first TCP fragments, IPv4'
+ atf_set require.user root
+ atf_set require.progs python3 scapy
+}
+
+frag4_body()
+{
+ tcpmss_test inet 192.0.2.1 192.0.2.2 24
+}
+
+frag4_cleanup()
+{
+ firewall_cleanup ipfw
+}
+
+atf_test_case "frag6" "cleanup"
+frag6_head()
+{
+ atf_set descr 'tcpmss rule and non-first TCP fragments, IPv6'
+ atf_set require.user root
+ atf_set require.progs python3 scapy
+}
+
+frag6_body()
+{
+ tcpmss_test inet6 2001:db8:42::1 2001:db8:42::2 64
+}
+
+frag6_cleanup()
+{
+ firewall_cleanup ipfw
+}
+
+atf_init_test_cases()
+{
+ atf_add_test_case "frag4"
+ atf_add_test_case "frag6"
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 29, 10:07 PM (5 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42056312
Default Alt Text
D62125.1795990032.diff (5 KB)

Event Timeline