Skip to contentSkip to conversation

API minor fixes and improvement proposals

PATCH In review Tomasz Bursztyka 2 participants 10 messages Oct 14, 2013

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

cover API minor fixes and improvement proposals

Hi,

First 2 minor fixes:
-------------------

patch 1: don't know why I messed up here in my previous documentation patch, my mistake anyway. patch 2: an old left-over. Why has priv->len been corrupted before?

And 5 proposals:
---------------

patch 3: Let's expose table's usage (so user might decide if it's worth dumping table's content, it can useful info finally)

patch 4: That one is debatable: NFTA_CHAIN_USE is - imho - not enough self-explanatory.
NFTA_CHAIN_REFS (or NFTA_CHAIN_REFERENCES?) is much better. If there is a time to change such minor details, it's now or never.

patch 5: add a meta key to get dev->addr_len

patch 6: add lshift/rshift to bitwise expression. It's a first shot, which should work pretty well already. Maybe there are improvments which could be done, depending on data length (2 or 4: we could cast to u64 thus reducing shifts by 2).
At least I tried to keep the API sane, and c/p the byteorder way. sreg, dreg, op, len, mask are mandatory. xor and shift are related to the selected op.

patch 7: patch's commit message says all.

Tomasz Bursztyka (7):
netfilter: nf_tables: Fix API documentation on a missing part
netfilter: nf_tables: Use byteorder operands length relevantly
netfilter: nf_tables: Expose the table's chain usage to the netlink
API
netfilter: nf_tables: Use a semantically relevant name on chain's refs
netfilter: nf_tables: Add a meta key to get the device address length
netfilter: nf_tables: Add left and right shifts to bitwise expression
netfilter: nf_tables: Improve payload expression for an extra offset

 include/net/netfilter/nf_tables_core.h   |  1 +
 include/uapi/linux/netfilter/nf_tables.h | 42 +++++++++++++++++--
 net/netfilter/nf_tables_api.c            |  5 ++-
 net/netfilter/nf_tables_core.c           |  6 ++-
 net/netfilter/nft_bitwise.c              | 70 +++++++++++++++++++++++++++-----
 net/netfilter/nft_byteorder.c            |  2 -
 net/netfilter/nft_meta.c                 | 12 +++++-
 net/netfilter/nft_payload.c              | 25 +++++++++---
 8 files changed, 136 insertions(+), 27 deletions(-)
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 1/7 netfilter: nf_tables: Fix API documentation on a missing part

Add the documentation for NFTA_BYTEORDER_SIZE attribute which was left with a non relevant _TODO_.

---
 include/uapi/linux/netfilter/nf_tables.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +1 -1
index be8a062..b108548 100644
@@ -398,7 +398,7 @@ enum nft_byteorder_ops {
  * @NFTA_BYTEORDER_DREG: destination register (NLA_U32: nft_registers)
  * @NFTA_BYTEORDER_OP: operator (NLA_U32: enum nft_byteorder_ops)
  * @NFTA_BYTEORDER_LEN: length of the data (NLA_U32)
- * @NFTA_BYTEORDER_SIZE: _TODO_ (NLA_U32)
+ * @NFTA_BYTEORDER_SIZE: data size in bytes (NLA_U32: 2 or 4)
  */
 enum nft_byteorder_attributes {
 	NFTA_BYTEORDER_UNSPEC,
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH 2/7 netfilter: nf_tables: Use byteorder operands length relevantly

In reply to Tomasz Bursztyka
---
 net/netfilter/nft_byteorder.c | 2 --
 1 file changed, 2 deletions(-)
net/netfilter/nft_byteorder.c +0 -2
index d213b98..c39ed8d 100644
@@ -110,8 +110,6 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
 	}
 
 	priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
-	// FIXME: something is corrupting the attribute value
-	priv->len = 4;
 	if (priv->len == 0 || priv->len > FIELD_SIZEOF(struct nft_data, data))
 		return -EINVAL;
 
Signature · 1 line
1.8.3.2
#3 lore raw

PATCH 3/7 netfilter: nf_tables: Expose the table's chain usage to the netlink API

In reply to Tomasz Bursztyka

User space can therefore know whether a table is in use or not, and by how many chains.

Suggested by Pablo Neira Ayuso.

---
 include/uapi/linux/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c            | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index b108548..28f0ffb 100644
@@ -110,11 +110,13 @@ enum nft_table_flags {
  *
  * @NFTA_TABLE_NAME: name of the table (NLA_STRING)
  * @NFTA_TABLE_FLAGS: bitmask of enum nft_table_flags (NLA_U32)
+ * @NFTA_TABLE_CHAINS: number of chains in this table (NLA_U32)
  */
 enum nft_table_attributes {
 	NFTA_TABLE_UNSPEC,
 	NFTA_TABLE_NAME,
 	NFTA_TABLE_FLAGS,
+	NFTA_TABLE_CHAINS,
 	__NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX		(__NFTA_TABLE_MAX - 1)
net/netfilter/nf_tables_api.c +2 -1
index dcddc49..482b689 100644
@@ -180,7 +180,8 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
 	nfmsg->res_id		= 0;
 
 	if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
-	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)))
+	    nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
+	    nla_put_be32(skb, NFTA_TABLE_CHAINS, htonl(table->use)))
 		goto nla_put_failure;
 
 	return nlmsg_end(skb, nlh);
Signature · 1 line
1.8.3.2
#4 lore raw

PATCH 4/7 netfilter: nf_tables: Use a semantically relevant name on chain's refs

In reply to Tomasz Bursztyka

NFTA_CHAIN_USE is less semantically self-explaining than NFTA_CHAIN_REFS.

---
 include/uapi/linux/netfilter/nf_tables.h | 4 ++--
 net/netfilter/nf_tables_api.c            | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +2 -2
index 28f0ffb..81eec3b 100644
@@ -129,7 +129,7 @@ enum nft_table_attributes {
  * @NFTA_CHAIN_NAME: name of the chain (NLA_STRING)
  * @NFTA_CHAIN_HOOK: hook specification for basechains (NLA_NESTED: nft_hook_attributes)
  * @NFTA_CHAIN_POLICY: numeric policy of the chain (NLA_U32)
- * @NFTA_CHAIN_USE: number of references to this chain (NLA_U32)
+ * @NFTA_CHAIN_REFS: number of references to this chain (NLA_U32)
  * @NFTA_CHAIN_TYPE: type name of the string (NLA_NUL_STRING)
  * @NFTA_CHAIN_COUNTERS: counter specification of the chain (NLA_NESTED: nft_counter_attributes)
  */
@@ -140,7 +140,7 @@ enum nft_chain_attributes {
 	NFTA_CHAIN_NAME,
 	NFTA_CHAIN_HOOK,
 	NFTA_CHAIN_POLICY,
-	NFTA_CHAIN_USE,
+	NFTA_CHAIN_REFS,
 	NFTA_CHAIN_TYPE,
 	NFTA_CHAIN_COUNTERS,
 	__NFTA_CHAIN_MAX
net/netfilter/nf_tables_api.c +1 -1
index 482b689..951c264 100644
@@ -614,7 +614,7 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
 			goto nla_put_failure;
 	}
 
-	if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
+	if (nla_put_be32(skb, NFTA_CHAIN_REFS, htonl(chain->use)))
 		goto nla_put_failure;
 
 	return nlmsg_end(skb, nlh);
Signature · 1 line
1.8.3.2
#5 lore raw

PATCH 5/7 netfilter: nf_tables: Add a meta key to get the device address length

In reply to Tomasz Bursztyka

This will be useful to determine at runtime the position of some data in the packet.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nft_meta.c                 | 12 +++++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 81eec3b..65f41ff 100644
@@ -526,6 +526,7 @@ enum nft_exthdr_attributes {
  * @NFT_META_OIFNAME: packet output interface name (dev->name)
  * @NFT_META_IIFTYPE: packet input interface type (dev->type)
  * @NFT_META_OIFTYPE: packet output interface type (dev->type)
+ * @NFT_META_DEV_ADDRLEN: packet interface address length (dev->addr_len)
  * @NFT_META_SKUID: originating socket UID (fsuid)
  * @NFT_META_SKGID: originating socket GID (fsgid)
  * @NFT_META_NFTRACE: packet nftrace bit
@@ -543,6 +544,7 @@ enum nft_meta_keys {
 	NFT_META_OIFNAME,
 	NFT_META_IIFTYPE,
 	NFT_META_OIFTYPE,
+	NFT_META_DEV_ADDRLEN,
 	NFT_META_SKUID,
 	NFT_META_SKGID,
 	NFT_META_NFTRACE,
net/netfilter/nft_meta.c +11 -1
index 8c28220..8116535 100644
@@ -30,7 +30,7 @@ static void nft_meta_eval(const struct nft_expr *expr,
 {
 	const struct nft_meta *priv = nft_expr_priv(expr);
 	const struct sk_buff *skb = pkt->skb;
-	const struct net_device *in = pkt->in, *out = pkt->out;
+	const struct net_device *in = pkt->in, *out = pkt->out, *dev;
 	struct nft_data *dest = &data[priv->dreg];
 
 	switch (priv->key) {
@@ -76,6 +76,15 @@ static void nft_meta_eval(const struct nft_expr *expr,
 			goto err;
 		*(u16 *)dest->data = out->type;
 		break;
+	case NFT_META_DEV_ADDRLEN:
+		if (out != NULL)
+			dev = out;
+		else if (in != NULL)
+			dev = out;
+		else
+			goto err;
+		*(u8 *)dest->data = dev->addr_len;
+		break;
 	case NFT_META_SKUID:
 		if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT)
 			goto err;
@@ -159,6 +168,7 @@ static int nft_meta_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
 	case NFT_META_OIFNAME:
 	case NFT_META_IIFTYPE:
 	case NFT_META_OIFTYPE:
+	case NFT_META_DEV_ADDRLEN:
 	case NFT_META_SKUID:
 	case NFT_META_SKGID:
 #ifdef CONFIG_NET_CLS_ROUTE
Signature · 1 line
1.8.3.2
#6 lore raw

PATCH 6/7 netfilter: nf_tables: Add left and right shifts to bitwise expression

In reply to Tomasz Bursztyka

This add left and right shift operators. Thus it is possible to make logical shifts to nft_data.

A multiplication by 2 can be easily ordered this way and more.

---
 include/uapi/linux/netfilter/nf_tables.h | 30 +++++++++++++-
 net/netfilter/nft_bitwise.c              | 70 +++++++++++++++++++++++++++-----
 2 files changed, 88 insertions(+), 12 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +29 -1
index 65f41ff..8b12b8f 100644
@@ -351,16 +351,32 @@ enum nft_immediate_attributes {
 #define NFTA_IMMEDIATE_MAX	(__NFTA_IMMEDIATE_MAX - 1)
 
 /**
+ * enum nft_bitwise_ops - nf_tables bitwise operators
+ *
+ * @NFT_BITWISE_XOR: xor operator
+ * @NFT_BITWISE_LSHIFT: left shift operator
+ * @NFT_BITWISE_RSHIFT: right shift operator
+ */
+enum nft_bitwise_ops {
+	NFT_BITWISE_XOR,
+	NFT_BITWISE_LSHIFT,
+	NFT_BITWISE_RSHIFT,
+};
+
+/**
  * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes
  *
  * @NFTA_BITWISE_SREG: source register (NLA_U32: nft_registers)
  * @NFTA_BITWISE_DREG: destination register (NLA_U32: nft_registers)
+ * @NFTA_BITWISE_OP: operator (NLA_U32: enum nft_bitwise_ops)
  * @NFTA_BITWISE_LEN: length of operands (NLA_U32)
  * @NFTA_BITWISE_MASK: mask value (NLA_NESTED: nft_data_attributes)
  * @NFTA_BITWISE_XOR: xor value (NLA_NESTED: nft_data_attributes)
+ * @NFTA_BITWISE_SHIFT: shift length (NLA_U32)
  *
- * The bitwise expression performs the following operation:
+ * The bitwise expression performs the 3 following operations:
  *
+ * XOR:
  * dreg = (sreg & mask) ^ xor
  *
  * which allow to express all bitwise operations:
@@ -370,14 +386,26 @@ enum nft_immediate_attributes {
  * OR:		0	x
  * XOR:		1	x
  * AND:		x	0
+ *
+ * Then, LSHIFT/RSHIFT.
+ * For instance on LSHIFT:
+ * dreg = sreg << shift | (rest ? rest : 0)
+ *
+ * where rest (internal) is calculated on previous sreg:
+ * rest = (sreg & mask) >> (sizeof_u32 - shift)
+ *
+ * where provided mask is:
+ * ~(UINT32_MAX >> shift)
  */
 enum nft_bitwise_attributes {
 	NFTA_BITWISE_UNSPEC,
 	NFTA_BITWISE_SREG,
 	NFTA_BITWISE_DREG,
+	NFTA_BITWISE_OP,
 	NFTA_BITWISE_LEN,
 	NFTA_BITWISE_MASK,
 	NFTA_BITWISE_XOR,
+	NFTA_BITWISE_SHIFT,
 	__NFTA_BITWISE_MAX
 };
 #define NFTA_BITWISE_MAX	(__NFTA_BITWISE_MAX - 1)
net/netfilter/nft_bitwise.c +59 -11

111 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#7 lore raw

PATCH 7/7 netfilter: nf_tables: Improve payload expression for an extra offset

In reply to Tomasz Bursztyka

Payload expression gets a new attribute: NFTA_PAYLOAD_SREG_EXTRA This attribute will permit to provide a source register where an extra offset will be added to the fixed offset of the expression.

This will be useful where some information cannot be found at a pre-determined place. Such as the protocol address of an ARP-ETH packet:
its position is determined by the hardware address length.

Thus, in this example, a meta expression NFT_META_DEV_ADDRLEN will load the value of the hardware length and put it into a register: such register, as a NFTA_PAYLOAD_SREG_EXTRA in following payload expression, will be used to add the hardware length to the fixed offset.

Suggested by Pablo Neira Ayuso.

---
 include/net/netfilter/nf_tables_core.h   |  1 +
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_core.c           |  6 +++++-
 net/netfilter/nft_payload.c              | 25 +++++++++++++++++++------
 4 files changed, 27 insertions(+), 7 deletions(-)
include/net/netfilter/nf_tables_core.h +1 -0
index fe7b162..5962528 100644
@@ -31,6 +31,7 @@ struct nft_payload {
 	enum nft_payload_bases	base:8;
 	u8			offset;
 	u8			len;
+	enum nft_registers      sreg:8;
 	enum nft_registers	dreg:8;
 };
 
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 8b12b8f..a8d8aa6 100644
@@ -511,6 +511,7 @@ enum nft_payload_bases {
  * @NFTA_PAYLOAD_DREG: destination register to load data into (NLA_U32: nft_registers)
  * @NFTA_PAYLOAD_BASE: payload base (NLA_U32: nft_payload_bases)
  * @NFTA_PAYLOAD_OFFSET: payload offset relative to base (NLA_U32)
+ * @NFTA_PAYLOAD_SREG_EXTRA: source register of an extra offset (NLA_U32: nft_registers)
  * @NFTA_PAYLOAD_LEN: payload length (NLA_U32)
  */
 enum nft_payload_attributes {
@@ -518,6 +519,7 @@ enum nft_payload_attributes {
 	NFTA_PAYLOAD_DREG,
 	NFTA_PAYLOAD_BASE,
 	NFTA_PAYLOAD_OFFSET,
+	NFTA_PAYLOAD_SREG_EXTRA,
 	NFTA_PAYLOAD_LEN,
 	__NFTA_PAYLOAD_MAX
 };
net/netfilter/nf_tables_core.c +5 -1
index 2324dd8..e9200e7 100644
@@ -40,6 +40,7 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,
 	const struct nft_payload *priv = nft_expr_priv(expr);
 	const struct sk_buff *skb = pkt->skb;
 	struct nft_data *dest = &data[priv->dreg];
+	int extra_offset = 0;
 	unsigned char *ptr;
 
 	if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
@@ -47,7 +48,10 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,
 	else
 		ptr = skb_network_header(skb) + pkt->xt.thoff;
 
-	ptr += priv->offset;
+	if (priv->sreg)
+		extra_offset = (int) *data[priv->sreg].data;
+
+	ptr += extra_offset + priv->offset;
 
 	if (unlikely(ptr + priv->len >= skb_tail_pointer(skb)))
 		return false;
net/netfilter/nft_payload.c +19 -6
index a2aeb31..41d3f21 100644
@@ -24,7 +24,7 @@ static void nft_payload_eval(const struct nft_expr *expr,
 	const struct nft_payload *priv = nft_expr_priv(expr);
 	const struct sk_buff *skb = pkt->skb;
 	struct nft_data *dest = &data[priv->dreg];
-	int offset;
+	int offset, extra_offset = 0;
 
 	switch (priv->base) {
 	case NFT_PAYLOAD_LL_HEADER:
@@ -41,7 +41,11 @@ static void nft_payload_eval(const struct nft_expr *expr,
 	default:
 		BUG();
 	}
-	offset += priv->offset;
+
+	if (priv->sreg)
+		extra_offset = (int) *data[priv->sreg].data;
+
+	offset += extra_offset + priv->offset;
 
 	if (skb_copy_bits(skb, offset, dest->data, priv->len) < 0)
 		goto err;
@@ -51,10 +55,11 @@ err:
 }
 
 static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
-	[NFTA_PAYLOAD_DREG]	= { .type = NLA_U32 },
-	[NFTA_PAYLOAD_BASE]	= { .type = NLA_U32 },
-	[NFTA_PAYLOAD_OFFSET]	= { .type = NLA_U32 },
-	[NFTA_PAYLOAD_LEN]	= { .type = NLA_U32 },
+	[NFTA_PAYLOAD_DREG]		= { .type = NLA_U32 },
+	[NFTA_PAYLOAD_BASE]		= { .type = NLA_U32 },
+	[NFTA_PAYLOAD_OFFSET]		= { .type = NLA_U32 },
+	[NFTA_PAYLOAD_SREG_EXTRA]	= { .type = NLA_U32 },
+	[NFTA_PAYLOAD_LEN]		= { .type = NLA_U32 },
 };
 
 static int nft_payload_init(const struct nft_ctx *ctx,
@@ -68,6 +73,14 @@ static int nft_payload_init(const struct nft_ctx *ctx,
 	priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
 	priv->len    = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
 
+	if (tb[NFTA_PAYLOAD_SREG_EXTRA]) {
+		priv->sreg = ntohl(nla_get_be32(
+					tb[NFTA_PAYLOAD_SREG_EXTRA]));
+		err = nft_validate_input_register(priv->sreg);
+		if (err < 0)
+			return err;
+	}
+
 	priv->dreg = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_DREG]));
 	err = nft_validate_output_register(priv->dreg);
 	if (err < 0)
Signature · 1 line
1.8.3.2
#8 lore raw
In reply to Tomasz Bursztyka
Quoted 7 lines · Tomasz Bursztyka

Hi,

First 2 minor fixes:
-------------------

patch 1: don't know why I messed up here in my previous documentation patch, my mistake anyway. patch 2: an old left-over. Why has priv->len been corrupted before?

Will pick these two.

Quoted 4 lines

And 5 proposals:
---------------

patch 3: Let's expose table's usage (so user might decide if it's worth dumping table's content, it can useful info finally)

Call it _USE.

Quoted 2 lines

patch 4: That one is debatable: NFTA_CHAIN_USE is - imho - not enough self-explanatory.
NFTA_CHAIN_REFS (or NFTA_CHAIN_REFERENCES?) is much better. If there is a time to change such minor details, it's now or never.

We're using _USE in other parts of the netfilter code, that can be document, really I don't think it's worth to discuss this renaming thing.

Quoted 6 lines

patch 5: add a meta key to get dev->addr_len

patch 6: add lshift/rshift to bitwise expression. It's a first shot, which should work pretty well already. Maybe there are improvments which could be done, depending on data length (2 or 4: we could cast to u64 thus reducing shifts by 2).
At least I tried to keep the API sane, and c/p the byteorder way. sreg, dreg, op, len, mask are mandatory. xor and shift are related to the selected op.

patch 7: patch's commit message says all.

Code for user-space is missing, won't take these until finished.

#9 lore raw

Hi Pablo,

Quoted 1 line

Code for user-space is missing, won't take these until finished.

Sure, that's why I put them as "proposal" so it could be quickly reviewed, at least for the API part. (patch 6 and 7 mostly)

Tomasz