Skip to contentSkip to conversation

netfilter: bridge: remove broute hook

PATCH In review Florian Westphal 4 participants 8 messages Apr 11, 2019

Subsystems (5)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: bridge: remove broute hook

This series removes the 'broute' hook by promoting ebtables' broute table to a normal ebtables table (invoked via normal PREROUTING netfilter hook).

The downside is that nf_hook_slow() needs to be duplicated in br_input.c (see patch 3).

However, I think its worth the price as this allows to remove the br_should_route_hook.

There are quite some changes in bridge specific code, if you prefer I can re-submit this for net-next instead of nf-next.

Main motivation is to provide 'ebtables -t broute' functionality via nftables later on, this can then be done without touching the bridge or netfilter core infrastructure again.

Florian Westphal (4):
selftests: netfilter: add ebtables broute test case
bridge: reduce size of input cb to 16 bytes
bridge: netfilter: unroll NF_HOOK helper in bridge input path
bridge: broute: make broute a real ebtables table

 include/linux/if_bridge.h                           |    3 
 include/net/netfilter/nf_queue.h                    |    3 
 net/bridge/br_arp_nd_proxy.c                        |   18 +-
 net/bridge/br_input.c                               |   72 +++++++--
 net/bridge/br_private.h                             |   15 +-
 net/bridge/netfilter/ebtable_broute.c               |   63 ++++++--
 net/bridge/netfilter/ebtables.c                     |    7 
 net/netfilter/core.c                                |    1 
 net/netfilter/nf_internals.h                        |    3 
 net/netfilter/nf_queue.c                            |    1 
 tools/testing/selftests/netfilter/Makefile          |    2 
 tools/testing/selftests/netfilter/bridge_brouter.sh |  146 ++++++++++++++++++++
 12 files changed, 268 insertions(+), 66 deletions(-)
author maintainer
#1 lore raw

PATCH 1/4 selftests: netfilter: add ebtables broute test case

ebtables -t broute allows to redirect packets in a way that they get pushed up the stack, even if the interface is part of a bridge.

In case of IP packets to non-local address, this means those IP packets are routed instead of bridged-forwarded, just as if the bridge would not have existed.

Expected test output is:
PASS: netns connectivity: ns1 and ns2 can reach each other
PASS: ns1/ns2 connectivity with active broute rule
PASS: ns1/ns2 connectivity with active broute rule and bridge forward drop

---
 tools/testing/selftests/netfilter/Makefile    |   2 +-
 .../selftests/netfilter/bridge_brouter.sh     | 146 ++++++++++++++++++
 2 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/netfilter/bridge_brouter.sh
tools/testing/selftests/netfilter/Makefile +1 -1
index c9ff2b47bd1c..80dae72a25c7 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for netfilter selftests
 
-TEST_PROGS := nft_trans_stress.sh nft_nat.sh
+TEST_PROGS := nft_trans_stress.sh nft_nat.sh bridge_brouter.sh
 
 include ../lib.mk
tools/testing/selftests/netfilter/bridge_brouter.sh new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.21.0
author maintainer
#2 lore raw

PATCH 2/4 bridge: reduce size of input cb to 16 bytes

In reply to Florian Westphal

Reduce size of br_input_skb_cb from 24 to 16 bytes by using bitfield for those values that can only be 0 or 1.

igmp is the igmp type value, so it needs to be at least u8.

Furthermore, the bridge currently relies on step-by-step initialization of br_input_skb_cb fields as the skb passes through the stack.

Explicitly zero out the bridge input cb instead, this avoids having to review/validate that no BR_INPUT_SKB_CB(skb)->foo test can see a 'random' value from previous protocol cb.

AFAICS all current fields are always set up before they are read again, so this is not a bug fix.

Cc Roopa Prabhu <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
---
 net/bridge/br_arp_nd_proxy.c | 18 +++++++++---------
 net/bridge/br_input.c        |  2 ++
 net/bridge/br_private.h      | 12 +++++-------
 3 files changed, 16 insertions(+), 16 deletions(-)
net/bridge/br_arp_nd_proxy.c +9 -9
index 724b474ade54..15116752365a 100644
@@ -131,7 +131,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
 	u8 *arpptr, *sha;
 	__be32 sip, tip;
 
-	BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
+	BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
 
 	if ((dev->flags & IFF_NOARP) ||
 	    !pskb_may_pull(skb, arp_hdr_len(dev)))
