netfilter: add netfilter hooks to track SRv6-encapsulated flows
Subsystems (5)
v7 cover netfilter: add netfilter hooks to track SRv6-encapsulated flows
Tunneling protocols such as VXLAN or IPIP are implemented using virtual network devices (vxlan0 or ipip0). Therefore, conntrack can record both inner flows and outer flows correctly. In contrast, SRv6 is implemented using lightweight tunnel infrastructure. Therefore, SRv6 packets are encapsulated and decapsulated without passing through virtual network device. Due to the following problems caused by this, conntrack can't record both inner flows and outer flows correctly.
First problem is caused when SRv6 packets are encapsulated. In VXLAN, at first, packets received are passed to nf_conntrack_in called from ip_rcv/ipv6_rcv. These packets are sent to virtual network device and these flows are confirmed in ip_output/ip6_output. However, in SRv6, at first, packets are passed to nf_conntrack_in, encapsulated and flows are confirmed in ipv6_output even if inner packets are IPv4. Therefore, IPv6 conntrack needs to be enabled to track IPv4 inner flow.
Second problem is caused when SRv6 packets are decapsulated. If IPv6 conntrack is enabled, SRv6 packets are passed to nf_conntrack_in called from ipv6_rcv. Even if inner packets are passed to nf_conntrack_in after packets are decapsulated, flow aren't tracked because skb->_nfct is already set. Therefore, IPv6 conntrack needs to be disabled to track IPv4 flow when packets are decapsulated.
This patch series solves these problems and allows conntrack to record inner flows correctly. It introduces netfilter hooks to srv6 lwtunnel and srv6local lwtunnel. It also introduces new sysctl toggle to turn on lightweight tunnel netfilter hooks. You can enable lwtunnel netfilter as following:
sysctl net.netfilter.nf_hooks_lwtunnel=1
v7: fix some comments
v6: apply some code chunks suggested
v5: rename nf_conntrack_lwtunnel to nf_hooks_lwtunnel, fix link error
when CONFIG_SYSCTL, add example code
v4: fix compile error when CONFIG_LWTUNNEL isn't enabled
v3: fix warning in nf_conntrack_lwtunnel.c
v2: introduce nf_ct_lwtunnel_enabled static_key and sysctl toggle to turn
on lightweight tunnel netfilter hook
Ryoga Saito (2):
netfilter: add new sysctl toggle for lightweight tunnel netfilter
hooks
netfilter: add netfilter hooks to SRv6 data plane
.../networking/nf_conntrack-sysctl.rst | 7 ++ include/net/lwtunnel.h | 3 + include/net/netfilter/nf_hooks_lwtunnel.h | 15 +++ net/core/lwtunnel.c | 3 + net/ipv6/seg6_iptunnel.c | 74 +++++++++++- net/ipv6/seg6_local.c | 110 ++++++++++++------ net/netfilter/Makefile | 3 + net/netfilter/nf_conntrack_standalone.c | 15 +++ net/netfilter/nf_hooks_lwtunnel.c | 66 +++++++++++ 9 files changed, 260 insertions(+), 36 deletions(-) create mode 100644 include/net/netfilter/nf_hooks_lwtunnel.h create mode 100644 net/netfilter/nf_hooks_lwtunnel.c
Signature · 1 line
2.25.1
PATCH v7 1/2 netfilter: add new sysctl toggle for lightweight tunnel netfilter hooks
This patch introduces new sysctl toggle for enabling lightweight tunnel netfilter hooks.
--- .../networking/nf_conntrack-sysctl.rst | 7 ++ include/net/lwtunnel.h | 3 + include/net/netfilter/nf_hooks_lwtunnel.h | 15 +++++ net/core/lwtunnel.c | 3 + net/netfilter/Makefile | 3 + net/netfilter/nf_conntrack_standalone.c | 15 +++++ net/netfilter/nf_hooks_lwtunnel.c | 66 +++++++++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 include/net/netfilter/nf_hooks_lwtunnel.h create mode 100644 net/netfilter/nf_hooks_lwtunnel.c
Documentation/networking/nf_conntrack-sysctl.rst +7 -0
@@ -184,6 +184,13 @@ nf_conntrack_gre_timeout_stream - INTEGER (seconds) This extended timeout will be used in case there is an GRE stream detected. +nf_hooks_lwtunnel - BOOLEAN + - 0 - disabled (default) + - not 0 - enabled + + If this option is enabled, the lightweight tunnel netfilter hooks are + enabled. This option cannot be disabled once it is enabled. + nf_flowtable_tcp_timeout - INTEGER (seconds) default 30
include/net/lwtunnel.h +3 -0
@@ -51,6 +51,9 @@ struct lwtunnel_encap_ops { };#ifdef CONFIG_LWTUNNEL + +DECLARE_STATIC_KEY_FALSE (nf_hooks_lwtunnel_enabled); +void lwtstate_free (struct lwtunnel_state *lws); static inline struct lwtunnel_state *
include/net/netfilter/nf_hooks_lwtunnel.h new file +15 -0
@@ -0,0 +1,15 @@ +#include <linux/sysctl.h> +#include <linux/types.h> + +#ifdef CONFIG_LWTUNNEL +int nf_hooks_lwtunnel_sysctl_handler (struct ctl_table *table,int write, +void *buffer,size_t *lenp, +loff_t *ppos); +#else +int nf_hooks_lwtunnel_sysctl_handler (struct ctl_table *table,int write, +void *buffer,size_t *lenp, +loff_t *ppos) +{ +return 0 ; +} +#endif
net/core/lwtunnel.c +3 -0
@@ -23,6 +23,9 @@#include <net/ip6_fib.h> #include <net/rtnh.h> +DEFINE_STATIC_KEY_FALSE (nf_hooks_lwtunnel_enabled); +EXPORT_SYMBOL_GPL (nf_hooks_lwtunnel_enabled); +#ifdef CONFIG_MODULES static const char *lwtunnel_encap_str (enum lwtunnel_encap_types encap_type)
net/netfilter/Makefile +3 -0
@@ -212,3 +212,6 @@ obj-$(CONFIG_IP_SET) += ipset/ # IPVS obj-$(CONFIG_IP_VS) += ipvs/ + +# lwtunnel +obj-$(CONFIG_LWTUNNEL) += nf_hooks_lwtunnel.o
net/netfilter/nf_conntrack_standalone.c +15 -0
@@ -22,6 +22,9 @@#include <net/netfilter/nf_conntrack_acct.h> #include <net/netfilter/nf_conntrack_zones.h> #include <net/netfilter/nf_conntrack_timestamp.h> +#ifdef CONFIG_LWTUNNEL +#include <net/netfilter/nf_hooks_lwtunnel.h> +#endif #include <linux/rculist_nulls.h> static bool enable_hooks__read_mostly ;
@@ -612,6 +615,9 @@ enum nf_ct_sysctl_index { NF_SYSCTL_CT_PROTO_TIMEOUT_GRE, NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,#endif +#ifdef CONFIG_LWTUNNEL +NF_SYSCTL_CT_LWTUNNEL , +#endif __NF_SYSCTL_CT_LAST_SYSCTL, };
@@ -958,6 +964,15 @@ static struct ctl_table nf_ct_sysctl_table[] = { .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, +#endif +#ifdef CONFIG_LWTUNNEL + [NF_SYSCTL_CT_LWTUNNEL] = { + .procname = "nf_hooks_lwtunnel", + .data = NULL, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = nf_hooks_lwtunnel_sysctl_handler, + },#endif {} };
net/netfilter/nf_hooks_lwtunnel.c new file +66 -0
66 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v7 2/2 netfilter: add netfilter hooks to SRv6 data plane
In reply to Ryoga SaitoThis patch introduces netfilter hooks for solving the problem that conntrack couldn't record both inner flows and outer flows.
--- net/ipv6/seg6_iptunnel.c | 74 ++++++++++++++++++++++++-- net/ipv6/seg6_local.c | 110 +++++++++++++++++++++++++++------------ 2 files changed, 148 insertions(+), 36 deletions(-)
net/ipv6/seg6_iptunnel.c +71 -3
@@ -26,6 +26,7 @@#ifdef CONFIG_IPV6_SEG6_HMAC #include <net/seg6_hmac.h> #endif +#include <net/lwtunnel.h> static size_t seg6_lwt_headroom (struct seg6_iptunnel_encap *tuninfo) {
@@ -295,11 +296,19 @@ static int seg6_do_srh(struct sk_buff *skb)ipv6_hdr (skb)->payload_len =htons (skb->len -sizeof (struct ipv6hdr ));skb_set_transport_header (skb,sizeof (struct ipv6hdr )); +nf_reset_ct (skb);return 0 ; } -static int seg6_input (struct sk_buff *skb) +static int seg6_input_finish (struct net *net,struct sock *sk, +struct sk_buff *skb) +{ +return dst_input (skb); +} + +static int seg6_input_core (struct net *net,struct sock *sk, +struct sk_buff *skb) {struct dst_entry *orig_dst =skb_dst (skb);struct dst_entry *dst =NULL ;
@@ -337,10 +346,41 @@ static int seg6_input(struct sk_buff *skb)if (unlikely (err))return err; -return dst_input (skb); +if (static_branch_unlikely (&nf_hooks_lwtunnel_enabled)) +return NF_HOOK (NFPROTO_IPV6 ,NF_INET_LOCAL_OUT , +dev_net (skb->dev),NULL , skb,NULL , +skb_dst (skb)->dev, seg6_input_finish); + +return seg6_input_finish (dev_net (skb->dev),NULL , skb); } -static int seg6_output (struct net *net,struct sock *sk,struct sk_buff *skb) +static int seg6_input_nf (struct sk_buff *skb) +{ +struct net_device *dev =skb_dst (skb)->dev; +struct net *net =dev_net (skb->dev); + +switch (skb->protocol) { +case htons (ETH_P_IP ): +return NF_HOOK (NFPROTO_IPV4 ,NF_INET_POST_ROUTING , net,NULL , + skb,NULL , dev, seg6_input_core); +case htons (ETH_P_IPV6 ): +return NF_HOOK (NFPROTO_IPV6 ,NF_INET_POST_ROUTING , net,NULL , + skb,NULL , dev, seg6_input_core); + } + +return -EINVAL ; +} + +static int seg6_input (struct sk_buff *skb) +{ +if (static_branch_unlikely (&nf_hooks_lwtunnel_enabled)) +return seg6_input_nf (skb); + +return seg6_input_core (dev_net (skb->dev),NULL , skb); +} + +static int seg6_output_core (struct net *net,struct sock *sk, +struct sk_buff *skb) {struct dst_entry *orig_dst =skb_dst (skb);struct dst_entry *dst =NULL ;
@@ -387,12 +427,40 @@ static int seg6_output(struct net *net, struct sock *sk, struct sk_buff *skb)if (unlikely (err)) goto drop; +if (static_branch_unlikely (&nf_hooks_lwtunnel_enabled)) +return NF_HOOK (NFPROTO_IPV6 ,NF_INET_LOCAL_OUT , net, sk, skb, +NULL ,skb_dst (skb)->dev, dst_output); +return dst_output (net, sk, skb); drop:kfree_skb (skb);return err; } +static int seg6_output_nf (struct net *net,struct sock *sk,struct sk_buff *skb) +{ +struct net_device *dev =skb_dst (skb)->dev; + +switch (skb->protocol) { +case htons (ETH_P_IP ): +return NF_HOOK (NFPROTO_IPV4 ,NF_INET_POST_ROUTING , net, sk, skb, +NULL , dev, seg6_output_core); +case htons (ETH_P_IPV6 ): +return NF_HOOK (NFPROTO_IPV6 ,NF_INET_POST_ROUTING , net, sk, skb, +NULL , dev, seg6_output_core); + } + +return -EINVAL ; +} + +static int seg6_output (struct net *net,struct sock *sk,struct sk_buff *skb) +{ +if (static_branch_unlikely (&nf_hooks_lwtunnel_enabled)) +return seg6_output_nf (net, sk, skb); + +return seg6_output_core (net, sk, skb); +} + static int seg6_build_state(struct net *net, struct nlattr *nla, unsigned int family, const void *cfg, struct lwtunnel_state **ts,
net/ipv6/seg6_local.c +77 -33
179 lines not on this page · raw message
Signature · 1 line
2.25.1
Quoted 23 lines · Ryoga Saito
Tunneling protocols such as VXLAN or IPIP are implemented using virtual network devices (vxlan0 or ipip0). Therefore, conntrack can record both inner flows and outer flows correctly. In contrast, SRv6 is implemented using lightweight tunnel infrastructure. Therefore, SRv6 packets are encapsulated and decapsulated without passing through virtual network device. Due to the following problems caused by this, conntrack can't record both inner flows and outer flows correctly.
First problem is caused when SRv6 packets are encapsulated. In VXLAN, at first, packets received are passed to
nf_conntrack_incalled from ip_rcv/ipv6_rcv. These packets are sent to virtual network device and these flows are confirmed in ip_output/ip6_output. However, in SRv6, at first, packets are passed tonf_conntrack_in, encapsulated and flows are confirmed inipv6_outputeven if inner packets are IPv4. Therefore, IPv6 conntrack needs to be enabled to track IPv4 inner flow.Second problem is caused when SRv6 packets are decapsulated. If IPv6 conntrack is enabled, SRv6 packets are passed to
nf_conntrack_incalled fromipv6_rcv. Even if inner packets are passed tonf_conntrack_inafter packets are decapsulated, flow aren't tracked becauseskb->_nfctis already set. Therefore, IPv6 conntrack needs to be disabled to track IPv4 flow when packets are decapsulated.
Quoted 7 lines
This patch series solves these problems and allows conntrack to record inner flows correctly. It introduces netfilter hooks to srv6 lwtunnel and srv6local lwtunnel. It also introduces new sysctl toggle to turn on lightweight tunnel netfilter hooks. You can enable lwtunnel netfilter as following:
sysctl net.netfilter.nf_hooks_lwtunnel=1
Applied to nf-next with a few edits. I'll post it to net-next in the next pull request.