1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/rbtree.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
15 #include <net/netfilter/nf_tables.h>
16 #include <net/netfilter/nf_tables_core.h>
23 struct nft_set_binding binding;
26 void nft_lookup_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
30 const struct nft_lookup *priv = nft_expr_priv(expr);
31 const struct nft_set *set = priv->set;
32 const struct nft_set_ext *ext = NULL;
33 const struct net *net = nft_net(pkt);
36 found = set->ops->lookup(net, set, ®s->data[priv->sreg], &ext) ^
39 ext = nft_set_catchall_lookup(net, set);
41 regs->verdict.code = NFT_BREAK;
47 if (set->flags & NFT_SET_MAP)
48 nft_data_copy(®s->data[priv->dreg],
49 nft_set_ext_data(ext), set->dlen);
51 nft_set_elem_update_expr(ext, regs, pkt);
55 static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
56 [NFTA_LOOKUP_SET] = { .type = NLA_STRING,
57 .len = NFT_SET_MAXNAMELEN - 1 },
58 [NFTA_LOOKUP_SET_ID] = { .type = NLA_U32 },
59 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
60 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
61 [NFTA_LOOKUP_FLAGS] = { .type = NLA_U32 },
64 static int nft_lookup_init(const struct nft_ctx *ctx,
65 const struct nft_expr *expr,
66 const struct nlattr * const tb[])
68 struct nft_lookup *priv = nft_expr_priv(expr);
69 u8 genmask = nft_genmask_next(ctx->net);
74 if (tb[NFTA_LOOKUP_SET] == NULL ||
75 tb[NFTA_LOOKUP_SREG] == NULL)
78 set = nft_set_lookup_global(ctx->net, ctx->table, tb[NFTA_LOOKUP_SET],
79 tb[NFTA_LOOKUP_SET_ID], genmask);
83 err = nft_parse_register_load(tb[NFTA_LOOKUP_SREG], &priv->sreg,
88 if (tb[NFTA_LOOKUP_FLAGS]) {
89 flags = ntohl(nla_get_be32(tb[NFTA_LOOKUP_FLAGS]));
91 if (flags & ~NFT_LOOKUP_F_INV)
94 if (flags & NFT_LOOKUP_F_INV) {
95 if (set->flags & NFT_SET_MAP)
101 if (tb[NFTA_LOOKUP_DREG] != NULL) {
104 if (!(set->flags & NFT_SET_MAP))
107 err = nft_parse_register_store(ctx, tb[NFTA_LOOKUP_DREG],
108 &priv->dreg, NULL, set->dtype,
112 } else if (set->flags & NFT_SET_MAP)
115 priv->binding.flags = set->flags & NFT_SET_MAP;
117 err = nf_tables_bind_set(ctx, set, &priv->binding);
125 static void nft_lookup_deactivate(const struct nft_ctx *ctx,
126 const struct nft_expr *expr,
127 enum nft_trans_phase phase)
129 struct nft_lookup *priv = nft_expr_priv(expr);
131 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
134 static void nft_lookup_activate(const struct nft_ctx *ctx,
135 const struct nft_expr *expr)
137 struct nft_lookup *priv = nft_expr_priv(expr);
142 static void nft_lookup_destroy(const struct nft_ctx *ctx,
143 const struct nft_expr *expr)
145 struct nft_lookup *priv = nft_expr_priv(expr);
147 nf_tables_destroy_set(ctx, priv->set);
150 static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
152 const struct nft_lookup *priv = nft_expr_priv(expr);
153 u32 flags = priv->invert ? NFT_LOOKUP_F_INV : 0;
155 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
156 goto nla_put_failure;
157 if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
158 goto nla_put_failure;
159 if (priv->set->flags & NFT_SET_MAP)
160 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
161 goto nla_put_failure;
162 if (nla_put_be32(skb, NFTA_LOOKUP_FLAGS, htonl(flags)))
163 goto nla_put_failure;
170 static int nft_lookup_validate_setelem(const struct nft_ctx *ctx,
172 const struct nft_set_iter *iter,
173 struct nft_set_elem *elem)
175 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
176 struct nft_ctx *pctx = (struct nft_ctx *)ctx;
177 const struct nft_data *data;
180 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
181 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
184 data = nft_set_ext_data(ext);
185 switch (data->verdict.code) {
189 err = nft_chain_validate(ctx, data->verdict.chain);
201 static int nft_lookup_validate(const struct nft_ctx *ctx,
202 const struct nft_expr *expr,
203 const struct nft_data **d)
205 const struct nft_lookup *priv = nft_expr_priv(expr);
206 struct nft_set_iter iter;
208 if (!(priv->set->flags & NFT_SET_MAP) ||
209 priv->set->dtype != NFT_DATA_VERDICT)
212 iter.genmask = nft_genmask_next(ctx->net);
216 iter.fn = nft_lookup_validate_setelem;
218 priv->set->ops->walk(ctx, priv->set, &iter);
225 static const struct nft_expr_ops nft_lookup_ops = {
226 .type = &nft_lookup_type,
227 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
228 .eval = nft_lookup_eval,
229 .init = nft_lookup_init,
230 .activate = nft_lookup_activate,
231 .deactivate = nft_lookup_deactivate,
232 .destroy = nft_lookup_destroy,
233 .dump = nft_lookup_dump,
234 .validate = nft_lookup_validate,
237 struct nft_expr_type nft_lookup_type __read_mostly = {
239 .ops = &nft_lookup_ops,
240 .policy = nft_lookup_policy,
241 .maxattr = NFTA_LOOKUP_MAX,
242 .owner = THIS_MODULE,