Blame SOURCES/0007-set-Add-support-for-NFTA_SET_DESC_CONCAT-attributes.patch

763a55
From db234c0c95ac45b5ff8602d8686d2bb1bbaa7883 Mon Sep 17 00:00:00 2001
763a55
From: Phil Sutter <psutter@redhat.com>
763a55
Date: Thu, 13 Feb 2020 17:47:45 +0100
763a55
Subject: [PATCH] set: Add support for NFTA_SET_DESC_CONCAT attributes
763a55
763a55
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1795223
763a55
Upstream Status: libnftnl commit 7cd41b5387acf
763a55
763a55
commit 7cd41b5387acf84088e9299a796f7c2c72339a1a
763a55
Author: Stefano Brivio <sbrivio@redhat.com>
763a55
Date:   Thu Jan 30 01:16:33 2020 +0100
763a55
763a55
    set: Add support for NFTA_SET_DESC_CONCAT attributes
763a55
763a55
    If NFTNL_SET_DESC_CONCAT data is passed, pass that to the kernel
763a55
    as NFTA_SET_DESC_CONCAT attributes: it describes the length of
763a55
    single concatenated fields, in bytes.
763a55
763a55
    Similarly, parse NFTA_SET_DESC_CONCAT attributes if received
763a55
    from the kernel.
763a55
763a55
    This is the libnftnl counterpart for nftables patch:
763a55
      src: Add support for NFTNL_SET_DESC_CONCAT
763a55
763a55
    v4:
763a55
     - move NFTNL_SET_DESC_CONCAT to the end of enum nftnl_set_attr
763a55
       to avoid ABI breakage (Pablo Neira Ayuso)
763a55
    v3:
763a55
     - use NFTNL_SET_DESC_CONCAT and NFTA_SET_DESC_CONCAT instead of a
763a55
       stand-alone NFTA_SET_SUBKEY attribute (Pablo Neira Ayuso)
763a55
     - pass field length in bytes instead of bits, fields would get
763a55
       unnecessarily big otherwise
763a55
    v2:
763a55
     - fixed grammar in commit message
763a55
     - removed copy of array bytes in nftnl_set_nlmsg_build_subkey_payload(),
763a55
       we're simply passing values to htonl() (Phil Sutter)
763a55
763a55
    Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
763a55
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
763a55
---
763a55
 include/libnftnl/set.h |   1 +
763a55
 include/set.h          |   2 +
763a55
 src/set.c              | 111 ++++++++++++++++++++++++++++++++++++++++---------
763a55
 3 files changed, 95 insertions(+), 19 deletions(-)
763a55
763a55
diff --git a/include/libnftnl/set.h b/include/libnftnl/set.h
763a55
index db3fa68..bbbf58d 100644
763a55
--- a/include/libnftnl/set.h
763a55
+++ b/include/libnftnl/set.h
763a55
@@ -29,6 +29,7 @@ enum nftnl_set_attr {
763a55
 	NFTNL_SET_USERDATA,
763a55
 	NFTNL_SET_OBJ_TYPE,
763a55
 	NFTNL_SET_HANDLE,
763a55
+	NFTNL_SET_DESC_CONCAT,
763a55
 	__NFTNL_SET_MAX
763a55
 };
763a55
 #define NFTNL_SET_MAX (__NFTNL_SET_MAX - 1)
763a55
diff --git a/include/set.h b/include/set.h
763a55
index 446acd2..895ffdb 100644
763a55
--- a/include/set.h
763a55
+++ b/include/set.h
763a55
@@ -25,6 +25,8 @@ struct nftnl_set {
763a55
 	enum nft_set_policies	policy;
763a55
 	struct {
763a55
 		uint32_t	size;
763a55
+		uint8_t		field_len[NFT_REG32_COUNT];
763a55
+		uint8_t		field_count;
763a55
 	} desc;
763a55
 	struct list_head	element_list;
763a55
 
763a55
diff --git a/src/set.c b/src/set.c
763a55
index 78447c6..651dcfa 100644
763a55
--- a/src/set.c
763a55
+++ b/src/set.c
763a55
@@ -89,6 +89,7 @@ void nftnl_set_unset(struct nftnl_set *s, uint16_t attr)
763a55
 	case NFTNL_SET_ID:
763a55
 	case NFTNL_SET_POLICY:
763a55
 	case NFTNL_SET_DESC_SIZE:
763a55
+	case NFTNL_SET_DESC_CONCAT:
763a55
 	case NFTNL_SET_TIMEOUT:
763a55
 	case NFTNL_SET_GC_INTERVAL:
763a55
 		break;
763a55
@@ -174,6 +175,10 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
763a55
 	case NFTNL_SET_DESC_SIZE:
763a55
 		memcpy(&s->desc.size, data, sizeof(s->desc.size));
763a55
 		break;
763a55
+	case NFTNL_SET_DESC_CONCAT:
763a55
+		memcpy(&s->desc.field_len, data, data_len);
763a55
+		while (s->desc.field_len[++s->desc.field_count]);
763a55
+		break;
763a55
 	case NFTNL_SET_TIMEOUT:
763a55
 		memcpy(&s->timeout, data, sizeof(s->timeout));
763a55
 		break;
763a55
@@ -266,6 +271,9 @@ const void *nftnl_set_get_data(const struct nftnl_set *s, uint16_t attr,
763a55
 	case NFTNL_SET_DESC_SIZE:
763a55
 		*data_len = sizeof(uint32_t);
763a55
 		return &s->desc.size;
763a55
+	case NFTNL_SET_DESC_CONCAT:
763a55
+		*data_len = s->desc.field_count;
763a55
+		return s->desc.field_len;
763a55
 	case NFTNL_SET_TIMEOUT:
763a55
 		*data_len = sizeof(uint64_t);
763a55
 		return &s->timeout;
763a55
@@ -351,13 +359,42 @@ err:
763a55
 	return NULL;
763a55
 }
763a55
 
763a55
+static void nftnl_set_nlmsg_build_desc_size_payload(struct nlmsghdr *nlh,
763a55
+						    struct nftnl_set *s)
763a55
+{
763a55
+	mnl_attr_put_u32(nlh, NFTA_SET_DESC_SIZE, htonl(s->desc.size));
763a55
+}
763a55
+
763a55
+static void nftnl_set_nlmsg_build_desc_concat_payload(struct nlmsghdr *nlh,
763a55
+						      struct nftnl_set *s)
763a55
+{
763a55
+	struct nlattr *nest;
763a55
+	int i;
763a55
+
763a55
+	nest = mnl_attr_nest_start(nlh, NFTA_SET_DESC_CONCAT);
763a55
+	for (i = 0; i < NFT_REG32_COUNT && i < s->desc.field_count; i++) {
763a55
+		struct nlattr *nest_elem;
763a55
+
763a55
+		nest_elem = mnl_attr_nest_start(nlh, NFTA_LIST_ELEM);
763a55
+		mnl_attr_put_u32(nlh, NFTA_SET_FIELD_LEN,
763a55
+				 htonl(s->desc.field_len[i]));
763a55
+		mnl_attr_nest_end(nlh, nest_elem);
763a55
+	}
763a55
+	mnl_attr_nest_end(nlh, nest);
763a55
+}
763a55
+
763a55
 static void
763a55
 nftnl_set_nlmsg_build_desc_payload(struct nlmsghdr *nlh, struct nftnl_set *s)
763a55
 {
763a55
 	struct nlattr *nest;
763a55
 
763a55
 	nest = mnl_attr_nest_start(nlh, NFTA_SET_DESC);
763a55
-	mnl_attr_put_u32(nlh, NFTA_SET_DESC_SIZE, htonl(s->desc.size));
763a55
+
763a55
+	if (s->flags & (1 << NFTNL_SET_DESC_SIZE))
763a55
+		nftnl_set_nlmsg_build_desc_size_payload(nlh, s);
763a55
+	if (s->flags & (1 << NFTNL_SET_DESC_CONCAT))
763a55
+		nftnl_set_nlmsg_build_desc_concat_payload(nlh, s);
763a55
+
763a55
 	mnl_attr_nest_end(nlh, nest);
763a55
 }
763a55
 
763a55
@@ -387,7 +424,7 @@ void nftnl_set_nlmsg_build_payload(struct nlmsghdr *nlh, struct nftnl_set *s)
763a55
 		mnl_attr_put_u32(nlh, NFTA_SET_ID, htonl(s->id));
763a55
 	if (s->flags & (1 << NFTNL_SET_POLICY))
763a55
 		mnl_attr_put_u32(nlh, NFTA_SET_POLICY, htonl(s->policy));
763a55
-	if (s->flags & (1 << NFTNL_SET_DESC_SIZE))
763a55
+	if (s->flags & (1 << NFTNL_SET_DESC_SIZE | 1 << NFTNL_SET_DESC_CONCAT))
763a55
 		nftnl_set_nlmsg_build_desc_payload(nlh, s);
763a55
 	if (s->flags & (1 << NFTNL_SET_TIMEOUT))
763a55
 		mnl_attr_put_u64(nlh, NFTA_SET_TIMEOUT, htobe64(s->timeout));
763a55
@@ -445,39 +482,75 @@ static int nftnl_set_parse_attr_cb(const struct nlattr *attr, void *data)
763a55
 	return MNL_CB_OK;
763a55
 }
