netfilter: nf_tables: fix nft_meta_target module
Subsystems (2)
RFC PATCH 1/1 netfilter: nf_tables: fix nft_meta_target module
This patch adds kernel support for the meta_target expression.
This expression indicates that a given packet has to be set with a property, currently one of mark, priority, nftrace or secmark.
Most of the previous code was invalid, with lots of compilation errors and warnings.
In case of nftrace, the value is always 1. Such a behaviour is copied from net/netfilter/xt_TRACE.c
In case of secmark, the intention is to make the translation between the security_ctx and security_id outside this module, maybe in userspace. Otherwise, a string is needed to be passed from the userpsace to kernel as part of the attribute set, breaking the main KEY,VALUE pair approach. This is different from net/netfilter/xt_SECMARK.c. There, the context is translated in kernel side.
--- include/uapi/linux/netfilter/nf_tables.h | 30 ++++++++ net/netfilter/Kconfig | 4 + net/netfilter/Makefile | 2 - net/netfilter/nft_meta_target.c | 114 +++++++++++++++++------------- 4 files changed, 100 insertions(+), 50 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +30 -0
@@ -563,6 +563,36 @@ enum nft_meta_attributes {#define NFTA_META_MAX (__NFTA_META_MAX - 1)/** +* enum nft_meta_target_keys - nf_tables meta_target expression keys +* +* @NFT_META_TARGET_MARK: to stablish packet mark (skb->mark) +* @NFT_META_TARGET_PRIORITY: to fix packet priority (skb->priority) +* @NFT_META_TARGET_NFTRACE: to put the packet nftrace bit (skb->nf_trace) +* @NFT_META_TARGET_SECMARK: to set packet secmark (skb->secmark) +*/ +enum nft_meta_target_keys { +NFT_META_TARGET_MARK , +NFT_META_TARGET_PRIORITY , +NFT_META_TARGET_NFTRACE , +NFT_META_TARGET_SECMARK , +}; + +/** +* enum nft_meta_target_attributes - nf_tables meta_target expression netlink +* attributes +* +* @NFTA_META_TARGET_KEY: meta_target item to be set (NLA_U32: nft_meta_target_keys) +* @NFTA_META_TARGET_VALUE: value for the key (NLA_U32) +*/ +enum nft_meta_target_attributes { +NFTA_META_TARGET_UNSPEC , +NFTA_META_TARGET_KEY , +NFTA_META_TARGET_VALUE , + __NFTA_META_TARGET_MAX +}; +#define NFTA_META_TARGET_MAX (__NFTA_META_TARGET_MAX - 1) + +/** * enum nft_ct_keys - nf_tables ct expression keys * * @NFT_CT_STATE: conntrack state (bitmask of enum ip_conntrack_info)
net/netfilter/Kconfig +4 -0
@@ -425,6 +425,10 @@ config NFT_META depends on NF_TABLES tristate "Netfilter nf_tables meta module" +config NFT_META_TARGET + depends on NF_TABLES + tristate "Netfilter nf_tables meta target module" + config NFT_CT depends on NF_TABLES depends on NF_CONNTRACK
net/netfilter/Makefile +1 -1
@@ -76,7 +76,7 @@ obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o -#nf_tables-objs += nft_meta_target.o +obj-$(CONFIG_NFT_META_TARGET) += nft_meta_target.o obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o obj-$(CONFIG_NFT_HASH) += nft_hash.o obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
net/netfilter/nft_meta_target.c +65 -49
171 lines not on this page · raw message
Hi Arturo,
Minor stuff:
Quoted 1 line
+ * @NFT_META_TARGET_MARK: to stablish packet mark (
skb->mark)
to 'e'stablish
Why not reusing existing NFT_META_* keys?
It would just raise an error if not priority/mark/nftrace/secmark, as it does currently. Worth to keep that as it is imho, no need to duplicate.
Besides that, any other name that would be more relevant than meta_target? Target is already a critical in-use keyword in netfilter, so it's not clear enough. All expression have a short, one-word based name, which is nice.
Anyway, doesn't it work already: if you create an immediate expression (to load the value you want, at default dreg 0 aka NFT_REG_VERDICT) and a meta expression without the NFTA_META_DREG set? (didn't try myself)
If not maybe there is a shorter way to fix this, instead of creating a full new expression. Looks like it was the original plan.
Tomasz
Quoted 6 lines · Tomasz Bursztyka
Why not reusing existing NFT_META_* keys?
It would just raise an error if not priority/mark/nftrace/secmark, as it does currently.
Worth to keep that as it is imho, no need to duplicate.
That is a good approach, indeed.
My motivations for this was to give some additional flexibility, so both keys sets don't depend on each other.
One may think you can simply modify other meta elements, like iifname, etc..
Quoted 1 line
All expression have a short, one-word based name, which is nice.
Yes, any suggestion?
Quoted 5 lines
Anyway, doesn't it work already: if you create an immediate expression (to load the value you want, at default dreg 0 aka
NFT_REG_VERDICT) and a meta expression without theNFTA_META_DREGset? (didn't try myself)
I commented the immediate load with Pablo. He suggested the value to be fetched from the metatarget directly.
Quoted 3 lines
If not maybe there is a shorter way to fix this, instead of creating a full new expression. Looks like it was the original plan.
You mean the original plan was to avoid creating a new expression? In that case, I will have to revisit this, as I wasn't considering this approach.
Thanks Tomasz for your comments :-)
Regards.
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Arturo,
Quoted 2 lines
Quoted 1 line
All expression have a short, one-word based name, which is nice.
Yes, any suggestion?
Always the tricky part ^^ (complaining about is easy...). But we might avoid that:
Quoted 6 lines
Quoted 4 lines
Anyway, doesn't it work already: if you create an immediate expression (to load the value you want, at default dreg 0 aka
NFT_REG_VERDICT) and a meta expression without theNFTA_META_DREGset? (didn't try myself)I commented the immediate load with Pablo. He suggested the value to be fetched from the metatarget directly.
Ok I see it optimize things, then below:
Quoted 6 lines
Quoted 3 lines
If not maybe there is a shorter way to fix this, instead of creating a full new expression. Looks like it was the original plan.
You mean the original plan was to avoid creating a new expression? In that case, I will have to revisit this, as I wasn't considering this approach.
Thus, why not adding an attribute to the meta expression. Like: NFTA_META_SVAL? (Of course NFTA_META_DREG and this new attribute are mutually exclusive) I guess then you only need to adapt struct nft_policy in nft_meta_target.c to handle it, and adding value element in its nft_meta struct, and there you are.
Less code and API change.
Tomasz
Quoted 10 lines · Tomasz Bursztyka
Thus, why not adding an attribute to the meta expression. Like:
NFTA_META_SVAL? (Of courseNFTA_META_DREGand this new attribute are mutually exclusive)
I guess then you only need to adapt structnft_policyinnft_meta_target.cto handle it, and adding value element in itsnft_metastruct, and there you are.Less code and API change.
Sounds reasonable. Then we can kill the nft_meta_targe.c file.
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi Arturo,
Quoted 1 line
Sounds reasonable. Then we can kill the
nft_meta_targe.cfile.
Actually no, just a small update on it (+nf_tables.h) and that's it. :)
Tomasz
Pablo Neira Ayuso
maintainerHi Arturo,
Thanks for working on recovering the meta_target code.
Quoted 28 lines · Tomasz Bursztyka
Hi Arturo,
Quoted 2 lines
Quoted 1 line
All expression have a short, one-word based name, which is nice.
Yes, any suggestion?
Always the tricky part ^^ (complaining about is easy...). But we might avoid that:
Quoted 6 lines
Quoted 4 lines
Anyway, doesn't it work already: if you create an immediate expression (to load the value you want, at default dreg 0 aka
NFT_REG_VERDICT) and a meta expression without theNFTA_META_DREGset? (didn't try myself)I commented the immediate load with Pablo. He suggested the value to be fetched from the metatarget directly.
Ok I see it optimize things, then below:
Quoted 6 lines
Quoted 3 lines
If not maybe there is a shorter way to fix this, instead of creating a full new expression. Looks like it was the original plan.
You mean the original plan was to avoid creating a new expression? In that case, I will have to revisit this, as I wasn't considering this approach.
Thus, why not adding an attribute to the meta expression. Like:
NFTA_META_SVAL? (Of courseNFTA_META_DREGand this new attribute are mutually exclusive)
Quoted 5 lines
I guess then you only need to adapt struct
nft_policyinnft_meta_target.cto handle it, and adding value element in itsnft_metastruct, and there you are.Less code and API change.
I think we have to merge the meta_target into meta, so we provide one single "meta" instruction that allows us to load and store data in skbuff meta fields. We would also save having two kernel modules.
We have the select_ops API that allows us to have different "flavours" of our instructions depending on the attributes that we get. At the time the meta_target was written, there was no select_ops API.
static const struct nft_expr_ops *
nft_meta_select_ops(const struct nft_ctx *ctx,
const struct nlattr * const tb[])
{
struct nft_xt *nft_match;
struct xt_match *match;
char *mt_name;
__u32 rev, family;
if (tb[NFTA_META_SREG] && tb[NFTA_META_VALUE])
return ERR_PTR(-EINVAL);
if (tb[NFTA_META_SREG])
return &nft_meta_match_ops;
if (tb[NFTA_META_VALUE])
return &nft_meta_target_ops;
return ERR_PTR(-EINVAL);
}Then:
static struct nft_expr_type nft_meta_type; static const struct nft_expr_ops nft_meta_match_ops = {
.type = &nft_meta_type,
.size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
.eval = nft_meta_match_eval,
.init = nft_meta_init,
.dump = nft_meta_dump,};
static const struct nft_expr_ops nft_meta_target_ops = {
.type = &nft_meta_type,
.size = NFT_EXPR_SIZE(sizeof(struct nft_meta_target)),
.eval = nft_meta_target_eval,
.init = nft_meta_init,
.dump = nft_meta_dump,};
_init and _dump can be the same I guess.
@Arturo: Please, check nft_payload.c on how to use the select_ops API and let me know if you have any doubt. I can give you a hand on it.
Hi Pablo,
Quoted 1 line
We would also save having two kernel modules.
Good point indeed.
Tomasz
Quoted 3 lines · Pablo Neira Ayuso
@Arturo: Please, check
nft_payload.con how to use theselect_opsAPI and let me know if you have any doubt. I can give you a hand on it.
Thanks, quite useful info.
I will be back with this as soon as I can.
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html