Skip to contentSkip to conversation

VLAN match within iptables

DISCUSSION Jaime Nebrera 4 participants 16 messages Jun 26, 2007

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

Hi all,

We are seeking for a way to match rules based on VLAN Tag ID.

Searching the archives I have found this post:

http://lists.netfilter.org/pipermail/netfilter-devel/2006-August/025336.html

But the patch is missing and the answer was to use ebtables.

Of course ebtables already has this and this is kinda redundant, but
at the same time things get a bit complex when you have to worry about so many marks due to vlan id, ips stuff etc etc

Is there any way to access the patch Amin sent to the list?

Regards

Signature · 5 lines
Jaime Nebrera - [email protected]
Consultor TI - ENEO Tecnologia SL
Pol. PISA - C/ Manufactura 6, P1, 3B
Mairena del Aljarafe - 41927 - Sevilla
Telf.- 955 60 11 60 / 619 04 55 18
#1 lore raw
Quoted 2 lines · Jaime Nebrera

Is there any way to access the patch Amin sent to the list?

You do see the "Url: " part, don't you?

Jan

#2 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 1 line · Jaime Nebrera

Is there any way to access the patch Amin sent to the list?

You do see the "Url: " part, don't you?

Of course this behaviour is complete crap for a developer mailinglist archive:

A non-text attachment was scrubbed...
Name: vlan.2.6.17.patch

I'd just use a different archive until one of us gets around to changing this (which might take a looong time since I don't know if anyone is familiar with pipermail).

#3 lore raw
Quoted 11 lines · Patrick McHardy
Quoted 6 lines · Jan Engelhardt
Quoted 1 line · Jaime Nebrera

Is there any way to access the patch Amin sent to the list?

You do see the "Url: " part, don't you?

Of course this behaviour is complete crap for a developer mailinglist archive:

Quoted 6 lines

A non-text attachment was scrubbed...
Name: vlan.2.6.17.patch

I'd just use a different archive until one of us gets around to changing this (which might take a looong time since I don't know if anyone is familiar with pipermail).

FWIW,
http://lists.netfilter.org/pipermail/netfilter-devel/attachments/20060818/508fa575/vlan.2.6.11-0001.bin
brings up the 2nd attachment, for example.
"All" that would be needed is to transform that url into a clickable link.

Jan

#4 lore raw
In reply to Jaime Nebrera
Quoted 6 lines · Jaime Nebrera

We are seeking for a way to match rules based on VLAN Tag ID.

Searching the archives I have found this post:

http://lists.netfilter.org/pipermail/netfilter-devel/2006-August/025336.html

Quoted 7 lines

But the patch is missing and the answer was to use ebtables.

Of course ebtables already has this and this is kinda redundant, but
at the same time things get a bit complex when you have to worry about so many marks due to vlan id, ips stuff etc etc

Is there any way to access the patch Amin sent to the list?

Since you have to apply that patch to your kernel anyway, I suggest you to apply the u32 patch instead (it is scheduled for 2.6.23 IIRC).

http://lists.netfilter.org/pipermail/netfilter-devel/2007-June/028189.html
http://lists.netfilter.org/pipermail/netfilter-devel/2007-June/028176.html

Of course, you'll have to define the rule to match the vlan stuff but if later on you need to match any other minor bit, you'll already have the way to do so.

Signature · 3 lines
"Países en desarrollo es el nombre con que los expertos designan a los
países arrollados por el desarrollo ajeno" -- Patas Arriba. La Escuela
del Mundo al Revés -- E. Galeano
#5 lore raw
Quoted 2 lines · Pablo Neira Ayuso

Since you have to apply that patch to your kernel anyway, I suggest you to apply the u32 patch instead (it is scheduled for 2.6.23 IIRC).

I don't think that will work, it can't be used to match on data before skb->data since the offset can only be positive.

Jan, I just noticed the length checks are insufficient, very large positives offsets will lead to integer overflow and probably trigger the BUG afterwards. Another thing I missed before is that you should never do:

