Skip to content

net: Fix IP fragment forwarding and L4 handling - #19595

Merged
acassis merged 3 commits into
apache:masterfrom
masc2008:fix-ipv4-forward-fragments
Aug 1, 2026
Merged

net: Fix IP fragment forwarding and L4 handling#19595
acassis merged 3 commits into
apache:masterfrom
masc2008:fix-ipv4-forward-fragments

Conversation

@masc2008

@masc2008 masc2008 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Forward IPv4 fragments before local reassembly, and preserve the original fragment offset/MF bit when re-fragmenting forwarded IPv4 packets.
  • Reassemble received IPv6 fragments before NAT66, and dispatch inbound NAT66 using the parsed next-header after IPv6 extension headers.
  • Reassemble received IPv6 fragments before IPFILTER so L4 filter matching does not inspect incomplete IPv6 fragments.

Testing

  • tools/nxstyle net/devif/ipv4_input.c net/ipforward/ipv4_forward.c net/ipfrag/ipv4_frag.c net/devif/ipv6_input.c net/nat/ipv6_nat.c
  • git diff --check
  • /tmp/nuttx-ipfrag-verify: sim:dynconns + NAT44/NAT66/IPFRAG, make -j1 net/libnet.a
  • /tmp/nuttx-ipfrag-verify: sim:dynconns + CONFIG_NET_IPFILTER=y + CONFIG_NET_NAT66=y + CONFIG_NET_IPFRAG=y, make -j1 net/libnet.a
  • Full make -j8 reached the touched net objects, then stopped in external apps/nshlib at O_RDOK undeclared, unrelated to this PR.

@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: S The size of the change in this PR is small labels Aug 1, 2026
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@masc2008

masc2008 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

The rule comes directly from the IPv4 router requirements.

The strongest spec is RFC 1812, section 5.2.6:

“A router MUST NOT reassemble any datagram before forwarding it.”

It also gives the reason: fragments may take different paths, so a transit router cannot safely assume it will see all fragments. Source: RFC 1812 section 5.2.6
(https://www.rfc-editor.org/rfc/rfc1812.txt)

The original IPv4 spec, RFC 791, also says the basic model is fragmentation at gateways, with reassembly at the destination host’s IP layer. Source: RFC 791 section 3.2
(https://www.rfc-editor.org/info/rfc791/)

So the intended IPv4 router behavior is:

fragment received
check IPv4 header
decide local vs forward
if forward:
forward this fragment independently
re-fragment only if egress MTU requires it
if local:
reassemble before TCP/UDP/ICMP processing

One more related point: RFC 791’s fragmentation algorithm says that if a fragment is split again, the new fragment offset is based on the original offset plus the new split offset,
and MF follows the original fragment state. That is why the PR also fixes ipv4_fragout() to preserve original offset/MF when re-fragmenting.

Allow non-local IPv4 fragments to reach the forwarding path before local fragment reassembly. This lets routers forward fragmented datagrams without requiring local reassembly state.

Keep NAT44 and IP filter builds on the existing reassembly path because those features can depend on L4 headers. Preserve the original fragment offset and MF flag when fragout must split an already-fragmented packet for a smaller egress MTU.

Signed-off-by: shichunma <shichunma@bestechnic.com>
@masc2008
masc2008 force-pushed the fix-ipv4-forward-fragments branch from 9232dc0 to 48dfff5 Compare August 1, 2026 10:54
acassis
acassis previously approved these changes Aug 1, 2026
xiaoxiang781216
xiaoxiang781216 previously approved these changes Aug 1, 2026
@masc2008
masc2008 dismissed stale reviews from xiaoxiang781216 and acassis via 01e086b August 1, 2026 13:55
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Aug 1, 2026
@masc2008 masc2008 changed the title net/ipv4: Forward fragments before local reassembly net: Fix IPv4 fragment forwarding and NAT66 fragments Aug 1, 2026
NAT66 needs transport headers to create or match address and port mappings. Consume IPv6 fragments through local reassembly before NAT66 processing so forwarded fragmented traffic is not translated fragment-by-fragment without L4 context.

Also dispatch inbound NAT66 using the parsed IPv6 next-header value, matching the outbound path and allowing packets with extension headers before the transport header to be translated.

Signed-off-by: shichunma <shichunma@bestechnic.com>
@masc2008
masc2008 force-pushed the fix-ipv4-forward-fragments branch from 01e086b to 6dfb82d Compare August 1, 2026 13:59
IPv6 input currently lets fragmented packets continue into the IP filter path before reassembly. Forwarded IP filter rules can inspect L4 fields, but non-first IPv6 fragments do not carry the transport header.

Consume IPv6 fragments through the existing reassembly/drop helper before NAT66 or IP filter processing. This keeps plain IPv6 forwarding unchanged when neither L4-dependent feature is enabled, while avoiding filtering incomplete fragments.

Signed-off-by: shichunma <shichunma@bestechnic.com>
@masc2008 masc2008 changed the title net: Fix IPv4 fragment forwarding and NAT66 fragments net: Fix IP fragment forwarding and L4 handling Aug 1, 2026
@acassis
acassis merged commit 9aa6eb5 into apache:master Aug 1, 2026
65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants