2 * Copyright (c) 2012-2016 Pablo Neira Ayuso <pablo@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/skbuff.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>
17 #define nft_objref_priv(expr) *((struct nft_object **)nft_expr_priv(expr))
19 static void nft_objref_eval(const struct nft_expr *expr,
20 struct nft_regs *regs,
21 const struct nft_pktinfo *pkt)
23 struct nft_object *obj = nft_objref_priv(expr);
25 obj->ops->eval(obj, regs, pkt);
28 static int nft_objref_init(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nlattr * const tb[])
32 struct nft_object *obj = nft_objref_priv(expr);
33 u8 genmask = nft_genmask_next(ctx->net);
36 if (!tb[NFTA_OBJREF_IMM_NAME] ||
37 !tb[NFTA_OBJREF_IMM_TYPE])
40 objtype = ntohl(nla_get_be32(tb[NFTA_OBJREF_IMM_TYPE]));
41 obj = nft_obj_lookup(ctx->net, ctx->table,
42 tb[NFTA_OBJREF_IMM_NAME], objtype,
47 nft_objref_priv(expr) = obj;
53 static int nft_objref_dump(struct sk_buff *skb, const struct nft_expr *expr)
55 const struct nft_object *obj = nft_objref_priv(expr);
57 if (nla_put_string(skb, NFTA_OBJREF_IMM_NAME, obj->key.name) ||
58 nla_put_be32(skb, NFTA_OBJREF_IMM_TYPE,
59 htonl(obj->ops->type->type)))
68 static void nft_objref_destroy(const struct nft_ctx *ctx,
69 const struct nft_expr *expr)
71 struct nft_object *obj = nft_objref_priv(expr);
76 static struct nft_expr_type nft_objref_type;
77 static const struct nft_expr_ops nft_objref_ops = {
78 .type = &nft_objref_type,
79 .size = NFT_EXPR_SIZE(sizeof(struct nft_object *)),
80 .eval = nft_objref_eval,
81 .init = nft_objref_init,
82 .destroy = nft_objref_destroy,
83 .dump = nft_objref_dump,
86 struct nft_objref_map {
88 enum nft_registers sreg:8;
89 struct nft_set_binding binding;
92 static void nft_objref_map_eval(const struct nft_expr *expr,
93 struct nft_regs *regs,
94 const struct nft_pktinfo *pkt)
96 struct nft_objref_map *priv = nft_expr_priv(expr);
97 const struct nft_set *set = priv->set;
98 const struct nft_set_ext *ext;
99 struct nft_object *obj;
102 found = set->ops->lookup(nft_net(pkt), set, ®s->data[priv->sreg],
105 regs->verdict.code = NFT_BREAK;
108 obj = *nft_set_ext_obj(ext);
109 obj->ops->eval(obj, regs, pkt);
112 static int nft_objref_map_init(const struct nft_ctx *ctx,
113 const struct nft_expr *expr,
114 const struct nlattr * const tb[])
116 struct nft_objref_map *priv = nft_expr_priv(expr);
117 u8 genmask = nft_genmask_next(ctx->net);
121 set = nft_set_lookup_global(ctx->net, ctx->table,
122 tb[NFTA_OBJREF_SET_NAME],
123 tb[NFTA_OBJREF_SET_ID], genmask);
127 if (!(set->flags & NFT_SET_OBJECT))
130 priv->sreg = nft_parse_register(tb[NFTA_OBJREF_SET_SREG]);
131 err = nft_validate_register_load(priv->sreg, set->klen);
135 priv->binding.flags = set->flags & NFT_SET_OBJECT;
137 err = nf_tables_bind_set(ctx, set, &priv->binding);
145 static int nft_objref_map_dump(struct sk_buff *skb, const struct nft_expr *expr)
147 const struct nft_objref_map *priv = nft_expr_priv(expr);
149 if (nft_dump_register(skb, NFTA_OBJREF_SET_SREG, priv->sreg) ||
150 nla_put_string(skb, NFTA_OBJREF_SET_NAME, priv->set->name))
151 goto nla_put_failure;
159 static void nft_objref_map_deactivate(const struct nft_ctx *ctx,
160 const struct nft_expr *expr,
161 enum nft_trans_phase phase)
163 struct nft_objref_map *priv = nft_expr_priv(expr);
165 nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
168 static void nft_objref_map_activate(const struct nft_ctx *ctx,
169 const struct nft_expr *expr)
171 struct nft_objref_map *priv = nft_expr_priv(expr);
176 static void nft_objref_map_destroy(const struct nft_ctx *ctx,
177 const struct nft_expr *expr)
179 struct nft_objref_map *priv = nft_expr_priv(expr);
181 nf_tables_destroy_set(ctx, priv->set);
184 static struct nft_expr_type nft_objref_type;
185 static const struct nft_expr_ops nft_objref_map_ops = {
186 .type = &nft_objref_type,
187 .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)),
188 .eval = nft_objref_map_eval,
189 .init = nft_objref_map_init,
190 .activate = nft_objref_map_activate,
191 .deactivate = nft_objref_map_deactivate,
192 .destroy = nft_objref_map_destroy,
193 .dump = nft_objref_map_dump,
196 static const struct nft_expr_ops *
197 nft_objref_select_ops(const struct nft_ctx *ctx,
198 const struct nlattr * const tb[])
200 if (tb[NFTA_OBJREF_SET_SREG] &&
201 (tb[NFTA_OBJREF_SET_NAME] ||
202 tb[NFTA_OBJREF_SET_ID]))
203 return &nft_objref_map_ops;
204 else if (tb[NFTA_OBJREF_IMM_NAME] &&
205 tb[NFTA_OBJREF_IMM_TYPE])
206 return &nft_objref_ops;
208 return ERR_PTR(-EOPNOTSUPP);
211 static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = {
212 [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING,
213 .len = NFT_OBJ_MAXNAMELEN - 1 },
214 [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 },
215 [NFTA_OBJREF_SET_SREG] = { .type = NLA_U32 },
216 [NFTA_OBJREF_SET_NAME] = { .type = NLA_STRING,
217 .len = NFT_SET_MAXNAMELEN - 1 },
218 [NFTA_OBJREF_SET_ID] = { .type = NLA_U32 },
221 static struct nft_expr_type nft_objref_type __read_mostly = {
223 .select_ops = nft_objref_select_ops,
224 .policy = nft_objref_policy,
225 .maxattr = NFTA_OBJREF_MAX,
226 .owner = THIS_MODULE,
229 static int __init nft_objref_module_init(void)
231 return nft_register_expr(&nft_objref_type);
234 static void __exit nft_objref_module_exit(void)
236 nft_unregister_expr(&nft_objref_type);
239 module_init(nft_objref_module_init);
240 module_exit(nft_objref_module_exit);
242 MODULE_LICENSE("GPL");
243 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
244 MODULE_ALIAS_NFT_EXPR("objref");