BUG_ON(expression with side-effects);

since some people disable BUG_ON.

Can you send a fix for this please?

#6 lore raw
Quoted 6 lines · Patrick McHardy
Quoted 2 lines · Pablo Neira Ayuso

Since you have to apply that patch to your kernel anyway, I suggest you to apply the u32 patch instead (it is scheduled for 2.6.23 IIRC).

I don't think that will work, it can't be used to match on data before skb->data since the offset can only be positive.

The VLAN ID is not in the Layer3 window. xt_u32 uses skb_copy_bits, which seems to start at the Layer3 (IPv4/ipv6) header(why that?!).

Quoted 3 lines

Jan, I just noticed the length checks are insufficient, very large positives offsets will lead to integer overflow and probably trigger the BUG afterwards.

Is this the right way to check for overflows?

                if (at > skb->len || at + pos > skb->len ||                     
                    at + pos + 3 > skb->len)                                    
                        return false;                                           


	Jan
#7 lore raw
Quoted 7 lines · Jan Engelhardt
Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Pablo Neira Ayuso

Since you have to apply that patch to your kernel anyway, I suggest you to apply the u32 patch instead (it is scheduled for 2.6.23 IIRC).

Quoted 7 lines
Quoted 3 lines

I don't think that will work, it can't be used to match on data before skb->data since the offset can only be positive.

The VLAN ID is not in the Layer3 window. xt_u32 uses skb_copy_bits, which seems to start at the Layer3 (IPv4/ipv6) header(why that?!).

It stats at skb->data, since thats the most natural way for users. And it supports negative offsets, but you need to make sure they're valid.

Quoted 6 lines
Quoted 5 lines

Jan, I just noticed the length checks are insufficient, very large positives offsets will lead to integer overflow and probably trigger the BUG afterwards.

Quoted 7 lines

Is this the right way to check for overflows?

                if (at > skb->len || at + pos > skb->len ||                     
                    at + pos + 3 > skb->len)                                    
                        return false;                                           

It depends, I'm not sure I understand the code correctly:

       if (at + pos + 3 > skb->len || at + pos < 0)
              return false;

       BUG_ON(skb_copy_bits(skb, pos, &val, sizeof(val)) < 0);

You're only copying at pos here, so I don't get why you're checking for at + pos. Just doing:

if (skb->len < 3 || pos > skb->len - 3)
return false;

should be fine for this case.

The second one goes:

      if (at + pos + 3 > skb->len || at + pos < 0)
               return false;

      BUG_ON(skb_copy_bits(skb, at+pos, &val,
              sizeof(val)) < 0);

So here I would do:

if (at + 3 < at || skb->len < at + 3 || pos > skb->len - at - 3)
return false;

#8 lore raw

xt_u32 20070626 (Re: VLAN match within iptables)

Subject: Add the U32 match from POM-NG

xt_u32 is the successor of ipt_u32 which has been in from POM-NG.
* IPv6 support
* inversion
* lock-free, big buffer is gone
* misc fixes (overflow checks)

---
 include/linux/netfilter/xt_u32.h |   40 +++++++++++
 net/netfilter/Kconfig            |   13 +++
 net/netfilter/Makefile           |    1 
 net/netfilter/xt_u32.c           |  135 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 189 insertions(+)