763a55
 
763a55
-static int nftnl_set_desc_parse_attr_cb(const struct nlattr *attr, void *data)
763a55
+static int
763a55
+nftnl_set_desc_concat_field_parse_attr_cb(const struct nlattr *attr, void *data)
763a55
+{
763a55
+	int type = mnl_attr_get_type(attr);
763a55
+	struct nftnl_set *s = data;
763a55
+
763a55
+	if (type != NFTA_SET_FIELD_LEN)
763a55
+		return MNL_CB_OK;
763a55
+
763a55
+	if (mnl_attr_validate(attr, MNL_TYPE_U32))
763a55
+		return MNL_CB_ERROR;
763a55
+
763a55
+	s->desc.field_len[s->desc.field_count] = ntohl(mnl_attr_get_u32(attr));
763a55
+	s->desc.field_count++;
763a55
+
763a55
+	return MNL_CB_OK;
763a55
+}
763a55
+
763a55
+static int
763a55
+nftnl_set_desc_concat_parse_attr_cb(const struct nlattr *attr, void *data)
763a55
 {
763a55
-	const struct nlattr **tb = data;
763a55
 	int type = mnl_attr_get_type(attr);
763a55
+	struct nftnl_set *s = data;
763a55
+
763a55
+	if (type != NFTA_LIST_ELEM)
763a55
+		return MNL_CB_OK;
763a55
+
763a55
+	return mnl_attr_parse_nested(attr,
763a55
+				     nftnl_set_desc_concat_field_parse_attr_cb,
763a55
+				     s);
763a55
+}
763a55
+
763a55
+static int nftnl_set_desc_parse_attr_cb(const struct nlattr *attr, void *data)
763a55
+{
763a55
+	int type = mnl_attr_get_type(attr), err;
763a55
+	struct nftnl_set *s = data;
763a55
 
763a55
 	if (mnl_attr_type_valid(attr, NFTA_SET_DESC_MAX) < 0)
763a55
 		return MNL_CB_OK;
763a55
 
763a55
 	switch (type) {
763a55
 	case NFTA_SET_DESC_SIZE:
763a55
-		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
763a55
+		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
763a55
 			abi_breakage();
763a55
+			break;
763a55
+		}
763a55
+
763a55
+		s->desc.size = ntohl(mnl_attr_get_u32(attr));
763a55
+		s->flags |= (1 << NFTNL_SET_DESC_SIZE);
763a55
+		break;
763a55
+	case NFTA_SET_DESC_CONCAT:
763a55
+		err = mnl_attr_parse_nested(attr,
763a55
+					    nftnl_set_desc_concat_parse_attr_cb,
763a55
+					    s);
763a55
+		if (err != MNL_CB_OK)
763a55
+			abi_breakage();
763a55
+
763a55
+		s->flags |= (1 << NFTNL_SET_DESC_CONCAT);
763a55
+		break;
763a55
+	default:
763a55
 		break;
763a55
 	}
763a55
 
763a55
-	tb[type] = attr;
763a55
 	return MNL_CB_OK;
763a55
 }
763a55
 
763a55
-static int nftnl_set_desc_parse(struct nftnl_set *s,
763a55
-			      const struct nlattr *attr)
763a55
+static int nftnl_set_desc_parse(struct nftnl_set *s, const struct nlattr *attr)
763a55
 {
763a55
-	struct nlattr *tb[NFTA_SET_DESC_MAX + 1] = {};
763a55
-
763a55
-	if (mnl_attr_parse_nested(attr, nftnl_set_desc_parse_attr_cb, tb) < 0)
763a55
-		return -1;
763a55
-
763a55
-	if (tb[NFTA_SET_DESC_SIZE]) {
763a55
-		s->desc.size = ntohl(mnl_attr_get_u32(tb[NFTA_SET_DESC_SIZE]));
763a55
-		s->flags |= (1 << NFTNL_SET_DESC_SIZE);
763a55
-	}
763a55
-
763a55
-	return 0;
763a55
+	return mnl_attr_parse_nested(attr, nftnl_set_desc_parse_attr_cb, s);
763a55
 }
763a55
 
763a55
 EXPORT_SYMBOL(nftnl_set_nlmsg_parse);
763a55
-- 
763a55
1.8.3.1
763a55