1
2
3
4
5
6
7
8
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>
16
17#define nft_objref_priv(expr) *((struct nft_object **)nft_expr_priv(expr))
18
19static void nft_objref_eval(const struct nft_expr *expr,
20 struct nft_regs *regs,
21 const struct nft_pktinfo *pkt)
22{
23 struct nft_object *obj = nft_objref_priv(expr);
24
25 obj->ops->eval(obj, regs, pkt);
26}
27
28static int nft_objref_init(const struct nft_ctx *ctx,
29 const struct nft_expr *expr,
30 const struct nlattr * const tb[])
31{
32 struct nft_object *obj = nft_objref_priv(expr);
33 u8 genmask = nft_genmask_next(ctx->net);
34 u32 objtype;
35
36 if (!tb[NFTA_OBJREF_IMM_NAME] ||
37 !tb[NFTA_OBJREF_IMM_TYPE])
38 return -EINVAL;
39
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,
43 genmask);
44 if (IS_ERR(obj))
45 return -ENOENT;
46
47 nft_objref_priv(expr) = obj;
48 obj->use++;
49
50 return 0;
51}
52
53static int nft_objref_dump(struct sk_buff *skb, const struct nft_expr *expr)
54{
55 const struct nft_object *obj = nft_objref_priv(expr);
56
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)))
60 goto nla_put_failure;
61
62 return 0;
63
64nla_put_failure:
65 return -1;
66}
67
68static void nft_objref_destroy(const struct nft_ctx *ctx,
69 const struct nft_expr *expr)
70{
71 struct nft_object *obj = nft_objref_priv(expr);
72
73 obj->use--;
74}
75
76static struct nft_expr_type nft_objref_type;
77static 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,
84};
85
86struct nft_objref_map {
87 struct nft_set *set;
88 enum nft_registers sreg:8;
89 struct nft_set_binding binding;
90};
91
92static void nft_objref_map_eval(const struct nft_expr *expr,
93 struct nft_regs *regs,
94 const struct nft_pktinfo *pkt)
95{
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;
100 bool found;
101
102 found = set->ops->lookup(nft_net(pkt), set, ®s->data[priv->sreg],
103 &ext);
104 if (!found) {
105 regs->verdict.code = NFT_BREAK;
106 return;
107 }
108 obj = *nft_set_ext_obj(ext);
109 obj->ops->eval(obj, regs, pkt);
110}
111
112static int nft_objref_map_init(const struct nft_ctx *ctx,
113 const struct nft_expr *expr,
114 const struct nlattr * const tb[])
115{
116 struct nft_objref_map *priv = nft_expr_priv(expr);
117 u8 genmask = nft_genmask_next(ctx->net);
118 struct nft_set *set;
119 int err;
120
121 set = nft_set_lookup_global(ctx->net, ctx->table,
122 tb[NFTA_OBJREF_SET_NAME],
123 tb[NFTA_OBJREF_SET_ID], genmask);
124 if (IS_ERR(set))
125 return PTR_ERR(set);
126
127 if (!(set->flags & NFT_SET_OBJECT))
128 return -EINVAL;
129
130 priv->sreg = nft_parse_register(tb[NFTA_OBJREF_SET_SREG]);
131 err = nft_validate_register_load(priv->sreg, set->klen);
132 if (err < 0)
133 return err;
134
135 priv->binding.flags = set->flags & NFT_SET_OBJECT;
136
137 err = nf_tables_bind_set(ctx, set, &priv->binding);
138 if (err < 0)
139 return err;
140
141 priv->set = set;
142 return 0;
143}
144
145static int nft_objref_map_dump(struct sk_buff *skb, const struct nft_expr *expr)
146{
147 const struct nft_objref_map *priv = nft_expr_priv(expr);
148
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;
152
153 return 0;
154
155nla_put_failure:
156 return -1;
157}
158
159static void nft_objref_map_destroy(const struct nft_ctx *ctx,
160 const struct nft_expr *expr)
161{
162 struct nft_objref_map *priv = nft_expr_priv(expr);
163
164 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
165}
166
167static struct nft_expr_type nft_objref_type;
168static const struct nft_expr_ops nft_objref_map_ops = {
169 .type = &nft_objref_type,
170 .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)),
171 .eval = nft_objref_map_eval,
172 .init = nft_objref_map_init,
173 .destroy = nft_objref_map_destroy,
174 .dump = nft_objref_map_dump,
175};
176
177static const struct nft_expr_ops *
178nft_objref_select_ops(const struct nft_ctx *ctx,
179 const struct nlattr * const tb[])
180{
181 if (tb[NFTA_OBJREF_SET_SREG] &&
182 (tb[NFTA_OBJREF_SET_NAME] ||
183 tb[NFTA_OBJREF_SET_ID]))
184 return &nft_objref_map_ops;
185 else if (tb[NFTA_OBJREF_IMM_NAME] &&
186 tb[NFTA_OBJREF_IMM_TYPE])
187 return &nft_objref_ops;
188
189 return ERR_PTR(-EOPNOTSUPP);
190}
191
192static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = {
193 [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING,
194 .len = NFT_OBJ_MAXNAMELEN - 1 },
195 [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 },
196 [NFTA_OBJREF_SET_SREG] = { .type = NLA_U32 },
197 [NFTA_OBJREF_SET_NAME] = { .type = NLA_STRING,
198 .len = NFT_SET_MAXNAMELEN - 1 },
199 [NFTA_OBJREF_SET_ID] = { .type = NLA_U32 },
200};
201
202static struct nft_expr_type nft_objref_type __read_mostly = {
203 .name = "objref",
204 .select_ops = nft_objref_select_ops,
205 .policy = nft_objref_policy,
206 .maxattr = NFTA_OBJREF_MAX,
207 .owner = THIS_MODULE,
208};
209
210static int __init nft_objref_module_init(void)
211{
212 return nft_register_expr(&nft_objref_type);
213}
214
215static void __exit nft_objref_module_exit(void)
216{
217 nft_unregister_expr(&nft_objref_type);
218}
219
220module_init(nft_objref_module_init);
221module_exit(nft_objref_module_exit);
222
223MODULE_LICENSE("GPL");
224MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
225MODULE_ALIAS_NFT_EXPR("objref");
226