include/linux/netfilter/xt_u32.h new file +40 -0
Index: linux-2.6.22/include/linux/netfilter/xt_u32.h
===================================================================
@@ -0,0 +1,40 @@
+#ifndef _XT_U32_H
+#define _XT_U32_H 1
+
+enum xt_u32_ops {
+	XT_U32_AND,
+	XT_U32_LEFTSH,
+	XT_U32_RIGHTSH,
+	XT_U32_AT,
+};
+
+struct xt_u32_location_element {
+	u_int32_t number;
+	u_int8_t nextop;
+};
+
+struct xt_u32_value_element {
+	u_int32_t min;
+	u_int32_t max;
+};
+
+/*
+ * Any way to allow for an arbitrary number of elements?
+ * For now, I settle with a limit of 10 each.
+ */
+#define XT_U32_MAXSIZE 10
+
+struct xt_u32_test {
+	struct xt_u32_location_element location[XT_U32_MAXSIZE+1];
+	struct xt_u32_value_element value[XT_U32_MAXSIZE+1];
+	u_int8_t nnums;
+	u_int8_t nvalues;
+};
+
+struct xt_u32 {
+	struct xt_u32_test tests[XT_U32_MAXSIZE+1];
+	u_int8_t ntests;
+	u_int8_t invert;
+};
+
+#endif /* _XT_U32_H */
net/netfilter/Kconfig +13 -0
Index: linux-2.6.22/net/netfilter/Kconfig
===================================================================
@@ -635,6 +635,19 @@ config NETFILTER_XT_MATCH_TCPMSS
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_MATCH_U32
+	tristate '"u32" match support'
+	depends on NETFILTER_XTABLES
+	---help---
+	  u32 allows you to extract quantities of up to 4 bytes from a packet,
+	  AND them with specified masks, shift them by specified amounts and
+	  test whether the results are in any of a set of specified ranges.
+	  The specification of what to extract is general enough to skip over
+	  headers with lengths stored in the packet, as in IP or TCP header
+	  lengths.
+
+	  Details and examples are in the kernel module source.
+
 config NETFILTER_XT_MATCH_HASHLIMIT
 	tristate '"hashlimit" match support'
 	depends on NETFILTER_XTABLES && (IP6_NF_IPTABLES || IP6_NF_IPTABLES=n)
net/netfilter/Makefile +1 -0
Index: linux-2.6.22/net/netfilter/Makefile
===================================================================
@@ -72,4 +72,5 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_STATISTI
 obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) += xt_string.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_U32) += xt_u32.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_HASHLIMIT) += xt_hashlimit.o
net/netfilter/xt_u32.c new file +135 -0

135 lines not on this page · raw message

#9 lore raw

xt_u32 20070626 (iptables part)

# Small documentation update.
# ( s/skb->head/skb->data/ )

Refreshed libipt_u32 to work with xt_u32.
Introduces libip6t_u32.

---

symlink extensions/.u32-test6 -> extensions/.u32-test symlink extensions/libip6t_u32.man -> extensions/libipt_u32.man

 extensions/.u32-test      |    2 
 extensions/libip6t_u32.c  |  299 ++++++++++++++++++++++++++++++++++++++++++++++
 extensions/libipt_u32.c   |  299 ++++++++++++++++++++++++++++++++++++++++++++++
 extensions/libipt_u32.man |  129 +++++++++++++++++++
 4 files changed, 729 insertions(+)
extensions/.u32-test new file +2 -0
Index: iptables/extensions/.u32-test
===================================================================
@@ -0,0 +1,2 @@
+#!/bin/sh
+[ -f "$KERNEL_DIR/include/linux/netfilter/xt_u32.h" ] && echo u32
extensions/libip6t_u32.c new file +299 -0

299 lines not on this page · raw message

extensions/libipt_u32.c new file +299 -0

299 lines not on this page · raw message

extensions/libipt_u32.man new file +129 -0

129 lines not on this page · raw message

#11 lore raw

Re: xt_u32 20070626 (Re: VLAN match within iptables)

In reply to Jan Engelhardt
Quoted 1 line · Jan Engelhardt

Subject: Add the U32 match from POM-NG

I prefer incremental patches. Your patch looks good, I'm wondering about the constant 3 though. A int is four bytes long.

+ if (skb->len < 3 || pos > skb->len - 3);

assume skb->len == 3, pos = 0. Its obviously incorrent.

+ if (at + 3 < at || skb->len < at + 3 ||
+ pos > skb->len - at - 3)

Same here, just assume the same as above and at == 0.

So I'm replacing all 3s by 4s.

#12 lore raw

Re: xt_u32 20070626 (Re: VLAN match within iptables)

Quoted 4 lines · Patrick McHardy
Quoted 1 line · Jan Engelhardt

Subject: Add the U32 match from POM-NG

I prefer incremental patches.

Hm, some UTF-8 got broken. Hence I attach the (incremental) patch (also fixes 3 -> 4) with a 8 bit preserving mime type. Inline version below for by-standers to look at :)

===

Correct overflow checks in xt_u32 and separate BUG_ON from expressions with side-effects.

---
 net/netfilter/xt_u32.c |   19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
net/netfilter/xt_u32.c +12 -7
Index: linux-2.6.22/net/netfilter/xt_u32.c
===================================================================
@@ -2,7 +2,7 @@
  *	xt_u32 - kernel module to match u32 packet content
  *
  *	Original author: Don Cohen <[email protected]>
- *	 Jan Engelhardt <[email protected]>, 2007
+ *	Copyright © Jan Engelhardt <[email protected]>, 2007
  */
 
 #include <linux/module.h>
@@ -13,7 +13,6 @@
 #include <linux/netfilter/x_tables.h>
 #include <linux/netfilter/xt_u32.h>
 
