git: dd721990e8c0 - stable/15 - ipfilter: Only recalculate the UDP header when we have one

From: Cy Schubert <cy_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 19:32:50 UTC
The branch stable/15 has been updated by cy:

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

commit dd721990e8c03b3ff9be0523e5af452d6890b72f
Author:     Cy Schubert <cy@FreeBSD.org>
AuthorDate: 2026-08-26 05:27:28 +0000
Commit:     Cy Schubert <cy@FreeBSD.org>
CommitDate: 2026-11-13 19:32:36 +0000

    ipfilter: Only recalculate the UDP header when we have one
    
    When ipf_nat_out processes NAT_DIVERTOUT (ipnat rdr statements)
    it also fixes the checksum in the UDP header. Test for a UDP header
    before adjusting its length.
    
    While we are here, the length in the UDP header must be converted
    from network byte order to host byte order. Add the missing ntohs().
    
    PR:             296944
    
    (cherry picked from commit 72cc9e3766f4222fa7fc462bc669dcf668e8b334)
---
 sys/netpfil/ipfilter/netinet/ip_nat.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/sys/netpfil/ipfilter/netinet/ip_nat.c b/sys/netpfil/ipfilter/netinet/ip_nat.c
index b7f5eb94b1dd..c8125c10e3f0 100644
--- a/sys/netpfil/ipfilter/netinet/ip_nat.c
+++ b/sys/netpfil/ipfilter/netinet/ip_nat.c
@@ -5154,9 +5154,12 @@ ipf_nat_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
 		s2 += ntohs(ip->ip_len);
 		CALC_SUMD(s1, s2, sumd);
 
-		uh = (udphdr_t *)(ip + 1);
-		uh->uh_ulen += fin->fin_plen;
-		uh->uh_ulen = htons(uh->uh_ulen);
+		if (ip->ip_p == IPPROTO_UDP) {
+			uh = (udphdr_t *)(ip + 1);
+			uh->uh_ulen = ntohs(uh->uh_ulen);
+			uh->uh_ulen += fin->fin_plen;
+			uh->uh_ulen = htons(uh->uh_ulen);
+		}
 #if !defined(_KERNEL) || SOLARIS || \
     defined(BRIDGE_IPF) || defined(__FreeBSD__)
 		ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0);