Skip to contentSkip to conversation

Let {ip,arp}tables "see" bridged VLAN tagged {I,AR}P packets

PATCH/RFC Bart De Schuymer 4 participants 10 messages Oct 6, 2003 – Oct 7, 2003

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi all,

The patch below does four trivial changes and one big change Trivial changes, these are all in br_netfilter.c:
- check ar_pln==4 when giving bridged ARP packets to arptables
- delete unnecessary if in br_nf_local_in
- add more logging for the "Argh" message
- add some brag-comments in the file head comment

Big change: let {ip,arp}tables see VLAN tagged {I,AR}P packets. This patch also makes an oops go away when locally generated packets are sent through something like br0.1000.

This is what the patch does:
- add vlan_dev_hard_start_xmit_p, because br_netfilter.c needs to know the address of vlan_dev_hard_start_xmit(). When the local machine sends a packet through br0.1000, we need to allow filtering in LOCAL_OUT/FILTER on the bridge out port, so we need to be able to postpone the iptables filtering.
- add nf_bridge->netoutdev for vlan. When the local machine sends a packet through br0.1000, iptables -o br0.1000 should match instead of iptables -o br0. In the bridge code, it is not known that the out device was br0.1000, so we need to save this info in nf_bridge->netoutdev.
- change nf_bridge->hh size to 18, which is the Ethernet header size + VLAN header size.
- add BRNF_NF_BRIDGE_PREROUTING mask, to make sure the iptables PREROUTING chain isn't traversed twice. The old mechanism isn't satisfactory when the bridge port is a VLAN tagged device.
- add some code in vlan_dev.c::vlan_dev_hard_start_xmit():
skb->protocol = __constant_htons(ETH_P_8021Q);
skb->mac.raw -= VLAN_HLEN;
skb->nh.raw -= VLAN_HLEN;
When the logical VLAN device adds its VLAN header to the packet, I think it should update the skb. The protocol then becomes VLAN, the Ethernet header pointer should be updated correctly. Also, the network header pointer should now point to the VLAN header.
This code is needed for the code in br_netfilter.c to work, without it things will get more complicated inside br_netfilter.c.
I can put it between an #ifdef CONFIG_BRIDGE_NETFILTER if you like, but I think that to keep the skb correct these changes should always happen.
- {arp,ip}tables can filter the VLAN tagged packets thanks to some playing around with the skb->data and skb->nh.raw pointer inside br_netfilter.c.

When br0.1000 (or the like) exists, this is what happens to the device matching in {eb,ip}tables:
Suppose a frame arrives on br0: in PREROUTING the (logical) in-dev is br0. When the frame is bridged, the in-dev remains br0. When the frame is destined for the bridge box, the in-dev for iptables LOCAL_IN becomes br0.1000, for ebtables LOCAL_IN it remains br0.
Suppose the bridge box sends a locally generated IP packet through br0.1000. The out-dev for iptables is br0.1000, the out-dev for ebtables is br0.

Comments are welcome.
If people could test this patch and give feedback, that would be great.

cheers,
Bart

net/8021q/vlan.c +6 -0
@@ -103,6 +103,9 @@ static int __init vlan_proto_init(void)
 
 	vlan_ioctl_set(vlan_ioctl_handler);
 
+#ifdef CONFIG_BRIDGE_NETFILTER
+	vlan_dev_hard_start_xmit_p = vlan_dev_hard_start_xmit;
+#endif
 	return 0;
 }
 
@@ -125,6 +128,9 @@ static void __exit vlan_cleanup_devices(
 		}
 	}
 	rtnl_unlock();
