![Patrick McHardy](/assets/img/avatar_default.png)
This patch adds the new netlink API for maintaining nf_tables sets independently of the ruleset. The API supports the following operations: - creation of sets - deletion of sets - querying of specific sets - dumping of all sets - addition of set elements - removal of set elements - dumping of all set elements Sets are identified by name, each table defines an individual namespace. The name of a set may be allocated automatically, this is mostly useful in combination with the NFT_SET_ANONYMOUS flag, which destroys a set automatically once the last reference has been released. Sets can be marked constant, meaning they're not allowed to change while linked to a rule. This allows to perform lockless operation for set types that would otherwise require locking. Additionally, if the implementation supports it, sets can (as before) be used as maps, associating a data value with each key (or range), by specifying the NFT_SET_MAP flag and can be used for interval queries by specifying the NFT_SET_INTERVAL flag. Set elements are added and removed incrementally. All element operations support batching, reducing netlink message and set lookup overhead. The old "set" and "hash" expressions are replaced by a generic "lookup" expression, which binds to the specified set. Userspace is not aware of the actual set implementation used by the kernel anymore, all configuration options are generic. Currently the implementation selection logic is largely missing and the kernel will simply use the first registered implementation supporting the requested operation. Eventually, the plan is to have userspace supply a description of the data characteristics and select the implementation based on expected performance and memory use. This patch includes the new 'lookup' expression to look up for element matching in the set. This patch includes kernel-doc descriptions for this set API and it also includes the following fixes. From Patrick McHardy: * netfilter: nf_tables: fix set element data type in dumps * netfilter: nf_tables: fix indentation of struct nft_set_elem comments * netfilter: nf_tables: fix oops in nft_validate_data_load() * netfilter: nf_tables: fix oops while listing sets of built-in tables * netfilter: nf_tables: destroy anonymous sets immediately if binding fails * netfilter: nf_tables: propagate context to set iter callback * netfilter: nf_tables: add loop detection From Pablo Neira Ayuso: * netfilter: nf_tables: allow to dump all existing sets * netfilter: nf_tables: fix wrong type for flags variable in newelem Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
125 lines
3.2 KiB
C
125 lines
3.2 KiB
C
/*
|
|
* Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* Development of this code funded by Astaro AG (http://www.astaro.com/)
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/netfilter/nf_tables.h>
|
|
#include <net/netfilter/nf_tables_core.h>
|
|
#include <net/netfilter/nf_tables.h>
|
|
|
|
struct nft_immediate_expr {
|
|
struct nft_data data;
|
|
enum nft_registers dreg:8;
|
|
u8 dlen;
|
|
};
|
|
|
|
static void nft_immediate_eval(const struct nft_expr *expr,
|
|
struct nft_data data[NFT_REG_MAX + 1],
|
|
const struct nft_pktinfo *pkt)
|
|
{
|
|
const struct nft_immediate_expr *priv = nft_expr_priv(expr);
|
|
|
|
nft_data_copy(&data[priv->dreg], &priv->data);
|
|
}
|
|
|
|
static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
|
|
[NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
|
|
[NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
|
|
};
|
|
|
|
static int nft_immediate_init(const struct nft_ctx *ctx,
|
|
const struct nft_expr *expr,
|
|
const struct nlattr * const tb[])
|
|
{
|
|
struct nft_immediate_expr *priv = nft_expr_priv(expr);
|
|
struct nft_data_desc desc;
|
|
int err;
|
|
|
|
if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
|
|
tb[NFTA_IMMEDIATE_DATA] == NULL)
|
|
return -EINVAL;
|
|
|
|
priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG]));
|
|
err = nft_validate_output_register(priv->dreg);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
|
|
if (err < 0)
|
|
return err;
|
|
priv->dlen = desc.len;
|
|
|
|
err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type);
|
|
if (err < 0)
|
|
goto err1;
|
|
|
|
return 0;
|
|
|
|
err1:
|
|
nft_data_uninit(&priv->data, desc.type);
|
|
return err;
|
|
}
|
|
|
|
static void nft_immediate_destroy(const struct nft_expr *expr)
|
|
{
|
|
const struct nft_immediate_expr *priv = nft_expr_priv(expr);
|
|
return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
|
|
}
|
|
|
|
static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
|
|
{
|
|
const struct nft_immediate_expr *priv = nft_expr_priv(expr);
|
|
|
|
if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg)))
|
|
goto nla_put_failure;
|
|
|
|
return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
|
|
nft_dreg_to_type(priv->dreg), priv->dlen);
|
|
|
|
nla_put_failure:
|
|
return -1;
|
|
}
|
|
|
|
static const struct nft_data *nft_immediate_get_verdict(const struct nft_expr *expr)
|
|
{
|
|
const struct nft_immediate_expr *priv = nft_expr_priv(expr);
|
|
|
|
if (priv->dreg == NFT_REG_VERDICT)
|
|
return &priv->data;
|
|
else
|
|
return NULL;
|
|
}
|
|
|
|
static struct nft_expr_ops nft_imm_ops __read_mostly = {
|
|
.name = "immediate",
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
|
|
.owner = THIS_MODULE,
|
|
.eval = nft_immediate_eval,
|
|
.init = nft_immediate_init,
|
|
.destroy = nft_immediate_destroy,
|
|
.dump = nft_immediate_dump,
|
|
.get_verdict = nft_immediate_get_verdict,
|
|
.policy = nft_immediate_policy,
|
|
.maxattr = NFTA_IMMEDIATE_MAX,
|
|
};
|
|
|
|
int __init nft_immediate_module_init(void)
|
|
{
|
|
return nft_register_expr(&nft_imm_ops);
|
|
}
|
|
|
|
void nft_immediate_module_exit(void)
|
|
{
|
|
nft_unregister_expr(&nft_imm_ops);
|
|
}
|