Skip to content

system/iptables: Support --syn, --tcp-flags and -m limit. - #4149

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:iptables-syn-limit-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:iptables-syn-limit-up

Conversation

@casaroli

@casaroli casaroli commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

apache/nuttx#22367 adds the TCP flags match and the limit match of iptables to the kernel filter. The iptables and ip6tables commands cannot make such a rule: they ignore --syn and -m limit, so iptables -A INPUT -p tcp --syn -m limit --limit 5/s -j ACCEPT becomes a rule that accepts all TCP packets.

This PR adds the options of Linux iptables:

  • [!] --tcp-flags mask comp and [!] --syn (--tcp-flags SYN,RST,ACK,FIN SYN). They set flg_mask and flg_cmp of struct xt_tcp. A rule with TCP flags and no port also gets a TCP match. As in Linux, the flag names can be in upper or lower case, and ALL and NONE can be items of the list. The tool defines the six flag values itself (no kernel header).
  • -m limit, --limit rate[/second|minute|hour|day], --limit-burst number, with the defaults of Linux (3/hour, burst 5). netlib gets netlib_ipt_filter_entry_limit() and netlib_ip6t_filter_entry_limit(): a limit alone, or a limit after a TCP, UDP or ICMP match. --limit without -m limit gives an error message. As in Linux, ! before -m, --limit or --limit-burst gives an error message.
  • netlib starts each match and the target of an entry at an offset that is a multiple of XT_ALIGN(), as Linux iptables does. So each match_size and target_size is a multiple of XT_ALIGN(), which net/ipfilter: Match TCP flags and a packet rate. nuttx#22367 checks. Before, a TCP or UDP match had 52 bytes on 64-bit targets (now 56).
  • iptables -L and ip6tables -L show all matches of a rule, with the TCP flags and the rate (in the unit that Linux iptables would print).
  • A first commit adds a blank line that nxstyle asks for in iptables_utils.c (no code change; the CI checks the whole file).

Impact

Only system/iptables and the iptables part of netlib (with CONFIG_NET_IPFILTER). The existing options and rules do not change.

Testing

With apache/nuttx#22367, on qemu-armv8a:nsh (flat, QEMU TCG, TUN device), Arm GNU 13.2.Rel1, -Werror:

nsh> iptables -A INPUT -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT
nsh> iptables -A INPUT -p tcp --syn -j DROP
nsh> iptables -L INPUT
Chain INPUT (policy ACCEPT)
target        prot  idev  odev  source              destination
ACCEPT        tcp   any   any   anywhere            anywhere           tcp spts:0:65535 dpts:0:65535 flags:0x17/0x02 limit: avg 5/second burst 10
DROP          tcp   any   any   anywhere            anywhere           tcp spts:0:65535 dpts:0:65535 flags:0x17/0x02
  • 2000 SYNs from spoofed sources, written to the TUN device: with --limit 150/s --limit-burst 10, 1521 SYNs pass; without the rule 2000 pass. With nuttx-apps master the same commands make a rule that accepts all TCP packets, and all 2000 SYNs pass. Details in net/ipfilter: Match TCP flags and a packet rate. nuttx#22367.
  • --tcp-flags SYN,ACK SYN,ACK, --tcp-flags syn,ack SYN, --tcp-flags Syn,ALL none, --dport 5025 --syn, ! --syn, ! --tcp-flags ALL NONE, -p udp -m limit --limit 7/hour, -p udp --dport 53 -m limit --limit 3/s, -p icmp --icmp-type 8 -m limit --limit 1/s, -m limit alone (3/hour, burst 5), ip6tables ... --syn -m limit --limit 2/s, ip6tables -p icmp6 --icmpv6-type 128 -m limit --limit 1/s, ip6tables -p udp --dport 53 -m limit, and -D with the same rule: the expected rules in -L.
  • -p tcp ! --tcp-flags NONE NONE -j DROP makes the rule flags:!0x00/0x00, and a TCP connection over the loopback still works (the rule matches no packet, as in Linux).
  • Errors: ! -m limit, -m limit ! --limit 5/s, -m limit ! --limit-burst 5, --limit without -m limit, -p udp --syn, --limit-burst 0, --limit 20000/s, an unknown TCP flag.
  • The entries that netlib makes (TCP, UDP, ICMP and ICMPv6 matches, alone and with a limit, and no match) have match_size, target_offset, target_size and next_offset that are multiples of XT_ALIGN() (8) on qemu-armv8a, qemu-armv7a and rv-virt (64-bit).
  • Builds with -Werror: qemu-armv8a:netnsh (IPv4 + IPv6, and IPv4 only), qemu-armv8a:knsh (apps imported), qemu-armv7a:nsh (Arm GNU 13.2.Rel1); rv-virt:netnsh64, rv-virt:knetnsh64 (apps imported) and qemu-armv8a:netnsh (NuttX CI image).
  • tools/checkpatch.sh -c -u -m -g: pass.

