Skip to contentSkip to conversation

Extract SYNPROXY infrastructure

PATCH Superseded Fernando Fernandez Mancera 2 participants 9 messages May 24, 2019 – May 27, 2019

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Extract SYNPROXY infrastructure

The patch series have been tested by enabling iptables and ip6tables SYNPROXY. All the modules loaded as expected.

$ lsmod | grep synproxy
Only IPv4:
nf_synproxy            20480  1 ipt_SYNPROXY
nf_synproxy_core       16384  2 ipt_SYNPROXY,nf_synproxy
nf_conntrack          159744  5 xt_conntrack,xt_state,ipt_SYNPROXY,nf_synproxy_core,nf_synproxy

Only IPv6:

nf_synproxy            20480  1 ip6t_SYNPROXY
nf_synproxy_core       16384  2 ip6t_SYNPROXY,nf_synproxy
nf_conntrack          159744  5 ip6t_SYNPROXY,xt_conntrack,xt_state,nf_synproxy_core,nf_synproxy

IPv4 and IPv6:

nf_synproxy            20480  2 ip6t_SYNPROXY,ipt_SYNPROXY
nf_synproxy_core       16384  3 ip6t_SYNPROXY,ipt_SYNPROXY,nf_synproxy
nf_conntrack          159744  6 ip6t_SYNPROXY,xt_conntrack,xt_state,ipt_SYNPROXY,nf_synproxy_core,nf_synproxy

v1: Initial patch
v2: Unify nf_synproxy_ipv4 and nf_synproxy_ipv6 into nf_synproxy
v3: Remove synproxy_cookie dependency

Fernando Fernandez Mancera (4):
netfilter: synproxy: add common uapi for SYNPROXY infrastructure
netfilter: synproxy: remove module dependency on IPv6 SYNPROXY
netfilter: synproxy: extract SYNPROXY infrastructure from

    {ipt,ip6t}_SYNPROXY

netfilter: add NF_SYNPROXY symbol

 include/linux/netfilter_ipv6.h             |  17 +
 include/net/netfilter/nf_synproxy.h        |  46 ++
 include/uapi/linux/netfilter/nf_SYNPROXY.h |  19 +
 include/uapi/linux/netfilter/xt_SYNPROXY.h |  18 +-
 net/ipv4/netfilter/Kconfig                 |   2 +-
 net/ipv4/netfilter/ipt_SYNPROXY.c          | 394 +---------
 net/ipv6/netfilter.c                       |   1 +
 net/ipv6/netfilter/Kconfig                 |   2 +-
 net/ipv6/netfilter/ip6t_SYNPROXY.c         | 420 +----------
 net/netfilter/Kconfig                      |   4 +
 net/netfilter/Makefile                     |   1 +
 net/netfilter/nf_synproxy.c                | 836 +++++++++++++++++++++
 12 files changed, 948 insertions(+), 812 deletions(-)
 create mode 100644 include/net/netfilter/nf_synproxy.h
 create mode 100644 include/uapi/linux/netfilter/nf_SYNPROXY.h
 create mode 100644 net/netfilter/nf_synproxy.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v3 1/4 netfilter: synproxy: add common uapi for SYNPROXY infrastructure

This new UAPI file is going to be used by the xt and nft common SYNPROXY infrastructure. It is needed to avoid duplicated code.

---
 include/uapi/linux/netfilter/nf_SYNPROXY.h | 19 +++++++++++++++++++
 include/uapi/linux/netfilter/xt_SYNPROXY.h | 18 +++++++-----------
 2 files changed, 26 insertions(+), 11 deletions(-)
 create mode 100644 include/uapi/linux/netfilter/nf_SYNPROXY.h
include/uapi/linux/netfilter/nf_SYNPROXY.h new file +19 -0
new file mode 100644
index 000000000000..068d1b3a6f06
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _NF_SYNPROXY_H
+#define _NF_SYNPROXY_H
+
+#include <linux/types.h>
+
+#define NF_SYNPROXY_OPT_MSS		0x01
+#define NF_SYNPROXY_OPT_WSCALE		0x02
+#define NF_SYNPROXY_OPT_SACK_PERM	0x04
+#define NF_SYNPROXY_OPT_TIMESTAMP	0x08
+#define NF_SYNPROXY_OPT_ECN		0x10
+
+struct nf_synproxy_info {
+	__u8	options;
+	__u8	wscale;
+	__u16	mss;
+};
+
+#endif /* _NF_SYNPROXY_H */
include/uapi/linux/netfilter/xt_SYNPROXY.h +7 -11
index ea5eba15d4c1..4d5611d647df 100644
@@ -2,18 +2,14 @@
 #ifndef _XT_SYNPROXY_H
 #define _XT_SYNPROXY_H
 
