Skip to contentSkip to conversation

netfilter: Added vlan matching extension

PATCH In review Eddie Linder 3 participants 4 messages May 25, 2015 – May 27, 2015

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

PATCH 1/1 netfilter: Added vlan matching extension

---
 include/uapi/linux/netfilter/Kbuild    |  1 +
 include/uapi/linux/netfilter/xt_vlan.h | 11 ++++
 net/netfilter/Makefile                 |  1 +
 net/netfilter/xt_vlan.c                | 91 ++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+)
 create mode 100644 include/uapi/linux/netfilter/xt_vlan.h
 create mode 100644 net/netfilter/xt_vlan.c
include/uapi/linux/netfilter/Kbuild +1 -0
index 1d973d2..2ca36db 100644
@@ -85,3 +85,4 @@ header-y += xt_tcpmss.h
 header-y += xt_tcpudp.h
 header-y += xt_time.h
 header-y += xt_u32.h
+header-y += xt_vlan.h
include/uapi/linux/netfilter/xt_vlan.h new file +11 -0
new file mode 100644
index 0000000..3c31b6b
@@ -0,0 +1,11 @@
+#ifndef _XT_VLAN_H
+#define _XT_VLAN_H
+
+
+struct xt_vlan_info {
+	unsigned short vlan_id;
+	unsigned char vlan_pcp;
+	unsigned char invert;
+};
+#endif /*_XT_VLAN_H */
+
net/netfilter/Makefile +1 -0
index a87d8b8..37ecfb0 100644
@@ -169,6 +169,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_TIME) += xt_time.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_U32) += xt_u32.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_VLAN) += xt_vlan.o
 
 # ipset
 obj-$(CONFIG_IP_SET) += ipset/
net/netfilter/xt_vlan.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 1 line · Eddie Linder

Why do we need a vlan tag match in iptables?
For bridged traffic we have ebtables' net/bridge/netfilter/ebt_vlan.c .

And for routed traffic you'd just use -i vlan.id.0 or whatever?

What is this match being used for, and more importantly where/how? (Is this for call-iptables=1 invocation from bridge netfilter?)

Nevertheless, a couple of comments below.

Quoted 15 lines
include/uapi/linux/netfilter/Kbuild +1 -0
index 1d973d2..2ca36db 100644
@@ -85,3 +85,4 @@ header-y += xt_tcpmss.h
 header-y += xt_tcpudp.h
 header-y += xt_time.h
 header-y += xt_u32.h
+header-y += xt_vlan.h
include/uapi/linux/netfilter/xt_vlan.h new file +0 -0
new file mode 100644
index 0000000..3c31b6b

include/uapi/linux/netfilter/xt_vlan.h line 7

