git: 42bf1d9e83c8 - main - pf: implement RFC 7112 Section 5

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 16:28:39 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=42bf1d9e83c85c3f3bc1bf85dc7f655676b1f35a

commit 42bf1d9e83c85c3f3bc1bf85dc7f655676b1f35a
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2026-11-10 12:26:12 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2026-11-18 16:28:21 +0000

    pf: implement RFC 7112 Section 5
    
    RFC 7112 Section 5 and RFC 8200 Section 4.5 require IPv6 fragmented
    packets to contain all headers in the first fragment. If this is not the
    case we now discard the packet.
    
    We do not send the ICMPv6 Parameter Problem error RFC 8200 requires (but
    7112 lists this as "MAY").
    
    This makes bypassing the firewall, in cases where fragment reassembly is
    disabled, harder.
    
    RelNotes:       yes
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
---
 sys/netpfil/pf/pf.c           | 36 ++++++++-----------------------
 tests/sys/netpfil/pf/frag6.py | 50 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 27 deletions(-)

diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c
index 2d4efbe1e3e8..6601763d4b51 100644
--- a/sys/netpfil/pf/pf.c
+++ b/sys/netpfil/pf/pf.c
@@ -11014,12 +11014,6 @@ pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason)
 				REASON_SET(reason, PFRES_IPOPTIONS);
 				return (PF_DROP);
 			}
-			/* fragments may be short */
-			if (pd->fragoff != 0 && end < pd->off + sizeof(rthdr)) {
-				pd->off = pd->fragoff;
-				pd->proto = IPPROTO_FRAGMENT;
-				return (PF_PASS);
-			}
 			if (!pf_pull_hdr(pd->m, pd->off, &rthdr, sizeof(rthdr),
 			    reason, AF_INET6)) {
 				DPFPRINTF(PF_DEBUG_MISC, "IPv6 short rthdr");
@@ -11046,12 +11040,6 @@ pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason)
 				DPFPRINTF(PF_DEBUG_MISC, "IPv6 short exthdr");
 				return (PF_DROP);
 			}
-			/* fragments may be short */
-			if (pd->fragoff != 0 && end < pd->off + sizeof(ext)) {
-				pd->off = pd->fragoff;
-				pd->proto = IPPROTO_FRAGMENT;
-				return (PF_PASS);
-			}
 			/* reassembly needs the ext header before the frag */
 			if (pd->fragoff == 0)
 				pd->extoff = pd->off;
@@ -11066,11 +11054,6 @@ pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason)
 			else
 				extlen = (ext.ip6e_len + 1) * 8;
 			if (end < pd->off + extlen) {
-				if (pd->fragoff != 0) {
-					pd->off = pd->fragoff;
-					pd->proto = IPPROTO_FRAGMENT;
-					return (PF_PASS);
-				}
 				DPFPRINTF(PF_DEBUG_MISC, "IPv6 long exthdr");
 				REASON_SET(reason, PFRES_SHORT);
 				return (PF_DROP);
@@ -11079,12 +11062,6 @@ pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason)
 			pd->proto = ext.ip6e_nxt;
 			break;
 		case IPPROTO_ICMPV6:
-			/* fragments may be short, ignore inner header then */
-			if (pd->fragoff != 0 && end < pd->off + sizeof(icmp6)) {
-				pd->off = pd->fragoff;
-				pd->proto = IPPROTO_FRAGMENT;
-				return (PF_PASS);
-			}
 			if (!pf_pull_hdr(pd->m, pd->off, &icmp6, sizeof(icmp6),
 				reason, AF_INET6)) {
 				DPFPRINTF(PF_DEBUG_MISC,
@@ -11125,14 +11102,19 @@ pf_walk_header6(struct pf_pdesc *pd, struct ip6_hdr *h, u_short *reason)
 		case IPPROTO_TCP:
 		case IPPROTO_UDP:
 		case IPPROTO_SCTP:
-			/* fragments may be short, ignore inner header then */
-			if (pd->fragoff != 0 && end < pd->off +
+			/* If we don't have a full header */
+			if (end < pd->off +
 			    (pd->proto == IPPROTO_TCP ? sizeof(struct tcphdr) :
 			    pd->proto == IPPROTO_UDP ? sizeof(struct udphdr) :
 			    pd->proto == IPPROTO_SCTP ? sizeof(struct sctphdr) :
 			    sizeof(struct icmp6_hdr))) {
-				pd->off = pd->fragoff;
-				pd->proto = IPPROTO_FRAGMENT;
+				/*
+				 * The first fragment must contain a full header..
+				 * See RFC 7112 Section 5 and
+				 * RFC 8200 Section 4.5
+				 */
+				REASON_SET(reason, PFRES_SHORT);
+				return (PF_DROP);
 			}
 			/* FALLTHROUGH */
 		default:
diff --git a/tests/sys/netpfil/pf/frag6.py b/tests/sys/netpfil/pf/frag6.py
index ca2d231edccb..abcc0135e3b0 100644
--- a/tests/sys/netpfil/pf/frag6.py
+++ b/tests/sys/netpfil/pf/frag6.py
@@ -1,6 +1,7 @@
 import pytest
 import logging
 import random
+import socket
 logging.getLogger("scapy").setLevel(logging.CRITICAL)
 from utils import DelayedSend
 from atf_python.sys.net.tools import ToolsHelper
@@ -273,3 +274,52 @@ class TestFrag6_RouteTo(VnetTestTemplate):
         assert err_ip6
         assert err_ip6.src == "2001:db8::1"
         assert err_ip6.dst == "2001:db8:666::1"
+
+class TestFrag6NoReassembly(VnetTestTemplate):
+    REQUIRED_MODULES = ["pf"]
+    TOPOLOGY = {
+        "vnet1": {"ifaces": ["if1"]},
+        "vnet2": {"ifaces": ["if1"]},
+        "if1": {"prefixes6": [("2001:db8::1/64", "2001:db8::2/64")]},
+    }
+
+    def vnet2_handler(self, vnet):
+        ifname = vnet.iface_alias_map["if1"].name
+        ToolsHelper.print_output("/sbin/pfctl -e")
+        ToolsHelper.pf_rules([
+            "scrub in on lo0 from 2001:db8::99 to any", # just to put pf into scrub compat mode
+            "pass",
+            "block in inet6 proto tcp from any to port 1234",
+        ])
+
+    @pytest.mark.require_user("root")
+    @pytest.mark.require_progs(["scapy"])
+    def test_tcp(self):
+        import scapy.all as sp
+
+        # This gets dropped by pf, because it blocks port 1234
+        packet = sp.IPv6(src="2001:db8::1", dst="2001:db8::2") \
+            / sp.TCP(dport=1234, flags="S")
+        reply = sp.sr1(packet, timeout=3)
+        assert reply is None
+
+        # However, if we fragment the SYN...
+        frags = sp.fragment6(packet, 60)
+        frags[0].show()
+        frags[1].show()
+        assert len(frags) == 2
+
+        s = DelayedSend(frags)
+
+        replies = sp.sniff(iface=self.vnet.iface_alias_map["if1"].name,
+            timeout=3)
+        for p in replies:
+            if not sp.IPv6 in p:
+                continue
+            ip6 = p[sp.IPv6]
+            if ip6.dst != "2001:db8::1":
+                continue
+
+            # We should not get a TCP RST here
+            p.show()
+            assert ip6.nh != socket.IPPROTO_TCP