-#include <linux/types.h>
+#include <linux/netfilter/nf_SYNPROXY.h>
 
-#define XT_SYNPROXY_OPT_MSS		0x01
-#define XT_SYNPROXY_OPT_WSCALE		0x02
-#define XT_SYNPROXY_OPT_SACK_PERM	0x04
-#define XT_SYNPROXY_OPT_TIMESTAMP	0x08
-#define XT_SYNPROXY_OPT_ECN		0x10
+#define XT_SYNPROXY_OPT_MSS		NF_SYNPROXY_OPT_MSS
+#define XT_SYNPROXY_OPT_WSCALE		NF_SYNPROXY_OPT_WSCALE
+#define XT_SYNPROXY_OPT_SACK_PERM	NF_SYNPROXY_OPT_SACK_PERM
+#define XT_SYNPROXY_OPT_TIMESTAMP	NF_SYNPROXY_OPT_TIMESTAMP
+#define XT_SYNPROXY_OPT_ECN		NF_SYNPROXY_OPT_ECN
 
-struct xt_synproxy_info {
-	__u8	options;
-	__u8	wscale;
-	__u16	mss;
-};
+#define xt_synproxy_info		nf_synproxy_info
 
 #endif /* _XT_SYNPROXY_H */
Signature · 1 line
2.20.1
#2 lore raw

PATCH v3 2/4 netfilter: synproxy: remove module dependency on IPv6 SYNPROXY

In reply to Fernando Fernandez Mancera

This is a prerequisite for the new infrastructure module NF_SYNPROXY. The new module is needed to avoid duplicated code for the SYNPROXY nftables support.

---
 include/linux/netfilter_ipv6.h | 17 +++++++++++++++++
 net/ipv6/netfilter.c           |  1 +
 2 files changed, 18 insertions(+)
include/linux/netfilter_ipv6.h +17 -0
index 12113e502656..549a5df39cf9 100644
@@ -8,6 +8,7 @@
 #define __LINUX_IP6_NETFILTER_H
 
 #include <uapi/linux/netfilter_ipv6.h>