Quoted 7 lines
include/uapi/linux/netfilter/xt_vlan.h new file +7 -0
@@ -0,0 +1,11 @@
+#ifndef _XT_VLAN_H
+#define _XT_VLAN_H
+
+
+struct xt_vlan_info {
+	unsigned short vlan_id;
+	unsigned char vlan_pcp;

Please consider using explicit types for all of these, i.e.

	__u16 vlan_id;
	__u8 vlan_pcp;

include/uapi/linux/netfilter/xt_vlan.h line 11

Quoted 4 lines
include/uapi/linux/netfilter/xt_vlan.h new file +4 -0
+MODULE_ALIAS("ipt_vlan");
+MODULE_ALIAS("ip6t_vlan");
+MODULE_ALIAS("ipt_VLAN");
+MODULE_ALIAS("ip6t_VLAN");

_VLAN aliases are only needed for -j VLAN target, which isn't added here, please remove them.

include/uapi/linux/netfilter/xt_vlan.h

Quoted 9 lines
include/uapi/linux/netfilter/xt_vlan.h new file +9 -0
+static bool vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
+{
+	const struct xt_vlan_info *info = par->matchinfo;
+	u16 vlan_tci = ~VLAN_TAG_PRESENT;
+	bool ret;
+
+	if (skb->vlan_tci & VLAN_TAG_PRESENT)
+		/* Strip the 4096 bit (invalid vlan) from the vlan tci bits */
+		vlan_tci = skb->vlan_tci;

if (skb_vlan_tag_present(skb))

	vlan_tci = skb_vlan_tag_get(skb);

include/uapi/linux/netfilter/xt_vlan.h

Quoted 2 lines
include/uapi/linux/netfilter/xt_vlan.h new file +2 -0
+	else {
+		if (unlikely(skb->len < VLAN_ETH_HLEN &&

This is weird. Why test len < VLAN_ETH_HLEN (> ? >= ? )

include/uapi/linux/netfilter/xt_vlan.h

Quoted 6 lines
include/uapi/linux/netfilter/xt_vlan.h new file +6 -0
+			     skb->protocol == htons(ETH_P_8021Q)))
+			if (unlikely(!parse_vlan(skb, &vlan_tci)))
+				return false;
+			else
+				return false;
+	}

Why call parse_vlan? We always return false without looking at vlan_tci...

#2 lore raw

Thanks for answering so fast Florian,

Yes, I do plan to use it with a bridge, but I don't think ebtables is the proper solution, as I'd like to be able to use the matches and the targets provided by the iptables extensions.
I plan to use vlan matching on an hypervisor that uses vlans for tenant separation, and by using the iptables multiport extension I plan to block unwanted traffic per tenant (which means per vlan). I'll send my fixes shortly.

Thanks, Eddie

Quoted 75 lines · Florian Westphal
Quoted 1 line · Eddie Linder

Why do we need a vlan tag match in iptables?
For bridged traffic we have ebtables' net/bridge/netfilter/ebt_vlan.c .

And for routed traffic you'd just use -i vlan.id.0 or whatever?

What is this match being used for, and more importantly where/how? (Is this for call-iptables=1 invocation from bridge netfilter?)

Nevertheless, a couple of comments below.

Quoted 15 lines
include/uapi/linux/netfilter/Kbuild +1 -0
index 1d973d2..2ca36db 100644
@@ -85,3 +85,4 @@ header-y += xt_tcpmss.h
 header-y += xt_tcpudp.h
 header-y += xt_time.h
 header-y += xt_u32.h
+header-y += xt_vlan.h
include/uapi/linux/netfilter/xt_vlan.h new file +0 -0
new file mode 100644
index 0000000..3c31b6b

include/uapi/linux/netfilter/xt_vlan.h line 7

Quoted 7 lines
include/uapi/linux/netfilter/xt_vlan.h new file +7 -0
@@ -0,0 +1,11 @@
+#ifndef _XT_VLAN_H
+#define _XT_VLAN_H
+
+
+struct xt_vlan_info {
+     unsigned short vlan_id;
+     unsigned char vlan_pcp;

Please consider using explicit types for all of these, i.e.

        __u16 vlan_id;
        __u8 vlan_pcp;

include/uapi/linux/netfilter/xt_vlan.h line 11

Quoted 4 lines
include/uapi/linux/netfilter/xt_vlan.h new file +4 -0
+MODULE_ALIAS("ipt_vlan");
+MODULE_ALIAS("ip6t_vlan");
+MODULE_ALIAS("ipt_VLAN");
+MODULE_ALIAS("ip6t_VLAN");

_VLAN aliases are only needed for -j VLAN target, which isn't added here, please remove them.

include/uapi/linux/netfilter/xt_vlan.h

Quoted 9 lines
include/uapi/linux/netfilter/xt_vlan.h new file +9 -0
+static bool vlan_mt(const struct sk_buff *skb, struct xt_action_param *par)
+{
+     const struct xt_vlan_info *info = par->matchinfo;
+     u16 vlan_tci = ~VLAN_TAG_PRESENT;
+     bool ret;
+
+     if (skb->vlan_tci & VLAN_TAG_PRESENT)
+             /* Strip the 4096 bit (invalid vlan) from the vlan tci bits */
+             vlan_tci = skb->vlan_tci;

if (skb_vlan_tag_present(skb))

        vlan_tci = skb_vlan_tag_get(skb);

include/uapi/linux/netfilter/xt_vlan.h

Quoted 2 lines
include/uapi/linux/netfilter/xt_vlan.h new file +2 -0
+     else {
+             if (unlikely(skb->len < VLAN_ETH_HLEN &&

This is weird. Why test len < VLAN_ETH_HLEN (> ? >= ? )

include/uapi/linux/netfilter/xt_vlan.h

Quoted 6 lines
include/uapi/linux/netfilter/xt_vlan.h new file +6 -0
+                          skb->protocol == htons(ETH_P_8021Q)))
+                     if (unlikely(!parse_vlan(skb, &vlan_tci)))
+                             return false;
+                     else
+                             return false;
+     }

Why call parse_vlan? We always return false without looking at vlan_tci...

#3 lore raw

On Tue, May 26, 2015 at 05:24:56PM +0300, Eddi Linder wrote:
[...]

Quoted 3 lines

Yes, I do plan to use it with a bridge, but I don't think ebtables is the proper solution, as I'd like to be able to use the matches and the targets provided by the iptables extensions.

If we follow this path, we'll open the door to get more patches to bloat iptables with features that actually belong to the bridge family, just because you don't want to use ebtables for this.

We have the chance to resolve this situation from nf_tables.

Sorry, I'm not applying this.