]>
Commit | Line | Data |
---|---|---|
75a6faf6 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
c97d22e6 PNA |
2 | /* |
3 | * Copyright (c) 2012-2016 Pablo Neira Ayuso <[email protected]> | |
c97d22e6 PNA |
4 | */ |
5 | ||
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> | |
0974cff3 | 12 | #include <net/netfilter/nf_tables_core.h> |
c97d22e6 PNA |
13 | |
14 | #define nft_objref_priv(expr) *((struct nft_object **)nft_expr_priv(expr)) | |
15 | ||
16 | static void nft_objref_eval(const struct nft_expr *expr, | |
17 | struct nft_regs *regs, | |
18 | const struct nft_pktinfo *pkt) | |
19 | { | |
20 | struct nft_object *obj = nft_objref_priv(expr); | |
21 | ||
dfc46034 | 22 | obj->ops->eval(obj, regs, pkt); |
c97d22e6 PNA |
23 | } |
24 | ||
25 | static int nft_objref_init(const struct nft_ctx *ctx, | |
26 | const struct nft_expr *expr, | |
27 | const struct nlattr * const tb[]) | |
28 | { | |
29 | struct nft_object *obj = nft_objref_priv(expr); | |
30 | u8 genmask = nft_genmask_next(ctx->net); | |
31 | u32 objtype; | |
32 | ||
33 | if (!tb[NFTA_OBJREF_IMM_NAME] || | |
34 | !tb[NFTA_OBJREF_IMM_TYPE]) | |
35 | return -EINVAL; | |
36 | ||
37 | objtype = ntohl(nla_get_be32(tb[NFTA_OBJREF_IMM_TYPE])); | |
4d44175a FW |
38 | obj = nft_obj_lookup(ctx->net, ctx->table, |
39 | tb[NFTA_OBJREF_IMM_NAME], objtype, | |
cac20fcd | 40 | genmask); |
c97d22e6 PNA |
41 | if (IS_ERR(obj)) |
42 | return -ENOENT; | |
43 | ||
44 | nft_objref_priv(expr) = obj; | |
45 | obj->use++; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | static int nft_objref_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
51 | { | |
52 | const struct nft_object *obj = nft_objref_priv(expr); | |
53 | ||
d152159b | 54 | if (nla_put_string(skb, NFTA_OBJREF_IMM_NAME, obj->key.name) || |
dfc46034 PBG |
55 | nla_put_be32(skb, NFTA_OBJREF_IMM_TYPE, |
56 | htonl(obj->ops->type->type))) | |
c97d22e6 PNA |
57 | goto nla_put_failure; |
58 | ||
59 | return 0; | |
60 | ||
61 | nla_put_failure: | |
62 | return -1; | |
63 | } | |
64 | ||
8ffcd32f PNA |
65 | static void nft_objref_deactivate(const struct nft_ctx *ctx, |
66 | const struct nft_expr *expr, | |
67 | enum nft_trans_phase phase) | |
c97d22e6 PNA |
68 | { |
69 | struct nft_object *obj = nft_objref_priv(expr); | |
70 | ||
8ffcd32f PNA |
71 | if (phase == NFT_TRANS_COMMIT) |
72 | return; | |
73 | ||
c97d22e6 PNA |
74 | obj->use--; |
75 | } | |
76 | ||
8ffcd32f PNA |
77 | static void nft_objref_activate(const struct nft_ctx *ctx, |
78 | const struct nft_expr *expr) | |
79 | { | |
80 | struct nft_object *obj = nft_objref_priv(expr); | |
81 | ||
82 | obj->use++; | |
83 | } | |
84 | ||
c97d22e6 PNA |
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, | |
8ffcd32f PNA |
91 | .activate = nft_objref_activate, |
92 | .deactivate = nft_objref_deactivate, | |
c97d22e6 PNA |
93 | .dump = nft_objref_dump, |
94 | }; | |
95 | ||
63aea290 PNA |
96 | struct nft_objref_map { |
97 | struct nft_set *set; | |
4f16d25c | 98 | u8 sreg; |
63aea290 PNA |
99 | struct nft_set_binding binding; |
100 | }; | |
101 | ||
102 | static void nft_objref_map_eval(const struct nft_expr *expr, | |
103 | struct nft_regs *regs, | |
104 | const struct nft_pktinfo *pkt) | |
105 | { | |
106 | struct nft_objref_map *priv = nft_expr_priv(expr); | |
107 | const struct nft_set *set = priv->set; | |
aaa31047 | 108 | struct net *net = nft_net(pkt); |
63aea290 PNA |
109 | const struct nft_set_ext *ext; |
110 | struct nft_object *obj; | |
111 | bool found; | |
112 | ||
0974cff3 | 113 | found = nft_set_do_lookup(net, set, ®s->data[priv->sreg], &ext); |
63aea290 | 114 | if (!found) { |
aaa31047 PNA |
115 | ext = nft_set_catchall_lookup(net, set); |
116 | if (!ext) { | |
117 | regs->verdict.code = NFT_BREAK; | |
118 | return; | |
119 | } | |
63aea290 PNA |
120 | } |
121 | obj = *nft_set_ext_obj(ext); | |
dfc46034 | 122 | obj->ops->eval(obj, regs, pkt); |
63aea290 PNA |
123 | } |
124 | ||
125 | static int nft_objref_map_init(const struct nft_ctx *ctx, | |
126 | const struct nft_expr *expr, | |
127 | const struct nlattr * const tb[]) | |
128 | { | |
129 | struct nft_objref_map *priv = nft_expr_priv(expr); | |
130 | u8 genmask = nft_genmask_next(ctx->net); | |
131 | struct nft_set *set; | |
132 | int err; | |
133 | ||
10659cba PNA |
134 | set = nft_set_lookup_global(ctx->net, ctx->table, |
135 | tb[NFTA_OBJREF_SET_NAME], | |
136 | tb[NFTA_OBJREF_SET_ID], genmask); | |
c7a72e3f PNA |
137 | if (IS_ERR(set)) |
138 | return PTR_ERR(set); | |
63aea290 PNA |
139 | |
140 | if (!(set->flags & NFT_SET_OBJECT)) | |
141 | return -EINVAL; | |
142 | ||
4f16d25c PNA |
143 | err = nft_parse_register_load(tb[NFTA_OBJREF_SET_SREG], &priv->sreg, |
144 | set->klen); | |
63aea290 PNA |
145 | if (err < 0) |
146 | return err; | |
147 | ||
148 | priv->binding.flags = set->flags & NFT_SET_OBJECT; | |
149 | ||
150 | err = nf_tables_bind_set(ctx, set, &priv->binding); | |
151 | if (err < 0) | |
152 | return err; | |
153 | ||
154 | priv->set = set; | |
155 | return 0; | |
156 | } | |
157 | ||
158 | static int nft_objref_map_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
159 | { | |
160 | const struct nft_objref_map *priv = nft_expr_priv(expr); | |
161 | ||
162 | if (nft_dump_register(skb, NFTA_OBJREF_SET_SREG, priv->sreg) || | |
163 | nla_put_string(skb, NFTA_OBJREF_SET_NAME, priv->set->name)) | |
164 | goto nla_put_failure; | |
165 | ||
166 | return 0; | |
167 | ||
168 | nla_put_failure: | |
169 | return -1; | |
170 | } | |
171 | ||
cd5125d8 | 172 | static void nft_objref_map_deactivate(const struct nft_ctx *ctx, |
f6ac8585 PNA |
173 | const struct nft_expr *expr, |
174 | enum nft_trans_phase phase) | |
cd5125d8 FW |
175 | { |
176 | struct nft_objref_map *priv = nft_expr_priv(expr); | |
177 | ||
273fe3f1 PNA |
178 | nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase); |
179 | } | |
180 | ||
181 | static void nft_objref_map_activate(const struct nft_ctx *ctx, | |
182 | const struct nft_expr *expr) | |
183 | { | |
184 | struct nft_objref_map *priv = nft_expr_priv(expr); | |
f6ac8585 | 185 | |
273fe3f1 | 186 | priv->set->use++; |
cd5125d8 FW |
187 | } |
188 | ||
63aea290 PNA |
189 | static void nft_objref_map_destroy(const struct nft_ctx *ctx, |
190 | const struct nft_expr *expr) | |
191 | { | |
192 | struct nft_objref_map *priv = nft_expr_priv(expr); | |
193 | ||
cd5125d8 | 194 | nf_tables_destroy_set(ctx, priv->set); |
63aea290 PNA |
195 | } |
196 | ||
197 | static struct nft_expr_type nft_objref_type; | |
198 | static const struct nft_expr_ops nft_objref_map_ops = { | |
199 | .type = &nft_objref_type, | |
200 | .size = NFT_EXPR_SIZE(sizeof(struct nft_objref_map)), | |
201 | .eval = nft_objref_map_eval, | |
202 | .init = nft_objref_map_init, | |
273fe3f1 | 203 | .activate = nft_objref_map_activate, |
cd5125d8 | 204 | .deactivate = nft_objref_map_deactivate, |
63aea290 PNA |
205 | .destroy = nft_objref_map_destroy, |
206 | .dump = nft_objref_map_dump, | |
207 | }; | |
208 | ||
209 | static const struct nft_expr_ops * | |
210 | nft_objref_select_ops(const struct nft_ctx *ctx, | |
211 | const struct nlattr * const tb[]) | |
212 | { | |
213 | if (tb[NFTA_OBJREF_SET_SREG] && | |
214 | (tb[NFTA_OBJREF_SET_NAME] || | |
215 | tb[NFTA_OBJREF_SET_ID])) | |
216 | return &nft_objref_map_ops; | |
217 | else if (tb[NFTA_OBJREF_IMM_NAME] && | |
218 | tb[NFTA_OBJREF_IMM_TYPE]) | |
219 | return &nft_objref_ops; | |
220 | ||
221 | return ERR_PTR(-EOPNOTSUPP); | |
222 | } | |
223 | ||
c97d22e6 | 224 | static const struct nla_policy nft_objref_policy[NFTA_OBJREF_MAX + 1] = { |
b2fbd044 LZ |
225 | [NFTA_OBJREF_IMM_NAME] = { .type = NLA_STRING, |
226 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
c97d22e6 | 227 | [NFTA_OBJREF_IMM_TYPE] = { .type = NLA_U32 }, |
63aea290 | 228 | [NFTA_OBJREF_SET_SREG] = { .type = NLA_U32 }, |
b2fbd044 LZ |
229 | [NFTA_OBJREF_SET_NAME] = { .type = NLA_STRING, |
230 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
63aea290 | 231 | [NFTA_OBJREF_SET_ID] = { .type = NLA_U32 }, |
c97d22e6 PNA |
232 | }; |
233 | ||
234 | static struct nft_expr_type nft_objref_type __read_mostly = { | |
235 | .name = "objref", | |
63aea290 | 236 | .select_ops = nft_objref_select_ops, |
c97d22e6 PNA |
237 | .policy = nft_objref_policy, |
238 | .maxattr = NFTA_OBJREF_MAX, | |
239 | .owner = THIS_MODULE, | |
240 | }; | |
241 | ||
242 | static int __init nft_objref_module_init(void) | |
243 | { | |
244 | return nft_register_expr(&nft_objref_type); | |
245 | } | |
246 | ||
247 | static void __exit nft_objref_module_exit(void) | |
248 | { | |
249 | nft_unregister_expr(&nft_objref_type); | |
250 | } | |
251 | ||
252 | module_init(nft_objref_module_init); | |
253 | module_exit(nft_objref_module_exit); | |
254 | ||
255 | MODULE_LICENSE("GPL"); | |
256 | MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>"); | |
257 | MODULE_ALIAS_NFT_EXPR("objref"); | |
4cacc395 | 258 | MODULE_DESCRIPTION("nftables stateful object reference module"); |