+#include <net/tcp.h>
 
 /* Extra routing may needed on local out, as the QUEUE target never returns
  * control to the table.
@@ -34,6 +35,8 @@ struct nf_ipv6_ops {
 		       struct in6_addr *saddr);
 	int (*route)(struct net *net, struct dst_entry **dst, struct flowi *fl,
 		     bool strict);
+	u32 (*cookie_init_sequence)(const struct ipv6hdr *iph,
+				    const struct tcphdr *th, u16 *mssp);
 #endif
 	void (*route_input)(struct sk_buff *skb);
 	int (*fragment)(struct net *net, struct sock *sk, struct sk_buff *skb,
@@ -102,6 +105,20 @@ static inline int nf_ip6_route_me_harder(struct net *net, struct sk_buff *skb)
 #endif
 }
 
+static inline u32 nf_ipv6_cookie_init_sequence(const struct ipv6hdr *iph,
+					       const struct tcphdr *th,
+					       u16 *mssp)
+{
+#if IS_MODULE(CONFIG_IPV6)
+	const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
+
+	if (v6_ops)
+		return v6_ops->cookie_init_sequence(iph, th, mssp);
+#else
+	return __cookie_v6_init_sequence(iph, th, mssp);
+#endif
+}
+
 __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
 			unsigned int dataoff, u_int8_t protocol);
 
net/ipv6/netfilter.c +1 -0
index 1240ccd57f39..32b8b1f470f4 100644
@@ -115,6 +115,7 @@ static const struct nf_ipv6_ops ipv6ops = {
 	.route_me_harder	= ip6_route_me_harder,
 	.dev_get_saddr		= ipv6_dev_get_saddr,
 	.route			= __nf_ip6_route,
+	.cookie_init_sequence	= __cookie_v6_init_sequence,
 #endif
 	.route_input		= ip6_route_input,
 	.fragment		= ip6_fragment,
Signature · 1 line
2.20.1
maintainer
#3 lore raw

Re: [PATCH nf-next v3 2/4] netfilter: synproxy: remove module dependency on IPv6 SYNPROXY

Quoted 21 lines · Fernando Fernandez Mancera

This is a prerequisite for the new infrastructure module NF_SYNPROXY. The new module is needed to avoid duplicated code for the SYNPROXY nftables support.

---
 include/linux/netfilter_ipv6.h | 17 +++++++++++++++++
 net/ipv6/netfilter.c           |  1 +
 2 files changed, 18 insertions(+)
include/linux/netfilter_ipv6.h +1 -0
index 12113e502656..549a5df39cf9 100644
@@ -8,6 +8,7 @@
 #define __LINUX_IP6_NETFILTER_H
 
 #include <uapi/linux/netfilter_ipv6.h>
+#include <net/tcp.h>
 
 /* Extra routing may needed on local out, as the QUEUE target never returns
  * control to the table.

include/linux/netfilter_ipv6.h struct nf_ipv6_ops { · line 39

Quoted 5 lines
include/linux/netfilter_ipv6.h +2 -0
@@ -34,6 +35,8 @@ struct nf_ipv6_ops {
 		       struct in6_addr *saddr);
 	int (*route)(struct net *net, struct dst_entry **dst, struct flowi *fl,
 		     bool strict);
+	u32 (*cookie_init_sequence)(const struct ipv6hdr *iph,
+				    const struct tcphdr *th, u16 *mssp);

This is good, but not enough:

/tmp/foo/./lib/modules/5.2.0-rc1+/kernel/net/netfilter/nf_synproxy.ko needs "__cookie_v6_check": /tmp/foo/./lib/modules/5.2.0-rc1+/kernel/net/ipv6/ipv6.ko

IOW, you need to also add the same trick for __cookie_v6_check.

Otherwise, an ipv4 only rule involving synproxy will pull in ipv6.ko module.

include/linux/netfilter_ipv6.h static inline int nf_ip6_route_me_harder(struct net *net, struct sk_buff *skb) · line 116

Quoted 9 lines
include/linux/netfilter_ipv6.h +9 -0
+static inline u32 nf_ipv6_cookie_init_sequence(const struct ipv6hdr *iph,
+					       const struct tcphdr *th,
+					       u16 *mssp)
+{
+#if IS_MODULE(CONFIG_IPV6)
+	const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
+
+	if (v6_ops)
+		return v6_ops->cookie_init_sequence(iph, th, mssp);

This triggers a compiler warning for me, because return value is undefined in !v6ops case.

I think you can just return 0 here for the !v6ops case.

#4 lore raw

PATCH v3 3/4 netfilter: synproxy: extract SYNPROXY infrastructure from {ipt,ip6t}_SYNPROXY

In reply to Fernando Fernandez Mancera

Add common functions into nf_synproxy.c to prepare for nftables support.

---
 include/net/netfilter/nf_synproxy.h |  46 ++
 net/ipv4/netfilter/ipt_SYNPROXY.c   | 394 +------------
 net/ipv6/netfilter/ip6t_SYNPROXY.c  | 420 +-------------
 net/netfilter/nf_synproxy.c         | 836 ++++++++++++++++++++++++++++
 4 files changed, 897 insertions(+), 799 deletions(-)
 create mode 100644 include/net/netfilter/nf_synproxy.h
 create mode 100644 net/netfilter/nf_synproxy.c
include/net/netfilter/nf_synproxy.h new file +46 -0
new file mode 100644
index 000000000000..859945cabc4c
@@ -0,0 +1,46 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _NF_SYNPROXY_SHARED_H
+#define _NF_SYNPROXY_SHARED_H
+
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <net/ip6_checksum.h>
+#include <net/ip6_route.h>
+#include <net/tcp.h>
+
+#include <net/netfilter/nf_conntrack_seqadj.h>
+#include <net/netfilter/nf_conntrack_synproxy.h>
+
+/* IPv4 support */
+void synproxy_send_client_synack(struct net *net, const struct sk_buff *skb,
+				 const struct tcphdr *th,
+				 const struct synproxy_options *opts);
+
+bool synproxy_recv_client_ack(struct net *net,
+			      const struct sk_buff *skb,
+			      const struct tcphdr *th,
+			      struct synproxy_options *opts, u32 recv_seq);
+
+unsigned int ipv4_synproxy_hook(void *priv, struct sk_buff *skb,
+				const struct nf_hook_state *nhs);
+int nf_synproxy_ipv4_init(struct synproxy_net *snet, struct net *net);
+void nf_synproxy_ipv4_fini(struct synproxy_net *snet, struct net *net);
+
+#if IS_ENABLED(CONFIG_IPV6) /* IPv6 support */
+void synproxy_send_client_synack_ipv6(struct net *net,
+				      const struct sk_buff *skb,
+				      const struct tcphdr *th,
+				      const struct synproxy_options *opts);
+
+bool synproxy_recv_client_ack_ipv6(struct net *net, const struct sk_buff *skb,
+				   const struct tcphdr *th,
+				   struct synproxy_options *opts, u32 recv_seq);
+
+unsigned int ipv6_synproxy_hook(void *priv, struct sk_buff *skb,
+				const struct nf_hook_state *nhs);
+int nf_synproxy_ipv6_init(struct synproxy_net *snet, struct net *net);
+void nf_synproxy_ipv6_fini(struct synproxy_net *snet, struct net *net);
+
+#endif /* IPv6 support */
+
+#endif /* _NF_SYNPROXY_SHARED_H */
net/ipv4/netfilter/ipt_SYNPROXY.c +6 -388

