ipvs: add per-service secure_tcp
Subsystems (6)
v5 cover ipvs: add per-service secure_tcp
IPVS currently exposes secure_tcp as a per-netns sysctl. It switches the TCP state machine to the hardened tcp_states_dos table.
These patches make it per-service: a virtual service can set IP_VS_SVC_F_SECURE_TCP which is passed into IP_VS_CONN_F_SECURE_TCP at connection creation. set_tcp_state() then selects tcp_states_dos for those connections with IP_VS_CONN_F_SECURE_TCP set and keeps pd->tcp_state_table (the netns default, including the nomem floor) otherwise.
1. uapi: define the per-service secure_tcp flags and stamp the flag in ip_vs_bind_dest() (cache-bypass has no dest)
2. honor it in the TCP state machine, resolving the stale FIXME, and document the new default/opt-in split
3. kselftest contrasting a secure vs. a plain service
Changes in v2:
- Let IP_VS_SVC_F_SECURE_TCP be 0x0100, leaving 0x0040/0x0080 for the scheduler flags. Let IP_VS_CONN_F_SECURE_TCP be (1 << 17) and no longer in IP_VS_CONN_F_BACKUP_MASK.
- Set flag in ip_vs_bind_dest() instead of at every ip_vs_conn_new() call site.
- selftest: send probes with TTL=1 instead of an nft drop, and fix Sashiko comments.
Changes in v3:
- Merge patch 1/4 and 2/4
- Re-send patches (v2 dropped patches 3/4 and 4/4)
Changes in v4:
- selftest: check the return values of setsockopt(IP_HDRINCL), sendto() and mnl_socket_bind(), and fail do_add() when IPVS does not reply.
- selftest: fix the remaining Sashiko comments: check inet_pton(), initialize fam and the parsed addresses, and verify the flags attribute length before copying it.
Changes in v5:
- Document secure_tcp as the netns-wide default and the new per-service IP_VS_SVC_F_SECURE_TCP opt-in.
- selftest: reword the changelog to "a bare SYN followed by a bare ACK".
- selftest: skip when the ip_vs or ip_vs_rr module is unavailable.
- selftest: check the probe exit status so that a probe which dies after the SYN cannot leave the secure-side SYN_RECV assertion passing.
Adriano Cordova (3):
ipvs: add flags for per-service secure TCP state table
ipvs: tcp: enable per-connection secure_tcp in state machine
selftests: netfilter: ipvs: add per-service secure_tcp test
Documentation/networking/ipvs-sysctl.rst | 4 + include/uapi/linux/ip_vs.h | 2 + net/netfilter/ipvs/ip_vs_conn.c | 4 + net/netfilter/ipvs/ip_vs_core.c | 3 + net/netfilter/ipvs/ip_vs_proto_tcp.c | 16 +- .../testing/selftests/net/netfilter/Makefile | 6 + .../selftests/net/netfilter/gen_tcp_probe.c | 158 ++++++++ .../net/netfilter/ipvs_secure_tcp.sh | 169 +++++++++ .../net/netfilter/ipvs_secure_tcp_mln.c | 337 ++++++++++++++++++ 9 files changed, 692 insertions(+), 7 deletions(-) create mode 100644 tools/testing/selftests/net/netfilter/gen_tcp_probe.c create mode 100755 tools/testing/selftests/net/netfilter/ipvs_secure_tcp.sh create mode 100644 tools/testing/selftests/net/netfilter/ipvs_secure_tcp_mln.c
base-commit: 87b80c2f6b05cad9f0ff9136709c62a0f59923e3
Signature · 1 line
2.51.0
PATCH v5 1/3 ipvs: add flags for per-service secure TCP state table
Add IP_VS_SVC_F_SECURE_TCP to mark a virtual service for the DoS-hardened TCP connection state table, and IP_VS_CONN_F_SECURE_TCP to carry that capability on a connection so the TCP state machine can select the hardened table for it.
The service flag is 0x0100: bits 0x0008/0x0010/0x0020 are the scheduler bits, so leave 0x0040/0x0080 free for schedulers.
Set IP_VS_CONN_F_SECURE_TCP on every connection bound to a destination whose service carries IP_VS_SVC_F_SECURE_TCP.
---
Changes in v2:
- Let IP_VS_SVC_F_SECURE_TCP be 0x0100, leaving 0x0040/0x0080 free for scheduler flags.
- Let IP_VS_CONN_F_SECURE_TCP be (1 << 17) and drop it from IP_VS_CONN_F_BACKUP_MASK.
- Set the flag in ip_vs_bind_dest() instead of at every ip_vs_conn_new() call site.
Changes in v3:
- Merge the uapi flag definition and the ip_vs_bind_dest() stamping into a single patch.
(no changes since v3)
include/uapi/linux/ip_vs.h | 2 ++ net/netfilter/ipvs/ip_vs_conn.c | 4 ++++ net/netfilter/ipvs/ip_vs_core.c | 3 +++ 3 files changed, 9 insertions(+)
include/uapi/linux/ip_vs.h +2 -0
@@ -27,6 +27,7 @@#define IP_VS_SVC_F_SCHED_SH_FALLBACK IP_VS_SVC_F_SCHED1/* SH fallback */ #define IP_VS_SVC_F_SCHED_SH_PORT IP_VS_SVC_F_SCHED2/* SH use port */ +#define IP_VS_SVC_F_SECURE_TCP 0x0100/* use the hardened TCP table */ /* * IPVS sync daemon states
@@ -105,6 +106,7 @@/* Flags that are not sent to backup server start from bit 16 */ #define IP_VS_CONN_F_NFCT (1 << 16)/* use netfilter conntrack */ +#define IP_VS_CONN_F_SECURE_TCP (1 << 17)/* use the hardened TCP table */ /* Connection flags from destination that can be changed by user space */ #define IP_VS_CONN_F_DEST_MASK (IP_VS_CONN_F_FWD_MASK | \
net/netfilter/ipvs/ip_vs_conn.c +4 -0
@@ -1090,6 +1090,7 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) {unsigned int conn_flags;__u32 flags; +struct ip_vs_service *svc;/* if dest is NULL, then return directly */ if (!dest)
@@ -1102,6 +1103,9 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)if (cp->protocol !=IPPROTO_UDP ) conn_flags &= ~IP_VS_CONN_F_ONE_PACKET ; flags = cp->flags; + svc =rcu_dereference (dest->svc); +if (svc && (svc->flags &IP_VS_SVC_F_SECURE_TCP )) + flags |=IP_VS_CONN_F_SECURE_TCP ;/* Bind with the destination and its corresponding transmitter */ if (flags &IP_VS_CONN_F_SYNC ) {/* Synced conns are hashed, so they can not get this flag */
net/netfilter/ipvs/ip_vs_core.c +3 -0
@@ -784,6 +784,9 @@ int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, IP_VS_CONN_F_ONE_PACKET :0 ;union nf_inet_addr daddr = { .all = {0 ,0 ,0 ,0 } }; +if (svc->flags &IP_VS_SVC_F_SECURE_TCP ) + flags |=IP_VS_CONN_F_SECURE_TCP ; +/* create a new connection entry */ IP_VS_DBG (6 ,"%s(): create a cache_bypass entry\n" , __func__); {
Signature · 1 line
2.51.0
PATCH v5 2/3 ipvs: tcp: enable per-connection secure_tcp in state machine
In reply to Adriano CordovaLet set_tcp_state select tcp_states_dos when the connection carries IP_VS_CONN_F_SECURE_TCP, otherwise keep using the global pd->tcp_state_table (the netns default / or if nomem).
---
Changes in v5:
- Document that secure_tcp is the netns-wide default and that a service can opt in to the hardened table with IP_VS_SVC_F_SECURE_TCP.
Documentation/networking/ipvs-sysctl.rst | 4 ++++ net/netfilter/ipvs/ip_vs_proto_tcp.c | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-)
Documentation/networking/ipvs-sysctl.rst +4 -0
@@ -290,6 +290,10 @@ secure_tcp - INTEGER The value definition is the same as that of drop_entry and drop_packet. + This is the network-namespace default. A virtual service can opt + in to the hardened table regardless of this setting by setting + IP_VS_SVC_F_SECURE_TCP (0x0100) in its service flags. + svc_lfactor - INTEGER Possible values: -8 (larger table) .. 8 (smaller table)
net/netfilter/ipvs/ip_vs_proto_tcp.c +9 -7
@@ -451,11 +451,10 @@ static void tcp_timeout_change(struct ip_vs_proto_data *pd, int flags)int on = (flags &1 );/* secure_tcp */ /* -** FIXME: change secure_tcp to independent sysctl var -** or make it per-service or per-app because it is valid -** for most if not for all of the applications. Something -** like "capabilities" (flags) for each object. -*/ +* This remains the netns-wide default / global floor (e.g. when +* memory pressure kicks in). Per-service hardening is now carried +* by IP_VS_CONN_F_SECURE_TCP on each connection (set_tcp_state). +*/ pd->tcp_state_table = (on ? tcp_states_dos : tcp_states); }
@@ -479,6 +478,7 @@ set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp,int state_idx;int new_state =IP_VS_TCP_S_CLOSE ;int state_off = tcp_state_off[direction]; +const struct tcp_states_t *table;/* * Update state offset to INPUT_ONLY if necessary
@@ -496,8 +496,10 @@ set_tcp_state(struct ip_vs_proto_data *pd, struct ip_vs_conn *cp, goto tcp_state_out; } - new_state = - pd->tcp_state_table[state_off+state_idx].next_state[cp->state]; + table = pd->tcp_state_table; +if (cp->flags &IP_VS_CONN_F_SECURE_TCP ) + table = tcp_states_dos; + new_state = table[state_off + state_idx].next_state[cp->state]; tcp_state_out:if (new_state != cp->state) {
Signature · 1 line
2.51.0
PATCH v5 3/3 selftests: netfilter: ipvs: add per-service secure_tcp test
In reply to Adriano CordovaTwo services share a VIP, one carrying IP_VS_SVC_F_SECURE_TCP. A bare SYN followed by a bare ACK suffices to test the state
machine: the normal service reaches ESTABLISHED, but the secure one stays in SYN_RECV.
---
Changes in v2:
- send probes with TTL=1 instead of an nft drop, and fix Sashiko comments.
Changes in v4:
- Check the return value of setsockopt(IP_HDRINCL), sendto() and mnl_socket_bind(), and fail do_add() when IPVS does not reply.
- Check inet_pton(), initialize fam and the parsed addresses, and verify the flags attribute length before copying it (Sashiko).
Changes in v5:
- Reword the changelog: the probe sends a bare SYN followed by a bare ACK, not a single SYN+ACK.
- Skip the test when the ip_vs or ip_vs_rr module is unavailable.
- Check the probe exit status so a probe that dies after the SYN cannot leave the secure-side SYN_RECV assertion passing.
.../testing/selftests/net/netfilter/Makefile | 6 + .../selftests/net/netfilter/gen_tcp_probe.c | 158 ++++++++ .../net/netfilter/ipvs_secure_tcp.sh | 169 +++++++++ .../net/netfilter/ipvs_secure_tcp_mln.c | 337 ++++++++++++++++++ 4 files changed, 670 insertions(+) create mode 100644 tools/testing/selftests/net/netfilter/gen_tcp_probe.c create mode 100755 tools/testing/selftests/net/netfilter/ipvs_secure_tcp.sh create mode 100644 tools/testing/selftests/net/netfilter/ipvs_secure_tcp_mln.c
tools/testing/selftests/net/netfilter/Makefile +6 -0
@@ -22,6 +22,7 @@ TEST_PROGS := \ conntrack_tcp_unreplied.sh \ conntrack_vrf.sh \ ipvs.sh \ + ipvs_secure_tcp.sh \ nf_conntrack_packetdrill.sh \ nf_nat_edemux.sh \ nft_audit.sh \
@@ -52,6 +53,8 @@ TEST_GEN_FILES = \ connect_close \ conntrack_dump_flush \ conntrack_reverse_clash \ + gen_tcp_probe \ + ipvs_secure_tcp_mln \ nf_queue \ sctp_collision \ udpclash \
@@ -62,6 +65,9 @@ include ../../lib.mk $(OUTPUT)/nf_queue: CFLAGS += $(MNL_CFLAGS) $(OUTPUT)/nf_queue: LDLIBS += $(MNL_LDLIBS) +$(OUTPUT)/ipvs_secure_tcp_mln: CFLAGS += $(MNL_CFLAGS) +$(OUTPUT)/ipvs_secure_tcp_mln: LDLIBS += $(MNL_LDLIBS) + $(OUTPUT)/conntrack_dump_flush: CFLAGS += $(MNL_CFLAGS) $(OUTPUT)/conntrack_dump_flush: LDLIBS += $(MNL_LDLIBS) $(OUTPUT)/udpclash: LDLIBS += -lpthread
tools/testing/selftests/net/netfilter/gen_tcp_probe.c new file +158 -0
158 lines not on this page · raw message
tools/testing/selftests/net/netfilter/ipvs_secure_tcp.sh new file +169 -0
169 lines not on this page · raw message
tools/testing/selftests/net/netfilter/ipvs_secure_tcp_mln.c new file +337 -0
337 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v5 nf-next 3/3] selftests: netfilter: ipvs: add per-service secure_tcp test
From: David Dull
To: Adriano Cordova
This patch has already been reviewed by the netdev bot with Sashiko on 2026-09-21 for the v4 revision of this same selftest patch. That review found three possible issues. First the changelog wording claimed a bare SYN ACK suffices but the probe actually sends a bare SYN followed by a separate bare ACK. Second the script has no kernel side prerequisite check and no skip path for when ip_vs is unavailable. Third the secure side assertion cannot distinguish between the per service secure_tcp working correctly and the ACK probe never arriving at all.
This v5 revision appears to address the first and second points by rewording the changelog to say a bare SYN followed by a bare ACK and by adding module availability checks for ip_vs and ip_vs_rr. The third concern about the assertion being ambiguous if the ACK probe fails is addressed by the addition of checking the probe exit status so a probe that dies after the SYN cannot leave the secure side SYN_RECV assertion passing incorrectly.
The code itself is well structured with proper error checking in the libmnl helper for setsockopt sendto and mnl_socket_bind return values. The fallback definition for IP_VS_SVC_F_SECURE_TCP in the helper is a reasonable approach for older userspace headers. The test topology and the approach of using TTL one probes to prevent the packets from reaching the real server is sound.
However there is one remaining concern. The assertion that the plain service reaches ESTABLISHED state depends on the probe successfully sending both the SYN and the ACK. If the second sendto call fails in the probe the exit status will be non zero and the test will report failure. But if the ACK packet is sent successfully and simply does not reach IPVS for some reason the connection may still be in SYN_RECV when the assertion runs. The sleep between the probe and the assertion is only one second which may not be sufficient on a heavily loaded system. Consider increasing the sleep or adding a retry loop for the state check.
Overall the patch is in good shape and addresses the prior review comments appropriately.
Re: [PATCH v5 nf-next 3/3] selftests: netfilter: ipvs: add per-service secure_tcp test (David Dull)
Also to avoid misunderstanding. git added the signed-off line unintentionally. please ignore it.
Julian Anastasov
maintainerRe: [PATCH v5 nf-next 1/3] ipvs: add flags for per-service secure TCP state table
In reply to Adriano CordovaHello,
Quoted 65 lines · Adriano Cordova
Add
IP_VS_SVC_F_SECURE_TCPto mark a virtual service for the DoS-hardened TCP connection state table, andIP_VS_CONN_F_SECURE_TCPto carry that capability on a connection so the TCP state machine can select the hardened table for it.The service flag is
0x0100: bits 0x0008/0x0010/0x0020 are the scheduler bits, so leave 0x0040/0x0080 free for schedulers.Set
IP_VS_CONN_F_SECURE_TCPon every connection bound to a destination whose service carriesIP_VS_SVC_F_SECURE_TCP.Signed-off-by Adriano Cordova <[email protected]>---
Changes in v2:
- LetIP_VS_SVC_F_SECURE_TCPbe0x0100, leaving 0x0040/0x0080 free for scheduler flags.
- LetIP_VS_CONN_F_SECURE_TCPbe (1 << 17) and drop it fromIP_VS_CONN_F_BACKUP_MASK.
- Set the flag inip_vs_bind_dest()instead of at everyip_vs_conn_new()call site.Changes in v3:
- Merge the uapi flag definition and theip_vs_bind_dest()stamping into a single patch.(no changes since v3)
include/uapi/linux/ip_vs.h | 2 ++ net/netfilter/ipvs/ip_vs_conn.c | 4 ++++ net/netfilter/ipvs/ip_vs_core.c | 3 +++ 3 files changed, 9 insertions(+)include/uapi/linux/ip_vs.h +2
-0@@ -27,6 +27,7 @@#define IP_VS_SVC_F_SCHED_SH_FALLBACK IP_VS_SVC_F_SCHED1/* SH fallback */ #define IP_VS_SVC_F_SCHED_SH_PORT IP_VS_SVC_F_SCHED2/* SH use port */ +#define IP_VS_SVC_F_SECURE_TCP 0x0100/* use the hardened TCP table */ /* * IPVS sync daemon states @@ -105,6 +106,7 @@/* Flags that are not sent to backup server start from bit 16 */ #define IP_VS_CONN_F_NFCT (1 << 16)/* use netfilter conntrack */ +#define IP_VS_CONN_F_SECURE_TCP (1 << 17)/* use the hardened TCP table */ /* Connection flags from destination that can be changed by user space */ #define IP_VS_CONN_F_DEST_MASK (IP_VS_CONN_F_FWD_MASK | \net/netfilter/ipvs/ip_vs_conn.c +1
-0@@ -1090,6 +1090,7 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) {unsigned int conn_flags;__u32 flags; +struct ip_vs_service *svc;/* if dest is NULL, then return directly */ if (!dest)
net/netfilter/ipvs/ip_vs_conn.c ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest) · line 1108
Quoted 6 lines
net/netfilter/ipvs/ip_vs_conn.c +3
-0@@ -1102,6 +1103,9 @@ ip_vs_bind_dest(struct ip_vs_conn *cp, struct ip_vs_dest *dest)if (cp->protocol !=IPPROTO_UDP ) conn_flags &= ~IP_VS_CONN_F_ONE_PACKET ; flags = cp->flags; + svc =rcu_dereference (dest->svc); +if (svc && (svc->flags &IP_VS_SVC_F_SECURE_TCP )) + flags |=IP_VS_CONN_F_SECURE_TCP ;
Adriano, due to the long IPVS bugfix queue, sometimes
the -next work may conflict with other changes. This is the case with the above hunk. Looks like we should wait the patch "ipvs: do not create invisible templates" to be included into the net tree, the net tree to be merged into the net-next and nf-next tree and then you can post a version for nf-next which will be on top of this patch. In the mean time I'll review your changes soon, so that the patchset can be ready for that moment.
Quoted 19 lines
net/netfilter/ipvs/ip_vs_conn.c +0
-0/* Bind with the destination and its corresponding transmitter */ if (flags &IP_VS_CONN_F_SYNC ) {/* Synced conns are hashed, so they can not get this flag */ net/netfilter/ipvs/ip_vs_core.c +3
-0@@ -784,6 +784,9 @@ int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb, IP_VS_CONN_F_ONE_PACKET :0 ;union nf_inet_addr daddr = { .all = {0 ,0 ,0 ,0 } }; +if (svc->flags &IP_VS_SVC_F_SECURE_TCP ) + flags |=IP_VS_CONN_F_SECURE_TCP ; +/* create a new connection entry */ IP_VS_DBG (6 ,"%s(): create a cache_bypass entry\n" , __func__); {Signature · 1 line
2.51.0
Regards
--
Julian Anastasov <[email protected]>