@@ -161,7 +161,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
 			return;
 		if (ipv4_is_zeronet(sip) || sip == tip) {
 			/* prevent flooding to neigh suppress ports */
-			BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+			BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 			return;
 		}
 	}
@@ -181,7 +181,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
 		/* its our local ip, so don't proxy reply
 		 * and don't forward to neigh suppress ports
 		 */
-		BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+		BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		return;
 	}
 
@@ -217,7 +217,7 @@ void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
 			 */
 			if (replied ||
 			    br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
-				BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+				BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		}
 
 		neigh_release(n);
@@ -393,7 +393,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
 	struct ipv6hdr *iphdr;
 	struct neighbour *n;
 
-	BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
+	BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
 
 	if (p && (p->flags & BR_NEIGH_SUPPRESS))
 		return;
@@ -401,7 +401,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
 	if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
 	    !msg->icmph.icmp6_solicited) {
 		/* prevent flooding to neigh suppress ports */
-		BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+		BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		return;
 	}
 
@@ -414,7 +414,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
 
 	if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
 		/* prevent flooding to neigh suppress ports */
-		BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+		BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		return;
 	}
 
@@ -432,7 +432,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
 		/* its our own ip, so don't proxy reply
 		 * and don't forward to arp suppress ports
 		 */
-		BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+		BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		return;
 	}
 
@@ -465,7 +465,7 @@ void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
 			 */
 			if (replied ||
 			    br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
-				BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
+				BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
 		}
 		neigh_release(n);
 	}
net/bridge/br_input.c +2 -0
index 5ea7e56119c1..e2f93e5c72da 100644
@@ -227,6 +227,8 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
 	if (!skb)
 		return RX_HANDLER_CONSUMED;
 
