Skip to contentSkip to conversation

add support for VLAN header filtering in bridge family

PATCH In review Florian Westphal 1 participant 15 messages Aug 16, 2015 – Sep 21, 2015 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover add support for VLAN header filtering in bridge family

This adds the following features:
- stacked headers: vlan sits on top of ethernet, so when vlan id 42 is requested, add a dependency on eth and also adjust payload offsets by the size of ethernet header.

- allow matching header fields of lengths and/or offsets that are not byte divisible, e.g. vlan id, vlan pcp, ip hdrlength, etc.

Missing/not working:

- concat support for vlan ids, e.f. this will not work:
vlan id . ip saddr { 1 . 1.2.3.4 }

- payload merging of fields that have lengths/sizes not divisible by 8.

IOW when asking for vlan id 42 vlan pcp 0 then we generate 2 payload instructions, 2 binops, 2 cmps.

Oddities:

bridge filter input ip saddr 1.2.3.4

... will never match in some cases.

'Problem' (or feature...?) is that nft adds dependency on 'ether type ip' so if all traffic is VLAN encapsulated this will not match.

[ can use 'vlan type ip ip saddr 1.2.3.4' to match eth-vlan-ip packets ].

There might be a lot of pitfalls that i've missed.

Unless there are objections I will push the first two patches soon.

The vlan patch set has seen minimal testing inside kvm on x86_64, using a (pending) kernel patch to nft_payload to transparently insert the missing/stripped vlan header into the register set.

I'd be happy to receive feedback on these patches, I'm sure that some things can be implemented in a more straightforward fashion.

Cheers,
Florian

author maintainer
#1 lore raw

PATCH 1/12 tests: use the src/nft binary instead of $PATH one

... so one doesn't need to install new binary into $PATH (or change PATH... ) during development.

---
 tests/regression/nft-test.py | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
tests/regression/nft-test.py +18 -17
index 26fc2ec..c3d2aae 100755
@@ -20,6 +20,7 @@ import argparse
 import signal
 
 TERMINAL_PATH = os.getcwd()
+NFT_BIN = TERMINAL_PATH + "/src/nft"
 TESTS_PATH = os.path.dirname(os.path.abspath(__file__))
 TESTS_DIRECTORY = ["any", "arp", "bridge", "inet", "ip", "ip6"]
 LOGFILE = "/tmp/nftables-test.log"
@@ -77,7 +78,7 @@ def table_exist(table, filename, lineno):
     '''
     Exists a table.
     '''
-    cmd = "nft list -nnn table " + table[0] + " " + table[1]
+    cmd = NFT_BIN + " list -nnn table " + table[0] + " " + table[1]
     ret = execute_cmd(cmd, filename, lineno)
 
     return True if (ret == 0) else False
@@ -87,7 +88,7 @@ def table_flush(table, filename, lineno):
     '''
     Flush a table.
     '''
-    cmd = "nft flush table " + str(table[0]) + " " + str(table[1])
+    cmd = NFT_BIN + " flush table " + str(table[0]) + " " + str(table[1])
     ret = execute_cmd(cmd, filename, lineno)
 
     return cmd
@@ -106,7 +107,7 @@ def table_create(table, filename, lineno):
     table_list.append(table)
 
     ## We add a new table
-    cmd = "nft add table " + table[0] + " " + table[1]
+    cmd = NFT_BIN + " add table " + table[0] + " " + table[1]
     ret = execute_cmd(cmd, filename, lineno)
 
     if ret != 0:
@@ -138,7 +139,7 @@ def table_delete(table, filename=None, lineno=None):
         print_error(reason, filename, lineno)
         return -1
 
-    cmd = "nft delete table" + table_info
+    cmd = NFT_BIN + " delete table" + table_info
     ret = execute_cmd(cmd, filename, lineno)
     if ret != 0:
         reason = cmd + ": " \
