Skip to contentSkip to conversation

Attributes for concatenated ranges

PATCH Applied Stefano Brivio 2 participants 5 messages Jan 30, 2020 – Feb 5, 2020 netfilter

lore
Share & tools
lore
#0 lore raw

v4 cover Attributes for concatenated ranges

This series adds support for NFTA_SET_DESC_CONCAT set attribute and the additional key passed as NFTA_SET_ELEM_KEY_END to denote the upper bound of a range in a generic way, as suggested by Pablo.

v4: Sync nf_tables.h header, move NFTNL_SET_DESC_CONCAT to avoid
ABI breakage (Pablo Neira Ayuso)

v3: Support for separate "end" key added as 2/2, reworked 1/2 to
use set description data for length of concatenation fields

Stefano Brivio (3):
include: resync nf_tables.h cache copy
set: Add support for NFTA_SET_DESC_CONCAT attributes
set_elem: Introduce support for NFTNL_SET_ELEM_KEY_END

 include/libnftnl/set.h              |   2 +
 include/linux/netfilter/nf_tables.h |  17 +++++
 include/set.h                       |   2 +
 include/set_elem.h                  |   1 +
 src/set.c                           | 111 +++++++++++++++++++++++-----
 src/set_elem.c                      |  24 ++++++
 6 files changed, 138 insertions(+), 19 deletions(-)
Signature · 1 line
2.24.1
#1 lore raw

PATCH v4 1/3 include: resync nf_tables.h cache copy

Get this header in sync with nf-next as of merge commit b3a608222336 (5.6-rc1-ish).

---
v4: New patch

 include/linux/netfilter/nf_tables.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
include/linux/netfilter/nf_tables.h +17 -0
index 261864736b26..065218a20bb7 100644
@@ -48,6 +48,7 @@ enum nft_registers {
 
 #define NFT_REG_SIZE	16
 #define NFT_REG32_SIZE	4
+#define NFT_REG32_COUNT	(NFT_REG32_15 - NFT_REG32_00 + 1)
 
 /**
  * enum nft_verdicts - nf_tables internal verdicts
@@ -301,14 +302,28 @@ enum nft_set_policies {
  * enum nft_set_desc_attributes - set element description
  *
  * @NFTA_SET_DESC_SIZE: number of elements in set (NLA_U32)
+ * @NFTA_SET_DESC_CONCAT: description of field concatenation (NLA_NESTED)
  */
 enum nft_set_desc_attributes {
 	NFTA_SET_DESC_UNSPEC,
 	NFTA_SET_DESC_SIZE,
+	NFTA_SET_DESC_CONCAT,
 	__NFTA_SET_DESC_MAX
 };
 #define NFTA_SET_DESC_MAX	(__NFTA_SET_DESC_MAX - 1)
 
+/**
+ * enum nft_set_field_attributes - attributes of concatenated fields
+ *
+ * @NFTA_SET_FIELD_LEN: length of single field, in bits (NLA_U32)
+ */
+enum nft_set_field_attributes {
+	NFTA_SET_FIELD_UNSPEC,
+	NFTA_SET_FIELD_LEN,
+	__NFTA_SET_FIELD_MAX
+};
+#define NFTA_SET_FIELD_MAX	(__NFTA_SET_FIELD_MAX - 1)
+
 /**
  * enum nft_set_attributes - nf_tables set netlink attributes
  *
@@ -370,6 +385,7 @@ enum nft_set_elem_flags {
  * @NFTA_SET_ELEM_USERDATA: user data (NLA_BINARY)
  * @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes)
  * @NFTA_SET_ELEM_OBJREF: stateful object reference (NLA_STRING)
+ * @NFTA_SET_ELEM_KEY_END: closing key value (NLA_NESTED: nft_data)
  */
 enum nft_set_elem_attributes {
 	NFTA_SET_ELEM_UNSPEC,
@@ -382,6 +398,7 @@ enum nft_set_elem_attributes {
 	NFTA_SET_ELEM_EXPR,
 	NFTA_SET_ELEM_PAD,
 	NFTA_SET_ELEM_OBJREF,
+	NFTA_SET_ELEM_KEY_END,
 	__NFTA_SET_ELEM_MAX
 };
 #define NFTA_SET_ELEM_MAX	(__NFTA_SET_ELEM_MAX - 1)
Signature · 1 line
2.24.1
#2 lore raw

PATCH v4 2/3 set: Add support for NFTA_SET_DESC_CONCAT attributes

In reply to Stefano Brivio

If NFTNL_SET_DESC_CONCAT data is passed, pass that to the kernel as NFTA_SET_DESC_CONCAT attributes: it describes the length of single concatenated fields, in bytes.

Similarly, parse NFTA_SET_DESC_CONCAT attributes if received from the kernel.

This is the libnftnl counterpart for nftables patch:
src: Add support for NFTNL_SET_DESC_CONCAT

v4:
- move NFTNL_SET_DESC_CONCAT to the end of enum nftnl_set_attr to avoid ABI breakage (Pablo Neira Ayuso)
v3:
- use NFTNL_SET_DESC_CONCAT and NFTA_SET_DESC_CONCAT instead of a stand-alone NFTA_SET_SUBKEY attribute (Pablo Neira Ayuso)
- pass field length in bytes instead of bits, fields would get unnecessarily big otherwise
v2:
- fixed grammar in commit message
- removed copy of array bytes in nftnl_set_nlmsg_build_subkey_payload(), we're simply passing values to htonl() (Phil Sutter)

---
 include/libnftnl/set.h |   1 +
 include/set.h          |   2 +
 src/set.c              | 111 ++++++++++++++++++++++++++++++++++-------
 3 files changed, 95 insertions(+), 19 deletions(-)
include/libnftnl/set.h +1 -0
index db3fa686d60a..bbbf58ded99a 100644
@@ -29,6 +29,7 @@ enum nftnl_set_attr {
 	NFTNL_SET_USERDATA,
 	NFTNL_SET_OBJ_TYPE,
 	NFTNL_SET_HANDLE,
+	NFTNL_SET_DESC_CONCAT,
 	__NFTNL_SET_MAX
 };
 #define NFTNL_SET_MAX (__NFTNL_SET_MAX - 1)
include/set.h +2 -0
index 446acd24ca7c..895ffdb48bdb 100644
@@ -25,6 +25,8 @@ struct nftnl_set {
 	enum nft_set_policies	policy;
 	struct {
 		uint32_t	size;
+		uint8_t		field_len[NFT_REG32_COUNT];
+		uint8_t		field_count;
 	} desc;
 	struct list_head	element_list;
 
src/set.c +92 -19

169 lines not on this page · raw message

Signature · 1 line
2.24.1
#3 lore raw

PATCH v4 3/3 set_elem: Introduce support for NFTNL_SET_ELEM_KEY_END

In reply to Stefano Brivio

The new set element attribute maps to the netlink attribute NFTA_SET_ELEM_KEY_END in the same way as NFTNL_SET_ELEM_KEY maps to NFTA_SET_ELEM_KEY, and represents the key data used to express the upper bound of a range, in concatenations.

---
v4: No changes
v3: New patch

 include/libnftnl/set.h |  1 +
 include/set_elem.h     |  1 +
 src/set_elem.c         | 24 ++++++++++++++++++++++++
 3 files changed, 26 insertions(+)
include/libnftnl/set.h +1 -0
index bbbf58ded99a..6843adfa0c1e 100644
@@ -105,6 +105,7 @@ enum {
 	NFTNL_SET_ELEM_USERDATA,
 	NFTNL_SET_ELEM_EXPR,
 	NFTNL_SET_ELEM_OBJREF,
+	NFTNL_SET_ELEM_KEY_END,
 	__NFTNL_SET_ELEM_MAX
 };
 #define NFTNL_SET_ELEM_MAX (__NFTNL_SET_ELEM_MAX - 1)
include/set_elem.h +1 -0
index cc4d52943272..52f185aa11be 100644
@@ -8,6 +8,7 @@ struct nftnl_set_elem {
 	uint32_t		set_elem_flags;
 	uint32_t		flags;
 	union nftnl_data_reg	key;
+	union nftnl_data_reg	key_end;
 	union nftnl_data_reg	data;
 	struct nftnl_expr	*expr;
 	uint64_t		timeout;
src/set_elem.c +24 -0
index d3ce807d838c..22031938ebbc 100644
@@ -75,6 +75,7 @@ void nftnl_set_elem_unset(struct nftnl_set_elem *s, uint16_t attr)
 		break;
 	case NFTNL_SET_ELEM_FLAGS:
 	case NFTNL_SET_ELEM_KEY:	/* NFTA_SET_ELEM_KEY */
+	case NFTNL_SET_ELEM_KEY_END:	/* NFTA_SET_ELEM_KEY_END */
 	case NFTNL_SET_ELEM_VERDICT:	/* NFTA_SET_ELEM_DATA */
 	case NFTNL_SET_ELEM_DATA:	/* NFTA_SET_ELEM_DATA */
 	case NFTNL_SET_ELEM_TIMEOUT:	/* NFTA_SET_ELEM_TIMEOUT */
@@ -118,6 +119,10 @@ int nftnl_set_elem_set(struct nftnl_set_elem *s, uint16_t attr,
 		memcpy(&s->key.val, data, data_len);
 		s->key.len = data_len;
 		break;
+	case NFTNL_SET_ELEM_KEY_END:	/* NFTA_SET_ELEM_KEY_END */
+		memcpy(&s->key_end.val, data, data_len);
+		s->key_end.len = data_len;
+		break;
 	case NFTNL_SET_ELEM_VERDICT:	/* NFTA_SET_ELEM_DATA */
 		memcpy(&s->data.verdict, data, sizeof(s->data.verdict));
 		break;
@@ -193,6 +198,9 @@ const void *nftnl_set_elem_get(struct nftnl_set_elem *s, uint16_t attr, uint32_t
 	case NFTNL_SET_ELEM_KEY:	/* NFTA_SET_ELEM_KEY */
 		*data_len = s->key.len;
 		return &s->key.val;
+	case NFTNL_SET_ELEM_KEY_END:	/* NFTA_SET_ELEM_KEY_END */
+		*data_len = s->key_end.len;
+		return &s->key_end.val;
 	case NFTNL_SET_ELEM_VERDICT:	/* NFTA_SET_ELEM_DATA */
 		*data_len = sizeof(s->data.verdict);
 		return &s->data.verdict;
@@ -287,6 +295,14 @@ void nftnl_set_elem_nlmsg_build_payload(struct nlmsghdr *nlh,
 		mnl_attr_put(nlh, NFTA_DATA_VALUE, e->key.len, e->key.val);
 		mnl_attr_nest_end(nlh, nest1);
 	}
+	if (e->flags & (1 << NFTNL_SET_ELEM_KEY_END)) {
+		struct nlattr *nest1;
+
+		nest1 = mnl_attr_nest_start(nlh, NFTA_SET_ELEM_KEY_END);
+		mnl_attr_put(nlh, NFTA_DATA_VALUE, e->key_end.len,
+			     e->key_end.val);
+		mnl_attr_nest_end(nlh, nest1);
+	}
 	if (e->flags & (1 << NFTNL_SET_ELEM_VERDICT)) {
 		struct nlattr *nest1, *nest2;
 
@@ -373,6 +389,7 @@ static int nftnl_set_elem_parse_attr_cb(const struct nlattr *attr, void *data)
 			abi_breakage();
 		break;
 	case NFTA_SET_ELEM_KEY:
+	case NFTA_SET_ELEM_KEY_END:
 	case NFTA_SET_ELEM_DATA:
 	case NFTA_SET_ELEM_EXPR:
 		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0)
@@ -421,6 +438,13 @@ static int nftnl_set_elems_parse2(struct nftnl_set *s, const struct nlattr *nest
 			goto out_set_elem;
 		e->flags |= (1 << NFTNL_SET_ELEM_KEY);
         }
+	if (tb[NFTA_SET_ELEM_KEY_END]) {
+		ret = nftnl_parse_data(&e->key_end, tb[NFTA_SET_ELEM_KEY_END],
+				       &type);
+		if (ret < 0)
+			goto out_set_elem;
+		e->flags |= (1 << NFTNL_SET_ELEM_KEY_END);
+	}
         if (tb[NFTA_SET_ELEM_DATA]) {
 		ret = nftnl_parse_data(&e->data, tb[NFTA_SET_ELEM_DATA], &type);
 		if (ret < 0)
Signature · 1 line
2.24.1
maintainer Applied
#4 lore raw
In reply to Stefano Brivio
Quoted 3 lines · Stefano Brivio

This series adds support for NFTA_SET_DESC_CONCAT set attribute and the additional key passed as NFTA_SET_ELEM_KEY_END to denote the upper bound of a range in a generic way, as suggested by Pablo.

Applied, thanks.