+	memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
+
 	p = br_port_get_rcu(skb->dev);
 	if (p->flags & BR_VLAN_TUNNEL) {
 		if (br_handle_ingress_vlan_tunnel(skb, p,
net/bridge/br_private.h +5 -7
index 7946aa3b6e09..e7110a6e2b7e 100644
@@ -425,15 +425,13 @@ struct br_input_skb_cb {
 	struct net_device *brdev;
 
 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
-	int igmp;
-	int mrouters_only;
+	u8 igmp;
+	u8 mrouters_only:1;
 #endif
-
-	bool proxyarp_replied;
-	bool src_port_isolated;
-
+	u8 proxyarp_replied:1;
+	u8 src_port_isolated:1;
 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
-	bool vlan_filtered;
+	u8 vlan_filtered:1;
 #endif
 
 #ifdef CONFIG_NET_SWITCHDEV
Signature · 1 line
2.21.0
author maintainer
#3 lore raw

PATCH 3/4 bridge: netfilter: unroll NF_HOOK helper in bridge input path

In reply to Florian Westphal

Replace NF_HOOK() based invocation of the netfilter hooks with a private copy of nf_hook_slow().

This copy has one difference: it can return the rx handler value expected by the stack, i.e. RX_HANDLER_CONSUMED or RX_HANDLER_PASS.

This is needed by the next patch to invoke the ebtables "broute" table via the standard netfilter hooks rather than the custom "br_should_route_hook" indirection that is used now.

When the skb is to be "brouted", we must return RX_HANDLER_PASS from the bridge rx input handler, but there is no way to indicate this via NF_HOOK(), unless perhaps by some hack such as exposing bridge_cb in the netfilter core or a percpu flag.

  text    data     bss     dec   filename
  3369      56       0    3425   net/bridge/br_input.o.before
  3458      40       0    3498   net/bridge/br_input.o.after

This allows removal of the "br_should_route_hook" in the next patch.

Cc Roopa Prabhu <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
---
 include/net/netfilter/nf_queue.h |  3 ++
 net/bridge/br_input.c            | 55 +++++++++++++++++++++++++++++---
 net/netfilter/core.c             |  1 +
 net/netfilter/nf_internals.h     |  3 --
 net/netfilter/nf_queue.c         |  1 +
 5 files changed, 56 insertions(+), 7 deletions(-)
include/net/netfilter/nf_queue.h +3 -0
index a50a69f5334c..7239105d9d2e 100644
@@ -119,4 +119,7 @@ nfqueue_hash(const struct sk_buff *skb, u16 queue, u16 queues_total, u8 family,
 	return queue;
 }
 
+int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
+	     const struct nf_hook_entries *entries, unsigned int index,
+	     unsigned int verdict);
 #endif /* _NF_QUEUE_H */
net/bridge/br_input.c +51 -4

73 lines not on this page · raw message

net/netfilter/core.c +1 -0
index 93aaec3a54ec..71f06900473e 100644
@@ -23,6 +23,7 @@
 #include <linux/mm.h>
 #include <linux/rcupdate.h>
 #include <net/net_namespace.h>
+#include <net/netfilter/nf_queue.h>
 #include <net/sock.h>
 
 #include "nf_internals.h"
net/netfilter/nf_internals.h +0 -3
index e15779fd58e3..d6c43902ebd7 100644
@@ -7,9 +7,6 @@
 #include <linux/netdevice.h>
 
 /* nf_queue.c */
-int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
-	     const struct nf_hook_entries *entries, unsigned int index,
-	     unsigned int verdict);
 void nf_queue_nf_hook_drop(struct net *net);
 
 /* nf_log.c */
net/netfilter/nf_queue.c +1 -0
index a36a77bae1d6..9dc1d6e04946 100644
@@ -240,6 +240,7 @@ int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(nf_queue);
 
 static unsigned int nf_iterate(struct sk_buff *skb,
 			       struct nf_hook_state *state,
Signature · 1 line
2.21.0
author maintainer
#4 lore raw

PATCH 4/4 bridge: broute: make broute a real ebtables table

In reply to Florian Westphal

This makes broute a normal ebtables table, hooking at PREROUTING. The broute hook is removed.

It uses skb->cb to signal to bridge rx handler that the skb should be routed instead of being bridged.

This change is backwards compatible with ebtables as no userspace visible parts are changed.

This means we can also remove the !ops test in ebt_register_table, it was only there for broute table sake.

Cc Roopa Prabhu <[email protected]>
Cc Nikolay Aleksandrov <[email protected]>
---
 include/linux/if_bridge.h             |  3 --
 net/bridge/br_input.c                 | 18 ++------
 net/bridge/br_private.h               |  3 ++
 net/bridge/netfilter/ebtable_broute.c | 63 +++++++++++++++++++--------
 net/bridge/netfilter/ebtables.c       |  7 +--
 5 files changed, 52 insertions(+), 42 deletions(-)
include/linux/if_bridge.h +0 -3
index 627b788ba0ff..ef0819ced0fc 100644
@@ -56,9 +56,6 @@ struct br_ip_list {
 
 extern void brioctl_set(int (*ioctl_hook)(struct net *, unsigned int, void __user *));
 
-typedef int br_should_route_hook_t(struct sk_buff *skb);
-extern br_should_route_hook_t __rcu *br_should_route_hook;
-
 #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
 int br_multicast_list_adjacent(struct net_device *dev,
 			       struct list_head *br_ip_list);
net/bridge/br_input.c +4 -14
index 4ac34fb5f943..e0aacfedcfe1 100644
@@ -24,10 +24,6 @@
 #include "br_private.h"
 #include "br_private_tunnel.h"
 
-/* Hook for brouter */
-br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
-EXPORT_SYMBOL(br_should_route_hook);
-
 static int
 br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
 {
@@ -234,6 +230,10 @@ static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
 		verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
 		switch (verdict & NF_VERDICT_MASK) {
 		case NF_ACCEPT:
+			if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
+				*pskb = skb;
+				return RX_HANDLER_PASS;
+			}
 			break;
 		case NF_DROP:
 			kfree_skb(skb);
@@ -265,7 +265,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
 	struct net_bridge_port *p;
 	struct sk_buff *skb = *pskb;
 	const unsigned char *dest = eth_hdr(skb)->h_dest;
-	br_should_route_hook_t *rhook;
 
 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
 		return RX_HANDLER_PASS;
@@ -341,15 +340,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
 forward:
 	switch (p->state) {
 	case BR_STATE_FORWARDING:
-		rhook = rcu_dereference(br_should_route_hook);
-		if (rhook) {
-			if ((*rhook)(skb)) {
-				*pskb = skb;
-				return RX_HANDLER_PASS;
-			}
-			dest = eth_hdr(skb)->h_dest;
-		}
-		/* fall through */
 	case BR_STATE_LEARNING:
 		if (ether_addr_equal(p->br->dev->dev_addr, dest))
 			skb->pkt_type = PACKET_HOST;
net/bridge/br_private.h +3 -0
index e7110a6e2b7e..4bea2f11da9b 100644
@@ -433,6 +433,9 @@ struct br_input_skb_cb {
 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
 	u8 vlan_filtered:1;
 #endif
+#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
+	u8 br_netfilter_broute:1;
+#endif
 
 #ifdef CONFIG_NET_SWITCHDEV
 	int offload_fwd_mark;
net/bridge/netfilter/ebtable_broute.c +44 -19

101 lines not on this page · raw message

net/bridge/netfilter/ebtables.c +1 -6
index eb15891f8b9f..383f0328ff68 100644
@@ -1221,10 +1221,6 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table,
 	mutex_unlock(&ebt_mutex);
 
 	WRITE_ONCE(*res, table);
-
-	if (!ops)
-		return 0;
-
 	ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
 	if (ret) {
 		__ebt_unregister_table(net, table);
@@ -1248,8 +1244,7 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table,
 void ebt_unregister_table(struct net *net, struct ebt_table *table,
 			  const struct nf_hook_ops *ops)
 {
-	if (ops)
-		nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
+	nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
 	__ebt_unregister_table(net, table);
 }
 
Signature · 1 line
2.21.0
maintainer Acked-by on the v1 series
#5 lore raw
In reply to Florian Westphal

From: Florian Westphal <[email protected]>
Date: Thu, 11 Apr 2019 16:36:38 +0200

Quoted 9 lines

This series removes the 'broute' hook by promoting ebtables' broute table to a normal ebtables table (invoked via normal PREROUTING netfilter hook).

The downside is that nf_hook_slow() needs to be duplicated in br_input.c (see patch 3).

However, I think its worth the price as this allows to remove the br_should_route_hook.

Quoted 6 lines

There are quite some changes in bridge specific code, if you prefer I can re-submit this for net-next instead of nf-next.

Main motivation is to provide 'ebtables -t broute' functionality via nftables later on, this can then be done without touching the bridge or netfilter core infrastructure again.

Acked-by on the v1 series
#6 lore raw
In reply to Florian Westphal
Quoted 22 lines · Florian Westphal

This series removes the 'broute' hook by promoting ebtables' broute table to a normal ebtables table (invoked via normal PREROUTING netfilter hook).

The downside is that nf_hook_slow() needs to be duplicated in br_input.c (see patch 3).

However, I think its worth the price as this allows to remove the br_should_route_hook.

There are quite some changes in bridge specific code, if you prefer I can re-submit this for net-next instead of nf-next.

Main motivation is to provide 'ebtables -t broute' functionality via nftables later on, this can then be done without touching the bridge or netfilter core infrastructure again.

Florian Westphal (4):
selftests: netfilter: add ebtables broute test case
bridge: reduce size of input cb to 16 bytes
bridge: netfilter: unroll NF_HOOK helper in bridge input path
bridge: broute: make broute a real ebtables table

Quoted 14 lines
 include/linux/if_bridge.h                           |    3 
 include/net/netfilter/nf_queue.h                    |    3 
 net/bridge/br_arp_nd_proxy.c                        |   18 +-
 net/bridge/br_input.c                               |   72 +++++++--
 net/bridge/br_private.h                             |   15 +-
 net/bridge/netfilter/ebtable_broute.c               |   63 ++++++--
 net/bridge/netfilter/ebtables.c                     |    7 
 net/netfilter/core.c                                |    1 
 net/netfilter/nf_internals.h                        |    3 
 net/netfilter/nf_queue.c                            |    1 
 tools/testing/selftests/netfilter/Makefile          |    2 
 tools/testing/selftests/netfilter/bridge_brouter.sh |  146 ++++++++++++++++++++
 12 files changed, 268 insertions(+), 66 deletions(-)

The set looks good to me, the only little thing is the new memset() in br_handle_frame(), before we would lazily zero the fields when needed but that would save us from future bugs where one could forget to initialize the field.
Now we can remove most of the explicit cb field zeroing and rely on the memset.

Nice work! For the set:

#7 lore raw
In reply to Florian Westphal
Quoted 9 lines · Florian Westphal

This series removes the 'broute' hook by promoting ebtables' broute table to a normal ebtables table (invoked via normal PREROUTING netfilter hook).

The downside is that nf_hook_slow() needs to be duplicated in br_input.c (see patch 3).

However, I think its worth the price as this allows to remove the br_should_route_hook.

Quoted 6 lines

There are quite some changes in bridge specific code, if you prefer I can re-submit this for net-next instead of nf-next.

Main motivation is to provide 'ebtables -t broute' functionality via nftables later on, this can then be done without touching the bridge or netfilter core infrastructure again.

Series applied, thanks Florian.