Skip to contentSkip to conversation

connection tracking support for bridge

RFC PATCH In review Pablo Neira Ayuso 2 participants 10 messages Apr 3, 2019 – Apr 4, 2019

Subsystems (5)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover connection tracking support for bridge

Hi,

This patchset provides native connection tracking support for bridge, *this is work-in-progress RFC*.

ebtables has a very limited feature-set compared to iptables, among them no connection tracking support. Long time ago, to overcome this limitation, someone came up with the bridge netfilter infrastructure - the `br_netfilter' kernel module. This module steals frames from bridge prerouting hook, mangle them to make them look like IPv4 packets to make iptables happy, eg. by attaching a fake dst object, then inject them into the IP hooks, then take them back into the bridge frame layout to call the bridge routines. Going back and forth through all the netfilter IP and bridge hooks, not nice. In turn, this `br_netfilter' infrastructure has been allowing users to use iptables, conntrack and NAT for bridged frames, instead of ebtables. An iptables extension called 'physdev' was introduced at the time to match on the bridge interface as a way to expose some of the semantics behind this emulation layer.

However, along time, many design problems uncovered with this `br_netfilter' emulation, becoming a common topic of discussion.

This patchset is an initial step to introduce a replacement for this br_netfilter' emulation layer by introducing one of the missing features that makes users turn on this br_netfilter' emulation layer.

This patchset includes updates to expose two new refragmentation APIs in order to reuse them from the bridge. The existing ip_do_fragment() calls rely on dst objects that are not available from there, this function also triggers refragmentation where the maximum fragment size is larger than the device MTU, altering fragments geometry in that case, something that a bridge should not do, among other things. I think this new refragment infrastructure could be reused by folks with codebase away from the classic IP stack, eg. OVS.

Comments welcome, thanks.

Pablo Neira Ayuso (8):
net: use kfree_skb_list() from ip_do_fragment()
net: ipv4: add skbuff fraglist split iterator
net: ipv6: add skbuff fraglist split iterator
net: ipv4: split skbuff into fragments transformer
net: ipv6: split skbuff into fragments transformer
net: ipv4: place cb handling away from fraglist iterator
net: ipv4: place cb handling away from fragment transformer
netfilter: bridge: add basic conntrack support

 include/net/ip.h                           |  39 +++
 include/net/ipv6.h                         |  44 +++
 net/bridge/br_private.h                    |   2 +-
 net/bridge/netfilter/Kconfig               |  14 +
 net/bridge/netfilter/Makefile              |   3 +
 net/bridge/netfilter/nf_conntrack_bridge.c | 411 +++++++++++++++++++++++++++++
 net/ipv4/ip_output.c                       | 314 +++++++++++++---------
 net/ipv6/ip6_output.c                      | 315 +++++++++++++---------
 8 files changed, 884 insertions(+), 258 deletions(-)
 create mode 100644 net/bridge/netfilter/nf_conntrack_bridge.c
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

RFC PATCH 1/8 net: use kfree_skb_list() from ip_do_fragment()

Just like 46cfd725c377 ("net: use kfree_skb_list() helper in more places").

---
 net/ipv4/ip_output.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
net/ipv4/ip_output.c +2 -5
index c80188875f39..10b35328cfbc 100644
@@ -693,11 +693,8 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 			return 0;
 		}
 
-		while (frag) {
-			skb = frag->next;
-			kfree_skb(frag);
-			frag = skb;
-		}
+		kfree_skb_list(frag);
+
 		IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
 		return err;
 
Signature · 1 line
2.11.0
maintainer Acked-by on v1 1/8
#2 lore raw

Re: [PATCH net-next,RFC 1/8] net: use kfree_skb_list() from ip_do_fragment()

Quoted 1 line · Pablo Neira Ayuso

Just like 46cfd725c377 ("net: use kfree_skb_list() helper in more places").

Please consider submitting this as-is as standalone patch.

author maintainer
#3 lore raw

RFC PATCH 2/8 net: ipv4: add skbuff fraglist split iterator

In reply to Pablo Neira Ayuso

This patch adds skbuff fraglist split iterator. The API consists of:

* ip_fraglist_init(), that initializes the internal state of the fraglist iterator.
* ip_fraglist_prepare(), that restores the IPv4 and transport headers on the fragment.
* ip_fraglist_next(), that retrieves the fragment from the fraglist and updates the internal state of the iterator to point to the next fragment in the fraglist.

The ip_fraglist_iter object stores the internal state of the iterator.

This code has been extracted from ip_do_fragment(). Symbols are also exported to allow to reuse this iterator from the bridge codepath for refragmentation.

---
 include/net/ip.h     | 23 ++++++++++++++
 net/ipv4/ip_output.c | 88 ++++++++++++++++++++++++++++++++--------------------
 2 files changed, 78 insertions(+), 33 deletions(-)
include/net/ip.h +23 -0
index aa09ae5f01a5..cc3d83680f4f 100644
@@ -165,6 +165,29 @@ int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
 int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 		   int (*output)(struct net *, struct sock *, struct sk_buff *));
+
+struct ip_fraglist_iter {
+	struct sk_buff	*frag_list;
+	struct sk_buff	*frag;
+	struct iphdr	*iph;
+	int		offset;
+	unsigned int	hlen;
+};
+
+void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph,
+		      unsigned int hlen, struct ip_fraglist_iter *iter);
+void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter);
+
+static inline struct sk_buff *ip_fraglist_next(struct ip_fraglist_iter *iter)
+{
+	struct sk_buff *skb = iter->frag;
+
+	iter->frag = skb->next;
+	skb_mark_not_on_list(skb);
+
+	return skb;
+}
+
 void ip_send_check(struct iphdr *ip);
 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
net/ipv4/ip_output.c +55 -33

123 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#4 lore raw

RFC PATCH 3/8 net: ipv6: add skbuff fraglist split iterator

In reply to Pablo Neira Ayuso

This patch adds skbuff fraglist split iterator. The API consists of:

* ip6_fraglist_init(), that initializes the internal state of the fraglist iterator.
* ip6_fraglist_prepare(), that restores the IPv4 and transport headers on the fragment.
* ip6_fraglist_next(), that retrieves the fragment from the fraglist and updates the internal state of the iterator to point to the next fragment in the fraglist.

The ip6_fraglist_iter object stores the internal state of the iterator.

This code has been extracted from ip6_fragment(). Symbols are also exported to allow to reuse this iterator from the bridge codepath for refragmentation.

---
 include/net/ipv6.h    |  25 ++++++++++
 net/ipv6/ip6_output.c | 132 +++++++++++++++++++++++++++++---------------------
 2 files changed, 102 insertions(+), 55 deletions(-)
include/net/ipv6.h +25 -0
index daf80863d3a5..acefbc718abe 100644
@@ -154,6 +154,31 @@ struct frag_hdr {
 #define	IP6_MF		0x0001
 #define	IP6_OFFSET	0xFFF8
 
+struct ip6_fraglist_iter {
+	struct ipv6hdr	*tmp_hdr;
+	struct sk_buff	*frag_list;
+	struct sk_buff	*frag;
+	int		offset;
+	unsigned int	hlen;
+	__be32		frag_id;
+	u8		nexthdr;
+};
+
+int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
+		      u8 nexthdr, __be32 frag_id,
+		      struct ip6_fraglist_iter *iter);
+void ip6_fraglist_prepare(struct sk_buff *skb, struct ip6_fraglist_iter *iter);
+
+static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter)
+{
+	struct sk_buff *skb = iter->frag;
+
+	iter->frag = skb->next;
+	skb_mark_not_on_list(skb);
+
+	return skb;
+}
+
 #define IP6_REPLY_MARK(net, mark) \
 	((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
 
net/ipv6/ip6_output.c +77 -55

177 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#5 lore raw

RFC PATCH 4/8 net: ipv4: split skbuff into fragments transformer

In reply to Pablo Neira Ayuso

This patch exposes a new API to refragment a skbuff. The allows you to split either linear skbuff or force the refragmentation of an existing fraglist.

The API consists of:

* ip_frag_init(), that initializes the internal state of the transformer.
* ip_frag_next(), that allows you to fetch the next fragment. This function internally allocates the skbuff that represents the fragment, it pushes the IPv4 and transport headers, and it also copies the payload for this fragment.

The ip_frag_state object stores the internal state of the splitter.

This code has been extracted from ip_do_fragment(). Symbols are also exported to allow to reuse this transformer from the bridge codepath for refragmentation.

---
 include/net/ip.h     |  16 +++++
 net/ipv4/ip_output.c | 200 ++++++++++++++++++++++++++++-----------------------
 2 files changed, 128 insertions(+), 88 deletions(-)
include/net/ip.h +16 -0
index cc3d83680f4f..94f654e6bd41 100644
@@ -188,6 +188,22 @@ static inline struct sk_buff *ip_fraglist_next(struct ip_fraglist_iter *iter)
 	return skb;
 }
 
+struct ip_frag_state {
+	struct iphdr	*iph;
+	unsigned int	hlen;
+	unsigned int	ll_rs;
+	unsigned int	mtu;
+	unsigned int	left;
+	int		offset;
+	int		ptr;
+	__be16		not_last_frag;
+};
+
+void ip_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int ll_rs,
+		  unsigned int mtu, struct ip_frag_state *state);
+struct sk_buff *ip_frag_next(struct sk_buff *skb,
+			     struct ip_frag_state *state);
+
 void ip_send_check(struct iphdr *ip);
 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
net/ipv4/ip_output.c +112 -88

236 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#6 lore raw

RFC PATCH 5/8 net: ipv6: split skbuff into fragments transformer

In reply to Pablo Neira Ayuso

This patch exposes a new API to refragment a skbuff. The allows you to split either linear skbuff or force the refragmentation of an existing fraglist.

The API consists of:

* ip6_frag_init(), that initializes the internal state of the transformer.
* ip6_frag_next(), that allows you to fetch the next fragment. This function internally allocates the skbuff that represents the fragment, it pushes the IPv6 and transport headers, and it also copies the payload for this fragment.

The ip6_frag_state object stores the internal state of the splitter.

This code has been extracted from ip6_fragment(). Symbols are also exported to allow to reuse this transformer from the bridge codepath for refragmentation.

---
 include/net/ipv6.h    |  19 ++++++
 net/ipv6/ip6_output.c | 183 +++++++++++++++++++++++++++++---------------------
 2 files changed, 126 insertions(+), 76 deletions(-)
include/net/ipv6.h +19 -0
index acefbc718abe..21bb830e9679 100644
@@ -179,6 +179,25 @@ static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter)
 	return skb;
 }
 
+struct ip6_frag_state {
+	u8		*prevhdr;
+	unsigned int	hlen;
+	unsigned int	mtu;
+	unsigned int	left;
+	int		offset;
+	int		ptr;
+	int		hroom;
+	int		troom;
+	__be32		frag_id;
+	u8		nexthdr;
+};
+
+void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
+		   unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
+		   u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state);
+struct sk_buff *ip6_frag_next(struct sk_buff *skb,
+			      struct ip6_frag_state *state);
+
 #define IP6_REPLY_MARK(net, mark) \
 	((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
 
net/ipv6/ip6_output.c +107 -76

215 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#7 lore raw

RFC PATCH 6/8 net: ipv4: place cb handling away from fraglist iterator

In reply to Pablo Neira Ayuso

Deal with the IPCB() area away from the fraglist iterator.

The bridge codebase has its own control buffer layout, move specific IP control buffer into function.

---
 net/ipv4/ip_output.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
net/ipv4/ip_output.c +15 -6
index ab39bbfe8197..1dc731d7be0e 100644
@@ -526,9 +526,6 @@ static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
 
 	skb_copy_hash(to, from);
 
-	/* Copy the flags to each fragment. */
-	IPCB(to)->flags = IPCB(from)->flags;
-
 #ifdef CONFIG_NET_SCHED
 	to->tc_index = from->tc_index;
 #endif
@@ -583,6 +580,18 @@ void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph,
 }
 EXPORT_SYMBOL(ip_fraglist_init);
 
+static void ip_fraglist_ipcb_prepare(struct sk_buff *skb,
+				     struct ip_fraglist_iter *iter)
+{
+	struct sk_buff *to = iter->frag;
+
+	/* Copy the flags to each fragment. */
+	IPCB(to)->flags = IPCB(skb)->flags;
+
+	if (iter->offset == 0)
+		ip_options_fragment(to);
+}
+
 void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter)
 {
 	unsigned int hlen = iter->hlen;
@@ -599,8 +608,6 @@ void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter)
 	iph = iter->iph;
 	iph->tot_len = htons(frag->len);
 	ip_copy_metadata(frag, skb);
-	if (iter->offset == 0)
-		ip_options_fragment(frag);
 	iter->offset += skb->len - hlen;
 	iph->frag_off = htons(iter->offset >> 3);
 	if (frag->next)
@@ -800,8 +807,10 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 		for (;;) {
 			/* Prepare header of the next frame,
 			 * before previous one went down. */
-			if (iter.frag)
+			if (iter.frag) {
+				ip_fraglist_ipcb_prepare(skb, &iter);
 				ip_fraglist_prepare(skb, &iter);
+			}
 
 			err = output(net, sk, skb);
 
Signature · 1 line
2.11.0
author maintainer
#8 lore raw

RFC PATCH 7/8 net: ipv4: place cb handling away from fragment transformer

In reply to Pablo Neira Ayuso

Deal with the IPCB() area away from the fragment transformer.

The bridge codebase has its own control buffer layout, move specific IP control buffer into function.

---
 net/ipv4/ip_output.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
net/ipv4/ip_output.c +22 -12
index 1dc731d7be0e..bb0a13f78b43 100644
@@ -635,6 +635,25 @@ void ip_frag_init(struct sk_buff *skb, unsigned int hlen,
 }
 EXPORT_SYMBOL(ip_frag_init);
 
+static void ip_frag_ipcb(struct sk_buff *from, struct sk_buff *to,
+			 bool first_frag, struct ip_frag_state *state)
+{
+	/* Copy the flags to each fragment. */
+	IPCB(to)->flags = IPCB(from)->flags;
+
+	if (IPCB(from)->flags & IPSKB_FRAG_PMTU)
+		state->iph->frag_off |= htons(IP_DF);
+
+	/* ANK: dirty, but effective trick. Upgrade options only if
+	 * the segment to be fragmented was THE FIRST (otherwise,
+	 * options are already fixed) and make it ONCE
+	 * on the initial skb, so that all the following fragments
+	 * will inherit fixed options.
+	 */
+	if (first_frag)
+		ip_options_fragment(from);
+}
+
 struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state)
 {
 	unsigned int len = state->left;
@@ -693,18 +712,6 @@ struct sk_buff *ip_frag_next(struct sk_buff *skb, struct ip_frag_state *state)
 	iph = ip_hdr(skb2);
 	iph->frag_off = htons((state->offset >> 3));
 
-	if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
-		iph->frag_off |= htons(IP_DF);
-
-	/* ANK: dirty, but effective trick. Upgrade options only if
-	 * the segment to be fragmented was THE FIRST (otherwise,
-	 * options are already fixed) and make it ONCE
-	 * on the initial skb, so that all the following fragments
-	 * will inherit fixed options.
-	 */
-	if (state->offset == 0)
-		ip_options_fragment(skb);
-
 	/*
 	 *	Added AC : If we are fragmenting a fragment that's not the
 	 *		   last fragment then keep MF on each bit
@@ -854,11 +861,14 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	 */
 
 	while (state.left > 0) {
+		bool first_frag = (state.offset == 0);
+
 		skb2 = ip_frag_next(skb, &state);
 		if (IS_ERR(skb2)) {
 			err = PTR_ERR(skb2);
 			goto fail;
 		}
+		ip_frag_ipcb(skb, skb2, first_frag, &state);
 
 		/*
 		 *	Put this fragment into the sending queue.
Signature · 1 line
2.11.0
author maintainer
#9 lore raw

RFC PATCH 8/8 netfilter: bridge: add basic conntrack support

In reply to Pablo Neira Ayuso

This patch adds native connection tracking support for the bridge. This patch register two hooks, one at bridge prerouting to call nf_conntrack_in() and another at the bridge postrouting hook to confirm the entry.

The conntrack bridge prerouting hook defragments packets and it passes them to nf_conntrack_in() to look up for an existing entry, otherwise a new entry is created in the conntrack table.

The conntrack bridge postrouting hook confirms new entries, ie. this is the first packet seen of this flow, then - if needed - it refragments the skbuff into the original fragments, leaving the geometry in untouched, unless this skbuff has been already linearized - eg. passed up to nfqueue and conntrack helpers - or skbuff has been cloned either for the local delivery for tcpdump or in case bridge port flooding is needed.

The maximum fragment length is stored and it is used refragment the skbuff. In case the maximum fragment length is larger than the output port, the packet is silently discarded, ie. no refragmentation occurs.

The packet defragmentation is still done through the ip_defrag() call. This forces us to save the bridge control buffer, reset the IP control buffer area and then restore it after call. This function bumps the IP fragmentation statistics.

The new fraglist iterator and fragment transformer APIs is used to implement the refragmentation code. The br_ip_fragment() function drops the packet in case the maximum fragment size seen is larger than the output port MTU.

Health check validations are only performed in case of defragmentation, to follow the principle that conntrack should not drop packets, so users can do it through policy, ie. drop invalid packets.

Like br_netfilter, there is no need to refragment packets that are passed up for local delivery, ie. prerouting -> input path. There are calls to nf_reset() already in several spots that were placed there a bit of time ago already, eg. af_packet, that show that skbuff fraglist from the netif_rx path is supported already.

This is work-in-progress, missing parts are:

1. A track extension to allow userspace to pick up selected packets for tracking. We cannot inconditionally enable conntrack as in IPv4 and IPv6, we need an extension to specify what zone this conntrack belongs to to support VLAN scenarios.
2. Complete IPv6 support.
3. Netnamespace support.

This patch is based on original work from Florian Westphal.

---
 net/bridge/br_private.h                    |   2 +-
 net/bridge/netfilter/Kconfig               |  14 +
 net/bridge/netfilter/Makefile              |   3 +
 net/bridge/netfilter/nf_conntrack_bridge.c | 411 +++++++++++++++++++++++++++++
 4 files changed, 429 insertions(+), 1 deletion(-)
 create mode 100644 net/bridge/netfilter/nf_conntrack_bridge.c
net/bridge/br_private.h +1 -1
index 00deef7fc1f3..d8d150677fa1 100644
@@ -430,7 +430,7 @@ struct br_input_skb_cb {
 	int igmp;
 	int mrouters_only;
 #endif
-
+	u16 nf_frag_max_size;
 	bool proxyarp_replied;
 	bool src_port_isolated;
 
net/bridge/netfilter/Kconfig +14 -0
index 9a0159aebe1a..eb61197d8af8 100644
@@ -18,6 +18,20 @@ config NF_LOG_BRIDGE
 	tristate "Bridge packet logging"
 	select NF_LOG_COMMON
 
+config NF_CONNTRACK_BRIDGE
+	tristate "IPv4/IPV6 bridge connection tracking support"
+	depends on NF_CONNTRACK
+	default n
+	help
+	  Connection tracking keeps a record of what packets have passed
+	  through your machine, in order to figure out how they are related
+	  into connections. This is used to enhance packet filtering via
+	  stateful policies. Enable this if you want native tracking from
+	  the bridge. This is provides a replacement for the `br_netfilter'
+	  infrastructure.
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+
 endif # NF_TABLES_BRIDGE
 
 menuconfig BRIDGE_NF_EBTABLES
net/bridge/netfilter/Makefile +3 -0
index 9b868861f21a..9d7767322a64 100644
@@ -5,6 +5,9 @@
 
 obj-$(CONFIG_NFT_BRIDGE_REJECT)  += nft_reject_bridge.o
 
+# connection tracking
+obj-$(CONFIG_NF_CONNTRACK_BRIDGE) += nf_conntrack_bridge.o
+
 # packet logging
 obj-$(CONFIG_NF_LOG_BRIDGE) += nf_log_bridge.o
 
net/bridge/netfilter/nf_conntrack_bridge.c new file +411 -0

411 lines not on this page · raw message

Signature · 1 line
2.11.0