casaroli opened a new pull request, #22367: URL: https://github.com/apache/nuttx/pull/22367
## Summary The IP packet filter can match addresses, ports, the protocol and the interfaces. It cannot match a TCP SYN, and it cannot limit how many packets per second a rule accepts. So there is no rule that limits new TCP connections, for example: ``` iptables -A INPUT -p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT iptables -A INPUT -p tcp --syn -j DROP ``` This PR adds two matches of iptables to the filter: - net/ipfilter: the TCP flags match (`--tcp-flags`, `--syn`). `struct xt_tcp` already has `flg_mask` and `flg_cmp`, but the conversion did not copy them. - net/ipfilter: the `limit` match (`-m limit`). `struct xt_rateinfo` and `XT_LIMIT_SCALE` have the layout of Linux. Each rule has a token bucket. The credit unit is 1/10000 clock tick, so a rate above one packet per tick is also correct. The rule spends credit only when all its other parts match. - The rule above has two matches, but the conversion read only the first match of an entry. It now reads all matches, and one `convert_match()` replaces the protocol switch of the IPv4 and the IPv6 conversion. - net/netfilter: a first commit adds 5 blank lines that nxstyle asks for in `ipt_filter.c` (no code change; the CI checks the whole file). - Documentation: the IP packet filter page and the iptables and ip6tables pages describe the new matches and options. The iptables and ip6tables options come with apache/nuttx-apps#NNNNN. ## Impact - Only with `CONFIG_NET_IPFILTER` (default n). A rule without the new matches works as before. - Each filter rule takes about 32 (64-bit) to 40 (32-bit) more bytes (the bucket and the flag fields). Code size with the filter: +280 bytes of text on rv-virt:knetnsh64 with the filter (kernel only). No change without the filter. - No API change: `struct xt_rateinfo`, `XT_MATCH_NAME_LIMIT` and `XT_LIMIT_SCALE` are new. - Common network code: the same in flat, protected and kernel builds and on every architecture. ## Testing Host: Fedora x86_64, NuttX CI image (aarch64-none-elf-gcc 13.2.1, riscv-none-elf-gcc 14.3.0), `-Werror`. Target: qemu-armv8a:nsh (flat) with TCP, a TUN device, `NET_IPFILTER` and the iptables/ip6tables commands, under QEMU TCG (cortex-a53, 1 CPU). Master is 28db4789c34 with nuttx-apps master; this PR is tested with apache/nuttx-apps#NNNNN. A test program (not part of this PR) writes SYNs from spoofed sources to the TUN device, 200 per second for 10 s. After its first run the program has no listener, so TCP answers each SYN that passes the filter with a RST: the RSTs count the SYNs that pass. | Tick | Rules | SYNs sent | master: SYNs that pass | this PR: SYNs that pass | |---|---|---|---|---| | 1 ms | `-p tcp --syn -m limit --limit 5/s --limit-burst 10 -j ACCEPT`, then `-p tcp --syn -j DROP` | 2000 | all (TCP receives 2053 of 2053 packets) | TCP receives 104 of 2053 packets (all packets of the run) | | 1 ms | the same with `--limit 150/s` | 2000 | 2000 | 1521 (expected 10 + 151.5/s x 10 s) | | 1 ms | no rule | 2000 | 2000 | 2000 | | 10 ms | `--limit 150/s --limit-burst 10` | 1997 | - | 1518 | | 10 ms | `--limit 500/s --limit-burst 10`, 1000 SYNs per second | 9981 | - | 4989 | On master, iptables ignores `--syn` and `-m limit`, so the first rule accepts all TCP packets. The 10 ms rows show that a rate above one packet per tick is correct. - TCP flags: `-p tcp --dport 5025 --syn -j DROP` drops all 120 SYNs of a 20 SYN/s run (without it, 120 pass), and the outbound connections of the target still work (3 of 3). `--tcp-flags SYN,ACK SYN,ACK -j ACCEPT` makes the rule `flags:0x12/0x12`. `! --syn` and `ip6tables -A INPUT -p tcp --syn -m limit --limit 2/s -j ACCEPT` make the expected rules (`iptables -L`, `ip6tables -L`). - With the 5/s rule, the SYNs that pass still fill the 8 connections of the default pool, as before. apache/nuttx#22358 limits that part; this PR does not depend on it. - Builds with `-Werror`: qemu-armv8a:netnsh with IPv4 + IPv6 and with IPv4 only, qemu-armv8a:knsh, rv-virt:netnsh64, rv-virt:knetnsh64 (kernel builds with the apps imported), all with `NET_IPFILTER`, `NET_IPTABLES` and the iptables/ip6tables commands. - `tools/checkpatch.sh -c -u -m -g`: pass. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