-
 static bool u32_match_it(const struct xt_u32 *data,
 			 const struct sk_buff *skb)
 {
@@ -25,6 +24,7 @@ static bool u32_match_it(const struct xt
 	u_int32_t pos;
 	u_int32_t val;
 	u_int32_t at;
+	int ret;
 
 	/*
 	 * Small example: "0 >> 28 == 4 && 8 & 0xFF0000 >> 16 = 6, 17"
@@ -35,10 +35,11 @@ static bool u32_match_it(const struct xt
 		at  = 0;
 		pos = ct->location[0].number;
 
-		if (at + pos + 3 > skb->len || at + pos < 0)
+		if (skb->len < 4 || pos > skb->len - 4);
 			return false;
 
-		BUG_ON(skb_copy_bits(skb, pos, &val, sizeof(val)) < 0);
+		ret   = skb_copy_bits(skb, pos, &val, sizeof(val));
+		BUG_ON(ret < 0);
 		val   = ntohl(val);
 		nnums = ct->nnums;
 
@@ -56,13 +57,17 @@ static bool u32_match_it(const struct xt
 				val >>= number;
 				break;
 			case XT_U32_AT:
+				if (at + val < at)
+					return false;
 				at += val;
 				pos = number;
-				if (at + pos + 3 > skb->len || at + pos < 0)
+				if (at + 4 < at || skb->len < at + 4 ||
+				    pos > skb->len - at - 4)
 					return false;
 
-				BUG_ON(skb_copy_bits(skb, at+pos, &val,
-				       sizeof(val)) < 0);
+				ret = skb_copy_bits(skb, at + pos, &val,
+				      sizeof(val));
+				BUG_ON(ret < 0);
 				val = ntohl(val);
 				break;
 			}
#13 lore raw

Re: xt_u32 20070626 (Re: VLAN match within iptables)

Quoted 2 lines · Jan Engelhardt

Hm, some UTF-8 got broken.

I noticed that and removed the broken part. So unless there's some other difference (and the 3 -> 4) I'm leaving it as it is.

#14 lore raw

Re: xt_u32 20070626 (Re: VLAN match within iptables)

Quoted 6 lines · Patrick McHardy
Quoted 2 lines · Jan Engelhardt

Hm, some UTF-8 got broken.

I noticed that and removed the broken part. So unless there's some other difference (and the 3 -> 4) I'm leaving it as it is.

I also did the BUG_ON(side_effect) separation in the most recent patch.

Jan

#15 lore raw

Re: xt_u32 20070626 (Re: VLAN match within iptables)

Quoted 4 lines · Jan Engelhardt
Quoted 2 lines · Patrick McHardy

Jan Engelhardt wrote:

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Hm, some UTF-8 got broken.

I noticed that and removed the broken part. So unless there's some other difference (and the 3 -> 4) I'm leaving it as it is.

I also did the BUG_ON(side_effect) separation in the most recent patch.

That was already in the last one.

#10 lore raw

Re: xt_u32 20070626 (iptables part)

In reply to Jan Engelhardt
Quoted 10 lines · Jan Engelhardt

Index: iptables/extensions/.u32-test
===================================================================

extensions/.u32-test new file +2 -0
@@ -0,0 +1,2 @@
+#!/bin/sh
+[ -f "$KERNEL_DIR/include/linux/netfilter/xt_u32.h" ] && echo u32

Index: iptables/extensions/libip6t_u32.c
===================================================================

Having to build against a kernel tree sucks, please add the header and build unconditionally.

Quoted 196 lines
extensions/.u32-test new file +195 -0
+/* shared printing code */
+static void u32_dump(const struct xt_u32 *data)
+{
+	const struct xt_u32_test *ct;
+	unsigned int testind, i;
+
+	for (testind = 0; testind < data->ntests; ++testind) {
+		ct = &data->tests[testind];
+
+		if (testind > 0)
+			printf("&&");
+
+		printf("0x%x", ct->location[0].number);
+		for (i = 1; i < ct->nnums; ++i) {
+			switch (ct->location[i].nextop) {
+			case XT_U32_AND:
+				printf("&");
+				break;
+			case XT_U32_LEFTSH:
+				printf("<<");
+				break;
+			case XT_U32_RIGHTSH:
+				printf(">>");
+				break;
+			case XT_U32_AT:
+				printf("@");
+				break;
+			}
+			printf("0x%x", ct->location[i].number);
+		}
+
+		printf("=");
+		for (i = 0; i < ct->nvalues; ++i) {
+			if (i > 0)
+				printf(",");
+			if (ct->value[i].min == ct->value[i].max)
+				printf("0x%x", ct->value[i].min);
+			else
+				printf("0x%x:0x%x", ct->value[i].min,
+				       ct->value[i].max);
+		}
+	}
+	printf(" ");
+}
+
+/* string_to_number is not quite what we need here ... */
+static uint32_t parse_number(char **s, int pos)
+{
+	uint32_t number;
+	char *end;
+	errno = 0;
+
+	number = strtoul(*s, &end, 0);
+	if (end == *s)
+		exit_error(PARAMETER_PROBLEM,
+			   "u32: at char %d: expected number", pos);
+	if (errno)
+		exit_error(PARAMETER_PROBLEM,
+			   "u32: at char %d: error reading number", pos);
+	*s = end;
+	return number;
+}
+
+/* Function which parses command options; returns true if it ate an option */
+static int u32_parse(int c, char **argv, int invert, unsigned int *flags,
+		     const struct ip6t_entry *entry, unsigned int *nfcache,
+		     struct ip6t_entry_match **match)
+{
+	struct xt_u32 *data = (void *)(*match)->data;
+	unsigned int testind = 0, locind = 0, valind = 0;
+	struct xt_u32_test *ct = &data->tests[testind]; /* current test */
+	char *arg = argv[optind-1]; /* the argument string */
+	char *start = arg;
+	int state = 0;
+
+	if (c != '1')
+		return 0;
+
+	data->invert = invert;
+
+	/*
+	 * states:
+	 * 0 = looking for numbers and operations,
+	 * 1 = looking for ranges
+	 */
+	while (1) {
+		/* read next operand/number or range */
+		while (isspace(*arg))
+			++arg;
+
+		if (*arg == '\0') {
+			/* end of argument found */
+			if (state == 0)
+				exit_error(PARAMETER_PROBLEM,
+					   "u32: input ended in location spec");
+			if (valind == 0)
+				exit_error(PARAMETER_PROBLEM,
+					   "u32: test ended with no value spec");
+
+			ct->nnums    = locind;
+			ct->nvalues  = valind;
+			data->ntests = ++testind;
+
+			if (testind > XT_U32_MAXSIZE)
+				exit_error(PARAMETER_PROBLEM,
+				           "u32: at char %d: too many \"&&\"s",
+				           arg - start);
+			return 1;
+		}
+
+		if (state == 0) {
+			/*
+			 * reading location: read a number if nothing read yet,
+			 * otherwise either op number or = to end location spec
+			 */
+			if (*arg == '=') {
+				if (locind == 0) {
+					exit_error(PARAMETER_PROBLEM,
+					           "u32: at char %d: "
+					           "location spec missing",
+					           arg - start);
+				} else {
+					++arg;
+					state = 1;
+				}
+			} else {
+				if (locind != 0) {
+					/* need op before number */
+					if (*arg == '&') {
+						ct->location[locind].nextop = XT_U32_AND;
+					} else if (*arg == '<') {
+						if (*++arg != '<')
+							exit_error(PARAMETER_PROBLEM,
+								   "u32: at char %d: a second < expected", arg - start);
+						ct->location[locind].nextop = XT_U32_LEFTSH;
+					} else if (*arg == '>') {
+						if (*++arg != '>')
+							exit_error(PARAMETER_PROBLEM,
+								   "u32: at char %d: a second > expected", arg - start);
+						ct->location[locind].nextop = XT_U32_RIGHTSH;
+					} else if (*arg == '@') {
+						ct->location[locind].nextop = XT_U32_AT;
+					} else {
+						exit_error(PARAMETER_PROBLEM,
+							"u32: at char %d: operator expected", arg - start);
+					}
+					++arg;
+				}
+				/* now a number; string_to_number skips white space? */
+				ct->location[locind].number =
+					parse_number(&arg, arg - start);
+				if (++locind > XT_U32_MAXSIZE)
+					exit_error(PARAMETER_PROBLEM,
+						   "u32: at char %d: too many operators", arg - start);
+			}
+		} else {
+			/*
+			 * state 1 - reading values: read a range if nothing
+			 * read yet, otherwise either ,range or && to end
+			 * test spec
+			 */
+			if (*arg == '&') {
+				if (*++arg != '&')
+					exit_error(PARAMETER_PROBLEM,
+						   "u32: at char %d: a second & was expected", arg - start);
+				if (valind == 0) {
+					exit_error(PARAMETER_PROBLEM,
+						   "u32: at char %d: value spec missing", arg - start);
+				} else {
+					ct->nnums   = locind;
+					ct->nvalues = valind;
+					ct = &data->tests[++testind];
+					if (testind > XT_U32_MAXSIZE)
+						exit_error(PARAMETER_PROBLEM,
+							   "u32: at char %d: too many \"&&\"s", arg - start);
+					++arg;
+					state  = 0;
+					locind = 0;
+					valind = 0;
+				}
+			} else { /* read value range */
+				if (valind > 0) { /* need , before number */
+					if (*arg != ',')
+						exit_error(PARAMETER_PROBLEM,
+							   "u32: at char %d: expected , or &&", arg - start);
+					++arg;
+				}
+				ct->value[valind].min =
+					parse_number(&arg, arg - start);
+
+				while (isspace(*arg))
+					++arg;
+
+				if (*arg == ':') {
+					++arg;

extensions/.u32-test

Quoted 10 lines
extensions/.u32-test new file +10 -0
+					ct->value[valind].max =
+						parse_number(&arg, arg-start);
+				} else {
+					ct->value[valind].max =
+						ct->value[valind].min;
+				}
+
+				if (++valind > XT_U32_MAXSIZE)
+					exit_error(PARAMETER_PROBLEM,
+						   "u32: at char %d: too many \",\"s", arg - start);

Thats not very understandable code. I hope I never have to fix anything in there ..