Add the blank line after a declaration that nxstyle asks for.  No code
change.  The next commit changes this file, and the CI runs nxstyle on
the whole file.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@cederom

cederom commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Thanks! The layout of the two new entry structs matches what convert_matches() in the kernel walks (I checked that match_size padding and the match + 1 payload offset agree on 64-bit and 32-bit). The rate parse and print round-trip correctly (5/s, 7/m, 3/hour, 13/day, 10000/s all give the same rate back, and 10001/s is rejected). Comments:

1. ! before the new options carries over to the next option

-m, --limit and --limit-burst do not look at or clear inv. So iptables -A INPUT ! -m limit -p tcp --dport 22 -j DROP silently inverts --dport instead. Linux rejects ! for these options. I'd suggest an error when inv is set in those three branches.

2. TCP flag names (iptables_utils.c:262)

Linux iptables accepts flag names in any case (syn,ack) and accepts ALL/NONE as items of the list. Here only upper case works, and ALL/NONE only as the whole argument. Using strncasecmp() and handling ALL/NONE inside the loop would make scripts written for Linux work unchanged.

3. Limit after a UDP or ICMP match (netlib_iptables.c:814, same in ip6tables)

-p udp -m limit works (it takes the "no match" path), but -p udp --dport 53 -m limit and -p icmp --icmp-type 8 -m limit fail. The second one is probably the most common use of -m limit (limiting ping floods). Two more structs in the same pattern would cover it, since the kernel side already supports it. A follow-up is fine; it is documented as a limitation.

4. Small things

  • iptables_match2str(IPT_MATCH(entry), entry->target_offset - sizeof(struct ipt_entry)): if target_offset is ever smaller than the entry size, this wraps around to a huge length. A target_offset > sizeof(...) ? ... : 0 guard costs nothing.
  • #include <nuttx/net/tcp.h> only for TCP_SYN etc. pulls a kernel-internal header into an app. That's harmless in a flat build, but defining the six flag values locally (as the Linux tool does) might be cleaner. This is a question more than a request.

5. Merge order

This PR needs XT_MATCH_NAME_LIMIT, XT_LIMIT_SCALE and struct xt_rateinfo from apache/nuttx#22367, so that one has to be merged first (or CI here will fail on nuttx master).

The kernel filter can match TCP flags and a packet rate, but the
iptables and ip6tables commands cannot make such a rule.

Add these options of Linux iptables:

- --tcp-flags mask comp: the TCP flags to examine and the value that
  they must have.  --syn is "--tcp-flags SYN,RST,ACK,FIN SYN".  Both set
  flg_mask and flg_cmp of struct xt_tcp.  A rule with TCP flags and no
  port now also gets a TCP match.  As in Linux, the flag names can be in
  upper or lower case, and ALL and NONE can be items of the list.
- -m limit, --limit rate[/unit], --limit-burst number: a limit match
  (struct xt_rateinfo), with the defaults of Linux (3/hour, burst 5).
  The layout of an entry is fixed when it is allocated, so netlib gets
  netlib_ipt_filter_entry_limit() and netlib_ip6t_filter_entry_limit().
  They make a limit alone or a limit after a TCP, UDP or ICMP match.
  As in Linux, "!" before -m, --limit or --limit-burst gives an error.

iptables -L and ip6tables -L now show all matches of a rule, with the
TCP flags and the rate.

netlib now starts each match and the target of an entry at an offset
that is a multiple of XT_ALIGN(), as Linux iptables does.  So each
match_size is a multiple of XT_ALIGN(), which the kernel checks.  Before,
a TCP or UDP match had a match_size of 52 on 64-bit targets.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli

Copy link
Copy Markdown
Contributor Author

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants