Skip to contentSkip to conversation

netfilter: bridge: optionally set indev to vlan

PATCH Superseded Florian Westphal 3 participants 6 messages Apr 4, 2012 – Apr 5, 2012

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: bridge: optionally set indev to vlan

if net.bridge.bridge-nf-filter-vlan-tagged sysctl is enabled, bridge netfilter removes the vlan header temporarily and then feeds the packet to ip(6)tables.

When the new "bridge-nf-pass-vlan-input-device" sysctl is on (default off), then bridge netfilter will also change the in/out interface to the vlan interface; if such an interface exists.

This is needed to make iptables REDIRECT target work with "vlan-on-top-of-bridge" setups. Also, 'iptables -i' can then be used to match the vlan device name.

Also update Documentation with current brnf default settings.

---
 Documentation/networking/ip-sysctl.txt |   13 +++++++++-
 net/bridge/br_netfilter.c              |   39 +++++++++++++++++++++++++------
 2 files changed, 42 insertions(+), 10 deletions(-)
Documentation/networking/ip-sysctl.txt +11 -2
index ad3e80e..ee0374a 100644
@@ -1294,13 +1294,22 @@ bridge-nf-call-ip6tables - BOOLEAN
 bridge-nf-filter-vlan-tagged - BOOLEAN
 	1 : pass bridged vlan-tagged ARP/IP/IPv6 traffic to {arp,ip,ip6}tables.
 	0 : disable this.
-	Default: 1
+	Default: 0
 
 bridge-nf-filter-pppoe-tagged - BOOLEAN
 	1 : pass bridged pppoe-tagged IP/IPv6 traffic to {ip,ip6}tables.
 	0 : disable this.
-	Default: 1
+	Default: 0
 
+bridge-nf-pass-vlan-input-dev - BOOLEAN
+	1: if bridge-nf-filter-vlan-tagged is enabled, try to find a vlan
+	interface on the bridge and set the netfilter input device to the vlan.
+	This allows use of e.g. "iptables -i br0.1" and makes the REDIRECT
+	target work with vlan-on-top-of-bridge interfaces.  When no matching
+	vlan interface is found, or this switch is off, the input device is
+	set to the bridge interface.
+	0: disable bridge netfilter vlan interface lookup.
+	Default: 0
 
 proc/sys/net/sctp/* Variables:
 
net/bridge/br_netfilter.c +31 -8
index dec4f38..1d69feb 100644
@@ -53,6 +53,7 @@ static int brnf_call_iptables __read_mostly = 1;
 static int brnf_call_ip6tables __read_mostly = 1;
 static int brnf_call_arptables __read_mostly = 1;
 static int brnf_filter_vlan_tagged __read_mostly = 0;
+static int brnf_pass_vlan_indev __read_mostly = 0;
 static int brnf_filter_pppoe_tagged __read_mostly = 0;
 #else
 #define brnf_call_iptables 1
@@ -503,6 +504,19 @@ bridged_dnat:
 	return 0;
 }
 
+static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev)
+{
+	struct net_device *vlan, *br;
+
+	br = bridge_parent(dev);
+	if (brnf_pass_vlan_indev == 0 || !vlan_tx_tag_present(skb))
+		return br;
+
+	vlan = __vlan_find_dev_deep(br, vlan_tx_tag_get(skb) & VLAN_VID_MASK);
+
+	return vlan ? vlan : br;
+}
+
 /* Some common code for IPv4/IPv6 */
 static struct net_device *setup_pre_routing(struct sk_buff *skb)
 {
@@ -515,7 +529,7 @@ static struct net_device *setup_pre_routing(struct sk_buff *skb)
 
 	nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
 	nf_bridge->physindev = skb->dev;
-	skb->dev = bridge_parent(skb->dev);
+	skb->dev = brnf_get_logical_dev(skb, skb->dev);
 	if (skb->protocol == htons(ETH_P_8021Q))
 		nf_bridge->mask |= BRNF_8021Q;
 	else if (skb->protocol == htons(ETH_P_PPP_SES))
@@ -737,7 +751,7 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
 				     int (*okfn)(struct sk_buff *))
 {
 	struct nf_bridge_info *nf_bridge;
-	struct net_device *parent;
+	struct net_device *parent, *indev;
 	u_int8_t pf;
 
 	if (!skb->nf_bridge)
@@ -748,10 +762,6 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
 	if (!nf_bridge_unshare(skb))
 		return NF_DROP;
 
-	parent = bridge_parent(out);
-	if (!parent)
-		return NF_DROP;
-
 	if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
 		pf = PF_INET;
 	else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
@@ -759,6 +769,10 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
 	else
 		return NF_ACCEPT;
 
+	parent = brnf_get_logical_dev(skb, out);
+	if (!parent)
+		return NF_DROP;
+
 	nf_bridge_pull_encap_header(skb);
 
 	nf_bridge = skb->nf_bridge;
@@ -778,7 +792,9 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
 	else
 		skb->protocol = htons(ETH_P_IPV6);
 
-	NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
+	indev = brnf_get_logical_dev(skb, in);
+
+	NF_HOOK(pf, NF_INET_FORWARD, skb, indev, parent,
 		br_nf_forward_finish);
 
 	return NF_STOLEN;
@@ -850,7 +866,7 @@ static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
 				       int (*okfn)(struct sk_buff *))
 {
 	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
-	struct net_device *realoutdev = bridge_parent(skb->dev);
+	struct net_device *realoutdev = brnf_get_logical_dev(skb, out);
 	u_int8_t pf;
 
 	if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED))