422 lines not on this page · raw message

net/ipv6/netfilter/ip6t_SYNPROXY.c +9 -411

461 lines not on this page · raw message

net/netfilter/nf_synproxy.c new file +836 -0

836 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#5 lore raw

Re: [PATCH nf-next v3 3/4] netfilter: synproxy: extract SYNPROXY infrastructure from {ipt,ip6t}_SYNPROXY

Quoted 14 lines · Fernando Fernandez Mancera
(quoted diff) +14 -0
+static void
+synproxy_send_tcp_ipv6(struct net *net,
+		       const struct sk_buff *skb, struct sk_buff *nskb,
+		       struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
+		       struct ipv6hdr *niph, struct tcphdr *nth,
+		       unsigned int tcp_hdr_size)
+{
+	struct dst_entry *dst;
+	struct flowi6 fl6;
+
+	nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0);
+	nskb->ip_summed   = CHECKSUM_PARTIAL;
+	nskb->csum_start  = (unsigned char *)nth - nskb->head;
+	nskb->csum_offset = offsetof(struct tcphdr, check);

Review

Quoted 10 lines
(quoted diff) +10 -0
+
+	memset(&fl6, 0, sizeof(fl6));
+	fl6.flowi6_proto = IPPROTO_TCP;
+	fl6.saddr = niph->saddr;
+	fl6.daddr = niph->daddr;
+	fl6.fl6_sport = nth->source;
+	fl6.fl6_dport = nth->dest;
+	security_skb_classify_flow((struct sk_buff *)skb,
+				   flowi6_to_flowi(&fl6));
+	dst = ip6_route_output(net, NULL, &fl6);

All good, BUT the above function call also pulls in ipv6.ko. You can fold this patch to avoid it, it coverts it to use the nf_ip6_route() wrapper which internally uses the v6ops pointer for ip6_route_output if needed.

net/netfilter/nf_synproxy.c +4 -4
@@ -434,6 +434,7 @@ synproxy_send_tcp_ipv6(struct net *net,
 {
 	struct dst_entry *dst;
 	struct flowi6 fl6;
+	int err;
 
 	nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0);
 	nskb->ip_summed   = CHECKSUM_PARTIAL;
@@ -448,11 +449,10 @@ synproxy_send_tcp_ipv6(struct net *net,
 	fl6.fl6_dport = nth->dest;
 	security_skb_classify_flow((struct sk_buff *)skb,
 				   flowi6_to_flowi(&fl6));
-	dst = ip6_route_output(net, NULL, &fl6);
-	if (dst->error) {
-		dst_release(dst);
+	err = nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
+	if (err)
 		goto free_nskb;
-	}
+
 	dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
 	if (IS_ERR(dst))
 		goto free_nskb;
#6 lore raw

PATCH v3 4/4 netfilter: add NF_SYNPROXY symbol

In reply to Fernando Fernandez Mancera
---
 net/ipv4/netfilter/Kconfig | 2 +-
 net/ipv6/netfilter/Kconfig | 2 +-
 net/netfilter/Kconfig      | 4 ++++
 net/netfilter/Makefile     | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)
net/ipv4/netfilter/Kconfig +1 -1
index 1412b029f37f..87f6ec800e54 100644
@@ -197,7 +197,7 @@ config IP_NF_TARGET_SYNPROXY
 	tristate "SYNPROXY target support"
 	depends on NF_CONNTRACK && NETFILTER_ADVANCED
 	select NETFILTER_SYNPROXY
-	select SYN_COOKIES
+	select NF_SYNPROXY
 	help
 	  The SYNPROXY target allows you to intercept TCP connections and
 	  establish them using syncookies before they are passed on to the
net/ipv6/netfilter/Kconfig +1 -1
index 086fc669279e..f71879f875e1 100644
@@ -213,7 +213,7 @@ config IP6_NF_TARGET_SYNPROXY
 	tristate "SYNPROXY target support"
 	depends on NF_CONNTRACK && NETFILTER_ADVANCED
 	select NETFILTER_SYNPROXY
-	select SYN_COOKIES
+	select NF_SYNPROXY
 	help
 	  The SYNPROXY target allows you to intercept TCP connections and
 	  establish them using syncookies before they are passed on to the
net/netfilter/Kconfig +4 -0
index 02b281d3c167..ae65fca0d509 100644
@@ -435,6 +435,10 @@ config NF_NAT_REDIRECT
 config NF_NAT_MASQUERADE
 	bool
 
+config NF_SYNPROXY
+	tristate
+	depends on NF_CONNTRACK && NETFILTER_ADVANCED
+
 config NETFILTER_SYNPROXY
 	tristate
 
net/netfilter/Makefile +1 -0
index 72cca6b48960..7a6067513eee 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_NF_NAT_TFTP) += nf_nat_tftp.o
 
 # SYNPROXY
 obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o
