Skip to content

Commit a4878ee

Browse files
author
Florian Westphal
committed
netfilter: nf_tables: relax set/map validation checks
Its currently not allowed to perform queries on a map, for example: table t { map m { typeof ip saddr : meta mark .. chain c { ip saddr @m counter will fail, because kernel requires that userspace provides a destination register when the referenced set is a map. However, internally there is no real distinction between sets and maps, maps are just sets where each key is associated with a value. Relax this so that maps can be used just like sets. This allows to have rules that query if a given key exists without making use of the associated value. This also permits != checks which don't work for map lookups. When no destination reg is given for a map, then permit this for named maps. Data and dump paths need to be updated to consider priv->dreg_set instead of the 'set-is-a-map' check. Checks in reduce and validate callbacks are not changed, this can be relaxed later if a need arises. Signed-off-by: Florian Westphal <[email protected]>
1 parent b50a8b0 commit a4878ee

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

net/netfilter/nft_lookup.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct nft_lookup {
1919
struct nft_set *set;
2020
u8 sreg;
2121
u8 dreg;
22+
bool dreg_set;
2223
bool invert;
2324
struct nft_set_binding binding;
2425
};
@@ -75,7 +76,7 @@ void nft_lookup_eval(const struct nft_expr *expr,
7576
}
7677

7778
if (ext) {
78-
if (set->flags & NFT_SET_MAP)
79+
if (priv->dreg_set)
7980
nft_data_copy(&regs->data[priv->dreg],
8081
nft_set_ext_data(ext), set->dlen);
8182

@@ -122,11 +123,8 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
122123
if (flags & ~NFT_LOOKUP_F_INV)
123124
return -EINVAL;
124125

125-
if (flags & NFT_LOOKUP_F_INV) {
126-
if (set->flags & NFT_SET_MAP)
127-
return -EINVAL;
126+
if (flags & NFT_LOOKUP_F_INV)
128127
priv->invert = true;
129-
}
130128
}
131129

132130
if (tb[NFTA_LOOKUP_DREG] != NULL) {
@@ -140,8 +138,17 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
140138
set->dlen);
141139
if (err < 0)
142140
return err;
143-
} else if (set->flags & NFT_SET_MAP)
144-
return -EINVAL;
141+
priv->dreg_set = true;
142+
} else if (set->flags & NFT_SET_MAP) {
143+
/* Map given, but user asks for lookup only (i.e. to
144+
* ignore value assoicated with key).
145+
*
146+
* This makes no sense for anonymous maps since they are
147+
* scoped to the rule, but for named sets this can be useful.
148+
*/
149+
if (set->flags & NFT_SET_ANONYMOUS)
150+
return -EINVAL;
151+
}
145152

146153
priv->binding.flags = set->flags & NFT_SET_MAP;
147154

@@ -188,7 +195,7 @@ static int nft_lookup_dump(struct sk_buff *skb,
188195
goto nla_put_failure;
189196
if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
190197
goto nla_put_failure;
191-
if (priv->set->flags & NFT_SET_MAP)
198+
if (priv->dreg_set)
192199
if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
193200
goto nla_put_failure;
194201
if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))

0 commit comments

Comments
 (0)