@@ -1006,6 +1022,13 @@ static ctl_table brnf_table[] = {
 		.mode		= 0644,
 		.proc_handler	= brnf_sysctl_call_tables,
 	},
+	{
+		.procname	= "bridge-nf-pass-vlan-input-dev",
+		.data		= &brnf_pass_vlan_indev,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= brnf_sysctl_call_tables,
+	},
 	{ }
 };
 
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 8 lines · Florian Westphal

if net.bridge.bridge-nf-filter-vlan-tagged sysctl is enabled, bridge netfilter removes the vlan header temporarily and then feeds the packet to ip(6)tables.

When the new "bridge-nf-pass-vlan-input-device" sysctl is on (default off), then bridge netfilter will also change the in/out interface to the vlan interface; if such an interface exists.

Quoted 5 lines

This is needed to make iptables REDIRECT target work with "vlan-on-top-of-bridge" setups. Also, 'iptables -i' can then be used to match the vlan device name.

Also update Documentation with current brnf default settings.

Better to implement a vlan device match for iptables?

author maintainer
#2 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Florian Westphal

if net.bridge.bridge-nf-filter-vlan-tagged sysctl is enabled, bridge netfilter removes the vlan header temporarily and then feeds the packet to ip(6)tables.

When the new "bridge-nf-pass-vlan-input-device" sysctl is on (default off), then bridge netfilter will also change the in/out interface to the vlan interface; if such an interface exists.

Quoted 7 lines
Quoted 5 lines

This is needed to make iptables REDIRECT target work with "vlan-on-top-of-bridge" setups. Also, 'iptables -i' can then be used to match the vlan device name.

Also update Documentation with current brnf default settings.

Better to implement a vlan device match for iptables?

That would mean that REDIRECT target is unsupported in vlan-on-bridge case.
If so, would you accept a man page update that documents this?

Thanks,
Florian

#3 lore raw
Quoted 14 lines · Florian Westphal
Quoted 13 lines · Pablo Neira Ayuso
Quoted 12 lines · Florian Westphal

if net.bridge.bridge-nf-filter-vlan-tagged sysctl is enabled, bridge netfilter removes the vlan header temporarily and then feeds the packet to ip(6)tables.

When the new "bridge-nf-pass-vlan-input-device" sysctl is on (default off), then bridge netfilter will also change the in/out interface to the vlan interface; if such an interface exists.

This is needed to make iptables REDIRECT target work with "vlan-on-top-of-bridge" setups. Also, 'iptables -i' can then be used to match the vlan device name.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Also update Documentation with current brnf default settings.

Better to implement a vlan device match for iptables?

That would mean that REDIRECT target is unsupported in vlan-on-bridge case.

I see, it would not support any other targets that relies on skb->dev either.

Let me think if I can come with a better solution. If not, I'll take your patch.

Thanks Florian.

#4 lore raw
In reply to Florian Westphal

Op 4/04/2012 21:57, Florian Westphal schreef:

Quoted 29 lines

if net.bridge.bridge-nf-filter-vlan-tagged sysctl is enabled, bridge netfilter removes the vlan header temporarily and then feeds the packet to ip(6)tables.

When the new "bridge-nf-pass-vlan-input-device" sysctl is on (default off), then bridge netfilter will also change the in/out interface to the vlan interface; if such an interface exists.

This is needed to make iptables REDIRECT target work with "vlan-on-top-of-bridge" setups. Also, 'iptables -i' can then be used to match the vlan device name.

Also update Documentation with current brnf default settings.