+obj-$(CONFIG_NF_SYNPROXY) += nf_synproxy.o
 
 obj-$(CONFIG_NETFILTER_CONNCOUNT) += nf_conncount.o
 
Signature · 1 line
2.20.1
maintainer
#7 lore raw

Re: [PATCH nf-next v3 4/4] netfilter: add NF_SYNPROXY symbol

Quoted 13 lines · Fernando Fernandez Mancera
---
 net/ipv4/netfilter/Kconfig | 2 +-
 net/ipv6/netfilter/Kconfig | 2 +-
 net/netfilter/Kconfig      | 4 ++++
 net/netfilter/Makefile     | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)
net/ipv4/netfilter/Kconfig +0 -0
index 1412b029f37f..87f6ec800e54 100644

net/ipv4/netfilter/Kconfig config IP_NF_TARGET_SYNPROXY · line 200

Quoted 5 lines
net/ipv4/netfilter/Kconfig +1 -1
@@ -197,7 +197,7 @@ config IP_NF_TARGET_SYNPROXY
 	tristate "SYNPROXY target support"
 	depends on NF_CONNTRACK && NETFILTER_ADVANCED
 	select NETFILTER_SYNPROXY
-	select SYN_COOKIES
+	select NF_SYNPROXY

What is the difference between NETFILTER_SYNPROXY and NF_SYNPROXY?

Any chance we can ...

net/netfilter/Makefile obj-$(CONFIG_NF_NAT_TFTP) += nf_nat_tftp.o · line 70

Quoted 9 lines
net/netfilter/Makefile +1 -0
index 72cca6b48960..7a6067513eee 100644
@@ -67,6 +67,7 @@ obj-$(CONFIG_NF_NAT_TFTP) += nf_nat_tftp.o
 
 # SYNPROXY
 obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o
+obj-$(CONFIG_NF_SYNPROXY) += nf_synproxy.o

... make nf_synproxy.o and nf_synproxy_core.o be part of same module?

(Or place it even in same .c file?)

maintainer
#8 lore raw

Re: [PATCH nf-next v3 4/4] netfilter: add NF_SYNPROXY symbol

In reply to Fernando Fernandez Mancera
Quoted 13 lines · Fernando Fernandez Mancera
---
 net/ipv4/netfilter/Kconfig | 2 +-
 net/ipv6/netfilter/Kconfig | 2 +-
 net/netfilter/Kconfig      | 4 ++++
 net/netfilter/Makefile     | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)
net/ipv4/netfilter/Kconfig +0 -0
index 1412b029f37f..87f6ec800e54 100644

net/ipv4/netfilter/Kconfig config IP_NF_TARGET_SYNPROXY · line 200

Quoted 5 lines
net/ipv4/netfilter/Kconfig +1 -1
@@ -197,7 +197,7 @@ config IP_NF_TARGET_SYNPROXY
 	tristate "SYNPROXY target support"
 	depends on NF_CONNTRACK && NETFILTER_ADVANCED
 	select NETFILTER_SYNPROXY
-	select SYN_COOKIES
+	select NF_SYNPROXY

Careful. This means I can now create a kernel config with IP_NF_TARGET_SYNPROXY=y
SYN_COOKIES=n

... and that will fail to build.