+#ifdef CONFIG_BRIDGE_NETFILTER
+	vlan_dev_hard_start_xmit_p = NULL;
+#endif
 }
 
 /*
net/core/dev.c +8 -0
@@ -1489,6 +1489,14 @@ static void net_tx_action(struct softirq
 }
 
 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
+
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+#ifdef CONFIG_BRIDGE_NETFILTER
+/* net/bridge/br_netfilter.c needs the address of vlan_dev_hard_start_xmit */
+int (*vlan_dev_hard_start_xmit_p)(struct sk_buff *skb, struct net_device *dev);
+#endif
+#endif
+
 int (*br_handle_frame_hook)(struct sk_buff *skb);
 
 static __inline__ int handle_bridge(struct sk_buff *skb,
include/linux/if_vlan.h +6 -0
@@ -54,6 +54,12 @@ struct vlan_hdr {
 
 #define VLAN_VID_MASK	0xfff
 
+#ifdef CONFIG_BRIDGE_NETFILTER
+extern int (*vlan_dev_hard_start_xmit_p)(struct sk_buff *skb,
+					  struct net_device *dev);
+#endif
+
+
 /* found in socket.c */
 extern void vlan_ioctl_set(int (*hook)(unsigned long));
 
net/netsyms.c +5 -0
@@ -236,6 +236,11 @@ EXPORT_SYMBOL(scm_detach_fds);
 
 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
 EXPORT_SYMBOL(br_handle_frame_hook);
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+#ifdef CONFIG_BRIDGE_NETFILTER
+EXPORT_SYMBOL(vlan_dev_hard_start_xmit_p);
+#endif
+#endif
 #endif
 
 #ifdef CONFIG_NET_DIVERT
include/linux/skbuff.h +4 -1
@@ -103,8 +103,11 @@ struct nf_bridge_info {
 	atomic_t use;
 	struct net_device *physindev;
 	struct net_device *physoutdev;
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+	struct net_device *netoutdev;
+#endif
 	unsigned int mask;
-	unsigned long hh[16 / sizeof(unsigned long)];
+	unsigned char hh[18];
 };
 #endif
 
include/linux/netfilter_bridge.h +34 -0
@@ -8,6 +8,9 @@
 #include <linux/netfilter.h>
 #if defined(__KERNEL__) && defined(CONFIG_BRIDGE_NETFILTER)
 #include <asm/atomic.h>
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+#include <linux/if_ether.h>
+#endif
 #endif
 
 /* Bridge Hooks */
@@ -44,6 +47,7 @@ enum nf_br_hook_priorities {
 #define BRNF_BRIDGED_DNAT		0x02
 #define BRNF_DONT_TAKE_PARENT		0x04
 #define BRNF_BRIDGED			0x08
+#define BRNF_NF_BRIDGE_PREROUTING	0x10
 
 static inline
 struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
@@ -54,9 +58,39 @@ struct nf_bridge_info *nf_bridge_alloc(s
 		atomic_set(&(*nf_bridge)->use, 1);
 		(*nf_bridge)->mask = 0;
 		(*nf_bridge)->physindev = (*nf_bridge)->physoutdev = NULL;
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+		(*nf_bridge)->netoutdev = NULL;
+#endif
 	}
 
 	return *nf_bridge;
+}
+
+/* Only used in br_forward.c */
+static inline
+void nf_bridge_maybe_copy_header(struct sk_buff *skb)
+{
+	if (skb->nf_bridge) {
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+		if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
+			memcpy(skb->data - 18, skb->nf_bridge->hh, 18);
+			skb_push(skb, 4);
+		} else
+#endif
+			memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
+	}
+}
+
+static inline
+void nf_bridge_save_header(struct sk_buff *skb)
+{
+        int header_size = 16;
+
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+	if (skb->protocol == __constant_htons(ETH_P_8021Q))
+		header_size = 18;
+#endif
+	memcpy(skb->nf_bridge->hh, skb->data - header_size, header_size);
 }
 
 struct bridge_skb_cb {
net/bridge/br_forward.c +1 -2
@@ -35,8 +35,7 @@ int br_dev_queue_push_xmit(struct sk_buf
 {
 #ifdef CONFIG_BRIDGE_NETFILTER
 	/* ip_refrag calls ip_fragment, which doesn't copy the MAC header. */
-	if (skb->nf_bridge)
-		memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
+	nf_bridge_maybe_copy_header(skb);
 #endif
 	skb_push(skb, ETH_HLEN);
 
net/8021q/vlan_dev.c +4 -0
@@ -502,6 +502,10 @@ int vlan_dev_hard_start_xmit(struct sk_b
 	stats->tx_packets++; /* for statics only */
 	stats->tx_bytes += skb->len;
 
+	skb->protocol = __constant_htons(ETH_P_8021Q);
+	skb->mac.raw -= VLAN_HLEN;
+	skb->nh.raw -= VLAN_HLEN;
+
 	dev_queue_xmit(skb);
 
 	return 0;
net/bridge/br_netfilter.c +115 -26

321 lines not on this page · raw message

#1 lore raw
Quoted 20 lines · On Mon, 6 Oct 2003 22:04:07 +0200 Bart De Schuymer <[email protected]> wrote:

Hi all,

The patch below does four trivial changes and one big change Trivial changes, these are all in br_netfilter.c:
- check ar_pln==4 when giving bridged ARP packets to arptables
- delete unnecessary if in br_nf_local_in
- add more logging for the "Argh" message
- add some brag-comments in the file head comment

Big change: let {ip,arp}tables see VLAN tagged {I,AR}P packets. This patch also makes an oops go away when locally generated packets are sent through something like br0.1000.

This is what the patch does:
- add vlan_dev_hard_start_xmit_p, because br_netfilter.c needs to know the address of vlan_dev_hard_start_xmit(). When the local machine sends a packet through br0.1000, we need to allow filtering in LOCAL_OUT/FILTER on the bridge out port, so we need to be able to postpone the iptables filtering.

Quoted 5 lines

Comments are welcome.
If people could test this patch and give feedback, that would be great.

cheers,
Bart

I can test the no VLAN case, but actual VLAN's are a little harder to setup. How does this affect the ability to rmmod either vlan or bridge?

#2 lore raw
Quoted 2 lines · Stephen Hemminger

I can test the no VLAN case, but actual VLAN's are a little harder to setup.

Setting up vlan's is easy:
brctl addbr br0
brctl addif br0 eth0
ifconfig br0 $address
vconfig add br0 1000
ifconfig br0.1000 $address

Quoted 1 line

How does this affect the ability to rmmod either vlan or bridge?

Well, when br0.1000 exists and you then rmmod the bridge, it doesn't work. The way to do it is rmmod 8021q;rmmod bridge. I came across this issue, but I assumed it was not my fault. I thought the problem would exist without my code too, but I'm not sure if I checked. What also doesn't work is rmmod bridge followed by rmmod 8021q on a different console. I now just realize this is probably because the bridge depends on 8021q and we thus get a deadlock.
I think the right solution to this problem is, when rmmod bridge happens, it removes any tagged bridge devices first.

cheers,
Bart

#3 lore raw
In reply to Bart De Schuymer
Quoted 4 lines · Bart De Schuymer

- add some code in vlan_dev.c::vlan_dev_hard_start_xmit():
skb->protocol = __constant_htons(ETH_P_8021Q); skb->mac.raw -= VLAN_HLEN;
skb->nh.raw -= VLAN_HLEN;

I wonder if this is what was messing up the tcpdump packet capture as well?

It would be fine with me if you un-conditionally exported the vlan hard-start-xmit pointer, but I don't feel strongly either way.

Thanks,
Ben

Signature · 2 lines
Ben Greear <[email protected]>
Candela Technologies Inc  http://www.candelatech.com
#4 lore raw

Re: [Bridge] Re: [PATCH/RFC] Let {ip, arp}tables "see" bridged VLAN tagged {I, AR}P packets

Quoted 8 lines · Ben Greear
Quoted 4 lines · Bart De Schuymer

- add some code in vlan_dev.c::vlan_dev_hard_start_xmit():
skb->protocol = __constant_htons(ETH_P_8021Q); skb->mac.raw -= VLAN_HLEN;
skb->nh.raw -= VLAN_HLEN;

I wonder if this is what was messing up the tcpdump packet capture as well?

A wrong Ethernet header pointer should probably do the trick.

Quoted 2 lines

It would be fine with me if you un-conditionally exported the vlan hard-start-xmit pointer, but I don't feel strongly either way.

Me neither. I just let Dave decide in those cases :-)

cheers,
Bart

#5 lore raw
In reply to Bart De Schuymer

include/linux/skbuff.h struct nf_bridge_info { · line 110

Quoted 2 lines · On Mon, 6 Oct 2003 22:04:07 +0200 Bart De Schuymer <[email protected]> wrote:
include/linux/skbuff.h +1 -1
-	unsigned long hh[16 / sizeof(unsigned long)];
+	unsigned char hh[18];

Maybe don't do this? It should be aligned on a 'long' boundry I think.

The only real hard objection I have with the patch is the vlan xmit routine pointer crap, just export the symbol.

Once you fix that I'll apply your patch as long as Stephen has no objections.

#6 lore raw

Re: [Bridge] Re: [PATCH/RFC] Let {ip, arp}tables "see" bridged VLAN tagged {I,AR}P packets

Quoted 8 lines · David S. Miller

On Mon, 6 Oct 2003 22:04:07 +0200

Review

Quoted 2 lines · Bart De Schuymer
(quoted diff) +1 -1
-	unsigned long hh[16 / sizeof(unsigned long)];
+	unsigned char hh[18];

Maybe don't do this? It should be aligned on a 'long' boundry I think.

I've changed it to 32 instead of 16. This wastes some space, but it's OK with me if it's OK with you.

Quoted 5 lines

The only real hard objection I have with the patch is the vlan xmit routine pointer crap, just export the symbol.

Once you fix that I'll apply your patch as long as Stephen has no objections.

I've used Stephen's suggestion (out->priv_flags & IFF_802_1Q_VLAN), this removes all that cruft.

cheers,
Bart

include/linux/skbuff.h +4 -1
@@ -103,8 +103,11 @@ struct nf_bridge_info {
 	atomic_t use;
 	struct net_device *physindev;
 	struct net_device *physoutdev;
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+	struct net_device *netoutdev;
+#endif
 	unsigned int mask;
-	unsigned long hh[16 / sizeof(unsigned long)];
+	unsigned long hh[32 / sizeof(unsigned long)];
 };
 #endif
 
include/linux/netfilter_bridge.h +34 -0
@@ -8,6 +8,9 @@
 #include <linux/netfilter.h>
 #if defined(__KERNEL__) && defined(CONFIG_BRIDGE_NETFILTER)
 #include <asm/atomic.h>
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+#include <linux/if_ether.h>
+#endif
 #endif
 
 /* Bridge Hooks */
@@ -44,6 +47,7 @@ enum nf_br_hook_priorities {
 #define BRNF_BRIDGED_DNAT		0x02
 #define BRNF_DONT_TAKE_PARENT		0x04
 #define BRNF_BRIDGED			0x08
+#define BRNF_NF_BRIDGE_PREROUTING	0x10
 
 static inline
 struct nf_bridge_info *nf_bridge_alloc(struct sk_buff *skb)
@@ -54,9 +58,39 @@ struct nf_bridge_info *nf_bridge_alloc(s
 		atomic_set(&(*nf_bridge)->use, 1);
 		(*nf_bridge)->mask = 0;
 		(*nf_bridge)->physindev = (*nf_bridge)->physoutdev = NULL;
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+		(*nf_bridge)->netoutdev = NULL;
+#endif
 	}
 
 	return *nf_bridge;
+}
+
+/* Only used in br_forward.c */
+static inline
+void nf_bridge_maybe_copy_header(struct sk_buff *skb)
+{
+	if (skb->nf_bridge) {
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+		if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
+			memcpy(skb->data - 18, skb->nf_bridge->hh, 18);
+			skb_push(skb, 4);
+		} else
+#endif
+			memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
+	}
+}
+
+static inline
+void nf_bridge_save_header(struct sk_buff *skb)
+{
+        int header_size = 16;
+
+#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
+	if (skb->protocol == __constant_htons(ETH_P_8021Q))
+		header_size = 18;
+#endif
+	memcpy(skb->nf_bridge->hh, skb->data - header_size, header_size);
 }
 
 struct bridge_skb_cb {
net/bridge/br_forward.c +1 -2
@@ -35,8 +35,7 @@ int br_dev_queue_push_xmit(struct sk_buf
 {
 #ifdef CONFIG_BRIDGE_NETFILTER
 	/* ip_refrag calls ip_fragment, which doesn't copy the MAC header. */
-	if (skb->nf_bridge)
-		memcpy(skb->data - 16, skb->nf_bridge->hh, 16);
+	nf_bridge_maybe_copy_header(skb);
 #endif
 	skb_push(skb, ETH_HLEN);
 
net/8021q/vlan_dev.c +4 -0
@@ -502,6 +502,10 @@ int vlan_dev_hard_start_xmit(struct sk_b
 	stats->tx_packets++; /* for statics only */
 	stats->tx_bytes += skb->len;
 
+	skb->protocol = __constant_htons(ETH_P_8021Q);
+	skb->mac.raw -= VLAN_HLEN;
+	skb->nh.raw -= VLAN_HLEN;
+
 	dev_queue_xmit(skb);
 
 	return 0;
net/bridge/br_netfilter.c +115 -26

321 lines not on this page · raw message

#7 lore raw

Re: [Bridge] Re: [PATCH/RFC] Let {ip, arp}tables "see" bridged VLAN tagged {I,AR}P packets

Quoted 2 lines · On Tue, 7 Oct 2003 21:12:53 +0200 Bart De Schuymer <[email protected]> wrote:

I've used Stephen's suggestion (out->priv_flags & IFF_802_1Q_VLAN), this removes all that cruft.

The new patch definitely looks better. Unless someone barks I'll apply this after I catch some sleep.

Thanks Bart.

#8 lore raw

Re: [Bridge] Re: [PATCH/RFC] Let {ip, arp}tables "see" bridged VLAN tagged {I, AR}P packets

In reply to Bart De Schuymer

On

Quoted 3 lines
Quoted 3 lines

Once you fix that I'll apply your patch as long as Stephen has no objections.

Looks fine to me, there are some things that might want to be cleaned up,
but they can go in later.

#9 lore raw
In reply to Bart De Schuymer

Instead of using vlan_dev_hard_start_xmit_p which causes looking too deep inside vlan, use the fact that all bridges and vlan devices are marked with dev->priv_flags.

Instead of:

#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
	    || (out->hard_start_xmit == vlan_dev_hard_start_xmit_p &&
	    VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit

#endif

Try:

#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
	    || (out->priv_flags & IFF_802_1Q_VLAN) &&
	    (VLAN_DEV_INFO(out)->real_dev->priv_vlags & IFF_EBRIDGE)

#endif

Gets rid of all the symbol export and other nastiness