Re: [PATCH libnftnl] expr: add map lookups for hash statements

2018-05-23 Thread Pablo Neira Ayuso
On Fri, May 11, 2018 at 12:15:41AM +0200, Laura Garcia Liebana wrote:
> This patch introduces two new attributes for hash expression
> to allow map lookups where the hash is the key.
> 
> The new attributes are NFTNL_EXPR_HASH_SET_NAME and
> NFTNL_EXPR_HASH_SET_ID in order to identify the given map.

Applied, thanks Laura.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH libnftnl] expr: add map lookups for hash statements

2018-05-10 Thread Laura Garcia Liebana
This patch introduces two new attributes for hash expression
to allow map lookups where the hash is the key.

The new attributes are NFTNL_EXPR_HASH_SET_NAME and
NFTNL_EXPR_HASH_SET_ID in order to identify the given map.

Signed-off-by: Laura Garcia Liebana 
---
 include/libnftnl/expr.h |  2 ++
 include/linux/netfilter/nf_tables.h |  4 +++
 src/expr/hash.c | 49 +
 3 files changed, 55 insertions(+)

diff --git a/include/libnftnl/expr.h b/include/libnftnl/expr.h
index 25d4103..45ff533 100644
--- a/include/libnftnl/expr.h
+++ b/include/libnftnl/expr.h
@@ -238,6 +238,8 @@ enum {
NFTNL_EXPR_HASH_SEED,
NFTNL_EXPR_HASH_OFFSET,
NFTNL_EXPR_HASH_TYPE,
+   NFTNL_EXPR_HASH_SET_NAME,
+   NFTNL_EXPR_HASH_SET_ID,
 };
 
 enum {
diff --git a/include/linux/netfilter/nf_tables.h 
b/include/linux/netfilter/nf_tables.h
index 54e35c1..48b095e 100644
--- a/include/linux/netfilter/nf_tables.h
+++ b/include/linux/netfilter/nf_tables.h
@@ -851,6 +851,8 @@ enum nft_hash_types {
  * @NFTA_HASH_SEED: seed value (NLA_U32)
  * @NFTA_HASH_OFFSET: add this offset value to hash result (NLA_U32)
  * @NFTA_HASH_TYPE: hash operation (NLA_U32: nft_hash_types)
+ * @NFTA_HASH_SET_NAME: name of the map to lookup (NLA_STRING)
+ * @NFTA_HASH_SET_ID: id of the map (NLA_U32)
  */
 enum nft_hash_attributes {
NFTA_HASH_UNSPEC,
@@ -861,6 +863,8 @@ enum nft_hash_attributes {
NFTA_HASH_SEED,
NFTA_HASH_OFFSET,
NFTA_HASH_TYPE,
+   NFTA_HASH_SET_NAME,
+   NFTA_HASH_SET_ID,
__NFTA_HASH_MAX,
 };
 #define NFTA_HASH_MAX  (__NFTA_HASH_MAX - 1)
diff --git a/src/expr/hash.c b/src/expr/hash.c
index fcc4fa5..415537e 100644
--- a/src/expr/hash.c
+++ b/src/expr/hash.c
@@ -28,6 +28,10 @@ struct nftnl_expr_hash {
unsigned intmodulus;
unsigned intseed;
unsigned intoffset;
+   struct {
+   const char  *name;
+   uint32_tid;
+   } map;
 };
 
 static int
@@ -57,6 +61,14 @@ nftnl_expr_hash_set(struct nftnl_expr *e, uint16_t type,
case NFTNL_EXPR_HASH_TYPE:
hash->type = *((uint32_t *)data);
break;
+   case NFTNL_EXPR_HASH_SET_NAME:
+   hash->map.name = strdup(data);
+   if (!hash->map.name)
+   return -1;
+   break;
+   case NFTNL_EXPR_HASH_SET_ID:
+   hash->map.id = *((uint32_t *)data);
+   break;
default:
return -1;
}
@@ -91,6 +103,12 @@ nftnl_expr_hash_get(const struct nftnl_expr *e, uint16_t 
type,
case NFTNL_EXPR_HASH_TYPE:
*data_len = sizeof(hash->type);
return >type;
+   case NFTNL_EXPR_HASH_SET_NAME:
+   *data_len = strlen(hash->map.name) + 1;
+   return hash->map.name;
+   case NFTNL_EXPR_HASH_SET_ID:
+   *data_len = sizeof(hash->map.id);
+   return >map.id;
}
return NULL;
 }
@@ -111,9 +129,14 @@ static int nftnl_expr_hash_cb(const struct nlattr *attr, 
void *data)
case NFTA_HASH_SEED:
case NFTA_HASH_OFFSET:
case NFTA_HASH_TYPE:
+   case NFTA_HASH_SET_ID:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
abi_breakage();
break;
+   case NFTA_HASH_SET_NAME:
+   if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
+   abi_breakage();
+   break;
}
 
tb[type] = attr;
@@ -139,6 +162,10 @@ nftnl_expr_hash_build(struct nlmsghdr *nlh, const struct 
nftnl_expr *e)
mnl_attr_put_u32(nlh, NFTA_HASH_OFFSET, htonl(hash->offset));
if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE))
mnl_attr_put_u32(nlh, NFTA_HASH_TYPE, htonl(hash->type));
+   if (e->flags & (1 << NFTNL_EXPR_HASH_SET_NAME))
+   mnl_attr_put_str(nlh, NFTA_HASH_SET_NAME, hash->map.name);
+   if (e->flags & (1 << NFTNL_EXPR_HASH_SET_ID))
+   mnl_attr_put_u32(nlh, NFTA_HASH_SET_ID, htonl(hash->map.id));
 }
 
 static int
@@ -179,6 +206,16 @@ nftnl_expr_hash_parse(struct nftnl_expr *e, struct nlattr 
*attr)
hash->type = ntohl(mnl_attr_get_u32(tb[NFTA_HASH_TYPE]));
e->flags |= (1 << NFTNL_EXPR_HASH_TYPE);
}
+   if (tb[NFTA_HASH_SET_NAME]) {
+   hash->map.name =
+ strdup(mnl_attr_get_str(tb[NFTA_HASH_SET_NAME]));
+   e->flags |= (1 << NFTNL_EXPR_HASH_SET_NAME);
+   }
+   if (tb[NFTA_HASH_SET_ID]) {
+   hash->map.id =
+ ntohl(mnl_attr_get_u32(tb[NFTA_HASH_SET_ID]));
+   e->flags |= (1 << NFTNL_EXPR_HASH_SET_ID);
+   }
 
return ret;
 }
@@ -256,6 +293,12 @@ nftnl_expr_hash_snprintf_default(char *buf, size_t size,