@@ -161,7 +162,7 @@ def chain_exist(chain, table, filename, lineno):
     '''
 
     table_info = " " + table[0] + " " + table[1] + " "
-    cmd = "nft list -nnn chain" + table_info + chain
+    cmd = NFT_BIN + " list -nnn chain" + table_info + chain
     ret = execute_cmd(cmd, filename, lineno)
 
     return True if (ret == 0) else False
@@ -181,9 +182,9 @@ def chain_create(chain, chain_type, chain_list, table, filename, lineno):
         return -1
 
     if chain_type:
-        cmd = "nft add chain" + table_info + chain + "\{ " + chain_type + "\; \}"
+        cmd = NFT_BIN + " add chain" + table_info + chain + "\{ " + chain_type + "\; \}"
     else:
-        cmd = "nft add chain" + table_info + chain
+        cmd = NFT_BIN + " add chain" + table_info + chain
 
     ret = execute_cmd(cmd, filename, lineno)
     if ret != 0:
@@ -216,14 +217,14 @@ def chain_delete(chain, table,  filename=None, lineno=None):
         print_error(reason, filename, lineno)
         return -1
 
-    cmd = "nft flush chain" + table_info + chain
+    cmd = NFT_BIN + " flush chain" + table_info + chain
     ret = execute_cmd(cmd, filename, lineno)
     if ret != 0:
         reason = "I cannot flush this chain " + chain
         print_error(reason, filename, lineno)
         return -1
 
-    cmd = "nft delete chain" + table_info + chain
+    cmd = NFT_BIN + " delete chain" + table_info + chain
     ret = execute_cmd(cmd, filename, lineno)
     if ret != 0:
         reason = cmd + "I cannot delete this chain. DD"
@@ -258,7 +259,7 @@ def set_add(set_info, table_list, filename, lineno):
 
         table_info = " " + table[0] + " " + table[1] + " "
         set_text = " " + set_info[0] + " { type " + set_info[1] + " \;}"
-        cmd = "nft add set" + table_info + set_text
+        cmd = NFT_BIN + " add set" + table_info + set_text
         ret = execute_cmd(cmd, filename, lineno)
 
         if (ret == 0 and set_info[2].rstrip() == "fail") or \
@@ -306,7 +307,7 @@ def set_add_elements(set_element, set_name, set_all, state, table_list,
                 element = element + ", " + e
 
         set_text = set_name + " { " + element + " }"
-        cmd = "nft add element" + table_info + set_text
+        cmd = NFT_BIN + " add element" + table_info + set_text
         ret = execute_cmd(cmd, filename, lineno)
 
         if (state == "fail" and ret == 0) or (state == "ok" and ret != 0):
@@ -332,7 +333,7 @@ def set_delete_elements(set_element, set_name, table, filename=None,
 
     for element in set_element:
         set_text = set_name + " {" + element + "}"
-        cmd = "nft delete element" + table_info + set_text
+        cmd = NFT_BIN + " delete element" + table_info + set_text
         ret = execute_cmd(cmd, filename, lineno)
         if ret != 0:
             reason = "I cannot delete an element" + element + \
@@ -362,7 +363,7 @@ def set_delete(all_set, table, filename=None, lineno=None):
 
         # We delete the set.
         table_info = " " + table[0] + " " + table[1] + " "
-        cmd = "nft delete set " + table_info + " " + set_name
+        cmd = NFT_BIN + " delete set " + table_info + " " + set_name
         ret = execute_cmd(cmd, filename, lineno)
 
         # Check if the set still exists after I deleted it.
@@ -379,7 +380,7 @@ def set_exist(set_name, table, filename, lineno):
     Check if the set exists.
     '''
     table_info = " " + table[0] + " " + table[1] + " "
-    cmd = "nft list -nnn set" + table_info + set_name
+    cmd = NFT_BIN + " list -nnn set" + table_info + set_name
     ret = execute_cmd(cmd, filename, lineno)
 
     return True if (ret == 0) else False
