1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016 Pablo Neira Ayuso <pablo@netfilter.org>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/netlink.h>
10 #include <linux/netfilter.h>
11 #include <linux/netfilter/nf_tables.h>
12 #include <net/netfilter/nf_tables_core.h>
14 #define nft_objref_priv(expr) *((struct nft_object **)nft_expr_priv(expr))
16 static void nft_objref_eval(const struct nft_expr *expr,
17 struct nft_regs *regs,
18 const struct nft_pktinfo *pkt)
20 struct nft_object *obj = nft_objref_priv(expr);
22 obj->ops->eval(obj, regs, pkt);
25 static int nft_objref_init(const struct nft_ctx *ctx,
26 const struct nft_expr *expr,
27 const struct nlattr * const tb[])
29 struct nft_object *obj = nft_objref_priv(expr);
30 u8 genmask = nft_genmask_next(ctx->net);
33 if (!tb[NFTA_OBJREF_IMM_NAME] ||
34 !tb[NFTA_OBJREF_IMM_TYPE])
37 objtype = ntohl(nla_get_be32(tb[NFTA_OBJREF_IMM_TYPE]));
38 obj = nft_obj_lookup(ctx->net, ctx->table,
39 tb[NFTA_OBJREF_IMM_NAME], objtype,
44 nft_objref_priv(expr) = obj;
50 static int nft_objref_dump(struct sk_buff *skb, const struct nft_expr *expr)
52 const struct nft_object *obj = nft_objref_priv(expr);
54 if (nla_put_string(skb, NFTA_OBJREF_IMM_NAME, obj->key.name) ||
55 nla_put_be32(skb, NFTA_OBJREF_IMM_TYPE,
56 htonl(obj->ops->type->type)))
65 static void nft_objref_deactivate(const struct nft_ctx *ctx,
66 const struct nft_expr *expr,
67 enum nft_trans_phase phase)
69 struct nft_object *obj = nft_objref_priv(expr);
71 if (phase == NFT_TRANS_COMMIT)
77 static void nft_objref_activate(const struct nft_ctx *ctx,
78 const struct nft_expr *expr)
80 struct nft_object *obj = nft_objref_priv(expr);
85 static struct nft_expr_type nft_objref_type;
86 static const struct nft_expr_ops nft_objref_ops = {
87 .type = &nft_objref_type,
88 .size = NFT_EXPR_SIZE(sizeof(struct nft_object *)),
89 .eval = nft_objref_eval,
90 .init = nft_objref_init,
91 .activate = nft_objref_activate,
92 .deactivate = nft_objref_deactivate,
93 .dump = nft_objref_dump,
94 .reduce = NFT_REDUCE_READONLY,
97 struct nft_objref_map {
100 struct nft_set_binding binding;
103 static void nft_objref_map_eval(const struct nft_expr *expr,
104 struct nft_regs *regs,
105 const struct nft_pktinfo *pkt)
107 struct nft_objref_map *priv = nft_expr_priv(expr);
108 const struct nft_set *set = priv->set;
109 struct net *net = nft_net(pkt);
110 const struct nft_set_ext *ext;
111 struct nft_object *obj;
114 found = nft_set_do_lookup(net, set, ®s->data[priv->sreg], &ext);
116 ext = nft_set_catchall_lookup(net, set);
118 regs->verdict.code = NFT_BREAK;
122 obj = *nft_set_ext_obj(ext);
123 obj->ops->eval(obj, regs, pkt);
126 static int nft_objref_map_init(const struct nft_ctx *ctx,
127 const struct nft_expr *expr,
128 const struct nlattr * const tb[])
130 struct nft_objref_map *priv = nft_expr_priv(expr);
131 u8 genmask = nft_genmask_next(ctx->net);
135 set = nft_set_lookup_global(ctx->net, ctx->table,
136 tb[NFTA_OBJREF_SET_NAME],
137 tb[NFTA_OBJREF_SET_ID], genmask);
141 if (!(set->flags & NFT_SET_OBJECT))
144 err = nft_parse_register_load(tb[NFTA_OBJREF_SET_SREG], &priv->sreg,
149 priv->binding.flags = set->flags & NFT_SET_OBJECT;
151 err = nf_tables_bind_set(ctx, set, &priv->binding);
159 static int nft_objref_map_dump(struct sk_buff *skb, const struct nft_expr *expr)
161 const struct nft_objref_map *priv = nft_expr_priv(expr);
163 if (nft_dump_register(skb, NFTA_OBJREF_SET_SREG, priv->sreg) ||
164 nla_put_string(skb, NFTA_OBJREF_SET_NAME, priv->set->name))
165 goto nla_put_failure;
173 static void nft_objref_map_deactivate(const struct nft_ctx *ctx,
174 const struct nft_expr *expr,
175 enum nft_trans_phase phase)
177 struct nft_objref_map *priv = nft_expr_priv(expr);
179 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
182 static void nft_objref_map_activate(const struct nft_ctx *ctx,
183 const struct nft_expr *expr)
185 struct nft_objref_map *priv = nft_expr_priv(expr);
190 static void nft_objref_map_destroy(const struct nft_ctx *ctx,
191 const struct nft_expr *expr)
193 struct nft_objref_map *priv = nft_expr_priv(expr);
195 nf_tables_destroy_set(ctx, priv->set);
198 static struct nft_expr_type nft_objref_type;
199 static const struct nft_expr_ops nft_objref_map_ops = {
200 .type = &nft_objref_type,
201 .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)),
202 .eval = nft_objref_map_eval,
203 .init = nft_objref_map_init,
204 .activate = nft_objref_map_activate,
205 .deactivate = nft_objref_map_deactivate,
206 .destroy = nft_objref_map_destroy,
207 .dump = nft_objref_map_dump,
208 .reduce = NFT_REDUCE_READONLY,
211 static const struct nft_expr_ops *
212 nft_objref_select_ops(const struct nft_ctx *ctx,
213 const struct nlattr * const tb[])
215 if (tb[NFTA_OBJREF_SET_SREG] &&
216 (tb[NFTA_OBJREF_SET_NAME] ||
217 tb[NFTA_OBJREF_SET_ID]))
218 return &nft_objref_map_ops;
219 else if (tb[NFTA_OBJREF_IMM_NAME] &&
220 tb[NFTA_OBJREF_IMM_TYPE])
221 return &nft_objref_ops;
223 return ERR_PTR(-EOPNOTSUPP);
226 static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = {
227 [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING,
228 .len = NFT_OBJ_MAXNAMELEN - 1 },
229 [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 },
230 [NFTA_OBJREF_SET_SREG] = { .type = NLA_U32 },
231 [NFTA_OBJREF_SET_NAME] = { .type = NLA_STRING,
232 .len = NFT_SET_MAXNAMELEN - 1 },
233 [NFTA_OBJREF_SET_ID] = { .type = NLA_U32 },
236 static struct nft_expr_type nft_objref_type __read_mostly = {
238 .select_ops = nft_objref_select_ops,
239 .policy = nft_objref_policy,
240 .maxattr = NFTA_OBJREF_MAX,
241 .owner = THIS_MODULE,
244 static int __init nft_objref_module_init(void)
246 return nft_register_expr(&nft_objref_type);
249 static void __exit nft_objref_module_exit(void)
251 nft_unregister_expr(&nft_objref_type);
254 module_init(nft_objref_module_init);
255 module_exit(nft_objref_module_exit);
257 MODULE_LICENSE("GPL");
258 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
259 MODULE_ALIAS_NFT_EXPR("objref");
260 MODULE_DESCRIPTION("nftables stateful object reference module");