]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
expr: add map lookups for hash statements
authorLaura Garcia Liebana <nevola@gmail.com>
Thu, 10 May 2018 22:15:41 +0000 (00:15 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 23 May 2018 07:39:32 +0000 (09:39 +0200)
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 <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/libnftnl/expr.h
include/linux/netfilter/nf_tables.h
src/expr/hash.c

index 25d41037d086f1010d2cbc614a42e8ed9e1b5c36..45ff5334f6a0277a5cb902cec4c2ee0e4376fb85 100644 (file)
@@ -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 {
index 54e35c129eea6d2893b5473888f984a1b427529d..48b095eef96218ede2e55fb71aaed10108c76b13 100644 (file)
@@ -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)
index fcc4fa5c051611306a95e078f6f72e997a73037e..415537ea721b130f2905811dafa470661832dfda 100644 (file)
@@ -28,6 +28,10 @@ struct nftnl_expr_hash {
        unsigned int            modulus;
        unsigned int            seed;
        unsigned int            offset;
+       struct {
+               const char      *name;
+               uint32_t        id;
+       } 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 &hash->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 &hash->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,
                SNPRINTF_BUFFER_SIZE(ret, remain, offset);
        }
 
+       if (hash->map.id) {
+               ret = snprintf(buf + offset, remain, "set %s id %u ",
+                              hash->map.name, hash->map.id);
+               SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+       }
+
        return offset;
 }
 
@@ -280,6 +323,8 @@ static int nftnl_expr_hash_export(char *buf, size_t size,
                nftnl_buf_u32(&b, type, hash->offset, OFFSET);
        if (e->flags & (1 << NFTNL_EXPR_HASH_TYPE))
                nftnl_buf_u32(&b, type, hash->type, TYPE);
+       if (e->flags & (1 << NFTNL_EXPR_HASH_SET_NAME))
+               nftnl_buf_str(&b, type, hash->map.name, SET);
 
        return nftnl_buf_done(&b);
 }
@@ -321,6 +366,10 @@ static bool nftnl_expr_hash_cmp(const struct nftnl_expr *e1,
                eq &= (h1->offset == h2->offset);
        if (e1->flags & (1 << NFTNL_EXPR_HASH_TYPE))
                eq &= (h1->type == h2->type);
+       if (e1->flags & (1 << NFTNL_EXPR_HASH_SET_NAME))
+               eq &= !strcmp(h1->map.name, h2->map.name);
+       if (e1->flags & (1 << NFTNL_EXPR_HASH_SET_ID))
+               eq &= (h1->map.id == h2->map.id);
 
        return eq;
 }