Re: git: 86861520613f - main - ipfilter: Remove erroneous ntohs()
Date: Tue, 10 Nov 2026 01:22:00 UTC
> On Nov 10, 2026, at 7:22 AM, Cy Schubert <cy@freebsd.org> wrote:
>
> The branch main has been updated by cy:
>
> URL: https://cgit.FreeBSD.org/src/commit/?id=86861520613f99c85b5fbdf92b5c955a13ed6624
>
> commit 86861520613f99c85b5fbdf92b5c955a13ed6624
> Author: Cy Schubert <cy@FreeBSD.org>
> AuthorDate: 2026-11-09 23:13:21 +0000
> Commit: Cy Schubert <cy@FreeBSD.org>
> CommitDate: 2026-11-09 23:22:44 +0000
>
> ipfilter: Remove erroneous ntohs()
>
> The original commit was wrong. ipf_nat_builddivertmp() builds the UDP
> template with the length in host byte order
But ipf_nat_builddivertmp() build the ip header with network byte order,
```
ip->ip_len = htons(len);
```
I'd prefer consistent byte orders ( all in network byte order if possible ) to make the maintenance easier.
>
> M_DUP() copies that value unchanged. The old code was therefore correct.
> It added fin_plen in host order and then did a single htons().
>
> The original patch was discussed with Co-Pilot, which pointed out the
> original bug. claude-opus-5-5 discovered the ntohs() error that
> co-pilot suggested.
>
> Interestingly this code had been running here for a couple of months.
>
> Reported by: llm-postcr
> Fixes: 72cc9e3766f4
> ---
> sys/netpfil/ipfilter/netinet/ip_nat.c | 1 -
> 1 file changed, 1 deletion(-)
>
> diff --git a/sys/netpfil/ipfilter/netinet/ip_nat.c b/sys/netpfil/ipfilter/netinet/ip_nat.c
> index c8125c10e3f0..4b4890587f53 100644
> --- a/sys/netpfil/ipfilter/netinet/ip_nat.c
> +++ b/sys/netpfil/ipfilter/netinet/ip_nat.c
> @@ -5156,7 +5156,6 @@ ipf_nat_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags)
>
> 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);
> }
>
Best regards,
Zhenlei