@@ -487,11 +488,11 @@ def rule_add(rule, table_list, chain_list, filename, lineno,
             unit_tests += 1
             table_flush(table, filename, lineno)
             table_info = " " + table[0] + " " + table[1] + " "
-            cmd = "nft add rule" + table_info + chain + " " + rule[0]
+            cmd = NFT_BIN + " add rule" + table_info + chain + " " + rule[0]
 
             payload_log = os.tmpfile();
 
-            cmd = "nft add rule --debug=netlink" + table_info + chain + " " + rule[0]
+            cmd = NFT_BIN + " add rule --debug=netlink" + table_info + chain + " " + rule[0]
             ret = execute_cmd(cmd, filename, lineno, payload_log)
 
             state = rule[1].rstrip()
@@ -527,7 +528,7 @@ def rule_add(rule, table_list, chain_list, filename, lineno,
                     print_warning("Wrote payload for rule %s" % rule[0], gotf.name, 1)
 
             # Check output of nft
-                process = subprocess.Popen(['nft', '-nnn', 'list', 'table'] + table,
+                process = subprocess.Popen([NFT_BIN, '-nnn', 'list', 'table'] + table,
                                            shell=False, stdout=subprocess.PIPE,
                                            preexec_fn=preexec)
                 pre_output = process.communicate()
Signature · 1 line
2.0.5
author maintainer
#2 lore raw

PATCH 2/12 tests: add 'awkward' prefix match expression

In reply to Florian Westphal

Its just a more complicated way of saying 'ip saddr 255.255.0.0/16'.

---
 tests/regression/ip/ip.t              | 3 +++
 tests/regression/ip/ip.t.payload      | 6 ++++++
 tests/regression/ip/ip.t.payload.inet | 8 ++++++++
 3 files changed, 17 insertions(+)
tests/regression/ip/ip.t +3 -0
index 11a9598..6e29ffb 100644
@@ -108,3 +108,6 @@ ip daddr 192.168.0.2 log;ok
 
 ip saddr \& 0xff == 1;ok;ip saddr & 0.0.0.255 == 0.0.0.1
 ip saddr \& 0.0.0.255 \< 0.0.0.127;ok;ip saddr & 0.0.0.255 < 0.0.0.127
+
+ip saddr \& 0xffff0000 == 0xffff0000;ok;ip saddr 255.255.0.0/16
+
tests/regression/ip/ip.t.payload +6 -0
index ec4897e..7a77dc4 100644
@@ -347,3 +347,9 @@ ip test-ip4 input
   [ bitwise reg 1 = (reg=1 & 0xff000000 ) ^ 0x00000000 ]
   [ cmp lt reg 1 0x7f000000 ]
 
+# ip saddr \& 0xffff0000 == 0xffff0000
+ip test-ip4 input
+  [ payload load 4b @ network header + 12 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000ffff ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x0000ffff ]
+
tests/regression/ip/ip.t.payload.inet +8 -0
index 1a2b1b5..dbc7852 100644
@@ -457,3 +457,11 @@ inet test-inet input
   [ bitwise reg 1 = (reg=1 & 0xff000000 ) ^ 0x00000000 ]
   [ cmp lt reg 1 0x7f000000 ]
 
+# ip saddr \& 0xffff0000 == 0xffff0000
+inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x00000002 ]
+  [ payload load 4b @ network header + 12 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000ffff ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x0000ffff ]
+
Signature · 1 line
2.0.5
author maintainer
#3 lore raw

PATCH 3/12 nft: allow stacking vlan header on top of ethernet

In reply to Florian Westphal

currently 'vlan id 42' or even 'vlan type ip' doesn't work since we expect ethernet header but get vlan.

So if we want to add another protocol header to the same base, we attempt to figure out if the new header can fit on top of the existing one (i.e. proto_find_num gives a protocol number when asking to find link between the two).

We also annotate protocol description for eth and vlan with the full header size and track the offset from the current base.

Otherwise, 'vlan type ip' fetches the protocol field from mac header offset 0, which is some mac address.

Instead, we must consider full size of ethernet header.

---
 include/proto.h           |  4 +++
 src/evaluate.c            | 78 ++++++++++++++++++++++++++++++++++++++++++++++-
 src/netlink_delinearize.c | 48 +++++++++++++++++++++++------
 src/payload.c             |  1 +
 src/proto.c               |  2 ++
 5 files changed, 123 insertions(+), 10 deletions(-)
include/proto.h +4 -0
index 0e531b2..a43bf98 100644
@@ -69,6 +69,7 @@ struct proto_hdr_template {
  * @name:	protocol name
  * @base:	header base
  * @protocol_key: key of template containing upper layer protocol description
+ * @length:	total size of the header, in bits
  * @protocols:	link to upper layer protocol descriptions indexed by protocol value
  * @templates:	header templates
  */
@@ -76,6 +77,7 @@ struct proto_desc {
 	const char			*name;
 	enum proto_bases		base;
 	unsigned int			protocol_key;
+	unsigned int			length;
 	struct {
 		unsigned int			num;
 		const struct proto_desc		*desc;
@@ -122,6 +124,7 @@ extern const struct proto_desc *proto_dev_desc(uint16_t type);
  * @family:	hook family
  * @location:	location of the relational expression defining the context
  * @desc:	protocol description for this layer
+ * @offset:	offset from the base, for stacked headers (eg 8*14 for vlan on top of ether)
  *
  * The location of the context is the location of the relational expression
  * defining it, either directly through a protocol match or indirectly
@@ -132,6 +135,7 @@ struct proto_ctx {
 	struct {
 		struct location			location;
 		const struct proto_desc		*desc;
+		unsigned int			offset;
 	} protocol[PROTO_BASE_MAX + 1];
 };
 
src/evaluate.c +77 -1

90 lines not on this page · raw message

src/netlink_delinearize.c +39 -9

79 lines not on this page · raw message

src/payload.c +1 -0
index 5f8c4fe..bd574ee 100644
@@ -340,6 +340,7 @@ void payload_expr_expand(struct list_head *list, struct expr *expr,
 		} else
 			break;
 	}
+
 raw:
 	new = payload_expr_alloc(&expr->location, NULL, 0);
 	payload_init_raw(new, expr->payload.base, expr->payload.offset,
src/proto.c +2 -0
index 6302016..27ab4a2 100644
@@ -713,6 +713,7 @@ const struct proto_desc proto_vlan = {
 	.name		= "vlan",
 	.base		= PROTO_BASE_LL_HDR,
 	.protocol_key	= VLANHDR_TYPE,
+	.length		= sizeof(struct vlan_hdr) * BITS_PER_BYTE,
 	.protocols	= {
 		PROTO_LINK(__constant_htons(ETH_P_IP),		&proto_ip),
 		PROTO_LINK(__constant_htons(ETH_P_ARP),		&proto_arp),
@@ -782,6 +783,7 @@ const struct proto_desc proto_eth = {
 	.name		= "ether",
 	.base		= PROTO_BASE_LL_HDR,
 	.protocol_key	= ETHHDR_TYPE,
+	.length		= sizeof(struct ether_header) * BITS_PER_BYTE,
 	.protocols	= {
 		PROTO_LINK(__constant_htons(ETH_P_IP),		&proto_ip),
 		PROTO_LINK(__constant_htons(ETH_P_ARP),		&proto_arp),
Signature · 1 line
2.0.5
author maintainer
#4 lore raw

PATCH 4/12 payload: disable payload merge if offsets are not on byte boundary

In reply to Florian Westphal

... because it doesn't work, we attempt to merge it into wrong place, we would have to merge the second value at a specific location.

F.e. vlan hdr 4094 gives us

0xfe0f

Merging in the CFI should yield 0xfe1f, but the constant merging doesn't know how to achive that; at the moment 'vlan id 4094' and 'vlan id 4094 vlan cfi 1' give same result -- 0xfe0f.

For now just turn off the optimization step unless everything is byte divisible (the common case).

---
 src/payload.c | 4 ++++
 1 file changed, 4 insertions(+)
src/payload.c +4 -0
index bd574ee..372dcec 100644
@@ -357,6 +357,10 @@ raw:
  */
 bool payload_is_adjacent(const struct expr *e1, const struct expr *e2)
 {
+	if (e1->payload.offset % BITS_PER_BYTE || e1->len % BITS_PER_BYTE ||
+	    e2->payload.offset % BITS_PER_BYTE || e2->len % BITS_PER_BYTE)
+		return false;
+
 	if (e1->payload.base		 == e2->payload.base &&
 	    e1->payload.offset + e1->len == e2->payload.offset)
 		return true;
Signature · 1 line
2.0.5
author maintainer
#5 lore raw

PATCH 5/12 src: netlink_linearize: handle sub-byte lengths

In reply to Florian Westphal

Currently length is expr->len / BITS_PER_BYTE, i.e. expr->len has to be a multiple of 8.

When core asks for e.g. '9 bits', we truncate this to 8. Round up to 16 and inject a 9-bit mask to zero out the parts we're not interested in.

This will also need change to the delinarization step to remove the extra op when dumping rules from kernel.

---
 src/netlink_linearize.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
src/netlink_linearize.c +44 -1
index bf1e56b..0467872 100644
@@ -99,6 +99,44 @@ static void netlink_gen_concat(struct netlink_linearize_ctx *ctx,
 	}
 }
 
+static void netlink_gen_payload_mask(struct netlink_linearize_ctx *ctx,
+				     const struct expr *expr,
+				     enum nft_registers dreg)
+{
+	struct nft_data_linearize nld, zero = {};
+	struct nft_rule_expr *nle;
+	unsigned int offset, len, masklen;
+	mpz_t mask;
+
+	offset = expr->payload.offset % BITS_PER_BYTE;
+	masklen = expr->len + offset;
+
+	if (masklen > 128)
+		BUG("expr mask length is %u (len %u, offset %u)\n",
+				masklen, expr->len, offset);
+
+	mpz_init2(mask, masklen);
+	mpz_bitmask(mask, expr->len);
+
+	if (offset)
+		mpz_lshift_ui(mask, offset);
+
+	nle = alloc_nft_expr("bitwise");
+
+	len = div_round_up(expr->len, BITS_PER_BYTE);
+
+	nft_rule_expr_set_u32(nle, NFT_EXPR_BITWISE_SREG, dreg);
+	nft_rule_expr_set_u32(nle, NFT_EXPR_BITWISE_DREG, dreg);
+	nft_rule_expr_set_u32(nle, NFT_EXPR_BITWISE_LEN, len);
+
+	netlink_gen_raw_data(mask, expr->byteorder, len, &nld);
+	nft_rule_expr_set(nle, NFT_EXPR_BITWISE_MASK, nld.value, nld.len);
+	nft_rule_expr_set(nle, NFT_EXPR_BITWISE_XOR, &zero.value, nld.len);
+
+	mpz_clear(mask);
+	nft_rule_add_expr(ctx->nlr, nle);
+}
+
 static void netlink_gen_payload(struct netlink_linearize_ctx *ctx,
 				const struct expr *expr,
 				enum nft_registers dreg)
@@ -111,9 +149,14 @@ static void netlink_gen_payload(struct netlink_linearize_ctx *ctx,
 			      expr->payload.base - 1);
 	nft_rule_expr_set_u32(nle, NFT_EXPR_PAYLOAD_OFFSET,
 			      expr->payload.offset / BITS_PER_BYTE);
+
 	nft_rule_expr_set_u32(nle, NFT_EXPR_PAYLOAD_LEN,
-			      expr->len / BITS_PER_BYTE);
+			      div_round_up(expr->len, BITS_PER_BYTE));
+
 	nft_rule_add_expr(ctx->nlr, nle);
+
+	if (expr->len % BITS_PER_BYTE)
+		netlink_gen_payload_mask(ctx, expr, dreg);
 }
 
 static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx,
Signature · 1 line
2.0.5
author maintainer
#6 lore raw

PATCH 6/12 src: netlink: don't truncate set key lengths

In reply to Florian Westphal

If key is e.g. 12 bits, pretend its 16 instead of 8. This is needed to make sets work with header fields with size not divisible by 8.

---
 src/netlink.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/netlink.c +2 -2
index 8ede8e6..b1296cf 100644
@@ -1100,7 +1100,7 @@ static int netlink_add_set_compat(struct netlink_ctx *ctx,
 	nft_set_attr_set_u32(nls, NFT_SET_ATTR_KEY_TYPE,
 			     dtype_map_to_kernel(set->keytype));
 	nft_set_attr_set_u32(nls, NFT_SET_ATTR_KEY_LEN,
-			     set->keylen / BITS_PER_BYTE);
+			     div_round_up(set->keylen, BITS_PER_BYTE));
 	if (set->flags & NFT_SET_MAP) {
 		nft_set_attr_set_u32(nls, NFT_SET_ATTR_DATA_TYPE,
 				     dtype_map_to_kernel(set->datatype));
@@ -1135,7 +1135,7 @@ static int netlink_add_set_batch(struct netlink_ctx *ctx,
 	nft_set_attr_set_u32(nls, NFT_SET_ATTR_KEY_TYPE,
 			     dtype_map_to_kernel(set->keytype));
 	nft_set_attr_set_u32(nls, NFT_SET_ATTR_KEY_LEN,
-			     set->keylen / BITS_PER_BYTE);
+			     div_round_up(set->keylen, BITS_PER_BYTE));
 	if (set->flags & NFT_SET_MAP) {
 		nft_set_attr_set_u32(nls, NFT_SET_ATTR_DATA_TYPE,
 				     dtype_map_to_kernel(set->datatype));
Signature · 1 line
2.0.5
author maintainer
#7 lore raw

PATCH 7/12 nft: fill in doff and fix ihl/version template entries

In reply to Florian Westphal

This allows to use

nft add rule ip filter input tcp doff 8

or similar.

Furhermore, ip version looked at hdrlen and vice versa.

---
 src/proto.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/proto.c +6 -4
index 27ab4a2..82cce05 100644
@@ -402,8 +402,10 @@ const struct proto_desc proto_tcp = {
 		[TCPHDR_DPORT]		= INET_SERVICE("dport", struct tcphdr, dest),
 		[TCPHDR_SEQ]		= TCPHDR_FIELD("sequence", seq),
 		[TCPHDR_ACKSEQ]		= TCPHDR_FIELD("ackseq", ack_seq),
-		[TCPHDR_DOFF]		= {},
-		[TCPHDR_RESERVED]	= {},
+		[TCPHDR_DOFF]		= HDR_BITFIELD("doff", &integer_type,
+						       (12 * BITS_PER_BYTE) + 4, 4),
+		[TCPHDR_RESERVED]	= HDR_BITFIELD("reserved", &integer_type,
+						       (12 * BITS_PER_BYTE) + 0, 4),
 		[TCPHDR_FLAGS]		= HDR_BITFIELD("flags", &tcp_flag_type,
 						       13 * BITS_PER_BYTE,
 						       BITS_PER_BYTE),
@@ -501,8 +503,8 @@ const struct proto_desc proto_ip = {
 		PROTO_LINK(IPPROTO_SCTP,	&proto_sctp),
 	},
 	.templates	= {
-		[IPHDR_VERSION]		= HDR_BITFIELD("version", &integer_type, 0, 4),
-		[IPHDR_HDRLENGTH]	= HDR_BITFIELD("hdrlength", &integer_type, 4, 4),
+		[IPHDR_VERSION]		= HDR_BITFIELD("version", &integer_type, 4, 4),
+		[IPHDR_HDRLENGTH]	= HDR_BITFIELD("hdrlength", &integer_type, 0, 4),
 		[IPHDR_TOS]		= IPHDR_FIELD("tos",		tos),
 		[IPHDR_LENGTH]		= IPHDR_FIELD("length",		tot_len),
 		[IPHDR_ID]		= IPHDR_FIELD("id",		id),
Signature · 1 line
2.0.5
author maintainer
#8 lore raw

PATCH 8/12 netlink: cmp: shift rhs constant if lhs offset doesn't start on byte boundary

In reply to Florian Westphal

if we have payload(someoffset) == 42, then shift 42 in case someoffset doesn't start on a byte boundary.

We already insert a mask instruction to only load those bits into the register that we were interested in, but the cmp will fail without also adjusting rhs accordingly.

Needs additional patch in reverse direction to undo the shift again when dumping ruleset.

---
 src/netlink_linearize.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/netlink_linearize.c +10 -0
index 0467872..044bac8 100644
@@ -278,6 +278,15 @@ static void netlink_gen_range(struct netlink_linearize_ctx *ctx,
 			      const struct expr *expr,
 			      enum nft_registers dreg);
 
+static void payload_shift_value(const struct expr *left, struct expr *right)
+{
+	if (right->ops->type != EXPR_VALUE ||
+	    left->ops->type != EXPR_PAYLOAD)
+		return;
+
+	mpz_lshift_ui(right->value, left->payload.offset % BITS_PER_BYTE);
+}
+
 static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
 			    const struct expr *expr,
 			    enum nft_registers dreg)
@@ -326,6 +335,7 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,
 	netlink_put_register(nle, NFT_EXPR_CMP_SREG, sreg);
 	nft_rule_expr_set_u32(nle, NFT_EXPR_CMP_OP,
 			      netlink_gen_cmp_op(expr->op));
+	payload_shift_value(expr->left, right);
 	netlink_gen_data(right, &nld);
 	nft_rule_expr_set(nle, NFT_EXPR_CMP_DATA, nld.value, nld.len);
 	release_register(ctx, expr->left);
Signature · 1 line
2.0.5
author maintainer
#9 lore raw

PATCH 9/12 tests: add tests for ip version/hdrlength/tcp doff

In reply to Florian Westphal

Header fields of 4 bit lengths. Requires implicit masks and shifting of RHS constant.

---
 tests/regression/inet/tcp.t              |  2 ++
 tests/regression/inet/tcp.t.payload.inet |  8 ++++++++
 tests/regression/inet/tcp.t.payload.ip   |  8 ++++++++
 tests/regression/inet/tcp.t.payload.ip6  |  8 ++++++++
 tests/regression/ip/ip.t                 |  4 ++++
 tests/regression/ip/ip.t.payload         | 21 +++++++++++++++++++++
 tests/regression/ip/ip.t.payload.inet    | 27 +++++++++++++++++++++++++++
 7 files changed, 78 insertions(+)
tests/regression/inet/tcp.t +2 -0
index 95267da..53a1689 100644
@@ -101,3 +101,5 @@ tcp urgptr { 33, 55, 67, 88};ok
 - tcp urgptr != { 33, 55, 67, 88};ok
 tcp urgptr { 33-55};ok
 - tcp urgptr != { 33-55};ok
+
+tcp doff 8;ok
tests/regression/inet/tcp.t.payload.inet +8 -0
index e1673db..21b21ab 100644
@@ -498,3 +498,11 @@ inet test-inet input
   [ payload load 2b @ transport header + 18 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
+# tcp doff 8
+inet test-inet input
+  [ meta load l4proto => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ payload load 1b @ transport header + 12 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000080 ]
+
tests/regression/inet/tcp.t.payload.ip +8 -0
index c21cca1..34c9714 100644
@@ -498,3 +498,11 @@ ip test-ip4 input
   [ payload load 2b @ transport header + 18 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
+# tcp doff 8
+ip test-ip4 input
+  [ payload load 1b @ network header + 9 => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ payload load 1b @ transport header + 12 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000080 ]
+
tests/regression/inet/tcp.t.payload.ip6 +8 -0
index 32115ed..44decab 100644
@@ -498,3 +498,11 @@ ip6 test-ip6 input
   [ payload load 2b @ transport header + 18 => reg 1 ]
   [ lookup reg 1 set set%d ]
 
+# tcp doff 8
+ip6 test-ip6 input
+  [ payload load 1b @ network header + 6 => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ payload load 1b @ transport header + 12 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000080 ]
+
tests/regression/ip/ip.t +4 -0
index 6e29ffb..0339c2a 100644
@@ -111,3 +111,7 @@ ip saddr \& 0.0.0.255 \< 0.0.0.127;ok;ip saddr & 0.0.0.255 < 0.0.0.127
 
 ip saddr \& 0xffff0000 == 0xffff0000;ok;ip saddr 255.255.0.0/16
 
+ip version 4 ip hdrlength 5;ok
+ip hdrlength 0;ok
+ip hdrlength 15;ok
+ip hdrlength 16;fail
tests/regression/ip/ip.t.payload +21 -0
index 7a77dc4..31a57a2 100644
@@ -353,3 +353,24 @@ ip test-ip4 input
   [ bitwise reg 1 = (reg=1 & 0x0000ffff ) ^ 0x00000000 ]
   [ cmp eq reg 1 0x0000ffff ]
 
+# ip version 4 ip hdrlength 5
+ip test-ip4 input
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000040 ]
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000005 ]
+
+# ip hdrlength 0
+ip test-ip4 input
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000000 ]
+
+# ip hdrlength 15
+ip test-ip4 input
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x0000000f ]
+
tests/regression/ip/ip.t.payload.inet +27 -0
index dbc7852..8c31349 100644
@@ -465,3 +465,30 @@ inet test-inet input
   [ bitwise reg 1 = (reg=1 & 0x0000ffff ) ^ 0x00000000 ]
   [ cmp eq reg 1 0x0000ffff ]
 
+# ip version 4 ip hdrlength 5
+inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x00000002 ]
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000040 ]
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000005 ]
+
+# ip hdrlength 0
+inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x00000002 ]
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x00000000 ]
+
+# ip hdrlength 15
+inet test-inet input
+  [ meta load nfproto => reg 1 ]
+  [ cmp eq reg 1 0x00000002 ]
+  [ payload load 1b @ network header + 0 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000000f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x0000000f ]
+
Signature · 1 line
2.0.5
author maintainer
#10 lore raw

PATCH 10/12 nft: support listing expressions that use non-byte header fields

In reply to Florian Westphal

This allows to list rules that check fields that are not aligned on byte boundary, and allows decoding vlan headers, which reside on top of eth header and need the expr->payload.offset adjusted accordingly.

---
 include/payload.h         |  3 ++
 src/evaluate.c            |  6 ++++
 src/netlink_delinearize.c | 80 ++++++++++++++++++++++++++++++++++++++------
 src/payload.c             | 84 +++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 160 insertions(+), 13 deletions(-)
include/payload.h +3 -0
index 95364af..ca9b013 100644
@@ -19,7 +19,10 @@ extern bool payload_is_adjacent(const struct expr *e1, const struct expr *e2);
 extern struct expr *payload_expr_join(const struct expr *e1,
 				      const struct expr *e2);
 
+bool payload_expr_trim(struct expr *expr, struct expr *mask,
+		       const struct proto_ctx *ctx);
 extern void payload_expr_expand(struct list_head *list, struct expr *expr,
+				struct expr *mask,
 				const struct proto_ctx *ctx);
 extern void payload_expr_complete(struct expr *expr,
 				  const struct proto_ctx *ctx);
src/evaluate.c +6 -0
index c95d154..7ff94c0 100644
@@ -332,8 +332,13 @@ static bool resolve_protocol_conflict(struct eval_ctx *ctx,
 		const struct proto_desc *next = ctx->pctx.protocol[base + 1].desc;
 
 		if (payload->payload.desc == next) {
+			ctx->pctx.protocol[base + 1].desc = NULL;
+			ctx->pctx.protocol[base].desc = next;
+			ctx->pctx.protocol[base].offset += desc->length;
 			payload->payload.offset += desc->length;
 			return true;
+		} else if (next) {
+			return false;
 		}
 	}
 
@@ -343,6 +348,7 @@ static bool resolve_protocol_conflict(struct eval_ctx *ctx,
 
 	payload->payload.offset += ctx->pctx.protocol[base].offset;
 	list_add_tail(&nstmt->list, &ctx->stmt->list);
+	ctx->pctx.protocol[base + 1].desc = NULL;
 
 	return true;
 }
src/netlink_delinearize.c +70 -10

130 lines not on this page · raw message

src/payload.c +81 -3

118 lines not on this page · raw message

Signature · 1 line
2.0.5
author maintainer
#11 lore raw

PATCH 11/12 tests: vlan tests

In reply to Florian Westphal

add a few regression tests that match vlan id/dei/pcp fields of the vlan header.

---
 tests/regression/bridge/vlan.t         |  33 ++++++
 tests/regression/bridge/vlan.t.payload | 190 +++++++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+)
 create mode 100644 tests/regression/bridge/vlan.t
 create mode 100644 tests/regression/bridge/vlan.t.payload
tests/regression/bridge/vlan.t new file +33 -0
new file mode 100644
index 0000000..e80ea50
@@ -0,0 +1,33 @@
+*bridge;test-bridge
+:input;type filter hook input priority 0
+
+vlan id 4094;ok
+vlan id 0;ok
+# bad vlan id
+vlan id 4096;fail
+vlan id 4094 vlan cfi 0;ok
+vlan id 4094 vlan cfi 1;ok
+# bad cfi
+vlan id 4094 vlan cfi 2;fail
+vlan id 4094 vlan cfi 1 vlan pcp 8;fail
+vlan id 4094 vlan cfi 1 vlan pcp 7;ok
+vlan id 4094 vlan cfi 1 vlan pcp 3;ok
+
+ether type vlan vlan id 4094;ok;vlan id 4094
+ether type vlan vlan id 0;ok;vlan id 0
+ether type vlan vlan id 4094 vlan cfi 0;ok;vlan id 4094 vlan cfi 0
+ether type vlan vlan id 4094 vlan cfi 1;ok;vlan id 4094 vlan cfi 1
+ether type vlan vlan id 4094 vlan cfi 2;fail
+
+vlan id 4094 tcp dport 22;ok
+vlan id 1 ip saddr 10.0.0.1;ok
+vlan id 1 ip saddr 10.0.0.0/23;ok
+vlan id 1 ip saddr 10.0.0.0/23 udp dport 53;ok
+ether type vlan vlan id 1 ip saddr 10.0.0.0/23 udp dport 53;ok;vlan id 1 ip saddr 10.0.0.0/23 udp dport 53
+
+vlan id { 1, 2, 4, 100, 4095 } vlan pcp 1-3;ok
+vlan id { 1, 2, 4, 100, 4096 };fail
+
+# illegal dependencies
+ether type ip vlan id 1;fail
+ether type ip vlan id 1 ip saddr 10.0.0.1;fail
tests/regression/bridge/vlan.t.payload new file +190 -0

190 lines not on this page · raw message

Signature · 1 line
2.0.5
author maintainer
#12 lore raw

PATCH 12/13 vlan: make != tests work

In reply to Florian Westphal
---
 src/netlink_delinearize.c              |  1 +
 tests/regression/bridge/vlan.t         |  1 +
 tests/regression/bridge/vlan.t.payload | 11 +++++++++++
 3 files changed, 13 insertions(+)
src/netlink_delinearize.c +1 -0
index 537e34c..2e3b090 100644
@@ -1084,6 +1084,7 @@ static void relational_binop_postprocess(struct rule_pp_ctx *ctx, struct expr *e
 	struct expr *binop = expr->left, *value = expr->right;
 
 	if (binop->op == OP_AND && expr->op == OP_NEQ &&
+	    value->dtype->basetype &&
 	    value->dtype->basetype->type == TYPE_BITMASK &&
 	    !mpz_cmp_ui(value->value, 0)) {
 		/* Flag comparison: data & flags != 0
tests/regression/bridge/vlan.t +1 -0
index e80ea50..f86561a 100644
@@ -6,6 +6,7 @@ vlan id 0;ok
 # bad vlan id
 vlan id 4096;fail
 vlan id 4094 vlan cfi 0;ok
+vlan id 4094 vlan cfi != 1;ok
 vlan id 4094 vlan cfi 1;ok
 # bad cfi
 vlan id 4094 vlan cfi 2;fail
tests/regression/bridge/vlan.t.payload +11 -0
index 4a5714a..35dc437 100644
@@ -25,6 +25,17 @@ bridge test-bridge input
   [ bitwise reg 1 = (reg=1 & 0x00000010 ) ^ 0x00000000 ]
   [ cmp eq reg 1 0x00000000 ]
 
+# vlan id 4094 vlan cfi != 1
+bridge test-bridge input
+  [ payload load 2b @ link header + 12 => reg 1 ]
+  [ cmp eq reg 1 0x00000081 ]
+  [ payload load 2b @ link header + 14 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x0000ff0f ) ^ 0x00000000 ]
+  [ cmp eq reg 1 0x0000fe0f ]
+  [ payload load 1b @ link header + 15 => reg 1 ]
+  [ bitwise reg 1 = (reg=1 & 0x00000010 ) ^ 0x00000000 ]
+  [ cmp neq reg 1 0x00000010 ]
+
 # vlan id 4094 vlan cfi 1
 bridge test-bridge input
   [ payload load 2b @ link header + 12 => reg 1 ]
Signature · 1 line
2.0.5
author maintainer
#14 lore raw
In reply to Florian Westphal
Quoted 7 lines · Florian Westphal

This adds the following features:
- stacked headers: vlan sits on top of ethernet, so when vlan id 42 is requested, add a dependency on eth and also adjust payload offsets by the size of ethernet header.

- allow matching header fields of lengths and/or offsets that are not byte divisible, e.g. vlan id, vlan pcp, ip hdrlength, etc.

pushed it to master.