proc/sys/net/sctp/* Variables:

net/bridge/br_netfilter.c +1 -0
index dec4f38..1d69feb 100644
@@ -53,6 +53,7 @@ static int brnf_call_iptables __read_mostly = 1;
  static int brnf_call_ip6tables __read_mostly = 1;
  static int brnf_call_arptables __read_mostly = 1;
  static int brnf_filter_vlan_tagged __read_mostly = 0;
+static int brnf_pass_vlan_indev __read_mostly = 0;
  static int brnf_filter_pppoe_tagged __read_mostly = 0;
  #else
  #define brnf_call_iptables 1

net/bridge/br_netfilter.c bridged_dnat: · line 505

Quoted 2 lines
net/bridge/br_netfilter.c +0 -0
@@ -503,6 +504,19 @@ bridged_dnat:
  	return 0;
  }

You should also provide a macro in case the proc file system isn't enabled: currently it won't compile with your patch.

Quoted 55 lines
net/bridge/br_netfilter.c +9 -7
  /* Some common code for IPv4/IPv6 */
  static struct net_device *setup_pre_routing(struct sk_buff *skb)
  {
@@ -515,7 +529,7 @@ static struct net_device *setup_pre_routing(struct sk_buff *skb)
 
  	nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
  	nf_bridge->physindev = skb->dev;
-	skb->dev = bridge_parent(skb->dev);
+	skb->dev = brnf_get_logical_dev(skb, skb->dev);
  	if (skb->protocol == htons(ETH_P_8021Q))
  		nf_bridge->mask |= BRNF_8021Q;
  	else if (skb->protocol == htons(ETH_P_PPP_SES))
@@ -737,7 +751,7 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
  				     int (*okfn)(struct sk_buff *))
  {
  	struct nf_bridge_info *nf_bridge;
-	struct net_device *parent;
+	struct net_device *parent, *indev;
  	u_int8_t pf;
 
  	if (!skb->nf_bridge)
@@ -748,10 +762,6 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
  	if (!nf_bridge_unshare(skb))
  		return NF_DROP;
 
-	parent = bridge_parent(out);
-	if (!parent)
-		return NF_DROP;
-
  	if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb))
  		pf = PF_INET;
  	else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb))
@@ -759,6 +769,10 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
  	else
  		return NF_ACCEPT;
 
+	parent = brnf_get_logical_dev(skb, out);
+	if (!parent)
+		return NF_DROP;
+
  	nf_bridge_pull_encap_header(skb);
 
  	nf_bridge = skb->nf_bridge;
@@ -778,7 +792,9 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
  	else
  		skb->protocol = htons(ETH_P_IPV6);
 
-	NF_HOOK(pf, NF_INET_FORWARD, skb, bridge_parent(in), parent,
+	indev = brnf_get_logical_dev(skb, in);
+
+	NF_HOOK(pf, NF_INET_FORWARD, skb, indev, parent,
  		br_nf_forward_finish);
 
  	return NF_STOLEN;

net/bridge/br_netfilter.c static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb, · line 869

Quoted 5 lines
net/bridge/br_netfilter.c +1 -1
@@ -850,7 +866,7 @@ static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
  				       int (*okfn)(struct sk_buff *))
  {
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
-	struct net_device *realoutdev = bridge_parent(skb->dev);
+	struct net_device *realoutdev = brnf_get_logical_dev(skb, out);

I think it's best to keep the bridge_parent as output device in the FOWARD and POSTROUTING chain: only change the input device. Do you have a reason for changing the output device?

cheers,
Bart

Signature · 2 lines
Bart De Schuymer
www.artinalgorithms.be
author maintainer
#5 lore raw
Quoted 10 lines · Bart De Schuymer

net/bridge/br_netfilter.c bridged_dnat: · line 505

Quoted 7 lines
net/bridge/br_netfilter.c +1 -0
+static int brnf_pass_vlan_indev __read_mostly = 0;
  static int brnf_filter_pppoe_tagged __read_mostly = 0;
  #else
  #define brnf_call_iptables 1
@@ -503,6 +504,19 @@ bridged_dnat:
  	return 0;
  }

You should also provide a macro in case the proc file system isn't
enabled: currently it won't compile with your patch.

You're right, of course.
Thanks for spotting this, I'll fix it.

Quoted 6 lines

net/bridge/br_netfilter.c static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb, · line 869

Quoted 3 lines
net/bridge/br_netfilter.c +1 -1
  	struct nf_bridge_info *nf_bridge = skb->nf_bridge;
-	struct net_device *realoutdev = bridge_parent(skb->dev);
+	struct net_device *realoutdev = brnf_get_logical_dev(skb, out);

I think it's best to keep the bridge_parent as output device in the FOWARD and POSTROUTING chain: only change the input device. Do you have a reason for changing the output device?

Consistency. (locally generated packet is '-o br0.X, forwarded br0). OTOH, we could call it a feature ;-)

I have no special requirements for the outdev; we can keep it as-is.

Regards,
Florian