Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
20a69341 | 3 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netlink.h> | |
1ff75a3e | 13 | #include <linux/vmalloc.h> |
0eb71a9d | 14 | #include <linux/rhashtable.h> |
8e6cf365 | 15 | #include <linux/audit.h> |
96518518 PM |
16 | #include <linux/netfilter.h> |
17 | #include <linux/netfilter/nfnetlink.h> | |
18 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 19 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
20 | #include <net/netfilter/nf_tables_core.h> |
21 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 22 | #include <net/netfilter/nf_tables_offload.h> |
99633ab2 | 23 | #include <net/net_namespace.h> |
96518518 PM |
24 | #include <net/sock.h> |
25 | ||
9332d27d FW |
26 | #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) |
27 | ||
0854db2a FW |
28 | unsigned int nf_tables_net_id __read_mostly; |
29 | ||
96518518 | 30 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 31 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 32 | static LIST_HEAD(nf_tables_flowtables); |
0935d558 | 33 | static LIST_HEAD(nf_tables_destroy_list); |
5f68718b | 34 | static LIST_HEAD(nf_tables_gc_list); |
0935d558 | 35 | static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); |
5f68718b | 36 | static DEFINE_SPINLOCK(nf_tables_gc_list_lock); |
96518518 | 37 | |
a654de8f PNA |
38 | enum { |
39 | NFT_VALIDATE_SKIP = 0, | |
40 | NFT_VALIDATE_NEED, | |
41 | NFT_VALIDATE_DO, | |
42 | }; | |
43 | ||
4d44175a FW |
44 | static struct rhltable nft_objname_ht; |
45 | ||
1b2470e5 FW |
46 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
47 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
48 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
49 | ||
4d44175a FW |
50 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
51 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
52 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
53 | ||
1b2470e5 FW |
54 | static const struct rhashtable_params nft_chain_ht_params = { |
55 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
56 | .key_offset = offsetof(struct nft_chain, name), | |
57 | .hashfn = nft_chain_hash, | |
58 | .obj_hashfn = nft_chain_hash_obj, | |
59 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
60 | .automatic_shrinking = true, |
61 | }; | |
62 | ||
4d44175a FW |
63 | static const struct rhashtable_params nft_objname_ht_params = { |
64 | .head_offset = offsetof(struct nft_object, rhlhead), | |
65 | .key_offset = offsetof(struct nft_object, key), | |
66 | .hashfn = nft_objname_hash, | |
67 | .obj_hashfn = nft_objname_hash_obj, | |
68 | .obj_cmpfn = nft_objname_hash_cmp, | |
69 | .automatic_shrinking = true, | |
70 | }; | |
71 | ||
c520292f RGB |
72 | struct nft_audit_data { |
73 | struct nft_table *table; | |
74 | int entries; | |
75 | int op; | |
76 | struct list_head list; | |
77 | }; | |
78 | ||
79 | static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types | |
80 | [NFT_MSG_NEWTABLE] = AUDIT_NFT_OP_TABLE_REGISTER, | |
81 | [NFT_MSG_GETTABLE] = AUDIT_NFT_OP_INVALID, | |
82 | [NFT_MSG_DELTABLE] = AUDIT_NFT_OP_TABLE_UNREGISTER, | |
83 | [NFT_MSG_NEWCHAIN] = AUDIT_NFT_OP_CHAIN_REGISTER, | |
84 | [NFT_MSG_GETCHAIN] = AUDIT_NFT_OP_INVALID, | |
85 | [NFT_MSG_DELCHAIN] = AUDIT_NFT_OP_CHAIN_UNREGISTER, | |
86 | [NFT_MSG_NEWRULE] = AUDIT_NFT_OP_RULE_REGISTER, | |
87 | [NFT_MSG_GETRULE] = AUDIT_NFT_OP_INVALID, | |
88 | [NFT_MSG_DELRULE] = AUDIT_NFT_OP_RULE_UNREGISTER, | |
89 | [NFT_MSG_NEWSET] = AUDIT_NFT_OP_SET_REGISTER, | |
90 | [NFT_MSG_GETSET] = AUDIT_NFT_OP_INVALID, | |
91 | [NFT_MSG_DELSET] = AUDIT_NFT_OP_SET_UNREGISTER, | |
92 | [NFT_MSG_NEWSETELEM] = AUDIT_NFT_OP_SETELEM_REGISTER, | |
93 | [NFT_MSG_GETSETELEM] = AUDIT_NFT_OP_INVALID, | |
94 | [NFT_MSG_DELSETELEM] = AUDIT_NFT_OP_SETELEM_UNREGISTER, | |
95 | [NFT_MSG_NEWGEN] = AUDIT_NFT_OP_GEN_REGISTER, | |
96 | [NFT_MSG_GETGEN] = AUDIT_NFT_OP_INVALID, | |
97 | [NFT_MSG_TRACE] = AUDIT_NFT_OP_INVALID, | |
98 | [NFT_MSG_NEWOBJ] = AUDIT_NFT_OP_OBJ_REGISTER, | |
99 | [NFT_MSG_GETOBJ] = AUDIT_NFT_OP_INVALID, | |
100 | [NFT_MSG_DELOBJ] = AUDIT_NFT_OP_OBJ_UNREGISTER, | |
101 | [NFT_MSG_GETOBJ_RESET] = AUDIT_NFT_OP_OBJ_RESET, | |
102 | [NFT_MSG_NEWFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_REGISTER, | |
103 | [NFT_MSG_GETFLOWTABLE] = AUDIT_NFT_OP_INVALID, | |
104 | [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, | |
105 | }; | |
106 | ||
00c320f9 | 107 | static void nft_validate_state_update(struct nft_table *table, u8 new_validate_state) |
a654de8f | 108 | { |
00c320f9 | 109 | switch (table->validate_state) { |
a654de8f PNA |
110 | case NFT_VALIDATE_SKIP: |
111 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
112 | break; | |
113 | case NFT_VALIDATE_NEED: | |
114 | break; | |
115 | case NFT_VALIDATE_DO: | |
116 | if (new_validate_state == NFT_VALIDATE_NEED) | |
117 | return; | |
118 | } | |
119 | ||
00c320f9 | 120 | table->validate_state = new_validate_state; |
a654de8f | 121 | } |
0935d558 FW |
122 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
123 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 124 | |
5f68718b PNA |
125 | static void nft_trans_gc_work(struct work_struct *work); |
126 | static DECLARE_WORK(trans_gc_work, nft_trans_gc_work); | |
127 | ||
7c95f6d8 | 128 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 129 | struct net *net, |
7c95f6d8 PNA |
130 | const struct sk_buff *skb, |
131 | const struct nlmsghdr *nlh, | |
36596dad | 132 | u8 family, |
7c95f6d8 PNA |
133 | struct nft_table *table, |
134 | struct nft_chain *chain, | |
135 | const struct nlattr * const *nla) | |
136 | { | |
633c9a84 | 137 | ctx->net = net; |
36596dad | 138 | ctx->family = family; |
26b2f552 | 139 | ctx->level = 0; |
128ad332 PNA |
140 | ctx->table = table; |
141 | ctx->chain = chain; | |
142 | ctx->nla = nla; | |
143 | ctx->portid = NETLINK_CB(skb).portid; | |
144 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 145 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 146 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
147 | } |
148 | ||
8411b644 PNA |
149 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
150 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
151 | { |
152 | struct nft_trans *trans; | |
153 | ||
8411b644 | 154 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
155 | if (trans == NULL) |
156 | return NULL; | |
157 | ||
58007785 | 158 | INIT_LIST_HEAD(&trans->list); |
938154b9 | 159 | INIT_LIST_HEAD(&trans->binding_list); |
b380e5c7 | 160 | trans->msg_type = msg_type; |
1081d11b PNA |
161 | trans->ctx = *ctx; |
162 | ||
163 | return trans; | |
164 | } | |
165 | ||
8411b644 PNA |
166 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
167 | int msg_type, u32 size) | |
168 | { | |
169 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
170 | } | |
171 | ||
938154b9 | 172 | static void nft_trans_list_del(struct nft_trans *trans) |
1081d11b PNA |
173 | { |
174 | list_del(&trans->list); | |
938154b9 PNA |
175 | list_del(&trans->binding_list); |
176 | } | |
177 | ||
178 | static void nft_trans_destroy(struct nft_trans *trans) | |
179 | { | |
180 | nft_trans_list_del(trans); | |
1081d11b PNA |
181 | kfree(trans); |
182 | } | |
183 | ||
26b5a571 PNA |
184 | static void __nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set, |
185 | bool bind) | |
f6ac8585 | 186 | { |
0854db2a | 187 | struct nftables_pernet *nft_net; |
f6ac8585 PNA |
188 | struct net *net = ctx->net; |
189 | struct nft_trans *trans; | |
190 | ||
191 | if (!nft_set_is_anonymous(set)) | |
192 | return; | |
193 | ||
d59d2f82 | 194 | nft_net = nft_pernet(net); |
0854db2a | 195 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { |
6a0a8d10 PNA |
196 | switch (trans->msg_type) { |
197 | case NFT_MSG_NEWSET: | |
198 | if (nft_trans_set(trans) == set) | |
26b5a571 | 199 | nft_trans_set_bound(trans) = bind; |
6a0a8d10 PNA |
200 | break; |
201 | case NFT_MSG_NEWSETELEM: | |
202 | if (nft_trans_elem_set(trans) == set) | |
26b5a571 | 203 | nft_trans_elem_set_bound(trans) = bind; |
f6ac8585 PNA |
204 | break; |
205 | } | |
206 | } | |
207 | } | |
208 | ||
26b5a571 PNA |
209 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
210 | { | |
211 | return __nft_set_trans_bind(ctx, set, true); | |
212 | } | |
213 | ||
214 | static void nft_set_trans_unbind(const struct nft_ctx *ctx, struct nft_set *set) | |
215 | { | |
216 | return __nft_set_trans_bind(ctx, set, false); | |
217 | } | |
218 | ||
219 | static void __nft_chain_trans_bind(const struct nft_ctx *ctx, | |
220 | struct nft_chain *chain, bool bind) | |
4bedf9ee PNA |
221 | { |
222 | struct nftables_pernet *nft_net; | |
223 | struct net *net = ctx->net; | |
224 | struct nft_trans *trans; | |
225 | ||
226 | if (!nft_chain_binding(chain)) | |
227 | return; | |
228 | ||
229 | nft_net = nft_pernet(net); | |
230 | list_for_each_entry_reverse(trans, &nft_net->commit_list, list) { | |
231 | switch (trans->msg_type) { | |
232 | case NFT_MSG_NEWCHAIN: | |
233 | if (nft_trans_chain(trans) == chain) | |
26b5a571 | 234 | nft_trans_chain_bound(trans) = bind; |
4bedf9ee PNA |
235 | break; |
236 | case NFT_MSG_NEWRULE: | |
237 | if (trans->ctx.chain == chain) | |
26b5a571 | 238 | nft_trans_rule_bound(trans) = bind; |
4bedf9ee PNA |
239 | break; |
240 | } | |
241 | } | |
242 | } | |
243 | ||
26b5a571 PNA |
244 | static void nft_chain_trans_bind(const struct nft_ctx *ctx, |
245 | struct nft_chain *chain) | |
246 | { | |
247 | __nft_chain_trans_bind(ctx, chain, true); | |
248 | } | |
249 | ||
4bedf9ee PNA |
250 | int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain) |
251 | { | |
252 | if (!nft_chain_binding(chain)) | |
253 | return 0; | |
254 | ||
255 | if (nft_chain_binding(ctx->chain)) | |
256 | return -EOPNOTSUPP; | |
257 | ||
258 | if (chain->bound) | |
259 | return -EBUSY; | |
260 | ||
1689f259 PNA |
261 | if (!nft_use_inc(&chain->use)) |
262 | return -EMFILE; | |
263 | ||
4bedf9ee | 264 | chain->bound = true; |
4bedf9ee PNA |
265 | nft_chain_trans_bind(ctx, chain); |
266 | ||
267 | return 0; | |
268 | } | |
269 | ||
26b5a571 PNA |
270 | void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain) |
271 | { | |
272 | __nft_chain_trans_bind(ctx, chain, false); | |
273 | } | |
274 | ||
d54725cd PNA |
275 | static int nft_netdev_register_hooks(struct net *net, |
276 | struct list_head *hook_list) | |
277 | { | |
278 | struct nft_hook *hook; | |
279 | int err, j; | |
280 | ||
281 | j = 0; | |
282 | list_for_each_entry(hook, hook_list, list) { | |
283 | err = nf_register_net_hook(net, &hook->ops); | |
284 | if (err < 0) | |
285 | goto err_register; | |
286 | ||
287 | j++; | |
288 | } | |
289 | return 0; | |
290 | ||
291 | err_register: | |
292 | list_for_each_entry(hook, hook_list, list) { | |
293 | if (j-- <= 0) | |
294 | break; | |
295 | ||
296 | nf_unregister_net_hook(net, &hook->ops); | |
297 | } | |
298 | return err; | |
299 | } | |
300 | ||
301 | static void nft_netdev_unregister_hooks(struct net *net, | |
f9a43007 PNA |
302 | struct list_head *hook_list, |
303 | bool release_netdev) | |
d54725cd | 304 | { |
f9a43007 | 305 | struct nft_hook *hook, *next; |
d54725cd | 306 | |
f9a43007 | 307 | list_for_each_entry_safe(hook, next, hook_list, list) { |
d54725cd | 308 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
309 | if (release_netdev) { |
310 | list_del(&hook->list); | |
311 | kfree_rcu(hook, rcu); | |
312 | } | |
313 | } | |
d54725cd PNA |
314 | } |
315 | ||
c974a3a3 PNA |
316 | static int nf_tables_register_hook(struct net *net, |
317 | const struct nft_table *table, | |
318 | struct nft_chain *chain) | |
d8ee8f7c | 319 | { |
d54725cd | 320 | struct nft_base_chain *basechain; |
a37061a6 | 321 | const struct nf_hook_ops *ops; |
ae6153b5 | 322 | |
d8ee8f7c | 323 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 324 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
325 | return 0; |
326 | ||
4e25ceb8 FW |
327 | basechain = nft_base_chain(chain); |
328 | ops = &basechain->ops; | |
ae6153b5 | 329 | |
4e25ceb8 FW |
330 | if (basechain->type->ops_register) |
331 | return basechain->type->ops_register(net, ops); | |
332 | ||
d3519cb8 | 333 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
1e9451cb FW |
334 | return nft_netdev_register_hooks(net, &basechain->hook_list); |
335 | ||
336 | return nf_register_net_hook(net, &basechain->ops); | |
d8ee8f7c PNA |
337 | } |
338 | ||
f9a43007 PNA |
339 | static void __nf_tables_unregister_hook(struct net *net, |
340 | const struct nft_table *table, | |
341 | struct nft_chain *chain, | |
342 | bool release_netdev) | |
c5598794 | 343 | { |
d54725cd | 344 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
345 | const struct nf_hook_ops *ops; |
346 | ||
d8ee8f7c | 347 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 348 | !nft_is_base_chain(chain)) |
d8ee8f7c | 349 | return; |
4e25ceb8 FW |
350 | basechain = nft_base_chain(chain); |
351 | ops = &basechain->ops; | |
d8ee8f7c | 352 | |
4e25ceb8 FW |
353 | if (basechain->type->ops_unregister) |
354 | return basechain->type->ops_unregister(net, ops); | |
355 | ||
d3519cb8 | 356 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) |
f9a43007 PNA |
357 | nft_netdev_unregister_hooks(net, &basechain->hook_list, |
358 | release_netdev); | |
1e9451cb FW |
359 | else |
360 | nf_unregister_net_hook(net, &basechain->ops); | |
c5598794 AB |
361 | } |
362 | ||
f9a43007 PNA |
363 | static void nf_tables_unregister_hook(struct net *net, |
364 | const struct nft_table *table, | |
365 | struct nft_chain *chain) | |
366 | { | |
367 | return __nf_tables_unregister_hook(net, table, chain, false); | |
368 | } | |
369 | ||
0854db2a FW |
370 | static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trans) |
371 | { | |
d59d2f82 | 372 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a | 373 | |
938154b9 PNA |
374 | switch (trans->msg_type) { |
375 | case NFT_MSG_NEWSET: | |
376 | if (!nft_trans_set_update(trans) && | |
377 | nft_set_is_anonymous(nft_trans_set(trans))) | |
378 | list_add_tail(&trans->binding_list, &nft_net->binding_list); | |
379 | break; | |
62e1e94b PNA |
380 | case NFT_MSG_NEWCHAIN: |
381 | if (!nft_trans_chain_update(trans) && | |
382 | nft_chain_binding(nft_trans_chain(trans))) | |
383 | list_add_tail(&trans->binding_list, &nft_net->binding_list); | |
384 | break; | |
938154b9 PNA |
385 | } |
386 | ||
0854db2a FW |
387 | list_add_tail(&trans->list, &nft_net->commit_list); |
388 | } | |
389 | ||
ee01d542 AB |
390 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
391 | { | |
392 | struct nft_trans *trans; | |
393 | ||
394 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
395 | if (trans == NULL) | |
396 | return -ENOMEM; | |
397 | ||
398 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 399 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 | 400 | |
0854db2a | 401 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
402 | return 0; |
403 | } | |
404 | ||
405 | static int nft_deltable(struct nft_ctx *ctx) | |
406 | { | |
407 | int err; | |
408 | ||
409 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
410 | if (err < 0) | |
411 | return err; | |
412 | ||
f2a6d766 | 413 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
414 | return err; |
415 | } | |
416 | ||
66293c46 | 417 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
418 | { |
419 | struct nft_trans *trans; | |
420 | ||
421 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
422 | if (trans == NULL) | |
66293c46 | 423 | return ERR_PTR(-ENOMEM); |
ee01d542 | 424 | |
74cccc3d | 425 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 426 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 427 | |
74cccc3d PNA |
428 | if (ctx->nla[NFTA_CHAIN_ID]) { |
429 | nft_trans_chain_id(trans) = | |
430 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
431 | } | |
432 | } | |
4bedf9ee | 433 | nft_trans_chain(trans) = ctx->chain; |
0854db2a | 434 | nft_trans_commit_list_add_tail(ctx->net, trans); |
4bedf9ee | 435 | |
66293c46 | 436 | return trans; |
ee01d542 AB |
437 | } |
438 | ||
439 | static int nft_delchain(struct nft_ctx *ctx) | |
440 | { | |
66293c46 | 441 | struct nft_trans *trans; |
ee01d542 | 442 | |
66293c46 FW |
443 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
444 | if (IS_ERR(trans)) | |
445 | return PTR_ERR(trans); | |
ee01d542 | 446 | |
1689f259 | 447 | nft_use_dec(&ctx->table->use); |
664b0f8c | 448 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 449 | |
66293c46 | 450 | return 0; |
ee01d542 AB |
451 | } |
452 | ||
4bedf9ee | 453 | void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae PNA |
454 | { |
455 | struct nft_expr *expr; | |
456 | ||
457 | expr = nft_expr_first(rule); | |
31cc578a | 458 | while (nft_expr_more(rule, expr)) { |
bb7b40ae PNA |
459 | if (expr->ops->activate) |
460 | expr->ops->activate(ctx, expr); | |
461 | ||
462 | expr = nft_expr_next(expr); | |
463 | } | |
464 | } | |
465 | ||
4bedf9ee PNA |
466 | void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule, |
467 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
468 | { |
469 | struct nft_expr *expr; | |
470 | ||
471 | expr = nft_expr_first(rule); | |
31cc578a | 472 | while (nft_expr_more(rule, expr)) { |
bb7b40ae | 473 | if (expr->ops->deactivate) |
f6ac8585 | 474 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
475 | |
476 | expr = nft_expr_next(expr); | |
477 | } | |
478 | } | |
479 | ||
ee01d542 AB |
480 | static int |
481 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
482 | { | |
483 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
484 | if (nft_is_active_next(ctx->net, rule)) { |
485 | nft_deactivate_next(ctx->net, rule); | |
1689f259 | 486 | nft_use_dec(&ctx->chain->use); |
ee01d542 AB |
487 | return 0; |
488 | } | |
489 | return -ENOENT; | |
490 | } | |
491 | ||
492 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
493 | struct nft_rule *rule) | |
494 | { | |
495 | struct nft_trans *trans; | |
496 | ||
497 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
498 | if (trans == NULL) | |
499 | return NULL; | |
500 | ||
1a94e38d PNA |
501 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
502 | nft_trans_rule_id(trans) = | |
503 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
504 | } | |
ee01d542 | 505 | nft_trans_rule(trans) = rule; |
0854db2a | 506 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
507 | |
508 | return trans; | |
509 | } | |
510 | ||
511 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
512 | { | |
63b48c73 | 513 | struct nft_flow_rule *flow; |
ee01d542 AB |
514 | struct nft_trans *trans; |
515 | int err; | |
516 | ||
517 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
518 | if (trans == NULL) | |
519 | return -ENOMEM; | |
520 | ||
63b48c73 PNA |
521 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
522 | flow = nft_flow_rule_create(ctx->net, rule); | |
523 | if (IS_ERR(flow)) { | |
524 | nft_trans_destroy(trans); | |
525 | return PTR_ERR(flow); | |
526 | } | |
527 | ||
528 | nft_trans_flow_rule(trans) = flow; | |
529 | } | |
530 | ||
ee01d542 AB |
531 | err = nf_tables_delrule_deactivate(ctx, rule); |
532 | if (err < 0) { | |
533 | nft_trans_destroy(trans); | |
534 | return err; | |
535 | } | |
f6ac8585 | 536 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
537 | |
538 | return 0; | |
539 | } | |
540 | ||
541 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
542 | { | |
543 | struct nft_rule *rule; | |
544 | int err; | |
545 | ||
546 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
547 | if (!nft_is_active_next(ctx->net, rule)) |
548 | continue; | |
549 | ||
ee01d542 AB |
550 | err = nft_delrule(ctx, rule); |
551 | if (err < 0) | |
552 | return err; | |
553 | } | |
554 | return 0; | |
555 | } | |
556 | ||
123b9961 PNA |
557 | static int __nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
558 | struct nft_set *set, | |
559 | const struct nft_set_desc *desc) | |
ee01d542 AB |
560 | { |
561 | struct nft_trans *trans; | |
562 | ||
563 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
564 | if (trans == NULL) | |
565 | return -ENOMEM; | |
566 | ||
123b9961 | 567 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] && !desc) { |
ee01d542 AB |
568 | nft_trans_set_id(trans) = |
569 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 570 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
571 | } |
572 | nft_trans_set(trans) = set; | |
123b9961 PNA |
573 | if (desc) { |
574 | nft_trans_set_update(trans) = true; | |
575 | nft_trans_set_gc_int(trans) = desc->gc_int; | |
576 | nft_trans_set_timeout(trans) = desc->timeout; | |
96b2ef9b | 577 | nft_trans_set_size(trans) = desc->size; |
123b9961 | 578 | } |
0854db2a | 579 | nft_trans_commit_list_add_tail(ctx->net, trans); |
ee01d542 AB |
580 | |
581 | return 0; | |
582 | } | |
583 | ||
123b9961 PNA |
584 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
585 | struct nft_set *set) | |
586 | { | |
587 | return __nft_trans_set_add(ctx, msg_type, set, NULL); | |
588 | } | |
589 | ||
628bd3e4 PNA |
590 | static int nft_mapelem_deactivate(const struct nft_ctx *ctx, |
591 | struct nft_set *set, | |
592 | const struct nft_set_iter *iter, | |
593 | struct nft_set_elem *elem) | |
594 | { | |
595 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
600 | struct nft_set_elem_catchall { | |
601 | struct list_head list; | |
602 | struct rcu_head rcu; | |
603 | void *elem; | |
604 | }; | |
605 | ||
606 | static void nft_map_catchall_deactivate(const struct nft_ctx *ctx, | |
607 | struct nft_set *set) | |
608 | { | |
609 | u8 genmask = nft_genmask_next(ctx->net); | |
610 | struct nft_set_elem_catchall *catchall; | |
611 | struct nft_set_elem elem; | |
612 | struct nft_set_ext *ext; | |
613 | ||
614 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
615 | ext = nft_set_elem_ext(set, catchall->elem); | |
616 | if (!nft_set_elem_active(ext, genmask)) | |
617 | continue; | |
618 | ||
619 | elem.priv = catchall->elem; | |
620 | nft_setelem_data_deactivate(ctx->net, set, &elem); | |
621 | break; | |
622 | } | |
623 | } | |
624 | ||
625 | static void nft_map_deactivate(const struct nft_ctx *ctx, struct nft_set *set) | |
626 | { | |
627 | struct nft_set_iter iter = { | |
628 | .genmask = nft_genmask_next(ctx->net), | |
629 | .fn = nft_mapelem_deactivate, | |
630 | }; | |
631 | ||
632 | set->ops->walk(ctx, set, &iter); | |
633 | WARN_ON_ONCE(iter.err); | |
634 | ||
635 | nft_map_catchall_deactivate(ctx, set); | |
636 | } | |
637 | ||
cd5125d8 | 638 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
639 | { |
640 | int err; | |
641 | ||
642 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
643 | if (err < 0) | |
644 | return err; | |
645 | ||
628bd3e4 PNA |
646 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
647 | nft_map_deactivate(ctx, set); | |
648 | ||
37a9cc52 | 649 | nft_deactivate_next(ctx->net, set); |
1689f259 | 650 | nft_use_dec(&ctx->table->use); |
ee01d542 AB |
651 | |
652 | return err; | |
653 | } | |
654 | ||
e5009240 PNA |
655 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
656 | struct nft_object *obj) | |
657 | { | |
658 | struct nft_trans *trans; | |
659 | ||
660 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
661 | if (trans == NULL) | |
662 | return -ENOMEM; | |
663 | ||
664 | if (msg_type == NFT_MSG_NEWOBJ) | |
665 | nft_activate_next(ctx->net, obj); | |
666 | ||
667 | nft_trans_obj(trans) = obj; | |
0854db2a | 668 | nft_trans_commit_list_add_tail(ctx->net, trans); |
e5009240 PNA |
669 | |
670 | return 0; | |
671 | } | |
672 | ||
673 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
674 | { | |
675 | int err; | |
676 | ||
677 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
678 | if (err < 0) | |
679 | return err; | |
680 | ||
681 | nft_deactivate_next(ctx->net, obj); | |
1689f259 | 682 | nft_use_dec(&ctx->table->use); |
e5009240 PNA |
683 | |
684 | return err; | |
685 | } | |
686 | ||
3b49e2e9 PNA |
687 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
688 | struct nft_flowtable *flowtable) | |
689 | { | |
690 | struct nft_trans *trans; | |
691 | ||
692 | trans = nft_trans_alloc(ctx, msg_type, | |
693 | sizeof(struct nft_trans_flowtable)); | |
694 | if (trans == NULL) | |
695 | return -ENOMEM; | |
696 | ||
697 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
698 | nft_activate_next(ctx->net, flowtable); | |
699 | ||
2c9e4559 | 700 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); |
3b49e2e9 | 701 | nft_trans_flowtable(trans) = flowtable; |
0854db2a | 702 | nft_trans_commit_list_add_tail(ctx->net, trans); |
3b49e2e9 PNA |
703 | |
704 | return 0; | |
705 | } | |
706 | ||
707 | static int nft_delflowtable(struct nft_ctx *ctx, | |
708 | struct nft_flowtable *flowtable) | |
709 | { | |
710 | int err; | |
711 | ||
712 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
713 | if (err < 0) | |
714 | return err; | |
715 | ||
716 | nft_deactivate_next(ctx->net, flowtable); | |
1689f259 | 717 | nft_use_dec(&ctx->table->use); |
3b49e2e9 PNA |
718 | |
719 | return err; | |
720 | } | |
721 | ||
34cc9e52 PNA |
722 | static void __nft_reg_track_clobber(struct nft_regs_track *track, u8 dreg) |
723 | { | |
724 | int i; | |
725 | ||
726 | for (i = track->regs[dreg].num_reg; i > 0; i--) | |
727 | __nft_reg_track_cancel(track, dreg - i); | |
728 | } | |
729 | ||
730 | static void __nft_reg_track_update(struct nft_regs_track *track, | |
731 | const struct nft_expr *expr, | |
732 | u8 dreg, u8 num_reg) | |
733 | { | |
734 | track->regs[dreg].selector = expr; | |
735 | track->regs[dreg].bitwise = NULL; | |
736 | track->regs[dreg].num_reg = num_reg; | |
737 | } | |
738 | ||
739 | void nft_reg_track_update(struct nft_regs_track *track, | |
740 | const struct nft_expr *expr, u8 dreg, u8 len) | |
741 | { | |
742 | unsigned int regcount; | |
743 | int i; | |
744 | ||
745 | __nft_reg_track_clobber(track, dreg); | |
746 | ||
747 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
748 | for (i = 0; i < regcount; i++, dreg++) | |
749 | __nft_reg_track_update(track, expr, dreg, i); | |
750 | } | |
751 | EXPORT_SYMBOL_GPL(nft_reg_track_update); | |
752 | ||
753 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len) | |
754 | { | |
755 | unsigned int regcount; | |
756 | int i; | |
757 | ||
758 | __nft_reg_track_clobber(track, dreg); | |
759 | ||
760 | regcount = DIV_ROUND_UP(len, NFT_REG32_SIZE); | |
761 | for (i = 0; i < regcount; i++, dreg++) | |
762 | __nft_reg_track_cancel(track, dreg); | |
763 | } | |
764 | EXPORT_SYMBOL_GPL(nft_reg_track_cancel); | |
765 | ||
766 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg) | |
767 | { | |
768 | track->regs[dreg].selector = NULL; | |
769 | track->regs[dreg].bitwise = NULL; | |
770 | track->regs[dreg].num_reg = 0; | |
771 | } | |
772 | EXPORT_SYMBOL_GPL(__nft_reg_track_cancel); | |
773 | ||
96518518 PM |
774 | /* |
775 | * Tables | |
776 | */ | |
777 | ||
36596dad | 778 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 779 | const struct nlattr *nla, |
6001a930 | 780 | u8 family, u8 genmask, u32 nlpid) |
96518518 | 781 | { |
0854db2a | 782 | struct nftables_pernet *nft_net; |
96518518 PM |
783 | struct nft_table *table; |
784 | ||
cac20fcd PNA |
785 | if (nla == NULL) |
786 | return ERR_PTR(-EINVAL); | |
787 | ||
d59d2f82 | 788 | nft_net = nft_pernet(net); |
0854db2a FW |
789 | list_for_each_entry_rcu(table, &nft_net->tables, list, |
790 | lockdep_is_held(&nft_net->commit_mutex)) { | |
f2a6d766 | 791 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 792 | table->family == family && |
6001a930 PNA |
793 | nft_active_genmask(table, genmask)) { |
794 | if (nft_table_has_owner(table) && | |
53479909 | 795 | nlpid && table->nlpid != nlpid) |
6001a930 PNA |
796 | return ERR_PTR(-EPERM); |
797 | ||
96518518 | 798 | return table; |
6001a930 | 799 | } |
96518518 | 800 | } |
cac20fcd PNA |
801 | |
802 | return ERR_PTR(-ENOENT); | |
96518518 PM |
803 | } |
804 | ||
3ecbfd65 HS |
805 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
806 | const struct nlattr *nla, | |
e31f072f | 807 | u8 genmask, u32 nlpid) |
3ecbfd65 | 808 | { |
0854db2a | 809 | struct nftables_pernet *nft_net; |
3ecbfd65 HS |
810 | struct nft_table *table; |
811 | ||
d59d2f82 | 812 | nft_net = nft_pernet(net); |
0854db2a | 813 | list_for_each_entry(table, &nft_net->tables, list) { |
3ecbfd65 | 814 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && |
e31f072f PNA |
815 | nft_active_genmask(table, genmask)) { |
816 | if (nft_table_has_owner(table) && | |
817 | nlpid && table->nlpid != nlpid) | |
818 | return ERR_PTR(-EPERM); | |
819 | ||
3ecbfd65 | 820 | return table; |
e31f072f | 821 | } |
3ecbfd65 | 822 | } |
3ecbfd65 HS |
823 | |
824 | return ERR_PTR(-ENOENT); | |
825 | } | |
826 | ||
96518518 PM |
827 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
828 | { | |
829 | return ++table->hgenerator; | |
830 | } | |
831 | ||
32537e91 | 832 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 833 | |
82603549 PNA |
834 | static const struct nft_chain_type * |
835 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
836 | { | |
837 | if (family >= NFPROTO_NUMPROTO || | |
838 | type >= NFT_CHAIN_T_MAX) | |
839 | return NULL; | |
840 | ||
841 | return chain_type[family][type]; | |
842 | } | |
843 | ||
32537e91 | 844 | static const struct nft_chain_type * |
1ea26cca | 845 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 846 | { |
82603549 | 847 | const struct nft_chain_type *type; |
9370761c PNA |
848 | int i; |
849 | ||
baae3e62 | 850 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
851 | type = __nft_chain_type_get(family, i); |
852 | if (!type) | |
853 | continue; | |
854 | if (!nla_strcmp(nla, type->name)) | |
855 | return type; | |
9370761c | 856 | } |
baae3e62 | 857 | return NULL; |
9370761c PNA |
858 | } |
859 | ||
eb014de4 PNA |
860 | struct nft_module_request { |
861 | struct list_head list; | |
862 | char module[MODULE_NAME_LEN]; | |
863 | bool done; | |
864 | }; | |
865 | ||
452238e8 | 866 | #ifdef CONFIG_MODULES |
cefa31a9 FW |
867 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, |
868 | ...) | |
452238e8 FW |
869 | { |
870 | char module_name[MODULE_NAME_LEN]; | |
0854db2a | 871 | struct nftables_pernet *nft_net; |
eb014de4 | 872 | struct nft_module_request *req; |
452238e8 FW |
873 | va_list args; |
874 | int ret; | |
875 | ||
452238e8 FW |
876 | va_start(args, fmt); |
877 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
878 | va_end(args); | |
9332d27d | 879 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 880 | return 0; |
452238e8 | 881 | |
d59d2f82 | 882 | nft_net = nft_pernet(net); |
0854db2a | 883 | list_for_each_entry(req, &nft_net->module_list, list) { |
eb014de4 PNA |
884 | if (!strcmp(req->module, module_name)) { |
885 | if (req->done) | |
886 | return 0; | |
ec7470b8 | 887 | |
eb014de4 PNA |
888 | /* A request to load this module already exists. */ |
889 | return -EAGAIN; | |
890 | } | |
891 | } | |
892 | ||
893 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
894 | if (!req) | |
895 | return -ENOMEM; | |
896 | ||
897 | req->done = false; | |
8556bceb | 898 | strscpy(req->module, module_name, MODULE_NAME_LEN); |
0854db2a | 899 | list_add_tail(&req->list, &nft_net->module_list); |
eb014de4 PNA |
900 | |
901 | return -EAGAIN; | |
452238e8 | 902 | } |
cefa31a9 | 903 | EXPORT_SYMBOL_GPL(nft_request_module); |
452238e8 FW |
904 | #endif |
905 | ||
f102d66b FW |
906 | static void lockdep_nfnl_nft_mutex_not_held(void) |
907 | { | |
908 | #ifdef CONFIG_PROVE_LOCKING | |
c0700dfa FW |
909 | if (debug_locks) |
910 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
f102d66b FW |
911 | #endif |
912 | } | |
913 | ||
32537e91 | 914 | static const struct nft_chain_type * |
452238e8 FW |
915 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
916 | u8 family, bool autoload) | |
9370761c | 917 | { |
32537e91 | 918 | const struct nft_chain_type *type; |
9370761c | 919 | |
1ea26cca | 920 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
921 | if (type != NULL) |
922 | return type; | |
f102d66b FW |
923 | |
924 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 925 | #ifdef CONFIG_MODULES |
93b0806f | 926 | if (autoload) { |
eb014de4 PNA |
927 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
928 | nla_len(nla), | |
929 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 930 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
931 | } |
932 | #endif | |
93b0806f | 933 | return ERR_PTR(-ENOENT); |
9370761c PNA |
934 | } |
935 | ||
802b8051 PNA |
936 | static __be16 nft_base_seq(const struct net *net) |
937 | { | |
d59d2f82 | 938 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
939 | |
940 | return htons(nft_net->base_seq & 0xffff); | |
802b8051 PNA |
941 | } |
942 | ||
96518518 | 943 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
944 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
945 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 946 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 947 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
7a81575b JGG |
948 | [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY, |
949 | .len = NFT_USERDATA_MAXLEN } | |
96518518 PM |
950 | }; |
951 | ||
84d7fce6 PNA |
952 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
953 | u32 portid, u32 seq, int event, u32 flags, | |
954 | int family, const struct nft_table *table) | |
96518518 PM |
955 | { |
956 | struct nlmsghdr *nlh; | |
96518518 | 957 | |
dedb67c4 | 958 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
959 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
960 | NFNETLINK_V0, nft_base_seq(net)); | |
961 | if (!nlh) | |
96518518 PM |
962 | goto nla_put_failure; |
963 | ||
9ddf6323 | 964 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
3ecbfd65 HS |
965 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
966 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
967 | NFTA_TABLE_PAD)) | |
96518518 | 968 | goto nla_put_failure; |
28339b21 PNA |
969 | |
970 | if (event == NFT_MSG_DELTABLE) { | |
971 | nlmsg_end(skb, nlh); | |
972 | return 0; | |
973 | } | |
974 | ||
975 | if (nla_put_be32(skb, NFTA_TABLE_FLAGS, | |
976 | htonl(table->flags & NFT_TABLE_F_MASK))) | |
977 | goto nla_put_failure; | |
978 | ||
6001a930 PNA |
979 | if (nft_table_has_owner(table) && |
980 | nla_put_be32(skb, NFTA_TABLE_OWNER, htonl(table->nlpid))) | |
981 | goto nla_put_failure; | |
96518518 | 982 | |
7a81575b JGG |
983 | if (table->udata) { |
984 | if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata)) | |
985 | goto nla_put_failure; | |
986 | } | |
987 | ||
053c095a JB |
988 | nlmsg_end(skb, nlh); |
989 | return 0; | |
96518518 PM |
990 | |
991 | nla_put_failure: | |
992 | nlmsg_trim(skb, nlh); | |
993 | return -1; | |
994 | } | |
995 | ||
67cc570e PNA |
996 | struct nftnl_skb_parms { |
997 | bool report; | |
998 | }; | |
999 | #define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb)) | |
1000 | ||
1001 | static void nft_notify_enqueue(struct sk_buff *skb, bool report, | |
1002 | struct list_head *notify_list) | |
1003 | { | |
1004 | NFT_CB(skb).report = report; | |
1005 | list_add_tail(&skb->list, notify_list); | |
1006 | } | |
1007 | ||
25e94a99 | 1008 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 | 1009 | { |
0854db2a | 1010 | struct nftables_pernet *nft_net; |
96518518 | 1011 | struct sk_buff *skb; |
6fb721cf | 1012 | u16 flags = 0; |
96518518 PM |
1013 | int err; |
1014 | ||
128ad332 PNA |
1015 | if (!ctx->report && |
1016 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1017 | return; |
96518518 | 1018 | |
96518518 PM |
1019 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1020 | if (skb == NULL) | |
1021 | goto err; | |
1022 | ||
6fb721cf PNA |
1023 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1024 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1025 | ||
84d7fce6 | 1026 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1027 | event, flags, ctx->family, ctx->table); |
96518518 PM |
1028 | if (err < 0) { |
1029 | kfree_skb(skb); | |
1030 | goto err; | |
1031 | } | |
1032 | ||
d59d2f82 | 1033 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1034 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1035 | return; |
96518518 | 1036 | err: |
25e94a99 | 1037 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1038 | } |
1039 | ||
1040 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
1041 | struct netlink_callback *cb) | |
1042 | { | |
1043 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
0854db2a | 1044 | struct nftables_pernet *nft_net; |
96518518 PM |
1045 | const struct nft_table *table; |
1046 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1047 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1048 | int family = nfmsg->nfgen_family; |
1049 | ||
e688a7f8 | 1050 | rcu_read_lock(); |
d59d2f82 | 1051 | nft_net = nft_pernet(net); |
34002783 | 1052 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1053 | |
0854db2a | 1054 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1055 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1056 | continue; |
1057 | ||
36596dad PNA |
1058 | if (idx < s_idx) |
1059 | goto cont; | |
1060 | if (idx > s_idx) | |
1061 | memset(&cb->args[1], 0, | |
1062 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1063 | if (!nft_is_active(net, table)) | |
1064 | continue; | |
1065 | if (nf_tables_fill_table_info(skb, net, | |
1066 | NETLINK_CB(cb->skb).portid, | |
1067 | cb->nlh->nlmsg_seq, | |
1068 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 1069 | table->family, table) < 0) |
36596dad PNA |
1070 | goto done; |
1071 | ||
1072 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 1073 | cont: |
36596dad | 1074 | idx++; |
96518518 PM |
1075 | } |
1076 | done: | |
e688a7f8 | 1077 | rcu_read_unlock(); |
96518518 PM |
1078 | cb->args[0] = idx; |
1079 | return skb->len; | |
1080 | } | |
1081 | ||
d9adf22a FW |
1082 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
1083 | const struct nlmsghdr *nlh, | |
1084 | struct netlink_dump_control *c) | |
1085 | { | |
1086 | int err; | |
1087 | ||
1088 | if (!try_module_get(THIS_MODULE)) | |
1089 | return -EINVAL; | |
1090 | ||
1091 | rcu_read_unlock(); | |
1092 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
1093 | rcu_read_lock(); | |
1094 | module_put(THIS_MODULE); | |
1095 | ||
1096 | return err; | |
1097 | } | |
1098 | ||
1099 | /* called with rcu_read_lock held */ | |
797d4980 PNA |
1100 | static int nf_tables_gettable(struct sk_buff *skb, const struct nfnl_info *info, |
1101 | const struct nlattr * const nla[]) | |
96518518 | 1102 | { |
797d4980 PNA |
1103 | struct netlink_ext_ack *extack = info->extack; |
1104 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1105 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1106 | const struct nft_table *table; |
797d4980 | 1107 | struct net *net = info->net; |
96518518 | 1108 | struct sk_buff *skb2; |
96518518 PM |
1109 | int err; |
1110 | ||
797d4980 | 1111 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1112 | struct netlink_dump_control c = { |
1113 | .dump = nf_tables_dump_tables, | |
d9adf22a | 1114 | .module = THIS_MODULE, |
96518518 | 1115 | }; |
d9adf22a | 1116 | |
797d4980 | 1117 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1118 | } |
1119 | ||
6001a930 | 1120 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask, 0); |
36dd1bcc PNA |
1121 | if (IS_ERR(table)) { |
1122 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 1123 | return PTR_ERR(table); |
36dd1bcc | 1124 | } |
96518518 | 1125 | |
d9adf22a | 1126 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1127 | if (!skb2) |
1128 | return -ENOMEM; | |
1129 | ||
84d7fce6 | 1130 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 PNA |
1131 | info->nlh->nlmsg_seq, NFT_MSG_NEWTABLE, |
1132 | 0, family, table); | |
96518518 | 1133 | if (err < 0) |
ee921183 | 1134 | goto err_fill_table_info; |
96518518 | 1135 | |
ee921183 | 1136 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1137 | |
ee921183 | 1138 | err_fill_table_info: |
96518518 PM |
1139 | kfree_skb(skb2); |
1140 | return err; | |
1141 | } | |
1142 | ||
c9c17211 | 1143 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
1144 | { |
1145 | struct nft_chain *chain; | |
1146 | u32 i = 0; | |
1147 | ||
1148 | list_for_each_entry(chain, &table->chains, list) { | |
1149 | if (!nft_is_active_next(net, chain)) | |
1150 | continue; | |
f323d954 | 1151 | if (!nft_is_base_chain(chain)) |
10435c11 F |
1152 | continue; |
1153 | ||
1154 | if (cnt && i++ == cnt) | |
1155 | break; | |
1156 | ||
1e9451cb | 1157 | nf_tables_unregister_hook(net, table, chain); |
10435c11 F |
1158 | } |
1159 | } | |
1160 | ||
c9c17211 | 1161 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
1162 | { |
1163 | struct nft_chain *chain; | |
1164 | int err, i = 0; | |
1165 | ||
1166 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1167 | if (!nft_is_active_next(net, chain)) |
1168 | continue; | |
f323d954 | 1169 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
1170 | continue; |
1171 | ||
1e9451cb | 1172 | err = nf_tables_register_hook(net, table, chain); |
9ddf6323 | 1173 | if (err < 0) |
d54725cd | 1174 | goto err_register_hooks; |
9ddf6323 PNA |
1175 | |
1176 | i++; | |
1177 | } | |
1178 | return 0; | |
d54725cd PNA |
1179 | |
1180 | err_register_hooks: | |
10435c11 | 1181 | if (i) |
c9c17211 | 1182 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
1183 | return err; |
1184 | } | |
1185 | ||
c9c17211 | 1186 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 1187 | { |
179d9ba5 | 1188 | table->flags &= ~NFT_TABLE_F_DORMANT; |
c9c17211 | 1189 | nft_table_disable(net, table, 0); |
179d9ba5 | 1190 | table->flags |= NFT_TABLE_F_DORMANT; |
9ddf6323 PNA |
1191 | } |
1192 | ||
179d9ba5 PNA |
1193 | #define __NFT_TABLE_F_INTERNAL (NFT_TABLE_F_MASK + 1) |
1194 | #define __NFT_TABLE_F_WAS_DORMANT (__NFT_TABLE_F_INTERNAL << 0) | |
1195 | #define __NFT_TABLE_F_WAS_AWAKEN (__NFT_TABLE_F_INTERNAL << 1) | |
1196 | #define __NFT_TABLE_F_UPDATE (__NFT_TABLE_F_WAS_DORMANT | \ | |
1197 | __NFT_TABLE_F_WAS_AWAKEN) | |
0ce7cf41 | 1198 | |
e1aaca93 | 1199 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 1200 | { |
55dd6f93 | 1201 | struct nft_trans *trans; |
e1aaca93 | 1202 | u32 flags; |
179d9ba5 | 1203 | int ret; |
9ddf6323 | 1204 | |
e1aaca93 PNA |
1205 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
1206 | return 0; | |
9ddf6323 | 1207 | |
e1aaca93 | 1208 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
6001a930 PNA |
1209 | if (flags & ~NFT_TABLE_F_MASK) |
1210 | return -EOPNOTSUPP; | |
e1aaca93 | 1211 | |
63283dd2 PNA |
1212 | if (flags == ctx->table->flags) |
1213 | return 0; | |
1214 | ||
9cc0001a PNA |
1215 | if ((nft_table_has_owner(ctx->table) && |
1216 | !(flags & NFT_TABLE_F_OWNER)) || | |
1217 | (!nft_table_has_owner(ctx->table) && | |
1218 | flags & NFT_TABLE_F_OWNER)) | |
1219 | return -EOPNOTSUPP; | |
1220 | ||
55dd6f93 PNA |
1221 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
1222 | sizeof(struct nft_trans_table)); | |
1223 | if (trans == NULL) | |
1224 | return -ENOMEM; | |
9ddf6323 | 1225 | |
e1aaca93 PNA |
1226 | if ((flags & NFT_TABLE_F_DORMANT) && |
1227 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
179d9ba5 PNA |
1228 | ctx->table->flags |= NFT_TABLE_F_DORMANT; |
1229 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) | |
1230 | ctx->table->flags |= __NFT_TABLE_F_WAS_AWAKEN; | |
e1aaca93 PNA |
1231 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
1232 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
179d9ba5 PNA |
1233 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
1234 | if (!(ctx->table->flags & __NFT_TABLE_F_UPDATE)) { | |
1235 | ret = nf_tables_table_enable(ctx->net, ctx->table); | |
1236 | if (ret < 0) | |
1237 | goto err_register_hooks; | |
1238 | ||
1239 | ctx->table->flags |= __NFT_TABLE_F_WAS_DORMANT; | |
1240 | } | |
9ddf6323 PNA |
1241 | } |
1242 | ||
55dd6f93 | 1243 | nft_trans_table_update(trans) = true; |
0854db2a | 1244 | nft_trans_commit_list_add_tail(ctx->net, trans); |
179d9ba5 | 1245 | |
55dd6f93 | 1246 | return 0; |
179d9ba5 PNA |
1247 | |
1248 | err_register_hooks: | |
55dd6f93 | 1249 | nft_trans_destroy(trans); |
9ddf6323 PNA |
1250 | return ret; |
1251 | } | |
1252 | ||
1b2470e5 FW |
1253 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
1254 | { | |
1255 | const char *name = data; | |
1256 | ||
1257 | return jhash(name, strlen(name), seed); | |
1258 | } | |
1259 | ||
1260 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
1261 | { | |
1262 | const struct nft_chain *chain = data; | |
1263 | ||
1264 | return nft_chain_hash(chain->name, 0, seed); | |
1265 | } | |
1266 | ||
1267 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
1268 | const void *ptr) | |
1269 | { | |
1270 | const struct nft_chain *chain = ptr; | |
1271 | const char *name = arg->key; | |
1272 | ||
1273 | return strcmp(chain->name, name); | |
1274 | } | |
1275 | ||
4d44175a FW |
1276 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
1277 | { | |
1278 | const struct nft_object_hash_key *k = data; | |
1279 | ||
1280 | seed ^= hash_ptr(k->table, 32); | |
1281 | ||
1282 | return jhash(k->name, strlen(k->name), seed); | |
1283 | } | |
1284 | ||
1285 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
1286 | { | |
1287 | const struct nft_object *obj = data; | |
1288 | ||
1289 | return nft_objname_hash(&obj->key, 0, seed); | |
1290 | } | |
1291 | ||
1292 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
1293 | const void *ptr) | |
1294 | { | |
1295 | const struct nft_object_hash_key *k = arg->key; | |
1296 | const struct nft_object *obj = ptr; | |
1297 | ||
1298 | if (obj->key.table != k->table) | |
1299 | return -1; | |
1300 | ||
1301 | return strcmp(obj->key.name, k->name); | |
1302 | } | |
1303 | ||
f1082dd3 PS |
1304 | static bool nft_supported_family(u8 family) |
1305 | { | |
1306 | return false | |
1307 | #ifdef CONFIG_NF_TABLES_INET | |
1308 | || family == NFPROTO_INET | |
1309 | #endif | |
1310 | #ifdef CONFIG_NF_TABLES_IPV4 | |
1311 | || family == NFPROTO_IPV4 | |
1312 | #endif | |
1313 | #ifdef CONFIG_NF_TABLES_ARP | |
1314 | || family == NFPROTO_ARP | |
1315 | #endif | |
1316 | #ifdef CONFIG_NF_TABLES_NETDEV | |
1317 | || family == NFPROTO_NETDEV | |
1318 | #endif | |
1319 | #if IS_ENABLED(CONFIG_NF_TABLES_BRIDGE) | |
1320 | || family == NFPROTO_BRIDGE | |
1321 | #endif | |
1322 | #ifdef CONFIG_NF_TABLES_IPV6 | |
1323 | || family == NFPROTO_IPV6 | |
1324 | #endif | |
1325 | ; | |
1326 | } | |
1327 | ||
7dab8ee3 PNA |
1328 | static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info, |
1329 | const struct nlattr * const nla[]) | |
96518518 | 1330 | { |
7dab8ee3 | 1331 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
1332 | struct netlink_ext_ack *extack = info->extack; |
1333 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1334 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1335 | struct net *net = info->net; |
36dd1bcc PNA |
1336 | const struct nlattr *attr; |
1337 | struct nft_table *table; | |
e1aaca93 | 1338 | struct nft_ctx ctx; |
7a81575b | 1339 | u32 flags = 0; |
55dd6f93 | 1340 | int err; |
96518518 | 1341 | |
f1082dd3 PS |
1342 | if (!nft_supported_family(family)) |
1343 | return -EOPNOTSUPP; | |
1344 | ||
0854db2a | 1345 | lockdep_assert_held(&nft_net->commit_mutex); |
36dd1bcc | 1346 | attr = nla[NFTA_TABLE_NAME]; |
6001a930 PNA |
1347 | table = nft_table_lookup(net, attr, family, genmask, |
1348 | NETLINK_CB(skb).portid); | |
96518518 PM |
1349 | if (IS_ERR(table)) { |
1350 | if (PTR_ERR(table) != -ENOENT) | |
1351 | return PTR_ERR(table); | |
1a28ad74 | 1352 | } else { |
7dab8ee3 | 1353 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 1354 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1355 | return -EEXIST; |
36dd1bcc | 1356 | } |
7dab8ee3 | 1357 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 | 1358 | return -EOPNOTSUPP; |
e1aaca93 | 1359 | |
7dab8ee3 PNA |
1360 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
1361 | ||
e1aaca93 | 1362 | return nf_tables_updtable(&ctx); |
96518518 PM |
1363 | } |
1364 | ||
c5c1f975 PM |
1365 | if (nla[NFTA_TABLE_FLAGS]) { |
1366 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
6001a930 PNA |
1367 | if (flags & ~NFT_TABLE_F_MASK) |
1368 | return -EOPNOTSUPP; | |
c5c1f975 PM |
1369 | } |
1370 | ||
ffdb210e | 1371 | err = -ENOMEM; |
33758c89 | 1372 | table = kzalloc(sizeof(*table), GFP_KERNEL_ACCOUNT); |
ffdb210e | 1373 | if (table == NULL) |
98319cb9 | 1374 | goto err_kzalloc; |
96518518 | 1375 | |
00c320f9 | 1376 | table->validate_state = NFT_VALIDATE_SKIP; |
33758c89 | 1377 | table->name = nla_strdup(attr, GFP_KERNEL_ACCOUNT); |
e46abbcc | 1378 | if (table->name == NULL) |
98319cb9 | 1379 | goto err_strdup; |
e46abbcc | 1380 | |
7a81575b | 1381 | if (nla[NFTA_TABLE_USERDATA]) { |
33758c89 | 1382 | table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL_ACCOUNT); |
7a81575b JGG |
1383 | if (table->udata == NULL) |
1384 | goto err_table_udata; | |
1385 | ||
85db827a | 1386 | table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]); |
7a81575b JGG |
1387 | } |
1388 | ||
1b2470e5 FW |
1389 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1390 | if (err) | |
1391 | goto err_chain_ht; | |
1392 | ||
96518518 | 1393 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1394 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1395 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1396 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1397 | table->family = family; |
c5c1f975 | 1398 | table->flags = flags; |
ab482c6b | 1399 | table->handle = ++nft_net->table_handle; |
6001a930 PNA |
1400 | if (table->flags & NFT_TABLE_F_OWNER) |
1401 | table->nlpid = NETLINK_CB(skb).portid; | |
9ddf6323 | 1402 | |
7dab8ee3 | 1403 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
55dd6f93 | 1404 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1405 | if (err < 0) |
98319cb9 | 1406 | goto err_trans; |
ffdb210e | 1407 | |
0854db2a | 1408 | list_add_tail_rcu(&table->list, &nft_net->tables); |
96518518 | 1409 | return 0; |
98319cb9 | 1410 | err_trans: |
1b2470e5 FW |
1411 | rhltable_destroy(&table->chains_ht); |
1412 | err_chain_ht: | |
7a81575b JGG |
1413 | kfree(table->udata); |
1414 | err_table_udata: | |
e46abbcc | 1415 | kfree(table->name); |
98319cb9 | 1416 | err_strdup: |
ffdb210e | 1417 | kfree(table); |
98319cb9 | 1418 | err_kzalloc: |
ffdb210e | 1419 | return err; |
96518518 PM |
1420 | } |
1421 | ||
b9ac12ef AB |
1422 | static int nft_flush_table(struct nft_ctx *ctx) |
1423 | { | |
3b49e2e9 | 1424 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1425 | struct nft_chain *chain, *nc; |
e5009240 | 1426 | struct nft_object *obj, *ne; |
b9ac12ef | 1427 | struct nft_set *set, *ns; |
3b49e2e9 | 1428 | int err; |
b9ac12ef | 1429 | |
a2f18db0 | 1430 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1431 | if (!nft_is_active_next(ctx->net, chain)) |
1432 | continue; | |
1433 | ||
d0e2c7de PNA |
1434 | if (nft_chain_is_bound(chain)) |
1435 | continue; | |
1436 | ||
b9ac12ef AB |
1437 | ctx->chain = chain; |
1438 | ||
1439 | err = nft_delrule_by_chain(ctx); | |
1440 | if (err < 0) | |
1441 | goto out; | |
b9ac12ef AB |
1442 | } |
1443 | ||
1444 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1445 | if (!nft_is_active_next(ctx->net, set)) |
1446 | continue; | |
1447 | ||
408070d6 | 1448 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1449 | !list_empty(&set->bindings)) |
1450 | continue; | |
1451 | ||
1452 | err = nft_delset(ctx, set); | |
1453 | if (err < 0) | |
1454 | goto out; | |
1455 | } | |
1456 | ||
3b49e2e9 | 1457 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1458 | if (!nft_is_active_next(ctx->net, flowtable)) |
1459 | continue; | |
1460 | ||
3b49e2e9 PNA |
1461 | err = nft_delflowtable(ctx, flowtable); |
1462 | if (err < 0) | |
1463 | goto out; | |
1464 | } | |
1465 | ||
e5009240 | 1466 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1467 | if (!nft_is_active_next(ctx->net, obj)) |
1468 | continue; | |
1469 | ||
e5009240 PNA |
1470 | err = nft_delobj(ctx, obj); |
1471 | if (err < 0) | |
1472 | goto out; | |
1473 | } | |
1474 | ||
a2f18db0 | 1475 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1476 | if (!nft_is_active_next(ctx->net, chain)) |
1477 | continue; | |
1478 | ||
d0e2c7de PNA |
1479 | if (nft_chain_is_bound(chain)) |
1480 | continue; | |
1481 | ||
a2f18db0 PNA |
1482 | ctx->chain = chain; |
1483 | ||
1484 | err = nft_delchain(ctx); | |
1485 | if (err < 0) | |
1486 | goto out; | |
1487 | } | |
1488 | ||
b9ac12ef AB |
1489 | err = nft_deltable(ctx); |
1490 | out: | |
1491 | return err; | |
1492 | } | |
1493 | ||
1494 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1495 | { | |
d59d2f82 | 1496 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
b9ac12ef | 1497 | const struct nlattr * const *nla = ctx->nla; |
d59d2f82 | 1498 | struct nft_table *table, *nt; |
b9ac12ef AB |
1499 | int err = 0; |
1500 | ||
0854db2a | 1501 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
98319cb9 | 1502 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1503 | continue; |
1504 | ||
98319cb9 | 1505 | ctx->family = table->family; |
f2a6d766 | 1506 | |
36596dad PNA |
1507 | if (!nft_is_active_next(ctx->net, table)) |
1508 | continue; | |
b9ac12ef | 1509 | |
6001a930 PNA |
1510 | if (nft_table_has_owner(table) && table->nlpid != ctx->portid) |
1511 | continue; | |
1512 | ||
36596dad PNA |
1513 | if (nla[NFTA_TABLE_NAME] && |
1514 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1515 | continue; | |
b9ac12ef | 1516 | |
36596dad PNA |
1517 | ctx->table = table; |
1518 | ||
1519 | err = nft_flush_table(ctx); | |
1520 | if (err < 0) | |
1521 | goto out; | |
b9ac12ef AB |
1522 | } |
1523 | out: | |
1524 | return err; | |
1525 | } | |
1526 | ||
7dab8ee3 PNA |
1527 | static int nf_tables_deltable(struct sk_buff *skb, const struct nfnl_info *info, |
1528 | const struct nlattr * const nla[]) | |
96518518 | 1529 | { |
7dab8ee3 PNA |
1530 | struct netlink_ext_ack *extack = info->extack; |
1531 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 1532 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 1533 | struct net *net = info->net; |
36dd1bcc PNA |
1534 | const struct nlattr *attr; |
1535 | struct nft_table *table; | |
55dd6f93 | 1536 | struct nft_ctx ctx; |
96518518 | 1537 | |
7dab8ee3 | 1538 | nft_ctx_init(&ctx, net, skb, info->nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1539 | if (family == AF_UNSPEC || |
1540 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1541 | return nft_flush(&ctx, family); |
1542 | ||
36dd1bcc PNA |
1543 | if (nla[NFTA_TABLE_HANDLE]) { |
1544 | attr = nla[NFTA_TABLE_HANDLE]; | |
e31f072f PNA |
1545 | table = nft_table_lookup_byhandle(net, attr, genmask, |
1546 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
1547 | } else { |
1548 | attr = nla[NFTA_TABLE_NAME]; | |
6001a930 PNA |
1549 | table = nft_table_lookup(net, attr, family, genmask, |
1550 | NETLINK_CB(skb).portid); | |
36dd1bcc | 1551 | } |
3ecbfd65 | 1552 | |
36dd1bcc | 1553 | if (IS_ERR(table)) { |
f80a612d FFM |
1554 | if (PTR_ERR(table) == -ENOENT && |
1555 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYTABLE) | |
1556 | return 0; | |
1557 | ||
36dd1bcc | 1558 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 1559 | return PTR_ERR(table); |
36dd1bcc | 1560 | } |
96518518 | 1561 | |
7dab8ee3 | 1562 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
a8278400 PNA |
1563 | table->use > 0) |
1564 | return -EBUSY; | |
1565 | ||
98319cb9 | 1566 | ctx.family = family; |
b9ac12ef | 1567 | ctx.table = table; |
55dd6f93 | 1568 | |
b9ac12ef | 1569 | return nft_flush_table(&ctx); |
96518518 PM |
1570 | } |
1571 | ||
55dd6f93 PNA |
1572 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1573 | { | |
fa5950e4 FW |
1574 | if (WARN_ON(ctx->table->use > 0)) |
1575 | return; | |
4fefee57 | 1576 | |
1b2470e5 | 1577 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1578 | kfree(ctx->table->name); |
bc7a7082 | 1579 | kfree(ctx->table->udata); |
55dd6f93 | 1580 | kfree(ctx->table); |
55dd6f93 PNA |
1581 | } |
1582 | ||
cc07eeb0 | 1583 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1584 | { |
96518518 | 1585 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1586 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1587 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1588 | return; | |
96518518 | 1589 | } |
9370761c | 1590 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1591 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1592 | } |
9370761c | 1593 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1594 | |
32537e91 | 1595 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1596 | { |
96518518 | 1597 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1598 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1599 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1600 | } | |
9370761c | 1601 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1602 | |
1603 | /* | |
1604 | * Chains | |
1605 | */ | |
1606 | ||
1607 | static struct nft_chain * | |
cac20fcd | 1608 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1609 | { |
1610 | struct nft_chain *chain; | |
1611 | ||
1612 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1613 | if (chain->handle == handle && |
1614 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1615 | return chain; |
1616 | } | |
1617 | ||
1618 | return ERR_PTR(-ENOENT); | |
1619 | } | |
1620 | ||
4d44175a | 1621 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1622 | { |
1623 | #ifdef CONFIG_PROVE_LOCKING | |
d59d2f82 | 1624 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
1625 | |
1626 | return lockdep_is_held(&nft_net->commit_mutex); | |
f102d66b FW |
1627 | #else |
1628 | return true; | |
1629 | #endif | |
1630 | } | |
1631 | ||
1632 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1633 | struct nft_table *table, | |
cac20fcd | 1634 | const struct nlattr *nla, u8 genmask) |
96518518 | 1635 | { |
1b2470e5 FW |
1636 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1637 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1638 | struct nft_chain *chain; |
1639 | ||
1640 | if (nla == NULL) | |
1641 | return ERR_PTR(-EINVAL); | |
1642 | ||
872f6903 | 1643 | nla_strscpy(search, nla, sizeof(search)); |
96518518 | 1644 | |
1b2470e5 | 1645 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1646 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1647 | |
1648 | chain = ERR_PTR(-ENOENT); | |
1649 | rcu_read_lock(); | |
1650 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1651 | if (!list) | |
1652 | goto out_unlock; | |
1653 | ||
1654 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1655 | if (nft_active_genmask(chain, genmask)) | |
1656 | goto out_unlock; | |
1657 | } | |
1658 | chain = ERR_PTR(-ENOENT); | |
1659 | out_unlock: | |
1660 | rcu_read_unlock(); | |
1661 | return chain; | |
96518518 PM |
1662 | } |
1663 | ||
1664 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1665 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1666 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1667 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1668 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1669 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1670 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1671 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1672 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1673 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1674 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1675 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1676 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
002f2176 JGG |
1677 | [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY, |
1678 | .len = NFT_USERDATA_MAXLEN }, | |
96518518 PM |
1679 | }; |
1680 | ||
1681 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1682 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1683 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1684 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1685 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1686 | }; |
1687 | ||
0ca743a5 PNA |
1688 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1689 | { | |
1690 | struct nft_stats *cpu_stats, total; | |
1691 | struct nlattr *nest; | |
ce355e20 ED |
1692 | unsigned int seq; |
1693 | u64 pkts, bytes; | |
0ca743a5 PNA |
1694 | int cpu; |
1695 | ||
edbd82c5 FW |
1696 | if (!stats) |
1697 | return 0; | |
1698 | ||
0ca743a5 PNA |
1699 | memset(&total, 0, sizeof(total)); |
1700 | for_each_possible_cpu(cpu) { | |
1701 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 | 1702 | do { |
d120d1a6 | 1703 | seq = u64_stats_fetch_begin(&cpu_stats->syncp); |
ce355e20 ED |
1704 | pkts = cpu_stats->pkts; |
1705 | bytes = cpu_stats->bytes; | |
d120d1a6 | 1706 | } while (u64_stats_fetch_retry(&cpu_stats->syncp, seq)); |
ce355e20 ED |
1707 | total.pkts += pkts; |
1708 | total.bytes += bytes; | |
0ca743a5 | 1709 | } |
ae0be8de | 1710 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1711 | if (nest == NULL) |
1712 | goto nla_put_failure; | |
1713 | ||
b46f6ded ND |
1714 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1715 | NFTA_COUNTER_PAD) || | |
1716 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1717 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1718 | goto nla_put_failure; |
1719 | ||
1720 | nla_nest_end(skb, nest); | |
1721 | return 0; | |
1722 | ||
1723 | nla_put_failure: | |
1724 | return -ENOSPC; | |
1725 | } | |
1726 | ||
d54725cd | 1727 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
b9703ed4 PNA |
1728 | const struct nft_base_chain *basechain, |
1729 | const struct list_head *hook_list) | |
d54725cd PNA |
1730 | { |
1731 | const struct nf_hook_ops *ops = &basechain->ops; | |
1732 | struct nft_hook *hook, *first = NULL; | |
1733 | struct nlattr *nest, *nest_devs; | |
1734 | int n = 0; | |
1735 | ||
1736 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1737 | if (nest == NULL) | |
1738 | goto nla_put_failure; | |
1739 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1740 | goto nla_put_failure; | |
1741 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1742 | goto nla_put_failure; | |
1743 | ||
d3519cb8 | 1744 | if (nft_base_chain_netdev(family, ops->hooknum)) { |
d54725cd | 1745 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); |
bd058763 GI |
1746 | if (!nest_devs) |
1747 | goto nla_put_failure; | |
b9703ed4 PNA |
1748 | |
1749 | if (!hook_list) | |
1750 | hook_list = &basechain->hook_list; | |
1751 | ||
1752 | list_for_each_entry(hook, hook_list, list) { | |
d54725cd PNA |
1753 | if (!first) |
1754 | first = hook; | |
1755 | ||
1756 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1757 | hook->ops.dev->name)) | |
1758 | goto nla_put_failure; | |
1759 | n++; | |
1760 | } | |
1761 | nla_nest_end(skb, nest_devs); | |
1762 | ||
1763 | if (n == 1 && | |
1764 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1765 | goto nla_put_failure; | |
1766 | } | |
1767 | nla_nest_end(skb, nest); | |
1768 | ||
1769 | return 0; | |
1770 | nla_put_failure: | |
1771 | return -1; | |
1772 | } | |
1773 | ||
84d7fce6 PNA |
1774 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1775 | u32 portid, u32 seq, int event, u32 flags, | |
1776 | int family, const struct nft_table *table, | |
b9703ed4 PNA |
1777 | const struct nft_chain *chain, |
1778 | const struct list_head *hook_list) | |
96518518 PM |
1779 | { |
1780 | struct nlmsghdr *nlh; | |
96518518 | 1781 | |
dedb67c4 | 1782 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
1783 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
1784 | NFNETLINK_V0, nft_base_seq(net)); | |
1785 | if (!nlh) | |
96518518 PM |
1786 | goto nla_put_failure; |
1787 | ||
28339b21 PNA |
1788 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name) || |
1789 | nla_put_string(skb, NFTA_CHAIN_NAME, chain->name) || | |
1790 | nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), | |
b46f6ded | 1791 | NFTA_CHAIN_PAD)) |
96518518 | 1792 | goto nla_put_failure; |
28339b21 | 1793 | |
7d937b10 | 1794 | if (event == NFT_MSG_DELCHAIN && !hook_list) { |
28339b21 PNA |
1795 | nlmsg_end(skb, nlh); |
1796 | return 0; | |
1797 | } | |
96518518 | 1798 | |
f323d954 | 1799 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1800 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1801 | struct nft_stats __percpu *stats; |
0ca743a5 | 1802 | |
b9703ed4 | 1803 | if (nft_dump_basechain_hook(skb, family, basechain, hook_list)) |
2cbce139 | 1804 | goto nla_put_failure; |
9370761c | 1805 | |
0ca743a5 PNA |
1806 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1807 | htonl(basechain->policy))) | |
1808 | goto nla_put_failure; | |
1809 | ||
baae3e62 PM |
1810 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1811 | goto nla_put_failure; | |
0ca743a5 | 1812 | |
edbd82c5 FW |
1813 | stats = rcu_dereference_check(basechain->stats, |
1814 | lockdep_commit_lock_is_held(net)); | |
1815 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1816 | goto nla_put_failure; |
96518518 PM |
1817 | } |
1818 | ||
d0e2c7de PNA |
1819 | if (chain->flags && |
1820 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags))) | |
1821 | goto nla_put_failure; | |
1822 | ||
0ca743a5 PNA |
1823 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1824 | goto nla_put_failure; | |
1825 | ||
002f2176 JGG |
1826 | if (chain->udata && |
1827 | nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata)) | |
1828 | goto nla_put_failure; | |
1829 | ||
053c095a JB |
1830 | nlmsg_end(skb, nlh); |
1831 | return 0; | |
96518518 PM |
1832 | |
1833 | nla_put_failure: | |
1834 | nlmsg_trim(skb, nlh); | |
1835 | return -1; | |
1836 | } | |
1837 | ||
b9703ed4 PNA |
1838 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event, |
1839 | const struct list_head *hook_list) | |
96518518 | 1840 | { |
0854db2a | 1841 | struct nftables_pernet *nft_net; |
96518518 | 1842 | struct sk_buff *skb; |
6fb721cf | 1843 | u16 flags = 0; |
96518518 PM |
1844 | int err; |
1845 | ||
128ad332 PNA |
1846 | if (!ctx->report && |
1847 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1848 | return; |
96518518 | 1849 | |
96518518 PM |
1850 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1851 | if (skb == NULL) | |
1852 | goto err; | |
1853 | ||
6fb721cf PNA |
1854 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
1855 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
1856 | ||
84d7fce6 | 1857 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
6fb721cf | 1858 | event, flags, ctx->family, ctx->table, |
b9703ed4 | 1859 | ctx->chain, hook_list); |
96518518 PM |
1860 | if (err < 0) { |
1861 | kfree_skb(skb); | |
1862 | goto err; | |
1863 | } | |
1864 | ||
d59d2f82 | 1865 | nft_net = nft_pernet(ctx->net); |
0854db2a | 1866 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 1867 | return; |
96518518 | 1868 | err: |
25e94a99 | 1869 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1870 | } |
1871 | ||
1872 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1873 | struct netlink_callback *cb) | |
1874 | { | |
1875 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 | 1876 | unsigned int idx = 0, s_idx = cb->args[0]; |
99633ab2 | 1877 | struct net *net = sock_net(skb->sk); |
96518518 | 1878 | int family = nfmsg->nfgen_family; |
0854db2a | 1879 | struct nftables_pernet *nft_net; |
d59d2f82 PNA |
1880 | const struct nft_table *table; |
1881 | const struct nft_chain *chain; | |
96518518 | 1882 | |
e688a7f8 | 1883 | rcu_read_lock(); |
d59d2f82 | 1884 | nft_net = nft_pernet(net); |
34002783 | 1885 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 1886 | |
0854db2a | 1887 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 1888 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1889 | continue; |
1890 | ||
36596dad PNA |
1891 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1892 | if (idx < s_idx) | |
1893 | goto cont; | |
1894 | if (idx > s_idx) | |
1895 | memset(&cb->args[1], 0, | |
1896 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1897 | if (!nft_is_active(net, chain)) | |
1898 | continue; | |
1899 | if (nf_tables_fill_chain_info(skb, net, | |
1900 | NETLINK_CB(cb->skb).portid, | |
1901 | cb->nlh->nlmsg_seq, | |
1902 | NFT_MSG_NEWCHAIN, | |
1903 | NLM_F_MULTI, | |
98319cb9 | 1904 | table->family, table, |
b9703ed4 | 1905 | chain, NULL) < 0) |
36596dad | 1906 | goto done; |
38e029f1 | 1907 | |
36596dad | 1908 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1909 | cont: |
36596dad | 1910 | idx++; |
96518518 PM |
1911 | } |
1912 | } | |
1913 | done: | |
e688a7f8 | 1914 | rcu_read_unlock(); |
96518518 PM |
1915 | cb->args[0] = idx; |
1916 | return skb->len; | |
1917 | } | |
1918 | ||
d9adf22a | 1919 | /* called with rcu_read_lock held */ |
797d4980 PNA |
1920 | static int nf_tables_getchain(struct sk_buff *skb, const struct nfnl_info *info, |
1921 | const struct nlattr * const nla[]) | |
96518518 | 1922 | { |
797d4980 PNA |
1923 | struct netlink_ext_ack *extack = info->extack; |
1924 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 1925 | u8 family = info->nfmsg->nfgen_family; |
96518518 | 1926 | const struct nft_chain *chain; |
797d4980 | 1927 | struct net *net = info->net; |
1b2470e5 | 1928 | struct nft_table *table; |
96518518 | 1929 | struct sk_buff *skb2; |
96518518 PM |
1930 | int err; |
1931 | ||
797d4980 | 1932 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 PM |
1933 | struct netlink_dump_control c = { |
1934 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1935 | .module = THIS_MODULE, |
96518518 | 1936 | }; |
d9adf22a | 1937 | |
797d4980 | 1938 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
1939 | } |
1940 | ||
6001a930 | 1941 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, 0); |
36dd1bcc PNA |
1942 | if (IS_ERR(table)) { |
1943 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1944 | return PTR_ERR(table); |
36dd1bcc | 1945 | } |
96518518 | 1946 | |
f102d66b | 1947 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1948 | if (IS_ERR(chain)) { |
1949 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1950 | return PTR_ERR(chain); |
36dd1bcc | 1951 | } |
96518518 | 1952 | |
d9adf22a | 1953 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1954 | if (!skb2) |
1955 | return -ENOMEM; | |
1956 | ||
84d7fce6 | 1957 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 1958 | info->nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, |
b9703ed4 | 1959 | 0, family, table, chain, NULL); |
96518518 | 1960 | if (err < 0) |
ee921183 | 1961 | goto err_fill_chain_info; |
96518518 | 1962 | |
ee921183 | 1963 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 1964 | |
ee921183 | 1965 | err_fill_chain_info: |
96518518 PM |
1966 | kfree_skb(skb2); |
1967 | return err; | |
1968 | } | |
1969 | ||
0ca743a5 PNA |
1970 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1971 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1972 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1973 | }; | |
1974 | ||
ff3cd7b3 | 1975 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1976 | { |
1977 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1978 | struct nft_stats __percpu *newstats; | |
1979 | struct nft_stats *stats; | |
1980 | int err; | |
1981 | ||
8cb08174 JB |
1982 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1983 | nft_counter_policy, NULL); | |
0ca743a5 | 1984 | if (err < 0) |
ff3cd7b3 | 1985 | return ERR_PTR(err); |
0ca743a5 PNA |
1986 | |
1987 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1988 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1989 | |
ce355e20 | 1990 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1991 | if (newstats == NULL) |
ff3cd7b3 | 1992 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1993 | |
1994 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1995 | * are not exposed to userspace. | |
1996 | */ | |
e8781f70 | 1997 | preempt_disable(); |
0ca743a5 PNA |
1998 | stats = this_cpu_ptr(newstats); |
1999 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
2000 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 2001 | preempt_enable(); |
0ca743a5 | 2002 | |
ff3cd7b3 PNA |
2003 | return newstats; |
2004 | } | |
2005 | ||
53315ac6 | 2006 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 2007 | { |
53315ac6 | 2008 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 2009 | |
53315ac6 | 2010 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
2011 | return; |
2012 | ||
b685b534 PM |
2013 | nft_trans_chain_stats(trans) = |
2014 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
2015 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
2016 | |
2017 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 2018 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
2019 | } |
2020 | ||
0cbc06b3 FW |
2021 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
2022 | { | |
2c865a8a PNA |
2023 | struct nft_rule_blob *g0 = rcu_dereference_raw(chain->blob_gen_0); |
2024 | struct nft_rule_blob *g1 = rcu_dereference_raw(chain->blob_gen_1); | |
0cbc06b3 FW |
2025 | |
2026 | if (g0 != g1) | |
2027 | kvfree(g1); | |
2028 | kvfree(g0); | |
2029 | ||
2030 | /* should be NULL either via abort or via successful commit */ | |
2c865a8a PNA |
2031 | WARN_ON_ONCE(chain->blob_next); |
2032 | kvfree(chain->blob_next); | |
0cbc06b3 FW |
2033 | } |
2034 | ||
d0e2c7de | 2035 | void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 2036 | { |
43a605f2 | 2037 | struct nft_chain *chain = ctx->chain; |
d54725cd | 2038 | struct nft_hook *hook, *next; |
43a605f2 | 2039 | |
fa5950e4 FW |
2040 | if (WARN_ON(chain->use > 0)) |
2041 | return; | |
91c7b38d | 2042 | |
0cbc06b3 FW |
2043 | /* no concurrent access possible anymore */ |
2044 | nf_tables_chain_free_chain_rules(chain); | |
2045 | ||
f323d954 | 2046 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
2047 | struct nft_base_chain *basechain = nft_base_chain(chain); |
2048 | ||
d3519cb8 | 2049 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
d54725cd PNA |
2050 | list_for_each_entry_safe(hook, next, |
2051 | &basechain->hook_list, list) { | |
2052 | list_del_rcu(&hook->list); | |
2053 | kfree_rcu(hook, rcu); | |
2054 | } | |
2055 | } | |
2cbce139 | 2056 | module_put(basechain->type->owner); |
4c05ec47 | 2057 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 2058 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
2059 | free_percpu(rcu_dereference_raw(basechain->stats)); |
2060 | } | |
b7263e07 | 2061 | kfree(chain->name); |
002f2176 | 2062 | kfree(chain->udata); |
2cbce139 | 2063 | kfree(basechain); |
91c7b38d | 2064 | } else { |
b7263e07 | 2065 | kfree(chain->name); |
002f2176 | 2066 | kfree(chain->udata); |
91c7b38d PNA |
2067 | kfree(chain); |
2068 | } | |
2069 | } | |
2070 | ||
3f0465a9 PNA |
2071 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
2072 | const struct nlattr *attr) | |
2073 | { | |
2074 | struct net_device *dev; | |
2075 | char ifname[IFNAMSIZ]; | |
2076 | struct nft_hook *hook; | |
2077 | int err; | |
2078 | ||
33758c89 | 2079 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL_ACCOUNT); |
3f0465a9 PNA |
2080 | if (!hook) { |
2081 | err = -ENOMEM; | |
2082 | goto err_hook_alloc; | |
2083 | } | |
2084 | ||
872f6903 | 2085 | nla_strscpy(ifname, attr, IFNAMSIZ); |
42f1c271 PNA |
2086 | /* nf_tables_netdev_event() is called under rtnl_mutex, this is |
2087 | * indirectly serializing all the other holders of the commit_mutex with | |
2088 | * the rtnl_mutex. | |
2089 | */ | |
3f0465a9 PNA |
2090 | dev = __dev_get_by_name(net, ifname); |
2091 | if (!dev) { | |
2092 | err = -ENOENT; | |
2093 | goto err_hook_dev; | |
2094 | } | |
2095 | hook->ops.dev = dev; | |
2096 | ||
2097 | return hook; | |
2098 | ||
2099 | err_hook_dev: | |
2100 | kfree(hook); | |
2101 | err_hook_alloc: | |
2102 | return ERR_PTR(err); | |
2103 | } | |
2104 | ||
abadb2f8 PNA |
2105 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
2106 | const struct nft_hook *this) | |
b75a3e83 PNA |
2107 | { |
2108 | struct nft_hook *hook; | |
2109 | ||
2110 | list_for_each_entry(hook, hook_list, list) { | |
2111 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 2112 | return hook; |
b75a3e83 PNA |
2113 | } |
2114 | ||
abadb2f8 | 2115 | return NULL; |
b75a3e83 PNA |
2116 | } |
2117 | ||
3f0465a9 PNA |
2118 | static int nf_tables_parse_netdev_hooks(struct net *net, |
2119 | const struct nlattr *attr, | |
c3c060ad PNA |
2120 | struct list_head *hook_list, |
2121 | struct netlink_ext_ack *extack) | |
3f0465a9 PNA |
2122 | { |
2123 | struct nft_hook *hook, *next; | |
2124 | const struct nlattr *tmp; | |
2125 | int rem, n = 0, err; | |
2126 | ||
2127 | nla_for_each_nested(tmp, attr, rem) { | |
2128 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
2129 | err = -EINVAL; | |
2130 | goto err_hook; | |
2131 | } | |
2132 | ||
2133 | hook = nft_netdev_hook_alloc(net, tmp); | |
2134 | if (IS_ERR(hook)) { | |
c3c060ad | 2135 | NL_SET_BAD_ATTR(extack, tmp); |
3f0465a9 PNA |
2136 | err = PTR_ERR(hook); |
2137 | goto err_hook; | |
2138 | } | |
b75a3e83 | 2139 | if (nft_hook_list_find(hook_list, hook)) { |
c3c060ad | 2140 | NL_SET_BAD_ATTR(extack, tmp); |
cd77e75b | 2141 | kfree(hook); |
b75a3e83 PNA |
2142 | err = -EEXIST; |
2143 | goto err_hook; | |
2144 | } | |
3f0465a9 PNA |
2145 | list_add_tail(&hook->list, hook_list); |
2146 | n++; | |
2147 | ||
cb662ac6 | 2148 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
2149 | err = -EFBIG; |
2150 | goto err_hook; | |
2151 | } | |
2152 | } | |
3f0465a9 PNA |
2153 | |
2154 | return 0; | |
2155 | ||
2156 | err_hook: | |
2157 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
2158 | list_del(&hook->list); | |
2159 | kfree(hook); | |
2160 | } | |
2161 | return err; | |
2162 | } | |
2163 | ||
508f8ccd PNA |
2164 | struct nft_chain_hook { |
2165 | u32 num; | |
84ba7dd7 | 2166 | s32 priority; |
32537e91 | 2167 | const struct nft_chain_type *type; |
d54725cd | 2168 | struct list_head list; |
508f8ccd PNA |
2169 | }; |
2170 | ||
207296f1 | 2171 | static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[], |
c3c060ad | 2172 | struct list_head *hook_list, |
207296f1 | 2173 | struct netlink_ext_ack *extack, u32 flags) |
d54725cd PNA |
2174 | { |
2175 | struct nft_hook *hook; | |
2176 | int err; | |
2177 | ||
2178 | if (tb[NFTA_HOOK_DEV]) { | |
2179 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
c3c060ad PNA |
2180 | if (IS_ERR(hook)) { |
2181 | NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]); | |
d54725cd | 2182 | return PTR_ERR(hook); |
c3c060ad | 2183 | } |
d54725cd PNA |
2184 | |
2185 | list_add_tail(&hook->list, hook_list); | |
2186 | } else if (tb[NFTA_HOOK_DEVS]) { | |
2187 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
c3c060ad | 2188 | hook_list, extack); |
d54725cd PNA |
2189 | if (err < 0) |
2190 | return err; | |
05abe445 | 2191 | |
d54725cd PNA |
2192 | } |
2193 | ||
207296f1 PNA |
2194 | if (flags & NFT_CHAIN_HW_OFFLOAD && |
2195 | list_empty(hook_list)) | |
2196 | return -EINVAL; | |
2197 | ||
d54725cd PNA |
2198 | return 0; |
2199 | } | |
2200 | ||
508f8ccd | 2201 | static int nft_chain_parse_hook(struct net *net, |
b9703ed4 | 2202 | struct nft_base_chain *basechain, |
508f8ccd | 2203 | const struct nlattr * const nla[], |
36596dad | 2204 | struct nft_chain_hook *hook, u8 family, |
207296f1 | 2205 | u32 flags, struct netlink_ext_ack *extack) |
508f8ccd | 2206 | { |
d59d2f82 | 2207 | struct nftables_pernet *nft_net = nft_pernet(net); |
508f8ccd | 2208 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; |
32537e91 | 2209 | const struct nft_chain_type *type; |
508f8ccd PNA |
2210 | int err; |
2211 | ||
0854db2a | 2212 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b FW |
2213 | lockdep_nfnl_nft_mutex_not_held(); |
2214 | ||
8cb08174 JB |
2215 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
2216 | nla[NFTA_CHAIN_HOOK], | |
2217 | nft_hook_policy, NULL); | |
508f8ccd PNA |
2218 | if (err < 0) |
2219 | return err; | |
2220 | ||
b9703ed4 PNA |
2221 | if (!basechain) { |
2222 | if (!ha[NFTA_HOOK_HOOKNUM] || | |
8509f62b PNA |
2223 | !ha[NFTA_HOOK_PRIORITY]) { |
2224 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
2225 | return -ENOENT; | |
2226 | } | |
508f8ccd | 2227 | |
b9703ed4 PNA |
2228 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); |
2229 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); | |
508f8ccd | 2230 | |
b9703ed4 PNA |
2231 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
2232 | if (!type) | |
2233 | return -EOPNOTSUPP; | |
82603549 | 2234 | |
b9703ed4 PNA |
2235 | if (nla[NFTA_CHAIN_TYPE]) { |
2236 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], | |
2237 | family, true); | |
2238 | if (IS_ERR(type)) { | |
2239 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
2240 | return PTR_ERR(type); | |
2241 | } | |
983c4fcb | 2242 | } |
b9703ed4 PNA |
2243 | if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
2244 | return -EOPNOTSUPP; | |
84ba7dd7 | 2245 | |
b9703ed4 PNA |
2246 | if (type->type == NFT_CHAIN_T_NAT && |
2247 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
2248 | return -EOPNOTSUPP; | |
2249 | } else { | |
2250 | if (ha[NFTA_HOOK_HOOKNUM]) { | |
2251 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
2252 | if (hook->num != basechain->ops.hooknum) | |
2253 | return -EOPNOTSUPP; | |
2254 | } | |
2255 | if (ha[NFTA_HOOK_PRIORITY]) { | |
2256 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); | |
2257 | if (hook->priority != basechain->ops.priority) | |
2258 | return -EOPNOTSUPP; | |
2259 | } | |
2260 | ||
2261 | type = basechain->type; | |
2262 | } | |
84ba7dd7 | 2263 | |
983c4fcb PNA |
2264 | if (!try_module_get(type->owner)) { |
2265 | if (nla[NFTA_CHAIN_TYPE]) | |
2266 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TYPE]); | |
508f8ccd | 2267 | return -ENOENT; |
983c4fcb | 2268 | } |
508f8ccd PNA |
2269 | |
2270 | hook->type = type; | |
2271 | ||
d54725cd | 2272 | INIT_LIST_HEAD(&hook->list); |
d3519cb8 | 2273 | if (nft_base_chain_netdev(family, hook->num)) { |
207296f1 | 2274 | err = nft_chain_parse_netdev(net, ha, &hook->list, extack, flags); |
d54725cd | 2275 | if (err < 0) { |
508f8ccd | 2276 | module_put(type->owner); |
d54725cd | 2277 | return err; |
508f8ccd | 2278 | } |
d54725cd | 2279 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
2280 | module_put(type->owner); |
2281 | return -EOPNOTSUPP; | |
2282 | } | |
2283 | ||
2284 | return 0; | |
2285 | } | |
2286 | ||
2287 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
2288 | { | |
d54725cd PNA |
2289 | struct nft_hook *h, *next; |
2290 | ||
2291 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
2292 | list_del(&h->list); | |
2293 | kfree(h); | |
2294 | } | |
508f8ccd | 2295 | module_put(hook->type->owner); |
508f8ccd PNA |
2296 | } |
2297 | ||
e38fbfa9 | 2298 | static void nft_last_rule(const struct nft_chain *chain, const void *ptr) |
0cbc06b3 | 2299 | { |
e38fbfa9 FW |
2300 | struct nft_rule_dp_last *lrule; |
2301 | ||
2302 | BUILD_BUG_ON(offsetof(struct nft_rule_dp_last, end) != 0); | |
2c865a8a | 2303 | |
e38fbfa9 FW |
2304 | lrule = (struct nft_rule_dp_last *)ptr; |
2305 | lrule->end.is_last = 1; | |
2306 | lrule->chain = chain; | |
2c865a8a PNA |
2307 | /* blob size does not include the trailer rule */ |
2308 | } | |
2309 | ||
e38fbfa9 FW |
2310 | static struct nft_rule_blob *nf_tables_chain_alloc_rules(const struct nft_chain *chain, |
2311 | unsigned int size) | |
2c865a8a PNA |
2312 | { |
2313 | struct nft_rule_blob *blob; | |
2314 | ||
2c865a8a | 2315 | if (size > INT_MAX) |
0cbc06b3 FW |
2316 | return NULL; |
2317 | ||
e38fbfa9 FW |
2318 | size += sizeof(struct nft_rule_blob) + sizeof(struct nft_rule_dp_last); |
2319 | ||
33758c89 | 2320 | blob = kvmalloc(size, GFP_KERNEL_ACCOUNT); |
2c865a8a | 2321 | if (!blob) |
0cbc06b3 FW |
2322 | return NULL; |
2323 | ||
2c865a8a | 2324 | blob->size = 0; |
e38fbfa9 | 2325 | nft_last_rule(chain, blob->data); |
0cbc06b3 | 2326 | |
2c865a8a | 2327 | return blob; |
0cbc06b3 FW |
2328 | } |
2329 | ||
d54725cd PNA |
2330 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
2331 | const struct nft_chain_hook *hook, | |
2332 | struct nft_chain *chain) | |
2333 | { | |
c5c6accd FW |
2334 | ops->pf = family; |
2335 | ops->hooknum = hook->num; | |
2336 | ops->priority = hook->priority; | |
2337 | ops->priv = chain; | |
2338 | ops->hook = hook->type->hooks[ops->hooknum]; | |
2339 | ops->hook_ops_type = NF_HOOK_OP_NF_TABLES; | |
d54725cd PNA |
2340 | } |
2341 | ||
2342 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
2343 | struct nft_chain_hook *hook, u32 flags) | |
2344 | { | |
2345 | struct nft_chain *chain; | |
2346 | struct nft_hook *h; | |
2347 | ||
2348 | basechain->type = hook->type; | |
2349 | INIT_LIST_HEAD(&basechain->hook_list); | |
2350 | chain = &basechain->chain; | |
2351 | ||
d3519cb8 | 2352 | if (nft_base_chain_netdev(family, hook->num)) { |
d54725cd PNA |
2353 | list_splice_init(&hook->list, &basechain->hook_list); |
2354 | list_for_each_entry(h, &basechain->hook_list, list) | |
2355 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
d54725cd | 2356 | } |
b9703ed4 | 2357 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); |
d54725cd | 2358 | |
67c49de4 | 2359 | chain->flags |= NFT_CHAIN_BASE | flags; |
d54725cd PNA |
2360 | basechain->policy = NF_ACCEPT; |
2361 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
77972a36 PNA |
2362 | !nft_chain_offload_support(basechain)) { |
2363 | list_splice_init(&basechain->hook_list, &hook->list); | |
d54725cd | 2364 | return -EOPNOTSUPP; |
77972a36 | 2365 | } |
d54725cd PNA |
2366 | |
2367 | flow_block_init(&basechain->flow_block); | |
2368 | ||
2369 | return 0; | |
2370 | } | |
2371 | ||
4bedf9ee | 2372 | int nft_chain_add(struct nft_table *table, struct nft_chain *chain) |
04b7db41 PNA |
2373 | { |
2374 | int err; | |
2375 | ||
2376 | err = rhltable_insert_key(&table->chains_ht, chain->name, | |
2377 | &chain->rhlhead, nft_chain_ht_params); | |
2378 | if (err) | |
2379 | return err; | |
2380 | ||
2381 | list_add_tail_rcu(&chain->list, &table->chains); | |
2382 | ||
2383 | return 0; | |
2384 | } | |
2385 | ||
d0e2c7de PNA |
2386 | static u64 chain_id; |
2387 | ||
4035285f | 2388 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
983c4fcb PNA |
2389 | u8 policy, u32 flags, |
2390 | struct netlink_ext_ack *extack) | |
4035285f PNA |
2391 | { |
2392 | const struct nlattr * const *nla = ctx->nla; | |
2393 | struct nft_table *table = ctx->table; | |
4035285f | 2394 | struct nft_base_chain *basechain; |
4035285f | 2395 | struct net *net = ctx->net; |
d0e2c7de | 2396 | char name[NFT_NAME_MAXLEN]; |
2c865a8a | 2397 | struct nft_rule_blob *blob; |
66293c46 | 2398 | struct nft_trans *trans; |
4035285f | 2399 | struct nft_chain *chain; |
4035285f PNA |
2400 | int err; |
2401 | ||
4035285f | 2402 | if (nla[NFTA_CHAIN_HOOK]) { |
921ebde3 | 2403 | struct nft_stats __percpu *stats = NULL; |
b9703ed4 | 2404 | struct nft_chain_hook hook = {}; |
4035285f | 2405 | |
d0e2c7de PNA |
2406 | if (flags & NFT_CHAIN_BINDING) |
2407 | return -EOPNOTSUPP; | |
2408 | ||
207296f1 | 2409 | err = nft_chain_parse_hook(net, NULL, nla, &hook, family, flags, |
b9703ed4 | 2410 | extack); |
4035285f PNA |
2411 | if (err < 0) |
2412 | return err; | |
2413 | ||
33758c89 | 2414 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2415 | if (basechain == NULL) { |
2416 | nft_chain_release_hook(&hook); | |
2417 | return -ENOMEM; | |
2418 | } | |
d54725cd | 2419 | chain = &basechain->chain; |
4035285f PNA |
2420 | |
2421 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2422 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2423 | if (IS_ERR(stats)) { | |
2424 | nft_chain_release_hook(&hook); | |
2425 | kfree(basechain); | |
2426 | return PTR_ERR(stats); | |
2427 | } | |
4c05ec47 | 2428 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
2429 | } |
2430 | ||
d54725cd PNA |
2431 | err = nft_basechain_init(basechain, family, &hook, flags); |
2432 | if (err < 0) { | |
2433 | nft_chain_release_hook(&hook); | |
2434 | kfree(basechain); | |
9a4d6dd5 | 2435 | free_percpu(stats); |
d54725cd PNA |
2436 | return err; |
2437 | } | |
921ebde3 TH |
2438 | if (stats) |
2439 | static_branch_inc(&nft_counters_enabled); | |
4035285f | 2440 | } else { |
d0e2c7de PNA |
2441 | if (flags & NFT_CHAIN_BASE) |
2442 | return -EINVAL; | |
2443 | if (flags & NFT_CHAIN_HW_OFFLOAD) | |
2444 | return -EOPNOTSUPP; | |
2445 | ||
33758c89 | 2446 | chain = kzalloc(sizeof(*chain), GFP_KERNEL_ACCOUNT); |
4035285f PNA |
2447 | if (chain == NULL) |
2448 | return -ENOMEM; | |
d0e2c7de PNA |
2449 | |
2450 | chain->flags = flags; | |
4035285f | 2451 | } |
43a605f2 PNA |
2452 | ctx->chain = chain; |
2453 | ||
4035285f PNA |
2454 | INIT_LIST_HEAD(&chain->rules); |
2455 | chain->handle = nf_tables_alloc_handle(table); | |
2456 | chain->table = table; | |
d0e2c7de PNA |
2457 | |
2458 | if (nla[NFTA_CHAIN_NAME]) { | |
33758c89 | 2459 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
d0e2c7de | 2460 | } else { |
59136aa3 FW |
2461 | if (!(flags & NFT_CHAIN_BINDING)) { |
2462 | err = -EINVAL; | |
002f2176 | 2463 | goto err_destroy_chain; |
59136aa3 | 2464 | } |
d0e2c7de PNA |
2465 | |
2466 | snprintf(name, sizeof(name), "__chain%llu", ++chain_id); | |
33758c89 | 2467 | chain->name = kstrdup(name, GFP_KERNEL_ACCOUNT); |
d0e2c7de PNA |
2468 | } |
2469 | ||
4035285f PNA |
2470 | if (!chain->name) { |
2471 | err = -ENOMEM; | |
002f2176 JGG |
2472 | goto err_destroy_chain; |
2473 | } | |
2474 | ||
2475 | if (nla[NFTA_CHAIN_USERDATA]) { | |
33758c89 | 2476 | chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL_ACCOUNT); |
002f2176 JGG |
2477 | if (chain->udata == NULL) { |
2478 | err = -ENOMEM; | |
2479 | goto err_destroy_chain; | |
2480 | } | |
2481 | chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]); | |
4035285f PNA |
2482 | } |
2483 | ||
e38fbfa9 | 2484 | blob = nf_tables_chain_alloc_rules(chain, 0); |
2c865a8a | 2485 | if (!blob) { |
0cbc06b3 | 2486 | err = -ENOMEM; |
002f2176 | 2487 | goto err_destroy_chain; |
0cbc06b3 FW |
2488 | } |
2489 | ||
2c865a8a PNA |
2490 | RCU_INIT_POINTER(chain->blob_gen_0, blob); |
2491 | RCU_INIT_POINTER(chain->blob_gen_1, blob); | |
0cbc06b3 | 2492 | |
c974a3a3 | 2493 | err = nf_tables_register_hook(net, table, chain); |
4035285f | 2494 | if (err < 0) |
002f2176 | 2495 | goto err_destroy_chain; |
4035285f | 2496 | |
1689f259 PNA |
2497 | if (!nft_use_inc(&table->use)) { |
2498 | err = -EMFILE; | |
2499 | goto err_use; | |
2500 | } | |
2501 | ||
66293c46 FW |
2502 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2503 | if (IS_ERR(trans)) { | |
2504 | err = PTR_ERR(trans); | |
002f2176 | 2505 | goto err_unregister_hook; |
1b2470e5 | 2506 | } |
4035285f | 2507 | |
ad652f38 | 2508 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2509 | if (nft_is_base_chain(chain)) |
2510 | nft_trans_chain_policy(trans) = policy; | |
2511 | ||
04b7db41 PNA |
2512 | err = nft_chain_add(table, chain); |
2513 | if (err < 0) { | |
2514 | nft_trans_destroy(trans); | |
002f2176 | 2515 | goto err_unregister_hook; |
04b7db41 PNA |
2516 | } |
2517 | ||
4035285f | 2518 | return 0; |
1689f259 | 2519 | |
002f2176 | 2520 | err_unregister_hook: |
1689f259 PNA |
2521 | nft_use_dec_restore(&table->use); |
2522 | err_use: | |
c974a3a3 | 2523 | nf_tables_unregister_hook(net, table, chain); |
002f2176 | 2524 | err_destroy_chain: |
43a605f2 | 2525 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2526 | |
2527 | return err; | |
2528 | } | |
2529 | ||
c9626a2c | 2530 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
98a381a7 PNA |
2531 | u32 flags, const struct nlattr *attr, |
2532 | struct netlink_ext_ack *extack) | |
2c4a488a PNA |
2533 | { |
2534 | const struct nlattr * const *nla = ctx->nla; | |
b9703ed4 | 2535 | struct nft_base_chain *basechain = NULL; |
2c4a488a PNA |
2536 | struct nft_table *table = ctx->table; |
2537 | struct nft_chain *chain = ctx->chain; | |
b9703ed4 | 2538 | struct nft_chain_hook hook = {}; |
2c4a488a | 2539 | struct nft_stats *stats = NULL; |
b9703ed4 | 2540 | struct nft_hook *h, *next; |
2c4a488a PNA |
2541 | struct nf_hook_ops *ops; |
2542 | struct nft_trans *trans; | |
b9703ed4 | 2543 | bool unregister = false; |
c974a3a3 | 2544 | int err; |
2c4a488a | 2545 | |
c9626a2c PNA |
2546 | if (chain->flags ^ flags) |
2547 | return -EOPNOTSUPP; | |
2548 | ||
b9703ed4 PNA |
2549 | INIT_LIST_HEAD(&hook.list); |
2550 | ||
2c4a488a | 2551 | if (nla[NFTA_CHAIN_HOOK]) { |
98a381a7 PNA |
2552 | if (!nft_is_base_chain(chain)) { |
2553 | NL_SET_BAD_ATTR(extack, attr); | |
77a92189 | 2554 | return -EEXIST; |
98a381a7 | 2555 | } |
b9703ed4 PNA |
2556 | |
2557 | basechain = nft_base_chain(chain); | |
2558 | err = nft_chain_parse_hook(ctx->net, basechain, nla, &hook, | |
207296f1 | 2559 | ctx->family, flags, extack); |
2c4a488a PNA |
2560 | if (err < 0) |
2561 | return err; | |
2562 | ||
2c4a488a PNA |
2563 | if (basechain->type != hook.type) { |
2564 | nft_chain_release_hook(&hook); | |
98a381a7 | 2565 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2566 | return -EEXIST; |
2c4a488a PNA |
2567 | } |
2568 | ||
b9703ed4 PNA |
2569 | if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) { |
2570 | list_for_each_entry_safe(h, next, &hook.list, list) { | |
2571 | h->ops.pf = basechain->ops.pf; | |
2572 | h->ops.hooknum = basechain->ops.hooknum; | |
2573 | h->ops.priority = basechain->ops.priority; | |
2574 | h->ops.priv = basechain->ops.priv; | |
2575 | h->ops.hook = basechain->ops.hook; | |
2576 | ||
2577 | if (nft_hook_list_find(&basechain->hook_list, h)) { | |
2578 | list_del(&h->list); | |
2579 | kfree(h); | |
2580 | } | |
d54725cd PNA |
2581 | } |
2582 | } else { | |
2583 | ops = &basechain->ops; | |
2584 | if (ops->hooknum != hook.num || | |
2585 | ops->priority != hook.priority) { | |
2586 | nft_chain_release_hook(&hook); | |
98a381a7 | 2587 | NL_SET_BAD_ATTR(extack, attr); |
77a92189 | 2588 | return -EEXIST; |
d54725cd | 2589 | } |
2c4a488a | 2590 | } |
2c4a488a PNA |
2591 | } |
2592 | ||
2593 | if (nla[NFTA_CHAIN_HANDLE] && | |
2594 | nla[NFTA_CHAIN_NAME]) { | |
2595 | struct nft_chain *chain2; | |
2596 | ||
f102d66b FW |
2597 | chain2 = nft_chain_lookup(ctx->net, table, |
2598 | nla[NFTA_CHAIN_NAME], genmask); | |
98a381a7 PNA |
2599 | if (!IS_ERR(chain2)) { |
2600 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
b9703ed4 PNA |
2601 | err = -EEXIST; |
2602 | goto err_hooks; | |
98a381a7 | 2603 | } |
2c4a488a PNA |
2604 | } |
2605 | ||
2606 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
b9703ed4 PNA |
2607 | if (!nft_is_base_chain(chain)) { |
2608 | err = -EOPNOTSUPP; | |
2609 | goto err_hooks; | |
2610 | } | |
2c4a488a PNA |
2611 | |
2612 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
b9703ed4 PNA |
2613 | if (IS_ERR(stats)) { |
2614 | err = PTR_ERR(stats); | |
2615 | goto err_hooks; | |
2616 | } | |
2c4a488a PNA |
2617 | } |
2618 | ||
b9703ed4 PNA |
2619 | if (!(table->flags & NFT_TABLE_F_DORMANT) && |
2620 | nft_is_base_chain(chain) && | |
2621 | !list_empty(&hook.list)) { | |
2622 | basechain = nft_base_chain(chain); | |
2623 | ops = &basechain->ops; | |
2624 | ||
2625 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) { | |
2626 | err = nft_netdev_register_hooks(ctx->net, &hook.list); | |
2627 | if (err < 0) | |
2628 | goto err_hooks; | |
2629 | } | |
2630 | } | |
2631 | ||
2632 | unregister = true; | |
c6cc94df | 2633 | err = -ENOMEM; |
2c4a488a PNA |
2634 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2635 | sizeof(struct nft_trans_chain)); | |
c6cc94df | 2636 | if (trans == NULL) |
b9703ed4 | 2637 | goto err_trans; |
2c4a488a PNA |
2638 | |
2639 | nft_trans_chain_stats(trans) = stats; | |
2640 | nft_trans_chain_update(trans) = true; | |
2641 | ||
2642 | if (nla[NFTA_CHAIN_POLICY]) | |
2643 | nft_trans_chain_policy(trans) = policy; | |
2644 | else | |
2645 | nft_trans_chain_policy(trans) = -1; | |
2646 | ||
c6cc94df FW |
2647 | if (nla[NFTA_CHAIN_HANDLE] && |
2648 | nla[NFTA_CHAIN_NAME]) { | |
d59d2f82 | 2649 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c6cc94df FW |
2650 | struct nft_trans *tmp; |
2651 | char *name; | |
2652 | ||
2653 | err = -ENOMEM; | |
33758c89 | 2654 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL_ACCOUNT); |
c6cc94df | 2655 | if (!name) |
b9703ed4 | 2656 | goto err_trans; |
c6cc94df FW |
2657 | |
2658 | err = -EEXIST; | |
0854db2a | 2659 | list_for_each_entry(tmp, &nft_net->commit_list, list) { |
c6cc94df FW |
2660 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && |
2661 | tmp->ctx.table == table && | |
2662 | nft_trans_chain_update(tmp) && | |
2663 | nft_trans_chain_name(tmp) && | |
2664 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
98a381a7 | 2665 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); |
c6cc94df | 2666 | kfree(name); |
b9703ed4 | 2667 | goto err_trans; |
c6cc94df | 2668 | } |
2c4a488a | 2669 | } |
c6cc94df FW |
2670 | |
2671 | nft_trans_chain_name(trans) = name; | |
2c4a488a | 2672 | } |
b9703ed4 PNA |
2673 | |
2674 | nft_trans_basechain(trans) = basechain; | |
2675 | INIT_LIST_HEAD(&nft_trans_chain_hooks(trans)); | |
2676 | list_splice(&hook.list, &nft_trans_chain_hooks(trans)); | |
043d2acf PNA |
2677 | if (nla[NFTA_CHAIN_HOOK]) |
2678 | module_put(hook.type->owner); | |
b9703ed4 | 2679 | |
0854db2a | 2680 | nft_trans_commit_list_add_tail(ctx->net, trans); |
2c4a488a PNA |
2681 | |
2682 | return 0; | |
b9703ed4 PNA |
2683 | |
2684 | err_trans: | |
c6cc94df FW |
2685 | free_percpu(stats); |
2686 | kfree(trans); | |
b9703ed4 PNA |
2687 | err_hooks: |
2688 | if (nla[NFTA_CHAIN_HOOK]) { | |
2689 | list_for_each_entry_safe(h, next, &hook.list, list) { | |
2690 | if (unregister) | |
2691 | nf_unregister_net_hook(ctx->net, &h->ops); | |
2692 | list_del(&h->list); | |
2693 | kfree_rcu(h, rcu); | |
2694 | } | |
2695 | module_put(hook.type->owner); | |
2696 | } | |
2697 | ||
c6cc94df | 2698 | return err; |
2c4a488a PNA |
2699 | } |
2700 | ||
837830a4 | 2701 | static struct nft_chain *nft_chain_lookup_byid(const struct net *net, |
95f466d2 | 2702 | const struct nft_table *table, |
515ad530 | 2703 | const struct nlattr *nla, u8 genmask) |
837830a4 | 2704 | { |
d59d2f82 | 2705 | struct nftables_pernet *nft_net = nft_pernet(net); |
837830a4 PNA |
2706 | u32 id = ntohl(nla_get_be32(nla)); |
2707 | struct nft_trans *trans; | |
2708 | ||
0854db2a | 2709 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
837830a4 PNA |
2710 | struct nft_chain *chain = trans->ctx.chain; |
2711 | ||
2712 | if (trans->msg_type == NFT_MSG_NEWCHAIN && | |
95f466d2 | 2713 | chain->table == table && |
515ad530 TLSC |
2714 | id == nft_trans_chain_id(trans) && |
2715 | nft_active_genmask(chain, genmask)) | |
837830a4 PNA |
2716 | return chain; |
2717 | } | |
2718 | return ERR_PTR(-ENOENT); | |
2719 | } | |
2720 | ||
7dab8ee3 PNA |
2721 | static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info, |
2722 | const struct nlattr * const nla[]) | |
96518518 | 2723 | { |
7dab8ee3 | 2724 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
2725 | struct netlink_ext_ack *extack = info->extack; |
2726 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2727 | u8 family = info->nfmsg->nfgen_family; |
74cccc3d | 2728 | struct nft_chain *chain = NULL; |
7dab8ee3 | 2729 | struct net *net = info->net; |
36dd1bcc | 2730 | const struct nlattr *attr; |
96518518 | 2731 | struct nft_table *table; |
57de2a0c | 2732 | u8 policy = NF_ACCEPT; |
4035285f | 2733 | struct nft_ctx ctx; |
96518518 | 2734 | u64 handle = 0; |
c9626a2c | 2735 | u32 flags = 0; |
96518518 | 2736 | |
0854db2a | 2737 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 2738 | |
6001a930 PNA |
2739 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2740 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2741 | if (IS_ERR(table)) { |
2742 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2743 | return PTR_ERR(table); |
36dd1bcc | 2744 | } |
96518518 | 2745 | |
96518518 | 2746 | chain = NULL; |
36dd1bcc | 2747 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2748 | |
2749 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2750 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2751 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2752 | if (IS_ERR(chain)) { |
2753 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2754 | return PTR_ERR(chain); |
36dd1bcc PNA |
2755 | } |
2756 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2757 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2758 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2759 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2760 | if (PTR_ERR(chain) != -ENOENT) { |
2761 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2762 | return PTR_ERR(chain); |
36dd1bcc | 2763 | } |
96518518 PM |
2764 | chain = NULL; |
2765 | } | |
74cccc3d PNA |
2766 | } else if (!nla[NFTA_CHAIN_ID]) { |
2767 | return -EINVAL; | |
96518518 PM |
2768 | } |
2769 | ||
57de2a0c | 2770 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2771 | if (chain != NULL && |
36dd1bcc PNA |
2772 | !nft_is_base_chain(chain)) { |
2773 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2774 | return -EOPNOTSUPP; |
36dd1bcc | 2775 | } |
d6b6cb1d PNA |
2776 | |
2777 | if (chain == NULL && | |
36dd1bcc PNA |
2778 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2779 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2780 | return -EOPNOTSUPP; |
36dd1bcc | 2781 | } |
57de2a0c | 2782 | |
8f46df18 | 2783 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2784 | switch (policy) { |
2785 | case NF_DROP: | |
2786 | case NF_ACCEPT: | |
2787 | break; | |
2788 | default: | |
2789 | return -EINVAL; | |
2790 | } | |
2791 | } | |
2792 | ||
c9626a2c PNA |
2793 | if (nla[NFTA_CHAIN_FLAGS]) |
2794 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2795 | else if (chain) |
2796 | flags = chain->flags; | |
c9626a2c | 2797 | |
c1f79a2e PNA |
2798 | if (flags & ~NFT_CHAIN_FLAGS) |
2799 | return -EOPNOTSUPP; | |
2800 | ||
7dab8ee3 | 2801 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
4035285f | 2802 | |
96518518 | 2803 | if (chain != NULL) { |
5dc52d83 PNA |
2804 | if (chain->flags & NFT_CHAIN_BINDING) |
2805 | return -EINVAL; | |
2806 | ||
7dab8ee3 | 2807 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 2808 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2809 | return -EEXIST; |
36dd1bcc | 2810 | } |
7dab8ee3 | 2811 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
2812 | return -EOPNOTSUPP; |
2813 | ||
67c49de4 | 2814 | flags |= chain->flags & NFT_CHAIN_BASE; |
98a381a7 PNA |
2815 | return nf_tables_updchain(&ctx, genmask, policy, flags, attr, |
2816 | extack); | |
96518518 PM |
2817 | } |
2818 | ||
983c4fcb | 2819 | return nf_tables_addchain(&ctx, family, genmask, policy, flags, extack); |
96518518 PM |
2820 | } |
2821 | ||
42e344f0 PS |
2822 | static int nft_delchain_hook(struct nft_ctx *ctx, |
2823 | struct nft_base_chain *basechain, | |
7d937b10 PNA |
2824 | struct netlink_ext_ack *extack) |
2825 | { | |
42e344f0 | 2826 | const struct nft_chain *chain = &basechain->chain; |
7d937b10 PNA |
2827 | const struct nlattr * const *nla = ctx->nla; |
2828 | struct nft_chain_hook chain_hook = {}; | |
7d937b10 PNA |
2829 | struct nft_hook *this, *hook; |
2830 | LIST_HEAD(chain_del_list); | |
2831 | struct nft_trans *trans; | |
2832 | int err; | |
2833 | ||
7d937b10 | 2834 | err = nft_chain_parse_hook(ctx->net, basechain, nla, &chain_hook, |
207296f1 | 2835 | ctx->family, chain->flags, extack); |
7d937b10 PNA |
2836 | if (err < 0) |
2837 | return err; | |
2838 | ||
2839 | list_for_each_entry(this, &chain_hook.list, list) { | |
2840 | hook = nft_hook_list_find(&basechain->hook_list, this); | |
2841 | if (!hook) { | |
2842 | err = -ENOENT; | |
2843 | goto err_chain_del_hook; | |
2844 | } | |
2845 | list_move(&hook->list, &chain_del_list); | |
2846 | } | |
2847 | ||
2848 | trans = nft_trans_alloc(ctx, NFT_MSG_DELCHAIN, | |
2849 | sizeof(struct nft_trans_chain)); | |
2850 | if (!trans) { | |
2851 | err = -ENOMEM; | |
2852 | goto err_chain_del_hook; | |
2853 | } | |
2854 | ||
2855 | nft_trans_basechain(trans) = basechain; | |
2856 | nft_trans_chain_update(trans) = true; | |
2857 | INIT_LIST_HEAD(&nft_trans_chain_hooks(trans)); | |
2858 | list_splice(&chain_del_list, &nft_trans_chain_hooks(trans)); | |
2859 | nft_chain_release_hook(&chain_hook); | |
2860 | ||
2861 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
2862 | ||
2863 | return 0; | |
2864 | ||
2865 | err_chain_del_hook: | |
2866 | list_splice(&chain_del_list, &basechain->hook_list); | |
2867 | nft_chain_release_hook(&chain_hook); | |
2868 | ||
2869 | return err; | |
2870 | } | |
2871 | ||
7dab8ee3 PNA |
2872 | static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info, |
2873 | const struct nlattr * const nla[]) | |
96518518 | 2874 | { |
7dab8ee3 PNA |
2875 | struct netlink_ext_ack *extack = info->extack; |
2876 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 2877 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 2878 | struct net *net = info->net; |
36dd1bcc | 2879 | const struct nlattr *attr; |
96518518 PM |
2880 | struct nft_table *table; |
2881 | struct nft_chain *chain; | |
9dee1474 | 2882 | struct nft_rule *rule; |
91c7b38d | 2883 | struct nft_ctx ctx; |
3ecbfd65 | 2884 | u64 handle; |
9dee1474 PNA |
2885 | u32 use; |
2886 | int err; | |
96518518 | 2887 | |
6001a930 PNA |
2888 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask, |
2889 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
2890 | if (IS_ERR(table)) { |
2891 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2892 | return PTR_ERR(table); |
36dd1bcc | 2893 | } |
96518518 | 2894 | |
3ecbfd65 | 2895 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2896 | attr = nla[NFTA_CHAIN_HANDLE]; |
2897 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2898 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2899 | } else { |
36dd1bcc | 2900 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2901 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2902 | } |
36dd1bcc | 2903 | if (IS_ERR(chain)) { |
f80a612d FFM |
2904 | if (PTR_ERR(chain) == -ENOENT && |
2905 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYCHAIN) | |
2906 | return 0; | |
2907 | ||
36dd1bcc | 2908 | NL_SET_BAD_ATTR(extack, attr); |
96518518 | 2909 | return PTR_ERR(chain); |
36dd1bcc | 2910 | } |
9dee1474 | 2911 | |
7d937b10 PNA |
2912 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
2913 | ||
2914 | if (nla[NFTA_CHAIN_HOOK]) { | |
2915 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
2916 | return -EOPNOTSUPP; | |
2917 | ||
42e344f0 PS |
2918 | if (nft_is_base_chain(chain)) { |
2919 | struct nft_base_chain *basechain = nft_base_chain(chain); | |
2920 | ||
2921 | if (nft_base_chain_netdev(table->family, basechain->ops.hooknum)) | |
2922 | return nft_delchain_hook(&ctx, basechain, extack); | |
2923 | } | |
7d937b10 PNA |
2924 | } |
2925 | ||
7dab8ee3 | 2926 | if (info->nlh->nlmsg_flags & NLM_F_NONREC && |
9dee1474 | 2927 | chain->use > 0) |
96518518 PM |
2928 | return -EBUSY; |
2929 | ||
9dee1474 PNA |
2930 | use = chain->use; |
2931 | list_for_each_entry(rule, &chain->rules, list) { | |
2932 | if (!nft_is_active_next(net, rule)) | |
2933 | continue; | |
2934 | use--; | |
2935 | ||
2936 | err = nft_delrule(&ctx, rule); | |
2937 | if (err < 0) | |
2938 | return err; | |
2939 | } | |
2940 | ||
2941 | /* There are rules and elements that are still holding references to us, | |
2942 | * we cannot do a recursive removal in this case. | |
2943 | */ | |
36dd1bcc PNA |
2944 | if (use > 0) { |
2945 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2946 | return -EBUSY; |
36dd1bcc | 2947 | } |
9dee1474 | 2948 | |
ee01d542 | 2949 | return nft_delchain(&ctx); |
96518518 PM |
2950 | } |
2951 | ||
96518518 PM |
2952 | /* |
2953 | * Expressions | |
2954 | */ | |
2955 | ||
2956 | /** | |
ef1f7df9 | 2957 | * nft_register_expr - register nf_tables expr type |
3db86c39 | 2958 | * @type: expr type |
96518518 | 2959 | * |
ef1f7df9 | 2960 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2961 | * success or a negative errno code otherwise. |
2962 | */ | |
ef1f7df9 | 2963 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2964 | { |
2965 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2966 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2967 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2968 | else |
e688a7f8 | 2969 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2970 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2971 | return 0; | |
2972 | } | |
2973 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2974 | ||
2975 | /** | |
ef1f7df9 | 2976 | * nft_unregister_expr - unregister nf_tables expr type |
3db86c39 | 2977 | * @type: expr type |
96518518 | 2978 | * |
ef1f7df9 | 2979 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2980 | */ |
ef1f7df9 | 2981 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2982 | { |
2983 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2984 | list_del_rcu(&type->list); |
96518518 PM |
2985 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2986 | } | |
2987 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2988 | ||
64d46806 PM |
2989 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2990 | struct nlattr *nla) | |
96518518 | 2991 | { |
9cff126f | 2992 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2993 | |
ef1f7df9 | 2994 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2995 | if (!nla_strcmp(nla, type->name)) { |
2996 | if (!type->family && !candidate) | |
2997 | candidate = type; | |
2998 | else if (type->family == family) | |
2999 | candidate = type; | |
3000 | } | |
96518518 | 3001 | } |
9cff126f | 3002 | return candidate; |
96518518 PM |
3003 | } |
3004 | ||
b9c04ae7 PNA |
3005 | #ifdef CONFIG_MODULES |
3006 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
3007 | struct nlattr *nla) | |
3008 | { | |
eb014de4 PNA |
3009 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
3010 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
3011 | return -EAGAIN; |
3012 | ||
3013 | return 0; | |
3014 | } | |
3015 | #endif | |
3016 | ||
452238e8 FW |
3017 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
3018 | u8 family, | |
64d46806 | 3019 | struct nlattr *nla) |
96518518 | 3020 | { |
ef1f7df9 | 3021 | const struct nft_expr_type *type; |
96518518 PM |
3022 | |
3023 | if (nla == NULL) | |
3024 | return ERR_PTR(-EINVAL); | |
3025 | ||
64d46806 | 3026 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
3027 | if (type != NULL && try_module_get(type->owner)) |
3028 | return type; | |
96518518 | 3029 | |
f102d66b | 3030 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 3031 | #ifdef CONFIG_MODULES |
ef1f7df9 | 3032 | if (type == NULL) { |
b9c04ae7 | 3033 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
3034 | return ERR_PTR(-EAGAIN); |
3035 | ||
eb014de4 PNA |
3036 | if (nft_request_module(net, "nft-expr-%.*s", |
3037 | nla_len(nla), | |
3038 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
3039 | return ERR_PTR(-EAGAIN); |
3040 | } | |
3041 | #endif | |
3042 | return ERR_PTR(-ENOENT); | |
3043 | } | |
3044 | ||
3045 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
3046 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
3047 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
3048 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
3049 | }; | |
3050 | ||
3051 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
8daa8fde | 3052 | const struct nft_expr *expr, bool reset) |
96518518 | 3053 | { |
ef1f7df9 | 3054 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
3055 | goto nla_put_failure; |
3056 | ||
3057 | if (expr->ops->dump) { | |
ae0be8de MK |
3058 | struct nlattr *data = nla_nest_start_noflag(skb, |
3059 | NFTA_EXPR_DATA); | |
96518518 PM |
3060 | if (data == NULL) |
3061 | goto nla_put_failure; | |
8daa8fde | 3062 | if (expr->ops->dump(skb, expr, reset) < 0) |
96518518 PM |
3063 | goto nla_put_failure; |
3064 | nla_nest_end(skb, data); | |
3065 | } | |
3066 | ||
3067 | return skb->len; | |
3068 | ||
3069 | nla_put_failure: | |
3070 | return -1; | |
3071 | }; | |
3072 | ||
0b2d8a7b | 3073 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
8daa8fde | 3074 | const struct nft_expr *expr, bool reset) |
0b2d8a7b PM |
3075 | { |
3076 | struct nlattr *nest; | |
3077 | ||
ae0be8de | 3078 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
3079 | if (!nest) |
3080 | goto nla_put_failure; | |
8daa8fde | 3081 | if (nf_tables_fill_expr_info(skb, expr, reset) < 0) |
0b2d8a7b PM |
3082 | goto nla_put_failure; |
3083 | nla_nest_end(skb, nest); | |
3084 | return 0; | |
3085 | ||
3086 | nla_put_failure: | |
3087 | return -1; | |
3088 | } | |
3089 | ||
96518518 PM |
3090 | struct nft_expr_info { |
3091 | const struct nft_expr_ops *ops; | |
83d9dcba | 3092 | const struct nlattr *attr; |
ef1f7df9 | 3093 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
3094 | }; |
3095 | ||
0ca743a5 PNA |
3096 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
3097 | const struct nlattr *nla, | |
96518518 PM |
3098 | struct nft_expr_info *info) |
3099 | { | |
ef1f7df9 | 3100 | const struct nft_expr_type *type; |
96518518 | 3101 | const struct nft_expr_ops *ops; |
ef1f7df9 | 3102 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
3103 | int err; |
3104 | ||
8cb08174 JB |
3105 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
3106 | nft_expr_policy, NULL); | |
96518518 PM |
3107 | if (err < 0) |
3108 | return err; | |
3109 | ||
452238e8 | 3110 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
3111 | if (IS_ERR(type)) |
3112 | return PTR_ERR(type); | |
3113 | ||
3114 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
3115 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
3116 | tb[NFTA_EXPR_DATA], | |
3117 | type->policy, NULL); | |
ef1f7df9 PM |
3118 | if (err < 0) |
3119 | goto err1; | |
3120 | } else | |
3121 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
3122 | ||
3123 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
3124 | ops = type->select_ops(ctx, |
3125 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
3126 | if (IS_ERR(ops)) { |
3127 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
3128 | #ifdef CONFIG_MODULES |
3129 | if (err == -EAGAIN) | |
eb014de4 PNA |
3130 | if (nft_expr_type_request_module(ctx->net, |
3131 | ctx->family, | |
3132 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
3133 | err = -ENOENT; | |
0ef1efd1 | 3134 | #endif |
ef1f7df9 PM |
3135 | goto err1; |
3136 | } | |
3137 | } else | |
3138 | ops = type->ops; | |
3139 | ||
83d9dcba | 3140 | info->attr = nla; |
96518518 | 3141 | info->ops = ops; |
83d9dcba | 3142 | |
96518518 | 3143 | return 0; |
ef1f7df9 PM |
3144 | |
3145 | err1: | |
3146 | module_put(type->owner); | |
3147 | return err; | |
96518518 PM |
3148 | } |
3149 | ||
3a07327d PNA |
3150 | int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, |
3151 | struct nft_expr_info *info) | |
3152 | { | |
3153 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; | |
3154 | const struct nft_expr_type *type; | |
3155 | int err; | |
3156 | ||
3157 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, | |
3158 | nft_expr_policy, NULL); | |
3159 | if (err < 0) | |
3160 | return err; | |
3161 | ||
3162 | if (!tb[NFTA_EXPR_DATA]) | |
3163 | return -EINVAL; | |
3164 | ||
3165 | type = __nft_expr_type_get(ctx->family, tb[NFTA_EXPR_NAME]); | |
98cbc40e DC |
3166 | if (!type) |
3167 | return -ENOENT; | |
3a07327d PNA |
3168 | |
3169 | if (!type->inner_ops) | |
3170 | return -EOPNOTSUPP; | |
3171 | ||
3172 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, | |
3173 | tb[NFTA_EXPR_DATA], | |
3174 | type->policy, NULL); | |
3175 | if (err < 0) | |
3176 | goto err_nla_parse; | |
3177 | ||
3178 | info->attr = nla; | |
3179 | info->ops = type->inner_ops; | |
3180 | ||
3181 | return 0; | |
3182 | ||
3183 | err_nla_parse: | |
3184 | return err; | |
3185 | } | |
3186 | ||
96518518 | 3187 | static int nf_tables_newexpr(const struct nft_ctx *ctx, |
7dab8ee3 | 3188 | const struct nft_expr_info *expr_info, |
96518518 PM |
3189 | struct nft_expr *expr) |
3190 | { | |
7dab8ee3 | 3191 | const struct nft_expr_ops *ops = expr_info->ops; |
96518518 PM |
3192 | int err; |
3193 | ||
3194 | expr->ops = ops; | |
3195 | if (ops->init) { | |
7dab8ee3 | 3196 | err = ops->init(ctx, expr, (const struct nlattr **)expr_info->tb); |
96518518 PM |
3197 | if (err < 0) |
3198 | goto err1; | |
3199 | } | |
3200 | ||
96518518 | 3201 | return 0; |
96518518 PM |
3202 | err1: |
3203 | expr->ops = NULL; | |
3204 | return err; | |
3205 | } | |
3206 | ||
62472bce PM |
3207 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
3208 | struct nft_expr *expr) | |
96518518 | 3209 | { |
3f3a390d PNA |
3210 | const struct nft_expr_type *type = expr->ops->type; |
3211 | ||
96518518 | 3212 | if (expr->ops->destroy) |
62472bce | 3213 | expr->ops->destroy(ctx, expr); |
3f3a390d | 3214 | module_put(type->owner); |
96518518 PM |
3215 | } |
3216 | ||
795a6d6b PNA |
3217 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
3218 | const struct nlattr *nla) | |
0b2d8a7b | 3219 | { |
7dab8ee3 | 3220 | struct nft_expr_info expr_info; |
0b2d8a7b | 3221 | struct nft_expr *expr; |
b8e20400 | 3222 | struct module *owner; |
0b2d8a7b PM |
3223 | int err; |
3224 | ||
7dab8ee3 | 3225 | err = nf_tables_expr_parse(ctx, nla, &expr_info); |
0b2d8a7b | 3226 | if (err < 0) |
52077804 PNA |
3227 | goto err_expr_parse; |
3228 | ||
3229 | err = -EOPNOTSUPP; | |
3230 | if (!(expr_info.ops->type->flags & NFT_EXPR_STATEFUL)) | |
3231 | goto err_expr_stateful; | |
0b2d8a7b PM |
3232 | |
3233 | err = -ENOMEM; | |
33758c89 | 3234 | expr = kzalloc(expr_info.ops->size, GFP_KERNEL_ACCOUNT); |
0b2d8a7b | 3235 | if (expr == NULL) |
52077804 | 3236 | goto err_expr_stateful; |
0b2d8a7b | 3237 | |
7dab8ee3 | 3238 | err = nf_tables_newexpr(ctx, &expr_info, expr); |
0b2d8a7b | 3239 | if (err < 0) |
52077804 | 3240 | goto err_expr_new; |
0b2d8a7b PM |
3241 | |
3242 | return expr; | |
52077804 | 3243 | err_expr_new: |
6cafaf47 | 3244 | kfree(expr); |
52077804 | 3245 | err_expr_stateful: |
7dab8ee3 PNA |
3246 | owner = expr_info.ops->type->owner; |
3247 | if (expr_info.ops->type->release_ops) | |
3248 | expr_info.ops->type->release_ops(expr_info.ops); | |
b8e20400 PNA |
3249 | |
3250 | module_put(owner); | |
52077804 | 3251 | err_expr_parse: |
0b2d8a7b PM |
3252 | return ERR_PTR(err); |
3253 | } | |
3254 | ||
c604cc69 PNA |
3255 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
3256 | { | |
3257 | int err; | |
3258 | ||
3259 | if (src->ops->clone) { | |
3260 | dst->ops = src->ops; | |
3261 | err = src->ops->clone(dst, src); | |
3262 | if (err < 0) | |
3263 | return err; | |
3264 | } else { | |
3265 | memcpy(dst, src, src->ops->size); | |
3266 | } | |
3267 | ||
3268 | __module_get(src->ops->type->owner); | |
3269 | ||
3270 | return 0; | |
3271 | } | |
3272 | ||
0b2d8a7b PM |
3273 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
3274 | { | |
3275 | nf_tables_expr_destroy(ctx, expr); | |
3276 | kfree(expr); | |
3277 | } | |
3278 | ||
96518518 PM |
3279 | /* |
3280 | * Rules | |
3281 | */ | |
3282 | ||
cac20fcd PNA |
3283 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
3284 | u64 handle) | |
96518518 PM |
3285 | { |
3286 | struct nft_rule *rule; | |
3287 | ||
3288 | // FIXME: this sucks | |
d9adf22a | 3289 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
3290 | if (handle == rule->handle) |
3291 | return rule; | |
3292 | } | |
3293 | ||
3294 | return ERR_PTR(-ENOENT); | |
3295 | } | |
3296 | ||
cac20fcd PNA |
3297 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
3298 | const struct nlattr *nla) | |
96518518 PM |
3299 | { |
3300 | if (nla == NULL) | |
3301 | return ERR_PTR(-EINVAL); | |
3302 | ||
cac20fcd | 3303 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
3304 | } |
3305 | ||
3306 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
3307 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
3308 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
3309 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
3310 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
3311 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
3312 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 3313 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 3314 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
3315 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
3316 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 3317 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 3318 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
837830a4 | 3319 | [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
3320 | }; |
3321 | ||
84d7fce6 PNA |
3322 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
3323 | u32 portid, u32 seq, int event, | |
3324 | u32 flags, int family, | |
96518518 PM |
3325 | const struct nft_table *table, |
3326 | const struct nft_chain *chain, | |
8daa8fde PS |
3327 | const struct nft_rule *rule, u64 handle, |
3328 | bool reset) | |
96518518 PM |
3329 | { |
3330 | struct nlmsghdr *nlh; | |
96518518 PM |
3331 | const struct nft_expr *expr, *next; |
3332 | struct nlattr *list; | |
dedb67c4 | 3333 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 3334 | |
19c28b13 PNA |
3335 | nlh = nfnl_msg_put(skb, portid, seq, type, flags, family, NFNETLINK_V0, |
3336 | nft_base_seq(net)); | |
3337 | if (!nlh) | |
96518518 PM |
3338 | goto nla_put_failure; |
3339 | ||
96518518 PM |
3340 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) |
3341 | goto nla_put_failure; | |
3342 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
3343 | goto nla_put_failure; | |
b46f6ded ND |
3344 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
3345 | NFTA_RULE_PAD)) | |
96518518 PM |
3346 | goto nla_put_failure; |
3347 | ||
e189ae16 PNA |
3348 | if (event != NFT_MSG_DELRULE && handle) { |
3349 | if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(handle), | |
b46f6ded | 3350 | NFTA_RULE_PAD)) |
5e948466 EL |
3351 | goto nla_put_failure; |
3352 | } | |
3353 | ||
b72920f6 PNA |
3354 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) |
3355 | nft_flow_rule_stats(chain, rule); | |
3356 | ||
ae0be8de | 3357 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
3358 | if (list == NULL) |
3359 | goto nla_put_failure; | |
3360 | nft_rule_for_each_expr(expr, next, rule) { | |
8daa8fde | 3361 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0) |
96518518 | 3362 | goto nla_put_failure; |
96518518 PM |
3363 | } |
3364 | nla_nest_end(skb, list); | |
3365 | ||
86f1ec32 PM |
3366 | if (rule->udata) { |
3367 | struct nft_userdata *udata = nft_userdata(rule); | |
3368 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
3369 | udata->data) < 0) | |
3370 | goto nla_put_failure; | |
3371 | } | |
0768b3b3 | 3372 | |
053c095a JB |
3373 | nlmsg_end(skb, nlh); |
3374 | return 0; | |
96518518 PM |
3375 | |
3376 | nla_put_failure: | |
3377 | nlmsg_trim(skb, nlh); | |
3378 | return -1; | |
3379 | } | |
3380 | ||
25e94a99 PNA |
3381 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
3382 | const struct nft_rule *rule, int event) | |
96518518 | 3383 | { |
d59d2f82 | 3384 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
e189ae16 | 3385 | const struct nft_rule *prule; |
96518518 | 3386 | struct sk_buff *skb; |
e189ae16 PNA |
3387 | u64 handle = 0; |
3388 | u16 flags = 0; | |
96518518 PM |
3389 | int err; |
3390 | ||
128ad332 PNA |
3391 | if (!ctx->report && |
3392 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3393 | return; |
96518518 | 3394 | |
96518518 PM |
3395 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
3396 | if (skb == NULL) | |
3397 | goto err; | |
3398 | ||
e189ae16 PNA |
3399 | if (event == NFT_MSG_NEWRULE && |
3400 | !list_is_first(&rule->list, &ctx->chain->rules) && | |
3401 | !list_is_last(&rule->list, &ctx->chain->rules)) { | |
3402 | prule = list_prev_entry(rule, list); | |
3403 | handle = prule->handle; | |
3404 | } | |
3405 | if (ctx->flags & (NLM_F_APPEND | NLM_F_REPLACE)) | |
3406 | flags |= NLM_F_APPEND; | |
6fb721cf PNA |
3407 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
3408 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
e189ae16 | 3409 | |
84d7fce6 | 3410 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
e189ae16 | 3411 | event, flags, ctx->family, ctx->table, |
8daa8fde | 3412 | ctx->chain, rule, handle, false); |
96518518 PM |
3413 | if (err < 0) { |
3414 | kfree_skb(skb); | |
3415 | goto err; | |
3416 | } | |
3417 | ||
0854db2a | 3418 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 3419 | return; |
96518518 | 3420 | err: |
25e94a99 | 3421 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
3422 | } |
3423 | ||
6e1f760e | 3424 | struct nft_rule_dump_ctx { |
e46abbcc | 3425 | char *table; |
b7263e07 | 3426 | char *chain; |
6e1f760e PNA |
3427 | }; |
3428 | ||
241faece PS |
3429 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
3430 | unsigned int *idx, | |
3431 | struct netlink_callback *cb, | |
3432 | const struct nft_table *table, | |
8daa8fde PS |
3433 | const struct nft_chain *chain, |
3434 | bool reset) | |
241faece PS |
3435 | { |
3436 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 3437 | const struct nft_rule *rule, *prule; |
241faece | 3438 | unsigned int s_idx = cb->args[0]; |
e189ae16 | 3439 | u64 handle; |
241faece | 3440 | |
2c82c7e7 | 3441 | prule = NULL; |
241faece PS |
3442 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
3443 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 3444 | goto cont_skip; |
241faece PS |
3445 | if (*idx < s_idx) |
3446 | goto cont; | |
3447 | if (*idx > s_idx) { | |
3448 | memset(&cb->args[1], 0, | |
3449 | sizeof(cb->args) - sizeof(cb->args[0])); | |
3450 | } | |
e189ae16 PNA |
3451 | if (prule) |
3452 | handle = prule->handle; | |
3453 | else | |
3454 | handle = 0; | |
3455 | ||
241faece PS |
3456 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, |
3457 | cb->nlh->nlmsg_seq, | |
3458 | NFT_MSG_NEWRULE, | |
3459 | NLM_F_MULTI | NLM_F_APPEND, | |
3460 | table->family, | |
8daa8fde | 3461 | table, chain, rule, handle, reset) < 0) |
310529e6 | 3462 | return 1; |
241faece PS |
3463 | |
3464 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
3465 | cont: | |
2c82c7e7 FW |
3466 | prule = rule; |
3467 | cont_skip: | |
241faece PS |
3468 | (*idx)++; |
3469 | } | |
310529e6 | 3470 | return 0; |
241faece PS |
3471 | } |
3472 | ||
96518518 PM |
3473 | static int nf_tables_dump_rules(struct sk_buff *skb, |
3474 | struct netlink_callback *cb) | |
3475 | { | |
3476 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 3477 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 3478 | struct nft_table *table; |
96518518 | 3479 | const struct nft_chain *chain; |
241faece | 3480 | unsigned int idx = 0; |
99633ab2 | 3481 | struct net *net = sock_net(skb->sk); |
96518518 | 3482 | int family = nfmsg->nfgen_family; |
0854db2a | 3483 | struct nftables_pernet *nft_net; |
8daa8fde PS |
3484 | bool reset = false; |
3485 | ||
3486 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) | |
3487 | reset = true; | |
96518518 | 3488 | |
e688a7f8 | 3489 | rcu_read_lock(); |
d59d2f82 | 3490 | nft_net = nft_pernet(net); |
34002783 | 3491 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 3492 | |
0854db2a | 3493 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 3494 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
3495 | continue; |
3496 | ||
3497 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
3498 | continue; |
3499 | ||
715849ab | 3500 | if (ctx && ctx->table && ctx->chain) { |
241faece | 3501 | struct rhlist_head *list, *tmp; |
6e1f760e | 3502 | |
241faece PS |
3503 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
3504 | nft_chain_ht_params); | |
3505 | if (!list) | |
3506 | goto done; | |
3507 | ||
3508 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
3509 | if (!nft_is_active(net, chain)) | |
3510 | continue; | |
3511 | __nf_tables_dump_rules(skb, &idx, | |
8daa8fde | 3512 | cb, table, chain, reset); |
241faece | 3513 | break; |
96518518 | 3514 | } |
241faece | 3515 | goto done; |
96518518 | 3516 | } |
241faece PS |
3517 | |
3518 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
8daa8fde PS |
3519 | if (__nf_tables_dump_rules(skb, &idx, |
3520 | cb, table, chain, reset)) | |
241faece PS |
3521 | goto done; |
3522 | } | |
3523 | ||
3524 | if (ctx && ctx->table) | |
3525 | break; | |
96518518 PM |
3526 | } |
3527 | done: | |
e688a7f8 | 3528 | rcu_read_unlock(); |
310529e6 PS |
3529 | |
3530 | cb->args[0] = idx; | |
96518518 PM |
3531 | return skb->len; |
3532 | } | |
3533 | ||
90fd131a FW |
3534 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
3535 | { | |
3536 | const struct nlattr * const *nla = cb->data; | |
3537 | struct nft_rule_dump_ctx *ctx = NULL; | |
3538 | ||
3539 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
3540 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
3541 | if (!ctx) | |
3542 | return -ENOMEM; | |
3543 | ||
3544 | if (nla[NFTA_RULE_TABLE]) { | |
3545 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
3546 | GFP_ATOMIC); | |
3547 | if (!ctx->table) { | |
3548 | kfree(ctx); | |
3549 | return -ENOMEM; | |
3550 | } | |
3551 | } | |
3552 | if (nla[NFTA_RULE_CHAIN]) { | |
3553 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
3554 | GFP_ATOMIC); | |
3555 | if (!ctx->chain) { | |
3556 | kfree(ctx->table); | |
3557 | kfree(ctx); | |
3558 | return -ENOMEM; | |
3559 | } | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | cb->data = ctx; | |
3564 | return 0; | |
3565 | } | |
3566 | ||
6e1f760e PNA |
3567 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
3568 | { | |
e46abbcc PS |
3569 | struct nft_rule_dump_ctx *ctx = cb->data; |
3570 | ||
3571 | if (ctx) { | |
3572 | kfree(ctx->table); | |
b7263e07 | 3573 | kfree(ctx->chain); |
e46abbcc PS |
3574 | kfree(ctx); |
3575 | } | |
6e1f760e PNA |
3576 | return 0; |
3577 | } | |
3578 | ||
d9adf22a | 3579 | /* called with rcu_read_lock held */ |
797d4980 PNA |
3580 | static int nf_tables_getrule(struct sk_buff *skb, const struct nfnl_info *info, |
3581 | const struct nlattr * const nla[]) | |
96518518 | 3582 | { |
797d4980 PNA |
3583 | struct netlink_ext_ack *extack = info->extack; |
3584 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 3585 | u8 family = info->nfmsg->nfgen_family; |
96518518 PM |
3586 | const struct nft_chain *chain; |
3587 | const struct nft_rule *rule; | |
797d4980 | 3588 | struct net *net = info->net; |
1b2470e5 | 3589 | struct nft_table *table; |
96518518 | 3590 | struct sk_buff *skb2; |
8daa8fde | 3591 | bool reset = false; |
96518518 PM |
3592 | int err; |
3593 | ||
797d4980 | 3594 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
96518518 | 3595 | struct netlink_dump_control c = { |
90fd131a | 3596 | .start= nf_tables_dump_rules_start, |
96518518 | 3597 | .dump = nf_tables_dump_rules, |
6e1f760e | 3598 | .done = nf_tables_dump_rules_done, |
d9adf22a | 3599 | .module = THIS_MODULE, |
90fd131a | 3600 | .data = (void *)nla, |
96518518 | 3601 | }; |
6e1f760e | 3602 | |
797d4980 | 3603 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
96518518 PM |
3604 | } |
3605 | ||
6001a930 | 3606 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, 0); |
36dd1bcc PNA |
3607 | if (IS_ERR(table)) { |
3608 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3609 | return PTR_ERR(table); |
36dd1bcc | 3610 | } |
96518518 | 3611 | |
f102d66b | 3612 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3613 | if (IS_ERR(chain)) { |
3614 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3615 | return PTR_ERR(chain); |
36dd1bcc | 3616 | } |
96518518 | 3617 | |
cac20fcd | 3618 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3619 | if (IS_ERR(rule)) { |
3620 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3621 | return PTR_ERR(rule); |
36dd1bcc | 3622 | } |
96518518 | 3623 | |
d9adf22a | 3624 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
3625 | if (!skb2) |
3626 | return -ENOMEM; | |
3627 | ||
8daa8fde PS |
3628 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETRULE_RESET) |
3629 | reset = true; | |
3630 | ||
84d7fce6 | 3631 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 3632 | info->nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
8daa8fde | 3633 | family, table, chain, rule, 0, reset); |
96518518 | 3634 | if (err < 0) |
ee921183 | 3635 | goto err_fill_rule_info; |
96518518 | 3636 | |
ee921183 | 3637 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
96518518 | 3638 | |
ee921183 | 3639 | err_fill_rule_info: |
96518518 PM |
3640 | kfree_skb(skb2); |
3641 | return err; | |
3642 | } | |
3643 | ||
4bedf9ee | 3644 | void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule) |
96518518 | 3645 | { |
29e38801 | 3646 | struct nft_expr *expr, *next; |
96518518 PM |
3647 | |
3648 | /* | |
3649 | * Careful: some expressions might not be initialized in case this | |
3650 | * is called on error from nf_tables_newrule(). | |
3651 | */ | |
3652 | expr = nft_expr_first(rule); | |
31cc578a | 3653 | while (nft_expr_more(rule, expr)) { |
29e38801 | 3654 | next = nft_expr_next(expr); |
62472bce | 3655 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 3656 | expr = next; |
96518518 PM |
3657 | } |
3658 | kfree(rule); | |
3659 | } | |
3660 | ||
4bedf9ee | 3661 | static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule) |
bb7b40ae | 3662 | { |
f6ac8585 | 3663 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
3664 | nf_tables_rule_destroy(ctx, rule); |
3665 | } | |
3666 | ||
a654de8f PNA |
3667 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
3668 | { | |
3669 | struct nft_expr *expr, *last; | |
3670 | const struct nft_data *data; | |
3671 | struct nft_rule *rule; | |
3672 | int err; | |
3673 | ||
26b2f552 TY |
3674 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
3675 | return -EMLINK; | |
3676 | ||
a654de8f PNA |
3677 | list_for_each_entry(rule, &chain->rules, list) { |
3678 | if (!nft_is_active_next(ctx->net, rule)) | |
3679 | continue; | |
3680 | ||
3681 | nft_rule_for_each_expr(expr, last, rule) { | |
3682 | if (!expr->ops->validate) | |
3683 | continue; | |
3684 | ||
3685 | err = expr->ops->validate(ctx, expr, &data); | |
3686 | if (err < 0) | |
3687 | return err; | |
3688 | } | |
3689 | } | |
3690 | ||
3691 | return 0; | |
3692 | } | |
3693 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
3694 | ||
3695 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
3696 | { | |
3697 | struct nft_chain *chain; | |
3698 | struct nft_ctx ctx = { | |
3699 | .net = net, | |
3700 | .family = table->family, | |
3701 | }; | |
3702 | int err; | |
3703 | ||
3704 | list_for_each_entry(chain, &table->chains, list) { | |
3705 | if (!nft_is_base_chain(chain)) | |
3706 | continue; | |
3707 | ||
3708 | ctx.chain = chain; | |
3709 | err = nft_chain_validate(&ctx, chain); | |
3710 | if (err < 0) | |
3711 | return err; | |
314c8284 FW |
3712 | |
3713 | cond_resched(); | |
a654de8f PNA |
3714 | } |
3715 | ||
3716 | return 0; | |
3717 | } | |
3718 | ||
d46fc894 PNA |
3719 | int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, |
3720 | const struct nft_set_iter *iter, | |
3721 | struct nft_set_elem *elem) | |
3722 | { | |
3723 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
3724 | struct nft_ctx *pctx = (struct nft_ctx *)ctx; | |
3725 | const struct nft_data *data; | |
3726 | int err; | |
3727 | ||
3728 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
3729 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
3730 | return 0; | |
3731 | ||
3732 | data = nft_set_ext_data(ext); | |
3733 | switch (data->verdict.code) { | |
3734 | case NFT_JUMP: | |
3735 | case NFT_GOTO: | |
3736 | pctx->level++; | |
3737 | err = nft_chain_validate(ctx, data->verdict.chain); | |
3738 | if (err < 0) | |
3739 | return err; | |
3740 | pctx->level--; | |
3741 | break; | |
3742 | default: | |
3743 | break; | |
3744 | } | |
3745 | ||
3746 | return 0; | |
3747 | } | |
3748 | ||
d46fc894 PNA |
3749 | int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set) |
3750 | { | |
3751 | u8 genmask = nft_genmask_next(ctx->net); | |
3752 | struct nft_set_elem_catchall *catchall; | |
3753 | struct nft_set_elem elem; | |
3754 | struct nft_set_ext *ext; | |
3755 | int ret = 0; | |
3756 | ||
3757 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
3758 | ext = nft_set_elem_ext(set, catchall->elem); | |
3759 | if (!nft_set_elem_active(ext, genmask)) | |
3760 | continue; | |
3761 | ||
3762 | elem.priv = catchall->elem; | |
3763 | ret = nft_setelem_validate(ctx, set, NULL, &elem); | |
3764 | if (ret < 0) | |
3765 | return ret; | |
3766 | } | |
3767 | ||
3768 | return ret; | |
3769 | } | |
3770 | ||
75dd48e2 | 3771 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 3772 | const struct nft_chain *chain, |
75dd48e2 PS |
3773 | const struct nlattr *nla); |
3774 | ||
1081d11b PNA |
3775 | #define NFT_RULE_MAXEXPRS 128 |
3776 | ||
7dab8ee3 PNA |
3777 | static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, |
3778 | const struct nlattr * const nla[]) | |
96518518 | 3779 | { |
7dab8ee3 | 3780 | struct nftables_pernet *nft_net = nft_pernet(info->net); |
7dab8ee3 PNA |
3781 | struct netlink_ext_ack *extack = info->extack; |
3782 | unsigned int size, i, n, ulen = 0, usize = 0; | |
3783 | u8 genmask = nft_genmask_next(info->net); | |
3784 | struct nft_rule *rule, *old_rule = NULL; | |
3785 | struct nft_expr_info *expr_info = NULL; | |
ef4b65e5 | 3786 | u8 family = info->nfmsg->nfgen_family; |
3c5e4462 | 3787 | struct nft_flow_rule *flow = NULL; |
7dab8ee3 | 3788 | struct net *net = info->net; |
7dab8ee3 | 3789 | struct nft_userdata *udata; |
96518518 PM |
3790 | struct nft_table *table; |
3791 | struct nft_chain *chain; | |
7dab8ee3 PNA |
3792 | struct nft_trans *trans; |
3793 | u64 handle, pos_handle; | |
96518518 PM |
3794 | struct nft_expr *expr; |
3795 | struct nft_ctx ctx; | |
3796 | struct nlattr *tmp; | |
96518518 | 3797 | int err, rem; |
96518518 | 3798 | |
0854db2a | 3799 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 3800 | |
6001a930 PNA |
3801 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
3802 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
3803 | if (IS_ERR(table)) { |
3804 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3805 | return PTR_ERR(table); |
36dd1bcc | 3806 | } |
96518518 | 3807 | |
837830a4 PNA |
3808 | if (nla[NFTA_RULE_CHAIN]) { |
3809 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], | |
3810 | genmask); | |
3811 | if (IS_ERR(chain)) { | |
3812 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
3813 | return PTR_ERR(chain); | |
3814 | } | |
d0e2c7de | 3815 | |
837830a4 | 3816 | } else if (nla[NFTA_RULE_CHAIN_ID]) { |
515ad530 TLSC |
3817 | chain = nft_chain_lookup_byid(net, table, nla[NFTA_RULE_CHAIN_ID], |
3818 | genmask); | |
837830a4 PNA |
3819 | if (IS_ERR(chain)) { |
3820 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]); | |
3821 | return PTR_ERR(chain); | |
3822 | } | |
3823 | } else { | |
3824 | return -EINVAL; | |
36dd1bcc | 3825 | } |
96518518 | 3826 | |
0ebc1064 PNA |
3827 | if (nft_chain_is_bound(chain)) |
3828 | return -EOPNOTSUPP; | |
3829 | ||
96518518 PM |
3830 | if (nla[NFTA_RULE_HANDLE]) { |
3831 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3832 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3833 | if (IS_ERR(rule)) { |
3834 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3835 | return PTR_ERR(rule); |
36dd1bcc | 3836 | } |
96518518 | 3837 | |
7dab8ee3 | 3838 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 3839 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
96518518 | 3840 | return -EEXIST; |
36dd1bcc | 3841 | } |
7dab8ee3 | 3842 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
96518518 PM |
3843 | old_rule = rule; |
3844 | else | |
3845 | return -EOPNOTSUPP; | |
3846 | } else { | |
7dab8ee3 PNA |
3847 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE) || |
3848 | info->nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3849 | return -EINVAL; |
3850 | handle = nf_tables_alloc_handle(table); | |
a0a7379e | 3851 | |
447750f2 FW |
3852 | if (nla[NFTA_RULE_POSITION]) { |
3853 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3854 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3855 | if (IS_ERR(old_rule)) { | |
3856 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3857 | return PTR_ERR(old_rule); | |
3858 | } | |
75dd48e2 | 3859 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
36d5b291 | 3860 | old_rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_POSITION_ID]); |
75dd48e2 PS |
3861 | if (IS_ERR(old_rule)) { |
3862 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3863 | return PTR_ERR(old_rule); | |
3864 | } | |
36dd1bcc | 3865 | } |
5e948466 EL |
3866 | } |
3867 | ||
7dab8ee3 | 3868 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0ca743a5 | 3869 | |
96518518 PM |
3870 | n = 0; |
3871 | size = 0; | |
3872 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
7dab8ee3 PNA |
3873 | expr_info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3874 | sizeof(struct nft_expr_info), | |
3875 | GFP_KERNEL); | |
3876 | if (!expr_info) | |
2a43ecf9 FW |
3877 | return -ENOMEM; |
3878 | ||
96518518 PM |
3879 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3880 | err = -EINVAL; | |
3881 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3c5e4462 | 3882 | goto err_release_expr; |
96518518 | 3883 | if (n == NFT_RULE_MAXEXPRS) |
3c5e4462 | 3884 | goto err_release_expr; |
7dab8ee3 | 3885 | err = nf_tables_expr_parse(&ctx, tmp, &expr_info[n]); |
c781471d PNA |
3886 | if (err < 0) { |
3887 | NL_SET_BAD_ATTR(extack, tmp); | |
3c5e4462 | 3888 | goto err_release_expr; |
c781471d | 3889 | } |
7dab8ee3 | 3890 | size += expr_info[n].ops->size; |
96518518 PM |
3891 | n++; |
3892 | } | |
3893 | } | |
9889840f PM |
3894 | /* Check for overflow of dlen field */ |
3895 | err = -EFBIG; | |
3896 | if (size >= 1 << 12) | |
3c5e4462 | 3897 | goto err_release_expr; |
96518518 | 3898 | |
86f1ec32 | 3899 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3900 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3901 | if (ulen > 0) |
3902 | usize = sizeof(struct nft_userdata) + ulen; | |
3903 | } | |
0768b3b3 | 3904 | |
96518518 | 3905 | err = -ENOMEM; |
33758c89 | 3906 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL_ACCOUNT); |
96518518 | 3907 | if (rule == NULL) |
3c5e4462 | 3908 | goto err_release_expr; |
96518518 | 3909 | |
889f7ee7 | 3910 | nft_activate_next(net, rule); |
0628b123 | 3911 | |
96518518 PM |
3912 | rule->handle = handle; |
3913 | rule->dlen = size; | |
86f1ec32 | 3914 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3915 | |
86f1ec32 PM |
3916 | if (ulen) { |
3917 | udata = nft_userdata(rule); | |
3918 | udata->len = ulen - 1; | |
3919 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3920 | } | |
96518518 | 3921 | |
96518518 PM |
3922 | expr = nft_expr_first(rule); |
3923 | for (i = 0; i < n; i++) { | |
7dab8ee3 | 3924 | err = nf_tables_newexpr(&ctx, &expr_info[i], expr); |
83d9dcba | 3925 | if (err < 0) { |
7dab8ee3 | 3926 | NL_SET_BAD_ATTR(extack, expr_info[i].attr); |
3c5e4462 | 3927 | goto err_release_rule; |
83d9dcba | 3928 | } |
a654de8f | 3929 | |
7dab8ee3 | 3930 | if (expr_info[i].ops->validate) |
00c320f9 | 3931 | nft_validate_state_update(table, NFT_VALIDATE_NEED); |
a654de8f | 3932 | |
7dab8ee3 | 3933 | expr_info[i].ops = NULL; |
96518518 PM |
3934 | expr = nft_expr_next(expr); |
3935 | } | |
3936 | ||
3c5e4462 PNA |
3937 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
3938 | flow = nft_flow_rule_create(net, rule); | |
3939 | if (IS_ERR(flow)) { | |
3940 | err = PTR_ERR(flow); | |
3941 | goto err_release_rule; | |
3942 | } | |
3943 | } | |
3944 | ||
1689f259 PNA |
3945 | if (!nft_use_inc(&chain->use)) { |
3946 | err = -EMFILE; | |
3947 | goto err_release_rule; | |
3948 | } | |
3949 | ||
7dab8ee3 | 3950 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) { |
2c964c55 PNA |
3951 | err = nft_delrule(&ctx, old_rule); |
3952 | if (err < 0) | |
3953 | goto err_destroy_flow_rule; | |
3954 | ||
ca089878 | 3955 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3956 | if (trans == NULL) { |
3957 | err = -ENOMEM; | |
3c5e4462 | 3958 | goto err_destroy_flow_rule; |
569ccae6 | 3959 | } |
569ccae6 FW |
3960 | list_add_tail_rcu(&rule->list, &old_rule->list); |
3961 | } else { | |
c9626a2c PNA |
3962 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3963 | if (!trans) { | |
569ccae6 | 3964 | err = -ENOMEM; |
3c5e4462 | 3965 | goto err_destroy_flow_rule; |
569ccae6 FW |
3966 | } |
3967 | ||
7dab8ee3 | 3968 | if (info->nlh->nlmsg_flags & NLM_F_APPEND) { |
569ccae6 FW |
3969 | if (old_rule) |
3970 | list_add_rcu(&rule->list, &old_rule->list); | |
3971 | else | |
3972 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3973 | } else { | |
3974 | if (old_rule) | |
3975 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3976 | else | |
3977 | list_add_rcu(&rule->list, &chain->rules); | |
3978 | } | |
0628b123 | 3979 | } |
7dab8ee3 | 3980 | kvfree(expr_info); |
96518518 | 3981 | |
3c5e4462 PNA |
3982 | if (flow) |
3983 | nft_trans_flow_rule(trans) = flow; | |
3984 | ||
00c320f9 | 3985 | if (table->validate_state == NFT_VALIDATE_DO) |
a654de8f PNA |
3986 | return nft_table_validate(net, table); |
3987 | ||
3988 | return 0; | |
3c5e4462 PNA |
3989 | |
3990 | err_destroy_flow_rule: | |
1689f259 | 3991 | nft_use_dec_restore(&chain->use); |
4ca041f9 CIK |
3992 | if (flow) |
3993 | nft_flow_rule_destroy(flow); | |
3c5e4462 | 3994 | err_release_rule: |
26b5a571 | 3995 | nft_rule_expr_deactivate(&ctx, rule, NFT_TRANS_PREPARE_ERROR); |
1240eb93 | 3996 | nf_tables_rule_destroy(&ctx, rule); |
3c5e4462 | 3997 | err_release_expr: |
96518518 | 3998 | for (i = 0; i < n; i++) { |
7dab8ee3 PNA |
3999 | if (expr_info[i].ops) { |
4000 | module_put(expr_info[i].ops->type->owner); | |
4001 | if (expr_info[i].ops->type->release_ops) | |
4002 | expr_info[i].ops->type->release_ops(expr_info[i].ops); | |
b25a31bf | 4003 | } |
96518518 | 4004 | } |
7dab8ee3 PNA |
4005 | kvfree(expr_info); |
4006 | ||
96518518 PM |
4007 | return err; |
4008 | } | |
4009 | ||
1a94e38d | 4010 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
36d5b291 | 4011 | const struct nft_chain *chain, |
1a94e38d PNA |
4012 | const struct nlattr *nla) |
4013 | { | |
d59d2f82 | 4014 | struct nftables_pernet *nft_net = nft_pernet(net); |
1a94e38d PNA |
4015 | u32 id = ntohl(nla_get_be32(nla)); |
4016 | struct nft_trans *trans; | |
4017 | ||
0854db2a | 4018 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
1a94e38d | 4019 | if (trans->msg_type == NFT_MSG_NEWRULE && |
36d5b291 | 4020 | trans->ctx.chain == chain && |
1a94e38d | 4021 | id == nft_trans_rule_id(trans)) |
e3c361b8 | 4022 | return nft_trans_rule(trans); |
1a94e38d PNA |
4023 | } |
4024 | return ERR_PTR(-ENOENT); | |
4025 | } | |
4026 | ||
7dab8ee3 PNA |
4027 | static int nf_tables_delrule(struct sk_buff *skb, const struct nfnl_info *info, |
4028 | const struct nlattr * const nla[]) | |
96518518 | 4029 | { |
7dab8ee3 | 4030 | struct netlink_ext_ack *extack = info->extack; |
7dab8ee3 | 4031 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 4032 | u8 family = info->nfmsg->nfgen_family; |
cf9dc09d | 4033 | struct nft_chain *chain = NULL; |
7dab8ee3 PNA |
4034 | struct net *net = info->net; |
4035 | struct nft_table *table; | |
cf9dc09d | 4036 | struct nft_rule *rule; |
0628b123 | 4037 | struct nft_ctx ctx; |
ef4b65e5 | 4038 | int err = 0; |
96518518 | 4039 | |
6001a930 PNA |
4040 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask, |
4041 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4042 | if (IS_ERR(table)) { |
4043 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 4044 | return PTR_ERR(table); |
36dd1bcc | 4045 | } |
96518518 | 4046 | |
cf9dc09d | 4047 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
4048 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
4049 | genmask); | |
36dd1bcc | 4050 | if (IS_ERR(chain)) { |
1fb7696a | 4051 | if (PTR_ERR(chain) == -ENOENT && |
f80a612d FFM |
4052 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) |
4053 | return 0; | |
4054 | ||
36dd1bcc | 4055 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); |
cf9dc09d | 4056 | return PTR_ERR(chain); |
36dd1bcc | 4057 | } |
d0e2c7de PNA |
4058 | if (nft_chain_is_bound(chain)) |
4059 | return -EOPNOTSUPP; | |
cf9dc09d | 4060 | } |
96518518 | 4061 | |
7dab8ee3 | 4062 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, chain, nla); |
0628b123 | 4063 | |
cf9dc09d PNA |
4064 | if (chain) { |
4065 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 4066 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc | 4067 | if (IS_ERR(rule)) { |
f80a612d FFM |
4068 | if (PTR_ERR(rule) == -ENOENT && |
4069 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYRULE) | |
4070 | return 0; | |
4071 | ||
36dd1bcc | 4072 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); |
cf9dc09d | 4073 | return PTR_ERR(rule); |
36dd1bcc | 4074 | } |
96518518 | 4075 | |
1a94e38d PNA |
4076 | err = nft_delrule(&ctx, rule); |
4077 | } else if (nla[NFTA_RULE_ID]) { | |
36d5b291 | 4078 | rule = nft_rule_lookup_byid(net, chain, nla[NFTA_RULE_ID]); |
36dd1bcc PNA |
4079 | if (IS_ERR(rule)) { |
4080 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 4081 | return PTR_ERR(rule); |
36dd1bcc | 4082 | } |
1a94e38d | 4083 | |
5e266fe7 | 4084 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 4085 | } else { |
ce24b721 | 4086 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
4087 | } |
4088 | } else { | |
4089 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
4090 | if (!nft_is_active_next(net, chain)) |
4091 | continue; | |
6eaf41e8 PNA |
4092 | if (nft_chain_is_bound(chain)) |
4093 | continue; | |
664b0f8c | 4094 | |
cf9dc09d | 4095 | ctx.chain = chain; |
ce24b721 | 4096 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
4097 | if (err < 0) |
4098 | break; | |
4099 | } | |
4100 | } | |
4101 | ||
4102 | return err; | |
4103 | } | |
4104 | ||
20a69341 PM |
4105 | /* |
4106 | * Sets | |
4107 | */ | |
e32a4dc6 FW |
4108 | static const struct nft_set_type *nft_set_types[] = { |
4109 | &nft_set_hash_fast_type, | |
4110 | &nft_set_hash_type, | |
4111 | &nft_set_rhash_type, | |
4112 | &nft_set_bitmap_type, | |
4113 | &nft_set_rbtree_type, | |
e6abef61 | 4114 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
4115 | &nft_set_pipapo_avx2_type, |
4116 | #endif | |
e32a4dc6 FW |
4117 | &nft_set_pipapo_type, |
4118 | }; | |
20a69341 | 4119 | |
2b664957 | 4120 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
4121 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
4122 | NFT_SET_EVAL) | |
2b664957 | 4123 | |
71cc0873 | 4124 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 4125 | { |
71cc0873 | 4126 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
4127 | } |
4128 | ||
c50b960c PM |
4129 | /* |
4130 | * Select a set implementation based on the data characteristics and the | |
4131 | * given policy. The total memory use might not be known if no size is | |
4132 | * given, in that case the amount of memory per element is used. | |
4133 | */ | |
4134 | static const struct nft_set_ops * | |
2b664957 PNA |
4135 | nft_select_set_ops(const struct nft_ctx *ctx, |
4136 | const struct nlattr * const nla[], | |
bed4a63e | 4137 | const struct nft_set_desc *desc) |
20a69341 | 4138 | { |
d59d2f82 | 4139 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
c50b960c PM |
4140 | const struct nft_set_ops *ops, *bops; |
4141 | struct nft_set_estimate est, best; | |
2b664957 PNA |
4142 | const struct nft_set_type *type; |
4143 | u32 flags = 0; | |
e32a4dc6 | 4144 | int i; |
20a69341 | 4145 | |
0854db2a | 4146 | lockdep_assert_held(&nft_net->commit_mutex); |
f102d66b | 4147 | lockdep_nfnl_nft_mutex_not_held(); |
e32a4dc6 | 4148 | |
2b664957 PNA |
4149 | if (nla[NFTA_SET_FLAGS] != NULL) |
4150 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 4151 | |
55af753c PNA |
4152 | bops = NULL; |
4153 | best.size = ~0; | |
4154 | best.lookup = ~0; | |
0b5a7874 | 4155 | best.space = ~0; |
c50b960c | 4156 | |
e32a4dc6 FW |
4157 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
4158 | type = nft_set_types[i]; | |
71cc0873 | 4159 | ops = &type->ops; |
2b664957 | 4160 | |
71cc0873 | 4161 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 4162 | continue; |
2b664957 | 4163 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
4164 | continue; |
4165 | ||
bed4a63e | 4166 | switch (desc->policy) { |
c50b960c | 4167 | case NFT_SET_POL_PERFORMANCE: |
55af753c | 4168 | if (est.lookup < best.lookup) |
c50b960c | 4169 | break; |
644e334e PNA |
4170 | if (est.lookup == best.lookup && |
4171 | est.space < best.space) | |
4172 | break; | |
c50b960c PM |
4173 | continue; |
4174 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
4175 | if (!desc->size) { |
4176 | if (est.space < best.space) | |
4177 | break; | |
4178 | if (est.space == best.space && | |
4179 | est.lookup < best.lookup) | |
4180 | break; | |
4f2921ca | 4181 | } else if (est.size < best.size || !bops) { |
c50b960c | 4182 | break; |
0b5a7874 | 4183 | } |
c50b960c PM |
4184 | continue; |
4185 | default: | |
4186 | break; | |
4187 | } | |
4188 | ||
c50b960c PM |
4189 | bops = ops; |
4190 | best = est; | |
20a69341 PM |
4191 | } |
4192 | ||
c50b960c PM |
4193 | if (bops != NULL) |
4194 | return bops; | |
4195 | ||
20a69341 PM |
4196 | return ERR_PTR(-EOPNOTSUPP); |
4197 | } | |
4198 | ||
4199 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
4200 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
4201 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 4202 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 4203 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
4204 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
4205 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
4206 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
4207 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
4208 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
4209 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
4210 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 4211 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
4212 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
4213 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
4214 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
4215 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 4216 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 4217 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 4218 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
48b0ae04 | 4219 | [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED }, |
c50b960c PM |
4220 | }; |
4221 | ||
4222 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
4223 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 4224 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
4225 | }; |
4226 | ||
cac20fcd PNA |
4227 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
4228 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
4229 | { |
4230 | struct nft_set *set; | |
4231 | ||
4232 | if (nla == NULL) | |
4233 | return ERR_PTR(-EINVAL); | |
4234 | ||
d9adf22a | 4235 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
4236 | if (!nla_strcmp(nla, set->name) && |
4237 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
4238 | return set; |
4239 | } | |
4240 | return ERR_PTR(-ENOENT); | |
4241 | } | |
4242 | ||
cac20fcd PNA |
4243 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
4244 | const struct nlattr *nla, | |
4245 | u8 genmask) | |
3ecbfd65 HS |
4246 | { |
4247 | struct nft_set *set; | |
4248 | ||
3ecbfd65 HS |
4249 | list_for_each_entry(set, &table->sets, list) { |
4250 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
4251 | nft_active_genmask(set, genmask)) | |
4252 | return set; | |
4253 | } | |
4254 | return ERR_PTR(-ENOENT); | |
4255 | } | |
4256 | ||
cac20fcd | 4257 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
470ee20e | 4258 | const struct nft_table *table, |
cac20fcd | 4259 | const struct nlattr *nla, u8 genmask) |
958bee14 | 4260 | { |
d59d2f82 | 4261 | struct nftables_pernet *nft_net = nft_pernet(net); |
958bee14 | 4262 | u32 id = ntohl(nla_get_be32(nla)); |
d59d2f82 | 4263 | struct nft_trans *trans; |
958bee14 | 4264 | |
0854db2a | 4265 | list_for_each_entry(trans, &nft_net->commit_list, list) { |
9c7f96fd AK |
4266 | if (trans->msg_type == NFT_MSG_NEWSET) { |
4267 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 4268 | |
9c7f96fd | 4269 | if (id == nft_trans_set_id(trans) && |
470ee20e | 4270 | set->table == table && |
9c7f96fd AK |
4271 | nft_active_genmask(set, genmask)) |
4272 | return set; | |
4273 | } | |
958bee14 PNA |
4274 | } |
4275 | return ERR_PTR(-ENOENT); | |
4276 | } | |
c7a72e3f | 4277 | |
10659cba PNA |
4278 | struct nft_set *nft_set_lookup_global(const struct net *net, |
4279 | const struct nft_table *table, | |
4280 | const struct nlattr *nla_set_name, | |
4281 | const struct nlattr *nla_set_id, | |
4282 | u8 genmask) | |
c7a72e3f PNA |
4283 | { |
4284 | struct nft_set *set; | |
4285 | ||
cac20fcd | 4286 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
4287 | if (IS_ERR(set)) { |
4288 | if (!nla_set_id) | |
4289 | return set; | |
4290 | ||
470ee20e | 4291 | set = nft_set_lookup_byid(net, table, nla_set_id, genmask); |
c7a72e3f PNA |
4292 | } |
4293 | return set; | |
4294 | } | |
10659cba | 4295 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 4296 | |
20a69341 PM |
4297 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
4298 | const char *name) | |
4299 | { | |
4300 | const struct nft_set *i; | |
4301 | const char *p; | |
4302 | unsigned long *inuse; | |
60eb1894 | 4303 | unsigned int n = 0, min = 0; |
20a69341 | 4304 | |
38745490 | 4305 | p = strchr(name, '%'); |
20a69341 PM |
4306 | if (p != NULL) { |
4307 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
4308 | return -EINVAL; | |
4309 | ||
4310 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
4311 | if (inuse == NULL) | |
4312 | return -ENOMEM; | |
60eb1894 | 4313 | cont: |
20a69341 | 4314 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
4315 | int tmp; |
4316 | ||
271c5ca8 | 4317 | if (!nft_is_active_next(ctx->net, i)) |
37a9cc52 | 4318 | continue; |
14662917 | 4319 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 4320 | continue; |
60eb1894 | 4321 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 4322 | continue; |
14662917 | 4323 | |
60eb1894 | 4324 | set_bit(tmp - min, inuse); |
20a69341 PM |
4325 | } |
4326 | ||
53b70287 | 4327 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
4328 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
4329 | min += BITS_PER_BYTE * PAGE_SIZE; | |
4330 | memset(inuse, 0, PAGE_SIZE); | |
4331 | goto cont; | |
4332 | } | |
20a69341 PM |
4333 | free_page((unsigned long)inuse); |
4334 | } | |
4335 | ||
33758c89 | 4336 | set->name = kasprintf(GFP_KERNEL_ACCOUNT, name, min + n); |
38745490 PS |
4337 | if (!set->name) |
4338 | return -ENOMEM; | |
4339 | ||
20a69341 | 4340 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
4341 | if (!nft_is_active_next(ctx->net, i)) |
4342 | continue; | |
e63aaaa6 AY |
4343 | if (!strcmp(set->name, i->name)) { |
4344 | kfree(set->name); | |
7fb6f78d | 4345 | set->name = NULL; |
20a69341 | 4346 | return -ENFILE; |
e63aaaa6 | 4347 | } |
20a69341 PM |
4348 | } |
4349 | return 0; | |
4350 | } | |
4351 | ||
917d80d3 | 4352 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
8e1102d5 FW |
4353 | { |
4354 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
4355 | u64 max = (u64)(~((u64)0)); | |
4356 | ||
4357 | max = div_u64(max, NSEC_PER_MSEC); | |
4358 | if (ms >= max) | |
4359 | return -ERANGE; | |
4360 | ||
4361 | ms *= NSEC_PER_MSEC; | |
4362 | *result = nsecs_to_jiffies64(ms); | |
4363 | return 0; | |
4364 | } | |
4365 | ||
917d80d3 | 4366 | __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 4367 | { |
3b15d09f | 4368 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
4369 | } |
4370 | ||
f3a2181e SB |
4371 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
4372 | const struct nft_set *set) | |
4373 | { | |
4374 | struct nlattr *concat, *field; | |
4375 | int i; | |
4376 | ||
4377 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
4378 | if (!concat) | |
4379 | return -ENOMEM; | |
4380 | ||
4381 | for (i = 0; i < set->field_count; i++) { | |
4382 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
4383 | if (!field) | |
4384 | return -ENOMEM; | |
4385 | ||
4386 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
4387 | htonl(set->field_len[i]))) | |
4388 | return -ENOMEM; | |
4389 | ||
4390 | nla_nest_end(skb, field); | |
4391 | } | |
4392 | ||
4393 | nla_nest_end(skb, concat); | |
4394 | ||
4395 | return 0; | |
4396 | } | |
4397 | ||
20a69341 PM |
4398 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
4399 | const struct nft_set *set, u16 event, u16 flags) | |
4400 | { | |
123b9961 PNA |
4401 | u64 timeout = READ_ONCE(set->timeout); |
4402 | u32 gc_int = READ_ONCE(set->gc_int); | |
128ad332 | 4403 | u32 portid = ctx->portid; |
123b9961 | 4404 | struct nlmsghdr *nlh; |
65038428 | 4405 | struct nlattr *nest; |
128ad332 | 4406 | u32 seq = ctx->seq; |
48b0ae04 | 4407 | int i; |
20a69341 | 4408 | |
dedb67c4 | 4409 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
4410 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
4411 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
4412 | if (!nlh) | |
20a69341 PM |
4413 | goto nla_put_failure; |
4414 | ||
20a69341 PM |
4415 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
4416 | goto nla_put_failure; | |
4417 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4418 | goto nla_put_failure; | |
3ecbfd65 HS |
4419 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
4420 | NFTA_SET_PAD)) | |
4421 | goto nla_put_failure; | |
28339b21 PNA |
4422 | |
4423 | if (event == NFT_MSG_DELSET) { | |
4424 | nlmsg_end(skb, nlh); | |
4425 | return 0; | |
4426 | } | |
4427 | ||
20a69341 PM |
4428 | if (set->flags != 0) |
4429 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
4430 | goto nla_put_failure; | |
4431 | ||
4432 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
4433 | goto nla_put_failure; | |
4434 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
4435 | goto nla_put_failure; | |
4436 | if (set->flags & NFT_SET_MAP) { | |
4437 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
4438 | goto nla_put_failure; | |
4439 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
4440 | goto nla_put_failure; | |
4441 | } | |
8aeff920 PNA |
4442 | if (set->flags & NFT_SET_OBJECT && |
4443 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
4444 | goto nla_put_failure; | |
20a69341 | 4445 | |
123b9961 | 4446 | if (timeout && |
d3e2a111 | 4447 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
123b9961 | 4448 | nf_jiffies64_to_msecs(timeout), |
b46f6ded | 4449 | NFTA_SET_PAD)) |
761da293 | 4450 | goto nla_put_failure; |
123b9961 PNA |
4451 | if (gc_int && |
4452 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(gc_int))) | |
761da293 PM |
4453 | goto nla_put_failure; |
4454 | ||
9363dc4b AB |
4455 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
4456 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
4457 | goto nla_put_failure; | |
4458 | } | |
4459 | ||
6f03bf43 PNA |
4460 | if (set->udata && |
4461 | nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) | |
e6d8ecac CFG |
4462 | goto nla_put_failure; |
4463 | ||
65038428 PNA |
4464 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
4465 | if (!nest) | |
c50b960c PM |
4466 | goto nla_put_failure; |
4467 | if (set->size && | |
4468 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
4469 | goto nla_put_failure; | |
f3a2181e SB |
4470 | |
4471 | if (set->field_count > 1 && | |
4472 | nf_tables_fill_set_concat(skb, set)) | |
4473 | goto nla_put_failure; | |
4474 | ||
65038428 PNA |
4475 | nla_nest_end(skb, nest); |
4476 | ||
8cfd9b0f | 4477 | if (set->num_exprs == 1) { |
65038428 | 4478 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); |
8daa8fde | 4479 | if (nf_tables_fill_expr_info(skb, set->exprs[0], false) < 0) |
65038428 PNA |
4480 | goto nla_put_failure; |
4481 | ||
4482 | nla_nest_end(skb, nest); | |
48b0ae04 PNA |
4483 | } else if (set->num_exprs > 1) { |
4484 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS); | |
4485 | if (nest == NULL) | |
65038428 PNA |
4486 | goto nla_put_failure; |
4487 | ||
48b0ae04 PNA |
4488 | for (i = 0; i < set->num_exprs; i++) { |
4489 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, | |
8daa8fde | 4490 | set->exprs[i], false) < 0) |
48b0ae04 PNA |
4491 | goto nla_put_failure; |
4492 | } | |
65038428 PNA |
4493 | nla_nest_end(skb, nest); |
4494 | } | |
c50b960c | 4495 | |
053c095a JB |
4496 | nlmsg_end(skb, nlh); |
4497 | return 0; | |
20a69341 PM |
4498 | |
4499 | nla_put_failure: | |
4500 | nlmsg_trim(skb, nlh); | |
4501 | return -1; | |
4502 | } | |
4503 | ||
25e94a99 PNA |
4504 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
4505 | const struct nft_set *set, int event, | |
4506 | gfp_t gfp_flags) | |
20a69341 | 4507 | { |
d59d2f82 | 4508 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
128ad332 | 4509 | u32 portid = ctx->portid; |
6fb721cf PNA |
4510 | struct sk_buff *skb; |
4511 | u16 flags = 0; | |
20a69341 PM |
4512 | int err; |
4513 | ||
128ad332 PNA |
4514 | if (!ctx->report && |
4515 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 4516 | return; |
20a69341 | 4517 | |
31f8441c | 4518 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
4519 | if (skb == NULL) |
4520 | goto err; | |
4521 | ||
6fb721cf PNA |
4522 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
4523 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
4524 | ||
4525 | err = nf_tables_fill_set(skb, ctx, set, event, flags); | |
20a69341 PM |
4526 | if (err < 0) { |
4527 | kfree_skb(skb); | |
4528 | goto err; | |
4529 | } | |
4530 | ||
0854db2a | 4531 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 4532 | return; |
20a69341 | 4533 | err: |
25e94a99 | 4534 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
4535 | } |
4536 | ||
5b96af77 | 4537 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
4538 | { |
4539 | const struct nft_set *set; | |
4540 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
4541 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
4542 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 4543 | struct nft_ctx *ctx = cb->data, ctx_set; |
0854db2a | 4544 | struct nftables_pernet *nft_net; |
c9c8e485 PNA |
4545 | |
4546 | if (cb->args[1]) | |
4547 | return skb->len; | |
4548 | ||
e688a7f8 | 4549 | rcu_read_lock(); |
d59d2f82 | 4550 | nft_net = nft_pernet(net); |
34002783 | 4551 | cb->seq = READ_ONCE(nft_net->base_seq); |
38e029f1 | 4552 | |
0854db2a | 4553 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 4554 | if (ctx->family != NFPROTO_UNSPEC && |
98319cb9 | 4555 | ctx->family != table->family) |
36596dad PNA |
4556 | continue; |
4557 | ||
4558 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
4559 | continue; |
4560 | ||
36596dad PNA |
4561 | if (cur_table) { |
4562 | if (cur_table != table) | |
c9c8e485 PNA |
4563 | continue; |
4564 | ||
36596dad | 4565 | cur_table = NULL; |
c9c8e485 | 4566 | } |
36596dad PNA |
4567 | idx = 0; |
4568 | list_for_each_entry_rcu(set, &table->sets, list) { | |
4569 | if (idx < s_idx) | |
4570 | goto cont; | |
4571 | if (!nft_is_active(net, set)) | |
4572 | goto cont; | |
5b96af77 | 4573 | |
36596dad PNA |
4574 | ctx_set = *ctx; |
4575 | ctx_set.table = table; | |
98319cb9 | 4576 | ctx_set.family = table->family; |
c9c8e485 | 4577 | |
36596dad PNA |
4578 | if (nf_tables_fill_set(skb, &ctx_set, set, |
4579 | NFT_MSG_NEWSET, | |
4580 | NLM_F_MULTI) < 0) { | |
4581 | cb->args[0] = idx; | |
4582 | cb->args[2] = (unsigned long) table; | |
4583 | goto done; | |
c9c8e485 | 4584 | } |
36596dad | 4585 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 4586 | cont: |
36596dad | 4587 | idx++; |
c9c8e485 | 4588 | } |
36596dad PNA |
4589 | if (s_idx) |
4590 | s_idx = 0; | |
c9c8e485 PNA |
4591 | } |
4592 | cb->args[1] = 1; | |
4593 | done: | |
e688a7f8 | 4594 | rcu_read_unlock(); |
c9c8e485 PNA |
4595 | return skb->len; |
4596 | } | |
4597 | ||
90fd131a FW |
4598 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
4599 | { | |
4600 | struct nft_ctx *ctx_dump = NULL; | |
4601 | ||
4602 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
4603 | if (ctx_dump == NULL) | |
4604 | return -ENOMEM; | |
4605 | ||
4606 | cb->data = ctx_dump; | |
4607 | return 0; | |
4608 | } | |
4609 | ||
5b96af77 | 4610 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 4611 | { |
5b96af77 PNA |
4612 | kfree(cb->data); |
4613 | return 0; | |
20a69341 PM |
4614 | } |
4615 | ||
d9adf22a | 4616 | /* called with rcu_read_lock held */ |
797d4980 PNA |
4617 | static int nf_tables_getset(struct sk_buff *skb, const struct nfnl_info *info, |
4618 | const struct nlattr * const nla[]) | |
20a69341 | 4619 | { |
797d4980 PNA |
4620 | struct netlink_ext_ack *extack = info->extack; |
4621 | u8 genmask = nft_genmask_cur(info->net); | |
67086651 PNA |
4622 | u8 family = info->nfmsg->nfgen_family; |
4623 | struct nft_table *table = NULL; | |
797d4980 | 4624 | struct net *net = info->net; |
20a69341 | 4625 | const struct nft_set *set; |
20a69341 | 4626 | struct sk_buff *skb2; |
797d4980 | 4627 | struct nft_ctx ctx; |
20a69341 PM |
4628 | int err; |
4629 | ||
67086651 PNA |
4630 | if (nla[NFTA_SET_TABLE]) { |
4631 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, | |
4632 | genmask, 0); | |
4633 | if (IS_ERR(table)) { | |
4634 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
4635 | return PTR_ERR(table); | |
4636 | } | |
4637 | } | |
4638 | ||
4639 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); | |
20a69341 | 4640 | |
797d4980 | 4641 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
20a69341 | 4642 | struct netlink_dump_control c = { |
90fd131a | 4643 | .start = nf_tables_dump_sets_start, |
20a69341 | 4644 | .dump = nf_tables_dump_sets, |
5b96af77 | 4645 | .done = nf_tables_dump_sets_done, |
90fd131a | 4646 | .data = &ctx, |
d9adf22a | 4647 | .module = THIS_MODULE, |
20a69341 | 4648 | }; |
5b96af77 | 4649 | |
797d4980 | 4650 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
20a69341 PM |
4651 | } |
4652 | ||
c9c8e485 | 4653 | /* Only accept unspec with dump */ |
ef4b65e5 | 4654 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
c9c8e485 | 4655 | return -EAFNOSUPPORT; |
eaa2bcd6 PT |
4656 | if (!nla[NFTA_SET_TABLE]) |
4657 | return -EINVAL; | |
c9c8e485 | 4658 | |
67086651 | 4659 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
4660 | if (IS_ERR(set)) |
4661 | return PTR_ERR(set); | |
4662 | ||
d9adf22a | 4663 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
4664 | if (skb2 == NULL) |
4665 | return -ENOMEM; | |
4666 | ||
4667 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
4668 | if (err < 0) | |
ee921183 | 4669 | goto err_fill_set_info; |
20a69341 | 4670 | |
ee921183 | 4671 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
20a69341 | 4672 | |
ee921183 | 4673 | err_fill_set_info: |
20a69341 PM |
4674 | kfree_skb(skb2); |
4675 | return err; | |
4676 | } | |
4677 | ||
f3a2181e SB |
4678 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
4679 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
4680 | }; | |
4681 | ||
4682 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
4683 | struct nft_set_desc *desc) | |
4684 | { | |
4685 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
4686 | u32 len; | |
4687 | int err; | |
4688 | ||
fecf31ee PNA |
4689 | if (desc->field_count >= ARRAY_SIZE(desc->field_len)) |
4690 | return -E2BIG; | |
4691 | ||
f3a2181e SB |
4692 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, |
4693 | nft_concat_policy, NULL); | |
4694 | if (err < 0) | |
4695 | return err; | |
4696 | ||
4697 | if (!tb[NFTA_SET_FIELD_LEN]) | |
4698 | return -EINVAL; | |
4699 | ||
4700 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
fecf31ee PNA |
4701 | if (!len || len > U8_MAX) |
4702 | return -EINVAL; | |
f3a2181e SB |
4703 | |
4704 | desc->field_len[desc->field_count++] = len; | |
4705 | ||
4706 | return 0; | |
4707 | } | |
4708 | ||
4709 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
4710 | const struct nlattr *nla) | |
4711 | { | |
4712 | struct nlattr *attr; | |
fecf31ee PNA |
4713 | u32 num_regs = 0; |
4714 | int rem, err, i; | |
f3a2181e SB |
4715 | |
4716 | nla_for_each_nested(attr, nla, rem) { | |
4717 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
4718 | return -EINVAL; | |
4719 | ||
4720 | err = nft_set_desc_concat_parse(attr, desc); | |
4721 | if (err < 0) | |
4722 | return err; | |
4723 | } | |
4724 | ||
fecf31ee PNA |
4725 | for (i = 0; i < desc->field_count; i++) |
4726 | num_regs += DIV_ROUND_UP(desc->field_len[i], sizeof(u32)); | |
4727 | ||
4728 | if (num_regs > NFT_REG32_COUNT) | |
4729 | return -E2BIG; | |
4730 | ||
f3a2181e SB |
4731 | return 0; |
4732 | } | |
4733 | ||
f7e840ee | 4734 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
4735 | const struct nlattr *nla) |
4736 | { | |
4737 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
4738 | int err; | |
4739 | ||
8cb08174 JB |
4740 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
4741 | nft_set_desc_policy, NULL); | |
c50b960c PM |
4742 | if (err < 0) |
4743 | return err; | |
4744 | ||
4745 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
4746 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
4747 | if (da[NFTA_SET_DESC_CONCAT]) |
4748 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 4749 | |
f3a2181e | 4750 | return err; |
c50b960c PM |
4751 | } |
4752 | ||
a8fe4154 PNA |
4753 | static int nft_set_expr_alloc(struct nft_ctx *ctx, struct nft_set *set, |
4754 | const struct nlattr * const *nla, | |
4755 | struct nft_expr **exprs, int *num_exprs, | |
4756 | u32 flags) | |
4757 | { | |
4758 | struct nft_expr *expr; | |
4759 | int err, i; | |
4760 | ||
4761 | if (nla[NFTA_SET_EXPR]) { | |
4762 | expr = nft_set_elem_expr_alloc(ctx, set, nla[NFTA_SET_EXPR]); | |
4763 | if (IS_ERR(expr)) { | |
4764 | err = PTR_ERR(expr); | |
4765 | goto err_set_expr_alloc; | |
4766 | } | |
4767 | exprs[0] = expr; | |
4768 | (*num_exprs)++; | |
4769 | } else if (nla[NFTA_SET_EXPRESSIONS]) { | |
4770 | struct nlattr *tmp; | |
4771 | int left; | |
4772 | ||
4773 | if (!(flags & NFT_SET_EXPR)) { | |
4774 | err = -EINVAL; | |
4775 | goto err_set_expr_alloc; | |
4776 | } | |
4777 | i = 0; | |
4778 | nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) { | |
4779 | if (i == NFT_SET_EXPR_MAX) { | |
4780 | err = -E2BIG; | |
4781 | goto err_set_expr_alloc; | |
4782 | } | |
4783 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
4784 | err = -EINVAL; | |
4785 | goto err_set_expr_alloc; | |
4786 | } | |
4787 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
4788 | if (IS_ERR(expr)) { | |
4789 | err = PTR_ERR(expr); | |
4790 | goto err_set_expr_alloc; | |
4791 | } | |
4792 | exprs[i++] = expr; | |
4793 | (*num_exprs)++; | |
4794 | } | |
4795 | } | |
4796 | ||
4797 | return 0; | |
4798 | ||
4799 | err_set_expr_alloc: | |
4800 | for (i = 0; i < *num_exprs; i++) | |
4801 | nft_expr_destroy(ctx, exprs[i]); | |
4802 | ||
4803 | return err; | |
4804 | } | |
4805 | ||
f6594c37 PNA |
4806 | static bool nft_set_is_same(const struct nft_set *set, |
4807 | const struct nft_set_desc *desc, | |
4808 | struct nft_expr *exprs[], u32 num_exprs, u32 flags) | |
4809 | { | |
4810 | int i; | |
4811 | ||
4812 | if (set->ktype != desc->ktype || | |
4813 | set->dtype != desc->dtype || | |
4814 | set->flags != flags || | |
4815 | set->klen != desc->klen || | |
4816 | set->dlen != desc->dlen || | |
4817 | set->field_count != desc->field_count || | |
4818 | set->num_exprs != num_exprs) | |
4819 | return false; | |
4820 | ||
4821 | for (i = 0; i < desc->field_count; i++) { | |
4822 | if (set->field_len[i] != desc->field_len[i]) | |
4823 | return false; | |
4824 | } | |
4825 | ||
4826 | for (i = 0; i < num_exprs; i++) { | |
4827 | if (set->exprs[i]->ops != exprs[i]->ops) | |
4828 | return false; | |
4829 | } | |
4830 | ||
4831 | return true; | |
4832 | } | |
4833 | ||
7dab8ee3 PNA |
4834 | static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, |
4835 | const struct nlattr * const nla[]) | |
20a69341 | 4836 | { |
7dab8ee3 PNA |
4837 | struct netlink_ext_ack *extack = info->extack; |
4838 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 4839 | u8 family = info->nfmsg->nfgen_family; |
20a69341 | 4840 | const struct nft_set_ops *ops; |
7dab8ee3 PNA |
4841 | struct net *net = info->net; |
4842 | struct nft_set_desc desc; | |
20a69341 | 4843 | struct nft_table *table; |
7dab8ee3 | 4844 | unsigned char *udata; |
20a69341 PM |
4845 | struct nft_set *set; |
4846 | struct nft_ctx ctx; | |
6c8774a9 | 4847 | size_t alloc_size; |
a8fe4154 | 4848 | int num_exprs = 0; |
7dab8ee3 PNA |
4849 | char *name; |
4850 | int err, i; | |
e6d8ecac | 4851 | u16 udlen; |
bed4a63e | 4852 | u32 flags; |
7dab8ee3 | 4853 | u64 size; |
20a69341 PM |
4854 | |
4855 | if (nla[NFTA_SET_TABLE] == NULL || | |
4856 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
4857 | nla[NFTA_SET_KEY_LEN] == NULL || |
4858 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
4859 | return -EINVAL; |
4860 | ||
c50b960c PM |
4861 | memset(&desc, 0, sizeof(desc)); |
4862 | ||
bed4a63e | 4863 | desc.ktype = NFT_DATA_VALUE; |
20a69341 | 4864 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { |
bed4a63e PNA |
4865 | desc.ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); |
4866 | if ((desc.ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
20a69341 PM |
4867 | return -EINVAL; |
4868 | } | |
4869 | ||
c50b960c | 4870 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 4871 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4872 | return -EINVAL; |
4873 | ||
4874 | flags = 0; | |
4875 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
4876 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
4877 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 4878 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 4879 | NFT_SET_MAP | NFT_SET_EVAL | |
b4e70d8d | 4880 | NFT_SET_OBJECT | NFT_SET_CONCAT | NFT_SET_EXPR)) |
d9583cdf | 4881 | return -EOPNOTSUPP; |
8aeff920 | 4882 | /* Only one of these operations is supported */ |
acab7131 FW |
4883 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
4884 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
4885 | return -EOPNOTSUPP; | |
4886 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
4887 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 4888 | return -EOPNOTSUPP; |
20a69341 PM |
4889 | } |
4890 | ||
bed4a63e | 4891 | desc.dtype = 0; |
20a69341 PM |
4892 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
4893 | if (!(flags & NFT_SET_MAP)) | |
4894 | return -EINVAL; | |
4895 | ||
bed4a63e PNA |
4896 | desc.dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); |
4897 | if ((desc.dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
4898 | desc.dtype != NFT_DATA_VERDICT) | |
20a69341 PM |
4899 | return -EINVAL; |
4900 | ||
bed4a63e | 4901 | if (desc.dtype != NFT_DATA_VERDICT) { |
20a69341 PM |
4902 | if (nla[NFTA_SET_DATA_LEN] == NULL) |
4903 | return -EINVAL; | |
c50b960c | 4904 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4905 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4906 | return -EINVAL; |
4907 | } else | |
7d740264 | 4908 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4909 | } else if (flags & NFT_SET_MAP) |
4910 | return -EINVAL; | |
4911 | ||
8aeff920 PNA |
4912 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4913 | if (!(flags & NFT_SET_OBJECT)) | |
4914 | return -EINVAL; | |
4915 | ||
bed4a63e PNA |
4916 | desc.objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); |
4917 | if (desc.objtype == NFT_OBJECT_UNSPEC || | |
4918 | desc.objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4919 | return -EOPNOTSUPP; |
8aeff920 PNA |
4920 | } else if (flags & NFT_SET_OBJECT) |
4921 | return -EINVAL; | |
4922 | else | |
bed4a63e | 4923 | desc.objtype = NFT_OBJECT_UNSPEC; |
8aeff920 | 4924 | |
bed4a63e | 4925 | desc.timeout = 0; |
761da293 PM |
4926 | if (nla[NFTA_SET_TIMEOUT] != NULL) { |
4927 | if (!(flags & NFT_SET_TIMEOUT)) | |
4928 | return -EINVAL; | |
8e1102d5 | 4929 | |
e26d3009 PNA |
4930 | if (flags & NFT_SET_ANONYMOUS) |
4931 | return -EOPNOTSUPP; | |
4932 | ||
bed4a63e | 4933 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &desc.timeout); |
8e1102d5 FW |
4934 | if (err) |
4935 | return err; | |
761da293 | 4936 | } |
bed4a63e | 4937 | desc.gc_int = 0; |
761da293 PM |
4938 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { |
4939 | if (!(flags & NFT_SET_TIMEOUT)) | |
4940 | return -EINVAL; | |
e26d3009 PNA |
4941 | |
4942 | if (flags & NFT_SET_ANONYMOUS) | |
4943 | return -EOPNOTSUPP; | |
4944 | ||
bed4a63e | 4945 | desc.gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); |
761da293 PM |
4946 | } |
4947 | ||
bed4a63e | 4948 | desc.policy = NFT_SET_POL_PERFORMANCE; |
c50b960c | 4949 | if (nla[NFTA_SET_POLICY] != NULL) |
bed4a63e | 4950 | desc.policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); |
c50b960c PM |
4951 | |
4952 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4953 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4954 | if (err < 0) |
4955 | return err; | |
1b6345d4 PNA |
4956 | |
4957 | if (desc.field_count > 1 && !(flags & NFT_SET_CONCAT)) | |
4958 | return -EINVAL; | |
4959 | } else if (flags & NFT_SET_CONCAT) { | |
4960 | return -EINVAL; | |
c50b960c PM |
4961 | } |
4962 | ||
48b0ae04 | 4963 | if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS]) |
d56aab26 PNA |
4964 | desc.expr = true; |
4965 | ||
6001a930 PNA |
4966 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask, |
4967 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
4968 | if (IS_ERR(table)) { |
4969 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4970 | return PTR_ERR(table); |
36dd1bcc | 4971 | } |
20a69341 | 4972 | |
7dab8ee3 | 4973 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
20a69341 | 4974 | |
cac20fcd | 4975 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4976 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4977 | if (PTR_ERR(set) != -ENOENT) { |
4978 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4979 | return PTR_ERR(set); |
36dd1bcc | 4980 | } |
1a28ad74 | 4981 | } else { |
a8fe4154 PNA |
4982 | struct nft_expr *exprs[NFT_SET_EXPR_MAX] = {}; |
4983 | ||
7dab8ee3 | 4984 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 4985 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); |
20a69341 | 4986 | return -EEXIST; |
36dd1bcc | 4987 | } |
7dab8ee3 | 4988 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
20a69341 | 4989 | return -EOPNOTSUPP; |
36dd1bcc | 4990 | |
b770283c PNA |
4991 | if (nft_set_is_anonymous(set)) |
4992 | return -EOPNOTSUPP; | |
4993 | ||
a8fe4154 PNA |
4994 | err = nft_set_expr_alloc(&ctx, set, nla, exprs, &num_exprs, flags); |
4995 | if (err < 0) | |
4996 | return err; | |
4997 | ||
f6594c37 PNA |
4998 | err = 0; |
4999 | if (!nft_set_is_same(set, &desc, exprs, num_exprs, flags)) { | |
5000 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
5001 | err = -EEXIST; | |
5002 | } | |
5003 | ||
a8fe4154 PNA |
5004 | for (i = 0; i < num_exprs; i++) |
5005 | nft_expr_destroy(&ctx, exprs[i]); | |
5006 | ||
123b9961 PNA |
5007 | if (err < 0) |
5008 | return err; | |
5009 | ||
5010 | return __nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set, &desc); | |
20a69341 PM |
5011 | } |
5012 | ||
7dab8ee3 | 5013 | if (!(info->nlh->nlmsg_flags & NLM_F_CREATE)) |
20a69341 PM |
5014 | return -ENOENT; |
5015 | ||
bed4a63e | 5016 | ops = nft_select_set_ops(&ctx, nla, &desc); |
20a69341 PM |
5017 | if (IS_ERR(ops)) |
5018 | return PTR_ERR(ops); | |
5019 | ||
e6d8ecac CFG |
5020 | udlen = 0; |
5021 | if (nla[NFTA_SET_USERDATA]) | |
5022 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
5023 | ||
20a69341 PM |
5024 | size = 0; |
5025 | if (ops->privsize != NULL) | |
347b408d | 5026 | size = ops->privsize(nla, &desc); |
6c8774a9 | 5027 | alloc_size = sizeof(*set) + size + udlen; |
45928afe | 5028 | if (alloc_size < size || alloc_size > INT_MAX) |
6c8774a9 | 5029 | return -ENOMEM; |
1689f259 PNA |
5030 | |
5031 | if (!nft_use_inc(&table->use)) | |
5032 | return -EMFILE; | |
5033 | ||
33758c89 | 5034 | set = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT); |
1689f259 PNA |
5035 | if (!set) { |
5036 | err = -ENOMEM; | |
5037 | goto err_alloc; | |
5038 | } | |
20a69341 | 5039 | |
33758c89 | 5040 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL_ACCOUNT); |
38745490 PS |
5041 | if (!name) { |
5042 | err = -ENOMEM; | |
65038428 | 5043 | goto err_set_name; |
38745490 PS |
5044 | } |
5045 | ||
20a69341 | 5046 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 5047 | kfree(name); |
20a69341 | 5048 | if (err < 0) |
ad9f151e PNA |
5049 | goto err_set_name; |
5050 | ||
5051 | udata = NULL; | |
5052 | if (udlen) { | |
5053 | udata = set->data + size; | |
5054 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
5055 | } | |
5056 | ||
5057 | INIT_LIST_HEAD(&set->bindings); | |
5058 | INIT_LIST_HEAD(&set->catchall_list); | |
5f68718b | 5059 | refcount_set(&set->refs, 1); |
ad9f151e PNA |
5060 | set->table = table; |
5061 | write_pnet(&set->net, net); | |
5062 | set->ops = ops; | |
bed4a63e | 5063 | set->ktype = desc.ktype; |
ad9f151e | 5064 | set->klen = desc.klen; |
bed4a63e PNA |
5065 | set->dtype = desc.dtype; |
5066 | set->objtype = desc.objtype; | |
ad9f151e PNA |
5067 | set->dlen = desc.dlen; |
5068 | set->flags = flags; | |
5069 | set->size = desc.size; | |
bed4a63e | 5070 | set->policy = desc.policy; |
ad9f151e PNA |
5071 | set->udlen = udlen; |
5072 | set->udata = udata; | |
bed4a63e PNA |
5073 | set->timeout = desc.timeout; |
5074 | set->gc_int = desc.gc_int; | |
ad9f151e PNA |
5075 | |
5076 | set->field_count = desc.field_count; | |
5077 | for (i = 0; i < desc.field_count; i++) | |
5078 | set->field_len[i] = desc.field_len[i]; | |
5079 | ||
5080 | err = ops->init(set, &desc, nla); | |
5081 | if (err < 0) | |
5082 | goto err_set_init; | |
65038428 | 5083 | |
a8fe4154 PNA |
5084 | err = nft_set_expr_alloc(&ctx, set, nla, set->exprs, &num_exprs, flags); |
5085 | if (err < 0) | |
5086 | goto err_set_destroy; | |
20a69341 | 5087 | |
a8fe4154 | 5088 | set->num_exprs = num_exprs; |
3ecbfd65 | 5089 | set->handle = nf_tables_alloc_handle(table); |
212ed75d | 5090 | INIT_LIST_HEAD(&set->pending_update); |
20a69341 | 5091 | |
958bee14 | 5092 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 5093 | if (err < 0) |
ad9f151e | 5094 | goto err_set_expr_alloc; |
20a69341 | 5095 | |
e688a7f8 | 5096 | list_add_tail_rcu(&set->list, &table->sets); |
1689f259 | 5097 | |
20a69341 PM |
5098 | return 0; |
5099 | ||
ad9f151e | 5100 | err_set_expr_alloc: |
8cfd9b0f PNA |
5101 | for (i = 0; i < set->num_exprs; i++) |
5102 | nft_expr_destroy(&ctx, set->exprs[i]); | |
a8fe4154 | 5103 | err_set_destroy: |
628bd3e4 | 5104 | ops->destroy(&ctx, set); |
ad9f151e | 5105 | err_set_init: |
2f6adf48 | 5106 | kfree(set->name); |
65038428 | 5107 | err_set_name: |
1ff75a3e | 5108 | kvfree(set); |
1689f259 PNA |
5109 | err_alloc: |
5110 | nft_use_dec_restore(&table->use); | |
5111 | ||
20a69341 PM |
5112 | return err; |
5113 | } | |
5114 | ||
aaa31047 PNA |
5115 | static void nft_set_catchall_destroy(const struct nft_ctx *ctx, |
5116 | struct nft_set *set) | |
5117 | { | |
0f7d9b31 | 5118 | struct nft_set_elem_catchall *next, *catchall; |
aaa31047 | 5119 | |
0f7d9b31 | 5120 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
aaa31047 | 5121 | list_del_rcu(&catchall->list); |
628bd3e4 | 5122 | nf_tables_set_elem_destroy(ctx, set, catchall->elem); |
ae089831 | 5123 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
5124 | } |
5125 | } | |
5126 | ||
5f68718b PNA |
5127 | static void nft_set_put(struct nft_set *set) |
5128 | { | |
5129 | if (refcount_dec_and_test(&set->refs)) { | |
5130 | kfree(set->name); | |
5131 | kvfree(set); | |
5132 | } | |
5133 | } | |
5134 | ||
0c2a85ed | 5135 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 5136 | { |
8cfd9b0f PNA |
5137 | int i; |
5138 | ||
273fe3f1 PNA |
5139 | if (WARN_ON(set->use > 0)) |
5140 | return; | |
5141 | ||
8cfd9b0f PNA |
5142 | for (i = 0; i < set->num_exprs; i++) |
5143 | nft_expr_destroy(ctx, set->exprs[i]); | |
65038428 | 5144 | |
628bd3e4 | 5145 | set->ops->destroy(ctx, set); |
aaa31047 | 5146 | nft_set_catchall_destroy(ctx, set); |
5f68718b | 5147 | nft_set_put(set); |
20a69341 PM |
5148 | } |
5149 | ||
7dab8ee3 PNA |
5150 | static int nf_tables_delset(struct sk_buff *skb, const struct nfnl_info *info, |
5151 | const struct nlattr * const nla[]) | |
20a69341 | 5152 | { |
7dab8ee3 PNA |
5153 | struct netlink_ext_ack *extack = info->extack; |
5154 | u8 genmask = nft_genmask_next(info->net); | |
67086651 | 5155 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 5156 | struct net *net = info->net; |
36dd1bcc | 5157 | const struct nlattr *attr; |
67086651 | 5158 | struct nft_table *table; |
20a69341 PM |
5159 | struct nft_set *set; |
5160 | struct nft_ctx ctx; | |
20a69341 | 5161 | |
ef4b65e5 | 5162 | if (info->nfmsg->nfgen_family == NFPROTO_UNSPEC) |
ec2c9935 | 5163 | return -EAFNOSUPPORT; |
20a69341 | 5164 | |
67086651 PNA |
5165 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
5166 | genmask, NETLINK_CB(skb).portid); | |
5167 | if (IS_ERR(table)) { | |
5168 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
5169 | return PTR_ERR(table); | |
5170 | } | |
20a69341 | 5171 | |
36dd1bcc PNA |
5172 | if (nla[NFTA_SET_HANDLE]) { |
5173 | attr = nla[NFTA_SET_HANDLE]; | |
67086651 | 5174 | set = nft_set_lookup_byhandle(table, attr, genmask); |
36dd1bcc PNA |
5175 | } else { |
5176 | attr = nla[NFTA_SET_NAME]; | |
67086651 | 5177 | set = nft_set_lookup(table, attr, genmask); |
36dd1bcc | 5178 | } |
a8278400 | 5179 | |
36dd1bcc | 5180 | if (IS_ERR(set)) { |
f80a612d FFM |
5181 | if (PTR_ERR(set) == -ENOENT && |
5182 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSET) | |
5183 | return 0; | |
5184 | ||
36dd1bcc PNA |
5185 | NL_SET_BAD_ATTR(extack, attr); |
5186 | return PTR_ERR(set); | |
5187 | } | |
273fe3f1 | 5188 | if (set->use || |
7dab8ee3 PNA |
5189 | (info->nlh->nlmsg_flags & NLM_F_NONREC && |
5190 | atomic_read(&set->nelems) > 0)) { | |
36dd1bcc | 5191 | NL_SET_BAD_ATTR(extack, attr); |
20a69341 | 5192 | return -EBUSY; |
36dd1bcc | 5193 | } |
20a69341 | 5194 | |
67086651 PNA |
5195 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5196 | ||
ee01d542 | 5197 | return nft_delset(&ctx, set); |
20a69341 PM |
5198 | } |
5199 | ||
345023b0 PNA |
5200 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
5201 | enum nft_registers reg, | |
5202 | const struct nft_data *data, | |
5203 | enum nft_data_types type, | |
5204 | unsigned int len); | |
5205 | ||
97c976d6 PNA |
5206 | static int nft_setelem_data_validate(const struct nft_ctx *ctx, |
5207 | struct nft_set *set, | |
5208 | struct nft_set_elem *elem) | |
20a69341 | 5209 | { |
fe2811eb | 5210 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5211 | enum nft_registers dreg; |
5212 | ||
5213 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
5214 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
5215 | set->dtype == NFT_DATA_VERDICT ? | |
5216 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
5217 | set->dlen); | |
20a69341 PM |
5218 | } |
5219 | ||
97c976d6 PNA |
5220 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, |
5221 | struct nft_set *set, | |
5222 | const struct nft_set_iter *iter, | |
5223 | struct nft_set_elem *elem) | |
5224 | { | |
5225 | return nft_setelem_data_validate(ctx, set, elem); | |
5226 | } | |
5227 | ||
aaa31047 PNA |
5228 | static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
5229 | struct nft_set *set) | |
5230 | { | |
5231 | u8 genmask = nft_genmask_next(ctx->net); | |
5232 | struct nft_set_elem_catchall *catchall; | |
5233 | struct nft_set_elem elem; | |
5234 | struct nft_set_ext *ext; | |
5235 | int ret = 0; | |
5236 | ||
5237 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5238 | ext = nft_set_elem_ext(set, catchall->elem); | |
5239 | if (!nft_set_elem_active(ext, genmask)) | |
5240 | continue; | |
5241 | ||
5242 | elem.priv = catchall->elem; | |
5243 | ret = nft_setelem_data_validate(ctx, set, &elem); | |
5244 | if (ret < 0) | |
5245 | break; | |
5246 | } | |
5247 | ||
5248 | return ret; | |
5249 | } | |
5250 | ||
20a69341 PM |
5251 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
5252 | struct nft_set_binding *binding) | |
5253 | { | |
5254 | struct nft_set_binding *i; | |
5255 | struct nft_set_iter iter; | |
5256 | ||
408070d6 | 5257 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
5258 | return -EBUSY; |
5259 | ||
11113e19 | 5260 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
5261 | /* If the set is already bound to the same chain all |
5262 | * jumps are already validated for that chain. | |
5263 | */ | |
5264 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 5265 | if (i->flags & NFT_SET_MAP && |
11113e19 | 5266 | i->chain == binding->chain) |
20a69341 PM |
5267 | goto bind; |
5268 | } | |
5269 | ||
8588ac09 | 5270 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
5271 | iter.skip = 0; |
5272 | iter.count = 0; | |
5273 | iter.err = 0; | |
5274 | iter.fn = nf_tables_bind_check_setelem; | |
5275 | ||
5276 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
5277 | if (!iter.err) |
5278 | iter.err = nft_set_catchall_bind_check(ctx, set); | |
5279 | ||
a02f4248 | 5280 | if (iter.err < 0) |
20a69341 | 5281 | return iter.err; |
20a69341 PM |
5282 | } |
5283 | bind: | |
1689f259 PNA |
5284 | if (!nft_use_inc(&set->use)) |
5285 | return -EMFILE; | |
5286 | ||
20a69341 | 5287 | binding->chain = ctx->chain; |
e688a7f8 | 5288 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 PNA |
5289 | nft_set_trans_bind(ctx, set); |
5290 | ||
20a69341 PM |
5291 | return 0; |
5292 | } | |
63aea290 | 5293 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 5294 | |
3b0a081d FW |
5295 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
5296 | struct nft_set_binding *binding, bool event) | |
20a69341 | 5297 | { |
e688a7f8 | 5298 | list_del_rcu(&binding->list); |
20a69341 | 5299 | |
f6ac8585 | 5300 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 5301 | list_del_rcu(&set->list); |
f6ac8585 PNA |
5302 | if (event) |
5303 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
5304 | GFP_KERNEL); | |
5305 | } | |
20a69341 PM |
5306 | } |
5307 | ||
628bd3e4 PNA |
5308 | static void nft_setelem_data_activate(const struct net *net, |
5309 | const struct nft_set *set, | |
5310 | struct nft_set_elem *elem); | |
5311 | ||
5312 | static int nft_mapelem_activate(const struct nft_ctx *ctx, | |
5313 | struct nft_set *set, | |
5314 | const struct nft_set_iter *iter, | |
5315 | struct nft_set_elem *elem) | |
5316 | { | |
5317 | nft_setelem_data_activate(ctx->net, set, elem); | |
5318 | ||
5319 | return 0; | |
5320 | } | |
5321 | ||
5322 | static void nft_map_catchall_activate(const struct nft_ctx *ctx, | |
5323 | struct nft_set *set) | |
5324 | { | |
5325 | u8 genmask = nft_genmask_next(ctx->net); | |
5326 | struct nft_set_elem_catchall *catchall; | |
5327 | struct nft_set_elem elem; | |
5328 | struct nft_set_ext *ext; | |
5329 | ||
5330 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
5331 | ext = nft_set_elem_ext(set, catchall->elem); | |
5332 | if (!nft_set_elem_active(ext, genmask)) | |
5333 | continue; | |
5334 | ||
5335 | elem.priv = catchall->elem; | |
5336 | nft_setelem_data_activate(ctx->net, set, &elem); | |
5337 | break; | |
5338 | } | |
5339 | } | |
5340 | ||
5341 | static void nft_map_activate(const struct nft_ctx *ctx, struct nft_set *set) | |
5342 | { | |
5343 | struct nft_set_iter iter = { | |
5344 | .genmask = nft_genmask_next(ctx->net), | |
5345 | .fn = nft_mapelem_activate, | |
5346 | }; | |
5347 | ||
5348 | set->ops->walk(ctx, set, &iter); | |
5349 | WARN_ON_ONCE(iter.err); | |
5350 | ||
5351 | nft_map_catchall_activate(ctx, set); | |
5352 | } | |
5353 | ||
c1592a89 PNA |
5354 | void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set) |
5355 | { | |
628bd3e4 PNA |
5356 | if (nft_set_is_anonymous(set)) { |
5357 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5358 | nft_map_activate(ctx, set); | |
5359 | ||
c1592a89 | 5360 | nft_clear(ctx->net, set); |
628bd3e4 | 5361 | } |
c1592a89 | 5362 | |
1689f259 | 5363 | nft_use_inc_restore(&set->use); |
c1592a89 PNA |
5364 | } |
5365 | EXPORT_SYMBOL_GPL(nf_tables_activate_set); | |
5366 | ||
273fe3f1 PNA |
5367 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
5368 | struct nft_set_binding *binding, | |
5369 | enum nft_trans_phase phase) | |
5370 | { | |
5371 | switch (phase) { | |
26b5a571 PNA |
5372 | case NFT_TRANS_PREPARE_ERROR: |
5373 | nft_set_trans_unbind(ctx, set); | |
c1592a89 PNA |
5374 | if (nft_set_is_anonymous(set)) |
5375 | nft_deactivate_next(ctx->net, set); | |
3e704897 PNA |
5376 | else |
5377 | list_del_rcu(&binding->list); | |
c1592a89 | 5378 | |
1689f259 | 5379 | nft_use_dec(&set->use); |
26b5a571 | 5380 | break; |
273fe3f1 | 5381 | case NFT_TRANS_PREPARE: |
628bd3e4 PNA |
5382 | if (nft_set_is_anonymous(set)) { |
5383 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5384 | nft_map_deactivate(ctx, set); | |
c1592a89 | 5385 | |
628bd3e4 PNA |
5386 | nft_deactivate_next(ctx->net, set); |
5387 | } | |
1689f259 | 5388 | nft_use_dec(&set->use); |
273fe3f1 PNA |
5389 | return; |
5390 | case NFT_TRANS_ABORT: | |
5391 | case NFT_TRANS_RELEASE: | |
628bd3e4 PNA |
5392 | if (nft_set_is_anonymous(set) && |
5393 | set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) | |
5394 | nft_map_deactivate(ctx, set); | |
5395 | ||
1689f259 | 5396 | nft_use_dec(&set->use); |
954d8297 | 5397 | fallthrough; |
273fe3f1 PNA |
5398 | default: |
5399 | nf_tables_unbind_set(ctx, set, binding, | |
5400 | phase == NFT_TRANS_COMMIT); | |
5401 | } | |
5402 | } | |
5403 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
5404 | ||
cd5125d8 FW |
5405 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
5406 | { | |
f6ac8585 | 5407 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 5408 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
5409 | } |
5410 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
5411 | ||
3ac4c07a PM |
5412 | const struct nft_set_ext_type nft_set_ext_types[] = { |
5413 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 5414 | .align = __alignof__(u32), |
3ac4c07a PM |
5415 | }, |
5416 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 5417 | .align = __alignof__(u32), |
3ac4c07a | 5418 | }, |
563125a7 PNA |
5419 | [NFT_SET_EXT_EXPRESSIONS] = { |
5420 | .align = __alignof__(struct nft_set_elem_expr), | |
f25ad2e9 | 5421 | }, |
8aeff920 PNA |
5422 | [NFT_SET_EXT_OBJREF] = { |
5423 | .len = sizeof(struct nft_object *), | |
5424 | .align = __alignof__(struct nft_object *), | |
5425 | }, | |
3ac4c07a PM |
5426 | [NFT_SET_EXT_FLAGS] = { |
5427 | .len = sizeof(u8), | |
5428 | .align = __alignof__(u8), | |
5429 | }, | |
c3e1b005 PM |
5430 | [NFT_SET_EXT_TIMEOUT] = { |
5431 | .len = sizeof(u64), | |
5432 | .align = __alignof__(u64), | |
5433 | }, | |
5434 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
5435 | .len = sizeof(u64), |
5436 | .align = __alignof__(u64), | |
c3e1b005 | 5437 | }, |
68e942e8 PM |
5438 | [NFT_SET_EXT_USERDATA] = { |
5439 | .len = sizeof(struct nft_userdata), | |
5440 | .align = __alignof__(struct nft_userdata), | |
5441 | }, | |
7b225d0b PNA |
5442 | [NFT_SET_EXT_KEY_END] = { |
5443 | .align = __alignof__(u32), | |
5444 | }, | |
3ac4c07a | 5445 | }; |
3ac4c07a | 5446 | |
20a69341 PM |
5447 | /* |
5448 | * Set elements | |
5449 | */ | |
5450 | ||
5451 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
5452 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
5453 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
5454 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 5455 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 5456 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
5457 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
5458 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 5459 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
5460 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
5461 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 5462 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
48b0ae04 | 5463 | [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED }, |
20a69341 PM |
5464 | }; |
5465 | ||
5466 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
5467 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
5468 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
5469 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
5470 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 5471 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 5472 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
5473 | }; |
5474 | ||
563125a7 PNA |
5475 | static int nft_set_elem_expr_dump(struct sk_buff *skb, |
5476 | const struct nft_set *set, | |
079cd633 PS |
5477 | const struct nft_set_ext *ext, |
5478 | bool reset) | |
563125a7 PNA |
5479 | { |
5480 | struct nft_set_elem_expr *elem_expr; | |
5481 | u32 size, num_exprs = 0; | |
5482 | struct nft_expr *expr; | |
48b0ae04 | 5483 | struct nlattr *nest; |
563125a7 PNA |
5484 | |
5485 | elem_expr = nft_set_ext_expr(ext); | |
5486 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
5487 | num_exprs++; | |
5488 | ||
5489 | if (num_exprs == 1) { | |
5490 | expr = nft_setelem_expr_at(elem_expr, 0); | |
079cd633 | 5491 | if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr, reset) < 0) |
563125a7 PNA |
5492 | return -1; |
5493 | ||
5494 | return 0; | |
48b0ae04 PNA |
5495 | } else if (num_exprs > 1) { |
5496 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS); | |
5497 | if (nest == NULL) | |
5498 | goto nla_put_failure; | |
563125a7 | 5499 | |
48b0ae04 PNA |
5500 | nft_setelem_expr_foreach(expr, elem_expr, size) { |
5501 | expr = nft_setelem_expr_at(elem_expr, size); | |
079cd633 | 5502 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr, reset) < 0) |
48b0ae04 PNA |
5503 | goto nla_put_failure; |
5504 | } | |
5505 | nla_nest_end(skb, nest); | |
5506 | } | |
563125a7 | 5507 | return 0; |
48b0ae04 PNA |
5508 | |
5509 | nla_put_failure: | |
5510 | return -1; | |
563125a7 PNA |
5511 | } |
5512 | ||
20a69341 PM |
5513 | static int nf_tables_fill_setelem(struct sk_buff *skb, |
5514 | const struct nft_set *set, | |
079cd633 PS |
5515 | const struct nft_set_elem *elem, |
5516 | bool reset) | |
20a69341 | 5517 | { |
fe2811eb | 5518 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5519 | unsigned char *b = skb_tail_pointer(skb); |
5520 | struct nlattr *nest; | |
079cd633 | 5521 | u64 timeout = 0; |
20a69341 | 5522 | |
ae0be8de | 5523 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
5524 | if (nest == NULL) |
5525 | goto nla_put_failure; | |
5526 | ||
aaa31047 PNA |
5527 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
5528 | nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), | |
fe2811eb | 5529 | NFT_DATA_VALUE, set->klen) < 0) |
20a69341 PM |
5530 | goto nla_put_failure; |
5531 | ||
7b225d0b PNA |
5532 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
5533 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
5534 | NFT_DATA_VALUE, set->klen) < 0) | |
5535 | goto nla_put_failure; | |
5536 | ||
fe2811eb PM |
5537 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5538 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
5539 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
5540 | set->dlen) < 0) | |
5541 | goto nla_put_failure; | |
5542 | ||
563125a7 | 5543 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) && |
079cd633 | 5544 | nft_set_elem_expr_dump(skb, set, ext, reset)) |
f25ad2e9 PM |
5545 | goto nla_put_failure; |
5546 | ||
8aeff920 PNA |
5547 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
5548 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 5549 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
5550 | goto nla_put_failure; |
5551 | ||
fe2811eb PM |
5552 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
5553 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
5554 | htonl(*nft_set_ext_flags(ext)))) | |
5555 | goto nla_put_failure; | |
20a69341 | 5556 | |
079cd633 PS |
5557 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) { |
5558 | timeout = *nft_set_ext_timeout(ext); | |
5559 | if (nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
5560 | nf_jiffies64_to_msecs(timeout), | |
5561 | NFTA_SET_ELEM_PAD)) | |
5562 | goto nla_put_failure; | |
5563 | } else if (set->flags & NFT_SET_TIMEOUT) { | |
5564 | timeout = READ_ONCE(set->timeout); | |
5565 | } | |
c3e1b005 PM |
5566 | |
5567 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 5568 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
5569 | |
5570 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 5571 | if (time_before64(now, expires)) |
c3e1b005 PM |
5572 | expires -= now; |
5573 | else | |
5574 | expires = 0; | |
5575 | ||
5576 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 5577 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 5578 | NFTA_SET_ELEM_PAD)) |
c3e1b005 | 5579 | goto nla_put_failure; |
079cd633 PS |
5580 | |
5581 | if (reset) | |
5582 | *nft_set_ext_expiration(ext) = now + timeout; | |
c3e1b005 PM |
5583 | } |
5584 | ||
68e942e8 PM |
5585 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
5586 | struct nft_userdata *udata; | |
5587 | ||
5588 | udata = nft_set_ext_userdata(ext); | |
5589 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
5590 | udata->len + 1, udata->data)) | |
5591 | goto nla_put_failure; | |
5592 | } | |
5593 | ||
20a69341 PM |
5594 | nla_nest_end(skb, nest); |
5595 | return 0; | |
5596 | ||
5597 | nla_put_failure: | |
5598 | nlmsg_trim(skb, b); | |
5599 | return -EMSGSIZE; | |
5600 | } | |
5601 | ||
5602 | struct nft_set_dump_args { | |
5603 | const struct netlink_callback *cb; | |
5604 | struct nft_set_iter iter; | |
5605 | struct sk_buff *skb; | |
079cd633 | 5606 | bool reset; |
20a69341 PM |
5607 | }; |
5608 | ||
5609 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 5610 | struct nft_set *set, |
20a69341 | 5611 | const struct nft_set_iter *iter, |
de70185d | 5612 | struct nft_set_elem *elem) |
20a69341 | 5613 | { |
24138933 | 5614 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
5615 | struct nft_set_dump_args *args; |
5616 | ||
24138933 FW |
5617 | if (nft_set_elem_expired(ext)) |
5618 | return 0; | |
5619 | ||
20a69341 | 5620 | args = container_of(iter, struct nft_set_dump_args, iter); |
079cd633 | 5621 | return nf_tables_fill_setelem(args->skb, set, elem, args->reset); |
20a69341 PM |
5622 | } |
5623 | ||
fa803605 LZ |
5624 | struct nft_set_dump_ctx { |
5625 | const struct nft_set *set; | |
5626 | struct nft_ctx ctx; | |
5627 | }; | |
5628 | ||
aaa31047 | 5629 | static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
079cd633 | 5630 | const struct nft_set *set, bool reset) |
aaa31047 PNA |
5631 | { |
5632 | struct nft_set_elem_catchall *catchall; | |
5633 | u8 genmask = nft_genmask_cur(net); | |
5634 | struct nft_set_elem elem; | |
5635 | struct nft_set_ext *ext; | |
5636 | int ret = 0; | |
5637 | ||
5638 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5639 | ext = nft_set_elem_ext(set, catchall->elem); | |
5640 | if (!nft_set_elem_active(ext, genmask) || | |
5641 | nft_set_elem_expired(ext)) | |
5642 | continue; | |
5643 | ||
5644 | elem.priv = catchall->elem; | |
079cd633 | 5645 | ret = nf_tables_fill_setelem(skb, set, &elem, reset); |
aaa31047 PNA |
5646 | break; |
5647 | } | |
5648 | ||
5649 | return ret; | |
5650 | } | |
5651 | ||
20a69341 PM |
5652 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
5653 | { | |
fa803605 | 5654 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 5655 | struct net *net = sock_net(skb->sk); |
0854db2a | 5656 | struct nftables_pernet *nft_net; |
fa803605 | 5657 | struct nft_table *table; |
de70185d | 5658 | struct nft_set *set; |
20a69341 | 5659 | struct nft_set_dump_args args; |
fa803605 | 5660 | bool set_found = false; |
20a69341 PM |
5661 | struct nlmsghdr *nlh; |
5662 | struct nlattr *nest; | |
079cd633 | 5663 | bool reset = false; |
20a69341 | 5664 | u32 portid, seq; |
fa803605 | 5665 | int event; |
20a69341 | 5666 | |
fa803605 | 5667 | rcu_read_lock(); |
d59d2f82 | 5668 | nft_net = nft_pernet(net); |
34002783 PNA |
5669 | cb->seq = READ_ONCE(nft_net->base_seq); |
5670 | ||
0854db2a | 5671 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
36596dad | 5672 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && |
98319cb9 | 5673 | dump_ctx->ctx.family != table->family) |
fa803605 | 5674 | continue; |
20a69341 | 5675 | |
36596dad PNA |
5676 | if (table != dump_ctx->ctx.table) |
5677 | continue; | |
20a69341 | 5678 | |
36596dad PNA |
5679 | list_for_each_entry_rcu(set, &table->sets, list) { |
5680 | if (set == dump_ctx->set) { | |
5681 | set_found = true; | |
5682 | break; | |
fa803605 | 5683 | } |
fa803605 LZ |
5684 | } |
5685 | break; | |
5686 | } | |
5687 | ||
5688 | if (!set_found) { | |
5689 | rcu_read_unlock(); | |
5690 | return -ENOENT; | |
5691 | } | |
20a69341 | 5692 | |
dedb67c4 | 5693 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
5694 | portid = NETLINK_CB(cb->skb).portid; |
5695 | seq = cb->nlh->nlmsg_seq; | |
5696 | ||
19c28b13 PNA |
5697 | nlh = nfnl_msg_put(skb, portid, seq, event, NLM_F_MULTI, |
5698 | table->family, NFNETLINK_V0, nft_base_seq(net)); | |
5699 | if (!nlh) | |
20a69341 PM |
5700 | goto nla_put_failure; |
5701 | ||
fa803605 | 5702 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
5703 | goto nla_put_failure; |
5704 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
5705 | goto nla_put_failure; | |
5706 | ||
ae0be8de | 5707 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
5708 | if (nest == NULL) |
5709 | goto nla_put_failure; | |
5710 | ||
079cd633 PS |
5711 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET) |
5712 | reset = true; | |
5713 | ||
8588ac09 PNA |
5714 | args.cb = cb; |
5715 | args.skb = skb; | |
079cd633 | 5716 | args.reset = reset; |
fa803605 | 5717 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
5718 | args.iter.skip = cb->args[0]; |
5719 | args.iter.count = 0; | |
5720 | args.iter.err = 0; | |
5721 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 | 5722 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
aaa31047 PNA |
5723 | |
5724 | if (!args.iter.err && args.iter.count == cb->args[0]) | |
079cd633 | 5725 | args.iter.err = nft_set_catchall_dump(net, skb, set, reset); |
fa803605 | 5726 | rcu_read_unlock(); |
20a69341 PM |
5727 | |
5728 | nla_nest_end(skb, nest); | |
5729 | nlmsg_end(skb, nlh); | |
5730 | ||
5731 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
5732 | return args.iter.err; | |
5733 | if (args.iter.count == cb->args[0]) | |
5734 | return 0; | |
5735 | ||
5736 | cb->args[0] = args.iter.count; | |
5737 | return skb->len; | |
5738 | ||
5739 | nla_put_failure: | |
fa803605 | 5740 | rcu_read_unlock(); |
20a69341 PM |
5741 | return -ENOSPC; |
5742 | } | |
5743 | ||
90fd131a FW |
5744 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
5745 | { | |
5746 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
5747 | ||
5748 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
5749 | ||
5750 | return cb->data ? 0 : -ENOMEM; | |
5751 | } | |
5752 | ||
fa803605 LZ |
5753 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
5754 | { | |
5755 | kfree(cb->data); | |
5756 | return 0; | |
5757 | } | |
5758 | ||
d60ce62f AB |
5759 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
5760 | const struct nft_ctx *ctx, u32 seq, | |
5761 | u32 portid, int event, u16 flags, | |
5762 | const struct nft_set *set, | |
079cd633 PS |
5763 | const struct nft_set_elem *elem, |
5764 | bool reset) | |
d60ce62f | 5765 | { |
d60ce62f AB |
5766 | struct nlmsghdr *nlh; |
5767 | struct nlattr *nest; | |
5768 | int err; | |
5769 | ||
dedb67c4 | 5770 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
5771 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, ctx->family, |
5772 | NFNETLINK_V0, nft_base_seq(ctx->net)); | |
5773 | if (!nlh) | |
d60ce62f AB |
5774 | goto nla_put_failure; |
5775 | ||
d60ce62f AB |
5776 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) |
5777 | goto nla_put_failure; | |
5778 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
5779 | goto nla_put_failure; | |
5780 | ||
ae0be8de | 5781 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
5782 | if (nest == NULL) |
5783 | goto nla_put_failure; | |
5784 | ||
079cd633 | 5785 | err = nf_tables_fill_setelem(skb, set, elem, reset); |
d60ce62f AB |
5786 | if (err < 0) |
5787 | goto nla_put_failure; | |
5788 | ||
5789 | nla_nest_end(skb, nest); | |
5790 | ||
053c095a JB |
5791 | nlmsg_end(skb, nlh); |
5792 | return 0; | |
d60ce62f AB |
5793 | |
5794 | nla_put_failure: | |
5795 | nlmsg_trim(skb, nlh); | |
5796 | return -1; | |
5797 | } | |
5798 | ||
ba0e4d99 PNA |
5799 | static int nft_setelem_parse_flags(const struct nft_set *set, |
5800 | const struct nlattr *attr, u32 *flags) | |
5801 | { | |
5802 | if (attr == NULL) | |
5803 | return 0; | |
5804 | ||
5805 | *flags = ntohl(nla_get_be32(attr)); | |
aaa31047 PNA |
5806 | if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
5807 | return -EOPNOTSUPP; | |
ba0e4d99 PNA |
5808 | if (!(set->flags & NFT_SET_INTERVAL) && |
5809 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
5810 | return -EINVAL; | |
fc0ae524 PNA |
5811 | if ((*flags & (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) == |
5812 | (NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) | |
5813 | return -EINVAL; | |
ba0e4d99 PNA |
5814 | |
5815 | return 0; | |
5816 | } | |
5817 | ||
20a1452c PNA |
5818 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
5819 | struct nft_data *key, struct nlattr *attr) | |
5820 | { | |
341b6941 PNA |
5821 | struct nft_data_desc desc = { |
5822 | .type = NFT_DATA_VALUE, | |
5823 | .size = NFT_DATA_VALUE_MAXLEN, | |
5824 | .len = set->klen, | |
5825 | }; | |
20a1452c | 5826 | |
341b6941 | 5827 | return nft_data_init(ctx, key, &desc, attr); |
20a1452c PNA |
5828 | } |
5829 | ||
fdb9c405 PNA |
5830 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
5831 | struct nft_data_desc *desc, | |
5832 | struct nft_data *data, | |
5833 | struct nlattr *attr) | |
5834 | { | |
7e6bc1f6 | 5835 | u32 dtype; |
fdb9c405 | 5836 | |
7e6bc1f6 PNA |
5837 | if (set->dtype == NFT_DATA_VERDICT) |
5838 | dtype = NFT_DATA_VERDICT; | |
5839 | else | |
5840 | dtype = NFT_DATA_VALUE; | |
5841 | ||
341b6941 PNA |
5842 | desc->type = dtype; |
5843 | desc->size = NFT_DATA_VALUE_MAXLEN; | |
5844 | desc->len = set->dlen; | |
f323ef3a | 5845 | desc->flags = NFT_DATA_DESC_SETELEM; |
fdb9c405 | 5846 | |
341b6941 | 5847 | return nft_data_init(ctx, data, desc, attr); |
fdb9c405 PNA |
5848 | } |
5849 | ||
aaa31047 PNA |
5850 | static void *nft_setelem_catchall_get(const struct net *net, |
5851 | const struct nft_set *set) | |
5852 | { | |
5853 | struct nft_set_elem_catchall *catchall; | |
5854 | u8 genmask = nft_genmask_cur(net); | |
5855 | struct nft_set_ext *ext; | |
5856 | void *priv = NULL; | |
5857 | ||
5858 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
5859 | ext = nft_set_elem_ext(set, catchall->elem); | |
5860 | if (!nft_set_elem_active(ext, genmask) || | |
5861 | nft_set_elem_expired(ext)) | |
5862 | continue; | |
5863 | ||
5864 | priv = catchall->elem; | |
5865 | break; | |
5866 | } | |
5867 | ||
5868 | return priv; | |
5869 | } | |
5870 | ||
5871 | static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, | |
5872 | struct nft_set_elem *elem, u32 flags) | |
5873 | { | |
5874 | void *priv; | |
5875 | ||
5876 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { | |
5877 | priv = set->ops->get(ctx->net, set, elem, flags); | |
5878 | if (IS_ERR(priv)) | |
5879 | return PTR_ERR(priv); | |
5880 | } else { | |
5881 | priv = nft_setelem_catchall_get(ctx->net, set); | |
5882 | if (!priv) | |
5883 | return -ENOENT; | |
5884 | } | |
5885 | elem->priv = priv; | |
5886 | ||
5887 | return 0; | |
5888 | } | |
5889 | ||
ba0e4d99 | 5890 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
079cd633 | 5891 | const struct nlattr *attr, bool reset) |
ba0e4d99 PNA |
5892 | { |
5893 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
5894 | struct nft_set_elem elem; |
5895 | struct sk_buff *skb; | |
5896 | uint32_t flags = 0; | |
ba0e4d99 PNA |
5897 | int err; |
5898 | ||
8cb08174 JB |
5899 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5900 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
5901 | if (err < 0) |
5902 | return err; | |
5903 | ||
ba0e4d99 PNA |
5904 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
5905 | if (err < 0) | |
5906 | return err; | |
5907 | ||
aaa31047 PNA |
5908 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
5909 | return -EINVAL; | |
5910 | ||
5911 | if (nla[NFTA_SET_ELEM_KEY]) { | |
5912 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
5913 | nla[NFTA_SET_ELEM_KEY]); | |
5914 | if (err < 0) | |
5915 | return err; | |
5916 | } | |
ba0e4d99 | 5917 | |
7b225d0b PNA |
5918 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5919 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5920 | nla[NFTA_SET_ELEM_KEY_END]); | |
5921 | if (err < 0) | |
5922 | return err; | |
5923 | } | |
5924 | ||
aaa31047 PNA |
5925 | err = nft_setelem_get(ctx, set, &elem, flags); |
5926 | if (err < 0) | |
5927 | return err; | |
ba0e4d99 PNA |
5928 | |
5929 | err = -ENOMEM; | |
d9adf22a | 5930 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 | 5931 | if (skb == NULL) |
ee921183 | 5932 | return err; |
ba0e4d99 PNA |
5933 | |
5934 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
079cd633 PS |
5935 | NFT_MSG_NEWSETELEM, 0, set, &elem, |
5936 | reset); | |
ba0e4d99 | 5937 | if (err < 0) |
ee921183 | 5938 | goto err_fill_setelem; |
ba0e4d99 | 5939 | |
ee921183 | 5940 | return nfnetlink_unicast(skb, ctx->net, ctx->portid); |
ba0e4d99 | 5941 | |
ee921183 | 5942 | err_fill_setelem: |
ba0e4d99 | 5943 | kfree_skb(skb); |
ee921183 | 5944 | return err; |
ba0e4d99 PNA |
5945 | } |
5946 | ||
d9adf22a | 5947 | /* called with rcu_read_lock held */ |
797d4980 PNA |
5948 | static int nf_tables_getsetelem(struct sk_buff *skb, |
5949 | const struct nfnl_info *info, | |
5950 | const struct nlattr * const nla[]) | |
ba0e4d99 | 5951 | { |
797d4980 PNA |
5952 | struct netlink_ext_ack *extack = info->extack; |
5953 | u8 genmask = nft_genmask_cur(info->net); | |
e2b750d7 | 5954 | u8 family = info->nfmsg->nfgen_family; |
797d4980 | 5955 | struct net *net = info->net; |
e2b750d7 | 5956 | struct nft_table *table; |
ba0e4d99 PNA |
5957 | struct nft_set *set; |
5958 | struct nlattr *attr; | |
5959 | struct nft_ctx ctx; | |
079cd633 | 5960 | bool reset = false; |
ba0e4d99 PNA |
5961 | int rem, err = 0; |
5962 | ||
e2b750d7 | 5963 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
92f3e96d | 5964 | genmask, 0); |
e2b750d7 PNA |
5965 | if (IS_ERR(table)) { |
5966 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
5967 | return PTR_ERR(table); | |
5968 | } | |
ba0e4d99 | 5969 | |
e2b750d7 | 5970 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
5971 | if (IS_ERR(set)) |
5972 | return PTR_ERR(set); | |
5973 | ||
e2b750d7 PNA |
5974 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
5975 | ||
797d4980 | 5976 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
ba0e4d99 | 5977 | struct netlink_dump_control c = { |
90fd131a | 5978 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
5979 | .dump = nf_tables_dump_set, |
5980 | .done = nf_tables_dump_set_done, | |
d9adf22a | 5981 | .module = THIS_MODULE, |
ba0e4d99 | 5982 | }; |
90fd131a FW |
5983 | struct nft_set_dump_ctx dump_ctx = { |
5984 | .set = set, | |
5985 | .ctx = ctx, | |
5986 | }; | |
ba0e4d99 | 5987 | |
90fd131a | 5988 | c.data = &dump_ctx; |
797d4980 | 5989 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
ba0e4d99 PNA |
5990 | } |
5991 | ||
5992 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
5993 | return -EINVAL; | |
5994 | ||
079cd633 PS |
5995 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETSETELEM_RESET) |
5996 | reset = true; | |
5997 | ||
ba0e4d99 | 5998 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
079cd633 | 5999 | err = nft_get_set_elem(&ctx, set, attr, reset); |
b53c1166 PNA |
6000 | if (err < 0) { |
6001 | NL_SET_BAD_ATTR(extack, attr); | |
ba0e4d99 | 6002 | break; |
b53c1166 | 6003 | } |
ba0e4d99 PNA |
6004 | } |
6005 | ||
6006 | return err; | |
6007 | } | |
6008 | ||
25e94a99 PNA |
6009 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
6010 | const struct nft_set *set, | |
6011 | const struct nft_set_elem *elem, | |
6fb721cf | 6012 | int event) |
d60ce62f | 6013 | { |
0854db2a | 6014 | struct nftables_pernet *nft_net; |
128ad332 PNA |
6015 | struct net *net = ctx->net; |
6016 | u32 portid = ctx->portid; | |
d60ce62f | 6017 | struct sk_buff *skb; |
6fb721cf | 6018 | u16 flags = 0; |
d60ce62f AB |
6019 | int err; |
6020 | ||
128ad332 | 6021 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 6022 | return; |
d60ce62f | 6023 | |
d60ce62f AB |
6024 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
6025 | if (skb == NULL) | |
6026 | goto err; | |
6027 | ||
6fb721cf PNA |
6028 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
6029 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
6030 | ||
d60ce62f | 6031 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, |
079cd633 | 6032 | set, elem, false); |
d60ce62f AB |
6033 | if (err < 0) { |
6034 | kfree_skb(skb); | |
6035 | goto err; | |
6036 | } | |
6037 | ||
d59d2f82 | 6038 | nft_net = nft_pernet(net); |
0854db2a | 6039 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
25e94a99 | 6040 | return; |
d60ce62f | 6041 | err: |
25e94a99 | 6042 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
6043 | } |
6044 | ||
60319eb1 PNA |
6045 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
6046 | int msg_type, | |
6047 | struct nft_set *set) | |
6048 | { | |
6049 | struct nft_trans *trans; | |
6050 | ||
6051 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
6052 | if (trans == NULL) | |
6053 | return NULL; | |
6054 | ||
6055 | nft_trans_elem_set(trans) = set; | |
6056 | return trans; | |
6057 | } | |
6058 | ||
a7fc9368 PNA |
6059 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
6060 | const struct nft_set *set, | |
6061 | const struct nlattr *attr) | |
6062 | { | |
6063 | struct nft_expr *expr; | |
6064 | int err; | |
6065 | ||
6066 | expr = nft_expr_init(ctx, attr); | |
6067 | if (IS_ERR(expr)) | |
6068 | return expr; | |
6069 | ||
6070 | err = -EOPNOTSUPP; | |
a7fc9368 PNA |
6071 | if (expr->ops->type->flags & NFT_EXPR_GC) { |
6072 | if (set->flags & NFT_SET_TIMEOUT) | |
6073 | goto err_set_elem_expr; | |
6074 | if (!set->ops->gc_init) | |
6075 | goto err_set_elem_expr; | |
6076 | set->ops->gc_init(set); | |
6077 | } | |
6078 | ||
6079 | return expr; | |
6080 | ||
6081 | err_set_elem_expr: | |
6082 | nft_expr_destroy(ctx, expr); | |
6083 | return ERR_PTR(err); | |
6084 | } | |
6085 | ||
34aae2c2 PNA |
6086 | static int nft_set_ext_check(const struct nft_set_ext_tmpl *tmpl, u8 id, u32 len) |
6087 | { | |
6088 | len += nft_set_ext_types[id].len; | |
6089 | if (len > tmpl->ext_len[id] || | |
6090 | len > U8_MAX) | |
6091 | return -1; | |
6092 | ||
6093 | return 0; | |
6094 | } | |
6095 | ||
6096 | static int nft_set_ext_memcpy(const struct nft_set_ext_tmpl *tmpl, u8 id, | |
6097 | void *to, const void *from, u32 len) | |
6098 | { | |
6099 | if (nft_set_ext_check(tmpl, id, len) < 0) | |
6100 | return -1; | |
6101 | ||
6102 | memcpy(to, from, len); | |
6103 | ||
6104 | return 0; | |
6105 | } | |
6106 | ||
22fe54d5 PM |
6107 | void *nft_set_elem_init(const struct nft_set *set, |
6108 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
6109 | const u32 *key, const u32 *key_end, |
6110 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
6111 | { |
6112 | struct nft_set_ext *ext; | |
6113 | void *elem; | |
6114 | ||
6115 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
6116 | if (elem == NULL) | |
34aae2c2 | 6117 | return ERR_PTR(-ENOMEM); |
fe2811eb PM |
6118 | |
6119 | ext = nft_set_elem_ext(set, elem); | |
6120 | nft_set_ext_init(ext, tmpl); | |
6121 | ||
34aae2c2 PNA |
6122 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
6123 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY, | |
6124 | nft_set_ext_key(ext), key, set->klen) < 0) | |
6125 | goto err_ext_check; | |
6126 | ||
6127 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && | |
6128 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_KEY_END, | |
6129 | nft_set_ext_key_end(ext), key_end, set->klen) < 0) | |
6130 | goto err_ext_check; | |
6131 | ||
6132 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && | |
6133 | nft_set_ext_memcpy(tmpl, NFT_SET_EXT_DATA, | |
6134 | nft_set_ext_data(ext), data, set->dlen) < 0) | |
6135 | goto err_ext_check; | |
6136 | ||
79ebb5bb LGL |
6137 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
6138 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
6139 | if (expiration == 0) | |
6140 | *nft_set_ext_expiration(ext) += timeout; | |
6141 | } | |
c3e1b005 PM |
6142 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
6143 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
6144 | |
6145 | return elem; | |
34aae2c2 PNA |
6146 | |
6147 | err_ext_check: | |
6148 | kfree(elem); | |
6149 | ||
6150 | return ERR_PTR(-EINVAL); | |
fe2811eb PM |
6151 | } |
6152 | ||
563125a7 PNA |
6153 | static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
6154 | struct nft_expr *expr) | |
475beb9c PNA |
6155 | { |
6156 | if (expr->ops->destroy_clone) { | |
6157 | expr->ops->destroy_clone(ctx, expr); | |
6158 | module_put(expr->ops->type->owner); | |
6159 | } else { | |
6160 | nf_tables_expr_destroy(ctx, expr); | |
6161 | } | |
6162 | } | |
6163 | ||
563125a7 PNA |
6164 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
6165 | struct nft_set_elem_expr *elem_expr) | |
6166 | { | |
6167 | struct nft_expr *expr; | |
6168 | u32 size; | |
6169 | ||
6170 | nft_setelem_expr_foreach(expr, elem_expr, size) | |
6171 | __nft_set_elem_expr_destroy(ctx, expr); | |
6172 | } | |
6173 | ||
628bd3e4 | 6174 | /* Drop references and destroy. Called from gc, dynset and abort path. */ |
61f9e292 LZ |
6175 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
6176 | bool destroy_expr) | |
61edafbb PM |
6177 | { |
6178 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
6179 | struct nft_ctx ctx = { |
6180 | .net = read_pnet(&set->net), | |
6181 | .family = set->table->family, | |
6182 | }; | |
61edafbb | 6183 | |
59105446 | 6184 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 6185 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 6186 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
563125a7 | 6187 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c | 6188 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); |
371ebcbb | 6189 | |
8aeff920 | 6190 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
1689f259 | 6191 | nft_use_dec(&(*nft_set_ext_obj(ext))->use); |
61edafbb PM |
6192 | kfree(elem); |
6193 | } | |
6194 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
6195 | ||
628bd3e4 PNA |
6196 | /* Destroy element. References have been already dropped in the preparation |
6197 | * path via nft_setelem_data_deactivate(). | |
59105446 | 6198 | */ |
628bd3e4 PNA |
6199 | void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
6200 | const struct nft_set *set, void *elem) | |
59105446 PNA |
6201 | { |
6202 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
6203 | ||
563125a7 | 6204 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) |
475beb9c PNA |
6205 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
6206 | ||
59105446 PNA |
6207 | kfree(elem); |
6208 | } | |
6209 | ||
fca05d4d PNA |
6210 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
6211 | struct nft_expr *expr_array[]) | |
8cfd9b0f PNA |
6212 | { |
6213 | struct nft_expr *expr; | |
6214 | int err, i, k; | |
6215 | ||
6216 | for (i = 0; i < set->num_exprs; i++) { | |
42193ffd | 6217 | expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL_ACCOUNT); |
8cfd9b0f PNA |
6218 | if (!expr) |
6219 | goto err_expr; | |
6220 | ||
6221 | err = nft_expr_clone(expr, set->exprs[i]); | |
6222 | if (err < 0) { | |
c485c35f | 6223 | kfree(expr); |
8cfd9b0f PNA |
6224 | goto err_expr; |
6225 | } | |
6226 | expr_array[i] = expr; | |
6227 | } | |
6228 | ||
6229 | return 0; | |
6230 | ||
6231 | err_expr: | |
161b838e CIK |
6232 | for (k = i - 1; k >= 0; k--) |
6233 | nft_expr_destroy(ctx, expr_array[k]); | |
8cfd9b0f PNA |
6234 | |
6235 | return -ENOMEM; | |
6236 | } | |
6237 | ||
4d8f9065 | 6238 | static int nft_set_elem_expr_setup(struct nft_ctx *ctx, |
34aae2c2 | 6239 | const struct nft_set_ext_tmpl *tmpl, |
4d8f9065 PNA |
6240 | const struct nft_set_ext *ext, |
6241 | struct nft_expr *expr_array[], | |
6242 | u32 num_exprs) | |
563125a7 PNA |
6243 | { |
6244 | struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext); | |
34aae2c2 | 6245 | u32 len = sizeof(struct nft_set_elem_expr); |
4d8f9065 PNA |
6246 | struct nft_expr *expr; |
6247 | int i, err; | |
6248 | ||
34aae2c2 PNA |
6249 | if (num_exprs == 0) |
6250 | return 0; | |
6251 | ||
6252 | for (i = 0; i < num_exprs; i++) | |
6253 | len += expr_array[i]->ops->size; | |
6254 | ||
6255 | if (nft_set_ext_check(tmpl, NFT_SET_EXT_EXPRESSIONS, len) < 0) | |
6256 | return -EINVAL; | |
6257 | ||
4d8f9065 PNA |
6258 | for (i = 0; i < num_exprs; i++) { |
6259 | expr = nft_setelem_expr_at(elem_expr, elem_expr->size); | |
6260 | err = nft_expr_clone(expr, expr_array[i]); | |
6261 | if (err < 0) | |
6262 | goto err_elem_expr_setup; | |
6263 | ||
6264 | elem_expr->size += expr_array[i]->ops->size; | |
6265 | nft_expr_destroy(ctx, expr_array[i]); | |
6266 | expr_array[i] = NULL; | |
6267 | } | |
6268 | ||
6269 | return 0; | |
6270 | ||
6271 | err_elem_expr_setup: | |
6272 | for (; i < num_exprs; i++) { | |
6273 | nft_expr_destroy(ctx, expr_array[i]); | |
6274 | expr_array[i] = NULL; | |
6275 | } | |
563125a7 | 6276 | |
4d8f9065 | 6277 | return -ENOMEM; |
563125a7 PNA |
6278 | } |
6279 | ||
aaa31047 PNA |
6280 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
6281 | const struct nft_set *set) | |
6282 | { | |
6283 | struct nft_set_elem_catchall *catchall; | |
6284 | u8 genmask = nft_genmask_cur(net); | |
6285 | struct nft_set_ext *ext; | |
6286 | ||
6287 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
6288 | ext = nft_set_elem_ext(set, catchall->elem); | |
6289 | if (nft_set_elem_active(ext, genmask) && | |
5f68718b PNA |
6290 | !nft_set_elem_expired(ext) && |
6291 | !nft_set_elem_is_dead(ext)) | |
aaa31047 PNA |
6292 | return ext; |
6293 | } | |
6294 | ||
6295 | return NULL; | |
6296 | } | |
6297 | EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); | |
6298 | ||
aaa31047 PNA |
6299 | static int nft_setelem_catchall_insert(const struct net *net, |
6300 | struct nft_set *set, | |
6301 | const struct nft_set_elem *elem, | |
6302 | struct nft_set_ext **pext) | |
6303 | { | |
6304 | struct nft_set_elem_catchall *catchall; | |
6305 | u8 genmask = nft_genmask_next(net); | |
6306 | struct nft_set_ext *ext; | |
6307 | ||
6308 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
6309 | ext = nft_set_elem_ext(set, catchall->elem); | |
6310 | if (nft_set_elem_active(ext, genmask)) { | |
6311 | *pext = ext; | |
6312 | return -EEXIST; | |
6313 | } | |
6314 | } | |
6315 | ||
6316 | catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); | |
6317 | if (!catchall) | |
6318 | return -ENOMEM; | |
6319 | ||
6320 | catchall->elem = elem->priv; | |
6321 | list_add_tail_rcu(&catchall->list, &set->catchall_list); | |
6322 | ||
6323 | return 0; | |
6324 | } | |
6325 | ||
6326 | static int nft_setelem_insert(const struct net *net, | |
6327 | struct nft_set *set, | |
6328 | const struct nft_set_elem *elem, | |
6329 | struct nft_set_ext **ext, unsigned int flags) | |
6330 | { | |
6331 | int ret; | |
6332 | ||
6333 | if (flags & NFT_SET_ELEM_CATCHALL) | |
6334 | ret = nft_setelem_catchall_insert(net, set, elem, ext); | |
6335 | else | |
6336 | ret = set->ops->insert(net, set, elem, ext); | |
6337 | ||
6338 | return ret; | |
6339 | } | |
6340 | ||
6341 | static bool nft_setelem_is_catchall(const struct nft_set *set, | |
6342 | const struct nft_set_elem *elem) | |
6343 | { | |
6344 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6345 | ||
6346 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
6347 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) | |
6348 | return true; | |
6349 | ||
6350 | return false; | |
6351 | } | |
6352 | ||
6353 | static void nft_setelem_activate(struct net *net, struct nft_set *set, | |
6354 | struct nft_set_elem *elem) | |
6355 | { | |
6356 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6357 | ||
6358 | if (nft_setelem_is_catchall(set, elem)) { | |
6359 | nft_set_elem_change_active(net, set, ext); | |
aaa31047 PNA |
6360 | } else { |
6361 | set->ops->activate(net, set, elem); | |
6362 | } | |
6363 | } | |
6364 | ||
6365 | static int nft_setelem_catchall_deactivate(const struct net *net, | |
6366 | struct nft_set *set, | |
6367 | struct nft_set_elem *elem) | |
6368 | { | |
6369 | struct nft_set_elem_catchall *catchall; | |
6370 | struct nft_set_ext *ext; | |
6371 | ||
6372 | list_for_each_entry(catchall, &set->catchall_list, list) { | |
6373 | ext = nft_set_elem_ext(set, catchall->elem); | |
f6c383b8 | 6374 | if (!nft_is_active(net, ext)) |
aaa31047 PNA |
6375 | continue; |
6376 | ||
6377 | kfree(elem->priv); | |
6378 | elem->priv = catchall->elem; | |
6379 | nft_set_elem_change_active(net, set, ext); | |
6380 | return 0; | |
6381 | } | |
6382 | ||
6383 | return -ENOENT; | |
6384 | } | |
6385 | ||
6386 | static int __nft_setelem_deactivate(const struct net *net, | |
6387 | struct nft_set *set, | |
6388 | struct nft_set_elem *elem) | |
6389 | { | |
6390 | void *priv; | |
6391 | ||
6392 | priv = set->ops->deactivate(net, set, elem); | |
6393 | if (!priv) | |
6394 | return -ENOENT; | |
6395 | ||
6396 | kfree(elem->priv); | |
6397 | elem->priv = priv; | |
6398 | set->ndeact++; | |
6399 | ||
6400 | return 0; | |
6401 | } | |
6402 | ||
6403 | static int nft_setelem_deactivate(const struct net *net, | |
6404 | struct nft_set *set, | |
6405 | struct nft_set_elem *elem, u32 flags) | |
6406 | { | |
6407 | int ret; | |
6408 | ||
6409 | if (flags & NFT_SET_ELEM_CATCHALL) | |
6410 | ret = nft_setelem_catchall_deactivate(net, set, elem); | |
6411 | else | |
6412 | ret = __nft_setelem_deactivate(net, set, elem); | |
6413 | ||
6414 | return ret; | |
6415 | } | |
6416 | ||
6417 | static void nft_setelem_catchall_remove(const struct net *net, | |
6418 | const struct nft_set *set, | |
6419 | const struct nft_set_elem *elem) | |
6420 | { | |
6421 | struct nft_set_elem_catchall *catchall, *next; | |
6422 | ||
6423 | list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { | |
6424 | if (catchall->elem == elem->priv) { | |
6425 | list_del_rcu(&catchall->list); | |
ae089831 | 6426 | kfree_rcu(catchall, rcu); |
aaa31047 PNA |
6427 | break; |
6428 | } | |
6429 | } | |
6430 | } | |
6431 | ||
6432 | static void nft_setelem_remove(const struct net *net, | |
6433 | const struct nft_set *set, | |
6434 | const struct nft_set_elem *elem) | |
6435 | { | |
6436 | if (nft_setelem_is_catchall(set, elem)) | |
6437 | nft_setelem_catchall_remove(net, set, elem); | |
6438 | else | |
6439 | set->ops->remove(net, set, elem); | |
6440 | } | |
6441 | ||
88cccd90 PNA |
6442 | static bool nft_setelem_valid_key_end(const struct nft_set *set, |
6443 | struct nlattr **nla, u32 flags) | |
6444 | { | |
6445 | if ((set->flags & (NFT_SET_CONCAT | NFT_SET_INTERVAL)) == | |
6446 | (NFT_SET_CONCAT | NFT_SET_INTERVAL)) { | |
6447 | if (flags & NFT_SET_ELEM_INTERVAL_END) | |
6448 | return false; | |
96df8360 PNA |
6449 | |
6450 | if (nla[NFTA_SET_ELEM_KEY_END] && | |
6451 | flags & NFT_SET_ELEM_CATCHALL) | |
88cccd90 PNA |
6452 | return false; |
6453 | } else { | |
6454 | if (nla[NFTA_SET_ELEM_KEY_END]) | |
6455 | return false; | |
6456 | } | |
6457 | ||
6458 | return true; | |
6459 | } | |
6460 | ||
60319eb1 | 6461 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 6462 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 | 6463 | { |
8cfd9b0f | 6464 | struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {}; |
20a69341 | 6465 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; |
8aeff920 | 6466 | u8 genmask = nft_genmask_next(ctx->net); |
664899e8 | 6467 | u32 flags = 0, size = 0, num_exprs = 0; |
fe2811eb | 6468 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 6469 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
6470 | struct nft_set_elem elem; |
6471 | struct nft_set_binding *binding; | |
8aeff920 | 6472 | struct nft_object *obj = NULL; |
68e942e8 | 6473 | struct nft_userdata *udata; |
20a1452c | 6474 | struct nft_data_desc desc; |
20a69341 | 6475 | enum nft_registers dreg; |
60319eb1 | 6476 | struct nft_trans *trans; |
c3e1b005 | 6477 | u64 timeout; |
79ebb5bb | 6478 | u64 expiration; |
8cfd9b0f | 6479 | int err, i; |
68e942e8 | 6480 | u8 ulen; |
20a69341 | 6481 | |
8cb08174 JB |
6482 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6483 | nft_set_elem_policy, NULL); | |
20a69341 PM |
6484 | if (err < 0) |
6485 | return err; | |
6486 | ||
fe2811eb PM |
6487 | nft_set_ext_prepare(&tmpl); |
6488 | ||
0e9091d6 PNA |
6489 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6490 | if (err < 0) | |
6491 | return err; | |
aaa31047 | 6492 | |
d4eb7e39 PNA |
6493 | if (((flags & NFT_SET_ELEM_CATCHALL) && nla[NFTA_SET_ELEM_KEY]) || |
6494 | (!(flags & NFT_SET_ELEM_CATCHALL) && !nla[NFTA_SET_ELEM_KEY])) | |
aaa31047 PNA |
6495 | return -EINVAL; |
6496 | ||
c39ba4de PNA |
6497 | if (flags != 0) { |
6498 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6499 | if (err < 0) | |
6500 | return err; | |
6501 | } | |
20a69341 PM |
6502 | |
6503 | if (set->flags & NFT_SET_MAP) { | |
6504 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 6505 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
6506 | return -EINVAL; |
6507 | } else { | |
6508 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
6509 | return -EINVAL; | |
6510 | } | |
6511 | ||
5a2f3dc3 PNA |
6512 | if (set->flags & NFT_SET_OBJECT) { |
6513 | if (!nla[NFTA_SET_ELEM_OBJREF] && | |
6514 | !(flags & NFT_SET_ELEM_INTERVAL_END)) | |
6515 | return -EINVAL; | |
6516 | } else { | |
6517 | if (nla[NFTA_SET_ELEM_OBJREF]) | |
6518 | return -EINVAL; | |
6519 | } | |
6520 | ||
88cccd90 PNA |
6521 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6522 | return -EINVAL; | |
6523 | ||
bffc124b PNA |
6524 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
6525 | (nla[NFTA_SET_ELEM_DATA] || | |
6526 | nla[NFTA_SET_ELEM_OBJREF] || | |
6527 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
6528 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
6529 | nla[NFTA_SET_ELEM_USERDATA] || | |
48b0ae04 | 6530 | nla[NFTA_SET_ELEM_EXPR] || |
4963674c | 6531 | nla[NFTA_SET_ELEM_KEY_END] || |
48b0ae04 | 6532 | nla[NFTA_SET_ELEM_EXPRESSIONS])) |
bffc124b PNA |
6533 | return -EINVAL; |
6534 | ||
c3e1b005 PM |
6535 | timeout = 0; |
6536 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
6537 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6538 | return -EINVAL; | |
8e1102d5 FW |
6539 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
6540 | &timeout); | |
6541 | if (err) | |
6542 | return err; | |
33c7aba0 PNA |
6543 | } else if (set->flags & NFT_SET_TIMEOUT && |
6544 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
123b9961 | 6545 | timeout = READ_ONCE(set->timeout); |
c3e1b005 PM |
6546 | } |
6547 | ||
79ebb5bb LGL |
6548 | expiration = 0; |
6549 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
6550 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
6551 | return -EINVAL; | |
6552 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
6553 | &expiration); | |
6554 | if (err) | |
6555 | return err; | |
6556 | } | |
6557 | ||
8cfd9b0f PNA |
6558 | if (nla[NFTA_SET_ELEM_EXPR]) { |
6559 | struct nft_expr *expr; | |
6560 | ||
664899e8 | 6561 | if (set->num_exprs && set->num_exprs != 1) |
8cfd9b0f PNA |
6562 | return -EOPNOTSUPP; |
6563 | ||
40944452 PNA |
6564 | expr = nft_set_elem_expr_alloc(ctx, set, |
6565 | nla[NFTA_SET_ELEM_EXPR]); | |
6566 | if (IS_ERR(expr)) | |
6567 | return PTR_ERR(expr); | |
65038428 | 6568 | |
8cfd9b0f | 6569 | expr_array[0] = expr; |
664899e8 | 6570 | num_exprs = 1; |
65038428 | 6571 | |
664899e8 | 6572 | if (set->num_exprs && set->exprs[0]->ops != expr->ops) { |
8cfd9b0f | 6573 | err = -EOPNOTSUPP; |
65038428 | 6574 | goto err_set_elem_expr; |
8cfd9b0f | 6575 | } |
48b0ae04 PNA |
6576 | } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) { |
6577 | struct nft_expr *expr; | |
6578 | struct nlattr *tmp; | |
6579 | int left; | |
65038428 | 6580 | |
48b0ae04 PNA |
6581 | i = 0; |
6582 | nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) { | |
664899e8 PNA |
6583 | if (i == NFT_SET_EXPR_MAX || |
6584 | (set->num_exprs && set->num_exprs == i)) { | |
48b0ae04 PNA |
6585 | err = -E2BIG; |
6586 | goto err_set_elem_expr; | |
6587 | } | |
6588 | if (nla_type(tmp) != NFTA_LIST_ELEM) { | |
6589 | err = -EINVAL; | |
6590 | goto err_set_elem_expr; | |
6591 | } | |
6592 | expr = nft_set_elem_expr_alloc(ctx, set, tmp); | |
6593 | if (IS_ERR(expr)) { | |
6594 | err = PTR_ERR(expr); | |
6595 | goto err_set_elem_expr; | |
6596 | } | |
6597 | expr_array[i] = expr; | |
664899e8 | 6598 | num_exprs++; |
48b0ae04 | 6599 | |
664899e8 | 6600 | if (set->num_exprs && expr->ops != set->exprs[i]->ops) { |
48b0ae04 PNA |
6601 | err = -EOPNOTSUPP; |
6602 | goto err_set_elem_expr; | |
6603 | } | |
6604 | i++; | |
6605 | } | |
664899e8 | 6606 | if (set->num_exprs && set->num_exprs != i) { |
48b0ae04 | 6607 | err = -EOPNOTSUPP; |
65038428 | 6608 | goto err_set_elem_expr; |
48b0ae04 | 6609 | } |
33c7aba0 PNA |
6610 | } else if (set->num_exprs > 0 && |
6611 | !(flags & NFT_SET_ELEM_INTERVAL_END)) { | |
8cfd9b0f PNA |
6612 | err = nft_set_elem_expr_clone(ctx, set, expr_array); |
6613 | if (err < 0) | |
6614 | goto err_set_elem_expr_clone; | |
664899e8 PNA |
6615 | |
6616 | num_exprs = set->num_exprs; | |
40944452 PNA |
6617 | } |
6618 | ||
aaa31047 PNA |
6619 | if (nla[NFTA_SET_ELEM_KEY]) { |
6620 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6621 | nla[NFTA_SET_ELEM_KEY]); | |
6622 | if (err < 0) | |
6623 | goto err_set_elem_expr; | |
20a69341 | 6624 | |
c39ba4de PNA |
6625 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6626 | if (err < 0) | |
6627 | goto err_parse_key; | |
aaa31047 | 6628 | } |
7b225d0b PNA |
6629 | |
6630 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
6631 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6632 | nla[NFTA_SET_ELEM_KEY_END]); | |
6633 | if (err < 0) | |
6634 | goto err_parse_key; | |
6635 | ||
c39ba4de PNA |
6636 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6637 | if (err < 0) | |
6638 | goto err_parse_key_end; | |
7b225d0b PNA |
6639 | } |
6640 | ||
c3e1b005 | 6641 | if (timeout > 0) { |
c39ba4de PNA |
6642 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); |
6643 | if (err < 0) | |
6644 | goto err_parse_key_end; | |
6645 | ||
123b9961 | 6646 | if (timeout != READ_ONCE(set->timeout)) { |
c39ba4de PNA |
6647 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); |
6648 | if (err < 0) | |
6649 | goto err_parse_key_end; | |
6650 | } | |
c3e1b005 | 6651 | } |
fe2811eb | 6652 | |
664899e8 PNA |
6653 | if (num_exprs) { |
6654 | for (i = 0; i < num_exprs; i++) | |
563125a7 PNA |
6655 | size += expr_array[i]->ops->size; |
6656 | ||
c39ba4de PNA |
6657 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS, |
6658 | sizeof(struct nft_set_elem_expr) + size); | |
6659 | if (err < 0) | |
6660 | goto err_parse_key_end; | |
563125a7 | 6661 | } |
40944452 | 6662 | |
8aeff920 | 6663 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
4d44175a FW |
6664 | obj = nft_obj_lookup(ctx->net, ctx->table, |
6665 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 6666 | set->objtype, genmask); |
8aeff920 PNA |
6667 | if (IS_ERR(obj)) { |
6668 | err = PTR_ERR(obj); | |
1689f259 | 6669 | obj = NULL; |
7b225d0b | 6670 | goto err_parse_key_end; |
8aeff920 | 6671 | } |
1689f259 PNA |
6672 | |
6673 | if (!nft_use_inc(&obj->use)) { | |
6674 | err = -EMFILE; | |
6675 | obj = NULL; | |
6676 | goto err_parse_key_end; | |
6677 | } | |
6678 | ||
c39ba4de PNA |
6679 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); |
6680 | if (err < 0) | |
6681 | goto err_parse_key_end; | |
8aeff920 PNA |
6682 | } |
6683 | ||
20a69341 | 6684 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
6685 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
6686 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 6687 | if (err < 0) |
7b225d0b | 6688 | goto err_parse_key_end; |
20a69341 | 6689 | |
20a69341 PM |
6690 | dreg = nft_type_to_reg(set->dtype); |
6691 | list_for_each_entry(binding, &set->bindings, list) { | |
6692 | struct nft_ctx bind_ctx = { | |
58c78e10 | 6693 | .net = ctx->net, |
36596dad | 6694 | .family = ctx->family, |
20a69341 | 6695 | .table = ctx->table, |
7c95f6d8 | 6696 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
6697 | }; |
6698 | ||
11113e19 PM |
6699 | if (!(binding->flags & NFT_SET_MAP)) |
6700 | continue; | |
6701 | ||
1ec10212 | 6702 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 6703 | &elem.data.val, |
20a1452c | 6704 | desc.type, desc.len); |
20a69341 | 6705 | if (err < 0) |
7b225d0b | 6706 | goto err_parse_data; |
a654de8f | 6707 | |
20a1452c | 6708 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
6709 | (elem.data.val.verdict.code == NFT_GOTO || |
6710 | elem.data.val.verdict.code == NFT_JUMP)) | |
00c320f9 | 6711 | nft_validate_state_update(ctx->table, |
a654de8f | 6712 | NFT_VALIDATE_NEED); |
20a69341 | 6713 | } |
fe2811eb | 6714 | |
c39ba4de PNA |
6715 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
6716 | if (err < 0) | |
6717 | goto err_parse_data; | |
20a69341 PM |
6718 | } |
6719 | ||
68e942e8 PM |
6720 | /* The full maximum length of userdata can exceed the maximum |
6721 | * offset value (U8_MAX) for following extensions, therefor it | |
6722 | * must be the last extension added. | |
6723 | */ | |
6724 | ulen = 0; | |
6725 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
6726 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
c39ba4de PNA |
6727 | if (ulen > 0) { |
6728 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
6729 | ulen); | |
6730 | if (err < 0) | |
6731 | goto err_parse_data; | |
6732 | } | |
68e942e8 PM |
6733 | } |
6734 | ||
7b225d0b | 6735 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 6736 | elem.key_end.val.data, elem.data.val.data, |
33758c89 | 6737 | timeout, expiration, GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6738 | if (IS_ERR(elem.priv)) { |
6739 | err = PTR_ERR(elem.priv); | |
7b225d0b | 6740 | goto err_parse_data; |
34aae2c2 | 6741 | } |
fe2811eb PM |
6742 | |
6743 | ext = nft_set_elem_ext(set, elem.priv); | |
6744 | if (flags) | |
6745 | *nft_set_ext_flags(ext) = flags; | |
34aae2c2 | 6746 | |
1689f259 | 6747 | if (obj) |
d6b47866 | 6748 | *nft_set_ext_obj(ext) = obj; |
1689f259 | 6749 | |
68e942e8 | 6750 | if (ulen > 0) { |
34aae2c2 PNA |
6751 | if (nft_set_ext_check(&tmpl, NFT_SET_EXT_USERDATA, ulen) < 0) { |
6752 | err = -EINVAL; | |
d6b47866 | 6753 | goto err_elem_free; |
34aae2c2 | 6754 | } |
68e942e8 PM |
6755 | udata = nft_set_ext_userdata(ext); |
6756 | udata->len = ulen - 1; | |
6757 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
6758 | } | |
34aae2c2 | 6759 | err = nft_set_elem_expr_setup(ctx, &tmpl, ext, expr_array, num_exprs); |
4d8f9065 | 6760 | if (err < 0) |
34aae2c2 | 6761 | goto err_elem_free; |
fe2811eb | 6762 | |
60319eb1 | 6763 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
4d8f9065 PNA |
6764 | if (trans == NULL) { |
6765 | err = -ENOMEM; | |
34aae2c2 | 6766 | goto err_elem_free; |
4d8f9065 | 6767 | } |
60319eb1 | 6768 | |
a2dd0233 | 6769 | ext->genmask = nft_genmask_cur(ctx->net); |
aaa31047 PNA |
6770 | |
6771 | err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); | |
c016c7e4 PNA |
6772 | if (err) { |
6773 | if (err == -EEXIST) { | |
9744a6fc PNA |
6774 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
6775 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
6776 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
77a92189 | 6777 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) |
7b225d0b | 6778 | goto err_element_clash; |
8aeff920 PNA |
6779 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
6780 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
6781 | memcmp(nft_set_ext_data(ext), | |
6782 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
6783 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
6784 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
6785 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
77a92189 | 6786 | goto err_element_clash; |
c016c7e4 PNA |
6787 | else if (!(nlmsg_flags & NLM_F_EXCL)) |
6788 | err = 0; | |
8c2d45b2 PNA |
6789 | } else if (err == -ENOTEMPTY) { |
6790 | /* ENOTEMPTY reports overlapping between this element | |
6791 | * and an existing one. | |
6792 | */ | |
6793 | err = -EEXIST; | |
c016c7e4 | 6794 | } |
7b225d0b | 6795 | goto err_element_clash; |
c016c7e4 | 6796 | } |
20a69341 | 6797 | |
d4b7f29e FW |
6798 | if (!(flags & NFT_SET_ELEM_CATCHALL)) { |
6799 | unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX; | |
6800 | ||
6801 | if (!atomic_add_unless(&set->nelems, 1, max)) { | |
6802 | err = -ENFILE; | |
6803 | goto err_set_full; | |
6804 | } | |
35d0ac90 PNA |
6805 | } |
6806 | ||
60319eb1 | 6807 | nft_trans_elem(trans) = elem; |
0854db2a | 6808 | nft_trans_commit_list_add_tail(ctx->net, trans); |
20a69341 PM |
6809 | return 0; |
6810 | ||
7b225d0b | 6811 | err_set_full: |
aaa31047 | 6812 | nft_setelem_remove(ctx->net, set, &elem); |
7b225d0b | 6813 | err_element_clash: |
60319eb1 | 6814 | kfree(trans); |
34aae2c2 | 6815 | err_elem_free: |
b389139f | 6816 | nf_tables_set_elem_destroy(ctx, set, elem.priv); |
7b225d0b | 6817 | err_parse_data: |
20a69341 | 6818 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 6819 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b | 6820 | err_parse_key_end: |
1689f259 PNA |
6821 | if (obj) |
6822 | nft_use_dec_restore(&obj->use); | |
6823 | ||
7b225d0b PNA |
6824 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); |
6825 | err_parse_key: | |
20a1452c | 6826 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 | 6827 | err_set_elem_expr: |
664899e8 | 6828 | for (i = 0; i < num_exprs && expr_array[i]; i++) |
8cfd9b0f PNA |
6829 | nft_expr_destroy(ctx, expr_array[i]); |
6830 | err_set_elem_expr_clone: | |
20a69341 PM |
6831 | return err; |
6832 | } | |
6833 | ||
7dab8ee3 PNA |
6834 | static int nf_tables_newsetelem(struct sk_buff *skb, |
6835 | const struct nfnl_info *info, | |
6836 | const struct nlattr * const nla[]) | |
20a69341 | 6837 | { |
7dab8ee3 PNA |
6838 | struct netlink_ext_ack *extack = info->extack; |
6839 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 6840 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 6841 | struct net *net = info->net; |
20a69341 | 6842 | const struct nlattr *attr; |
e2b750d7 | 6843 | struct nft_table *table; |
20a69341 PM |
6844 | struct nft_set *set; |
6845 | struct nft_ctx ctx; | |
a654de8f | 6846 | int rem, err; |
20a69341 | 6847 | |
7d5570ca PNA |
6848 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
6849 | return -EINVAL; | |
6850 | ||
e2b750d7 PNA |
6851 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
6852 | genmask, NETLINK_CB(skb).portid); | |
6853 | if (IS_ERR(table)) { | |
6854 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
6855 | return PTR_ERR(table); | |
6856 | } | |
20a69341 | 6857 | |
e2b750d7 | 6858 | set = nft_set_lookup_global(net, table, nla[NFTA_SET_ELEM_LIST_SET], |
a3073c17 PNA |
6859 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); |
6860 | if (IS_ERR(set)) | |
6861 | return PTR_ERR(set); | |
958bee14 | 6862 | |
c88c535b PNA |
6863 | if (!list_empty(&set->bindings) && |
6864 | (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS))) | |
20a69341 PM |
6865 | return -EBUSY; |
6866 | ||
e2b750d7 PNA |
6867 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
6868 | ||
20a69341 | 6869 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7dab8ee3 | 6870 | err = nft_add_set_elem(&ctx, set, attr, info->nlh->nlmsg_flags); |
b53c1166 PNA |
6871 | if (err < 0) { |
6872 | NL_SET_BAD_ATTR(extack, attr); | |
a654de8f | 6873 | return err; |
b53c1166 | 6874 | } |
20a69341 | 6875 | } |
a654de8f | 6876 | |
00c320f9 | 6877 | if (table->validate_state == NFT_VALIDATE_DO) |
e2b750d7 | 6878 | return nft_table_validate(net, table); |
a654de8f PNA |
6879 | |
6880 | return 0; | |
20a69341 PM |
6881 | } |
6882 | ||
59105446 PNA |
6883 | /** |
6884 | * nft_data_hold - hold a nft_data item | |
6885 | * | |
6886 | * @data: struct nft_data to release | |
6887 | * @type: type of data | |
6888 | * | |
6889 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
6890 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
6891 | * NFT_GOTO verdicts. This function must be called on active data objects | |
6892 | * from the second phase of the commit protocol. | |
6893 | */ | |
bb7b40ae | 6894 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 | 6895 | { |
d0e2c7de | 6896 | struct nft_chain *chain; |
d0e2c7de | 6897 | |
59105446 PNA |
6898 | if (type == NFT_DATA_VERDICT) { |
6899 | switch (data->verdict.code) { | |
6900 | case NFT_JUMP: | |
6901 | case NFT_GOTO: | |
d0e2c7de | 6902 | chain = data->verdict.chain; |
1689f259 | 6903 | nft_use_inc_restore(&chain->use); |
59105446 PNA |
6904 | break; |
6905 | } | |
6906 | } | |
6907 | } | |
6908 | ||
f8bb7889 PNA |
6909 | static void nft_setelem_data_activate(const struct net *net, |
6910 | const struct nft_set *set, | |
6911 | struct nft_set_elem *elem) | |
59105446 PNA |
6912 | { |
6913 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6914 | ||
6915 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6916 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
6917 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
1689f259 | 6918 | nft_use_inc_restore(&(*nft_set_ext_obj(ext))->use); |
59105446 PNA |
6919 | } |
6920 | ||
5f68718b PNA |
6921 | void nft_setelem_data_deactivate(const struct net *net, |
6922 | const struct nft_set *set, | |
6923 | struct nft_set_elem *elem) | |
59105446 PNA |
6924 | { |
6925 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
6926 | ||
6927 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
6928 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
6929 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
1689f259 | 6930 | nft_use_dec(&(*nft_set_ext_obj(ext))->use); |
59105446 PNA |
6931 | } |
6932 | ||
60319eb1 | 6933 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
6934 | const struct nlattr *attr) |
6935 | { | |
6936 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 6937 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 6938 | struct nft_set_elem elem; |
3971ca14 | 6939 | struct nft_set_ext *ext; |
60319eb1 | 6940 | struct nft_trans *trans; |
3971ca14 | 6941 | u32 flags = 0; |
20a69341 PM |
6942 | int err; |
6943 | ||
8cb08174 JB |
6944 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
6945 | nft_set_elem_policy, NULL); | |
20a69341 | 6946 | if (err < 0) |
20a1452c | 6947 | return err; |
20a69341 | 6948 | |
aaa31047 PNA |
6949 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
6950 | if (err < 0) | |
6951 | return err; | |
6952 | ||
6953 | if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) | |
20a1452c | 6954 | return -EINVAL; |
20a69341 | 6955 | |
88cccd90 PNA |
6956 | if (!nft_setelem_valid_key_end(set, nla, flags)) |
6957 | return -EINVAL; | |
6958 | ||
3971ca14 PNA |
6959 | nft_set_ext_prepare(&tmpl); |
6960 | ||
c39ba4de PNA |
6961 | if (flags != 0) { |
6962 | err = nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
6963 | if (err < 0) | |
6964 | return err; | |
6965 | } | |
3971ca14 | 6966 | |
aaa31047 PNA |
6967 | if (nla[NFTA_SET_ELEM_KEY]) { |
6968 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, | |
6969 | nla[NFTA_SET_ELEM_KEY]); | |
6970 | if (err < 0) | |
6971 | return err; | |
20a69341 | 6972 | |
c39ba4de PNA |
6973 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
6974 | if (err < 0) | |
6975 | goto fail_elem; | |
aaa31047 | 6976 | } |
3971ca14 | 6977 | |
7b225d0b PNA |
6978 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
6979 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
6980 | nla[NFTA_SET_ELEM_KEY_END]); | |
6981 | if (err < 0) | |
c39ba4de | 6982 | goto fail_elem; |
7b225d0b | 6983 | |
c39ba4de PNA |
6984 | err = nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); |
6985 | if (err < 0) | |
6986 | goto fail_elem_key_end; | |
7b225d0b PNA |
6987 | } |
6988 | ||
3971ca14 | 6989 | err = -ENOMEM; |
7b225d0b PNA |
6990 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
6991 | elem.key_end.val.data, NULL, 0, 0, | |
33758c89 | 6992 | GFP_KERNEL_ACCOUNT); |
34aae2c2 PNA |
6993 | if (IS_ERR(elem.priv)) { |
6994 | err = PTR_ERR(elem.priv); | |
c39ba4de | 6995 | goto fail_elem_key_end; |
34aae2c2 | 6996 | } |
3971ca14 PNA |
6997 | |
6998 | ext = nft_set_elem_ext(set, elem.priv); | |
6999 | if (flags) | |
7000 | *nft_set_ext_flags(ext) = flags; | |
7001 | ||
60319eb1 | 7002 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
7003 | if (trans == NULL) |
7004 | goto fail_trans; | |
20a69341 | 7005 | |
aaa31047 PNA |
7006 | err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
7007 | if (err < 0) | |
20a1452c | 7008 | goto fail_ops; |
cc02e457 | 7009 | |
f8bb7889 | 7010 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
59105446 | 7011 | |
60319eb1 | 7012 | nft_trans_elem(trans) = elem; |
0854db2a | 7013 | nft_trans_commit_list_add_tail(ctx->net, trans); |
0dc13625 | 7014 | return 0; |
cc02e457 | 7015 | |
20a1452c | 7016 | fail_ops: |
cc02e457 | 7017 | kfree(trans); |
20a1452c | 7018 | fail_trans: |
3971ca14 | 7019 | kfree(elem.priv); |
c39ba4de PNA |
7020 | fail_elem_key_end: |
7021 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
20a1452c PNA |
7022 | fail_elem: |
7023 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
7024 | return err; |
7025 | } | |
7026 | ||
e6ba7cb6 PNA |
7027 | static int nft_setelem_flush(const struct nft_ctx *ctx, |
7028 | struct nft_set *set, | |
7029 | const struct nft_set_iter *iter, | |
7030 | struct nft_set_elem *elem) | |
8411b644 PNA |
7031 | { |
7032 | struct nft_trans *trans; | |
7033 | int err; | |
7034 | ||
7035 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
7036 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
7037 | if (!trans) | |
7038 | return -ENOMEM; | |
7039 | ||
1ba1c414 | 7040 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
7041 | err = -ENOENT; |
7042 | goto err1; | |
7043 | } | |
b2c11e4b | 7044 | set->ndeact++; |
8411b644 | 7045 | |
f8bb7889 | 7046 | nft_setelem_data_deactivate(ctx->net, set, elem); |
de70185d PNA |
7047 | nft_trans_elem_set(trans) = set; |
7048 | nft_trans_elem(trans) = *elem; | |
0854db2a | 7049 | nft_trans_commit_list_add_tail(ctx->net, trans); |
8411b644 PNA |
7050 | |
7051 | return 0; | |
7052 | err1: | |
7053 | kfree(trans); | |
7054 | return err; | |
7055 | } | |
7056 | ||
aaa31047 PNA |
7057 | static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
7058 | struct nft_set *set, | |
7059 | struct nft_set_elem *elem) | |
7060 | { | |
7061 | struct nft_trans *trans; | |
7062 | ||
7063 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
7064 | sizeof(struct nft_trans_elem), GFP_KERNEL); | |
7065 | if (!trans) | |
7066 | return -ENOMEM; | |
7067 | ||
7068 | nft_setelem_data_deactivate(ctx->net, set, elem); | |
7069 | nft_trans_elem_set(trans) = set; | |
7070 | nft_trans_elem(trans) = *elem; | |
7071 | nft_trans_commit_list_add_tail(ctx->net, trans); | |
7072 | ||
7073 | return 0; | |
7074 | } | |
7075 | ||
7076 | static int nft_set_catchall_flush(const struct nft_ctx *ctx, | |
7077 | struct nft_set *set) | |
7078 | { | |
7079 | u8 genmask = nft_genmask_next(ctx->net); | |
7080 | struct nft_set_elem_catchall *catchall; | |
7081 | struct nft_set_elem elem; | |
7082 | struct nft_set_ext *ext; | |
7083 | int ret = 0; | |
7084 | ||
7085 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
7086 | ext = nft_set_elem_ext(set, catchall->elem); | |
f6c383b8 | 7087 | if (!nft_set_elem_active(ext, genmask)) |
aaa31047 PNA |
7088 | continue; |
7089 | ||
7090 | elem.priv = catchall->elem; | |
7091 | ret = __nft_set_catchall_flush(ctx, set, &elem); | |
7092 | if (ret < 0) | |
7093 | break; | |
90e5b346 | 7094 | nft_set_elem_change_active(ctx->net, set, ext); |
aaa31047 PNA |
7095 | } |
7096 | ||
7097 | return ret; | |
7098 | } | |
7099 | ||
e6ba7cb6 PNA |
7100 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
7101 | { | |
7102 | struct nft_set_iter iter = { | |
7103 | .genmask = genmask, | |
7104 | .fn = nft_setelem_flush, | |
7105 | }; | |
7106 | ||
7107 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
7108 | if (!iter.err) |
7109 | iter.err = nft_set_catchall_flush(ctx, set); | |
e6ba7cb6 PNA |
7110 | |
7111 | return iter.err; | |
7112 | } | |
7113 | ||
7dab8ee3 PNA |
7114 | static int nf_tables_delsetelem(struct sk_buff *skb, |
7115 | const struct nfnl_info *info, | |
7116 | const struct nlattr * const nla[]) | |
20a69341 | 7117 | { |
7dab8ee3 PNA |
7118 | struct netlink_ext_ack *extack = info->extack; |
7119 | u8 genmask = nft_genmask_next(info->net); | |
e2b750d7 | 7120 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7121 | struct net *net = info->net; |
20a69341 | 7122 | const struct nlattr *attr; |
e2b750d7 | 7123 | struct nft_table *table; |
20a69341 PM |
7124 | struct nft_set *set; |
7125 | struct nft_ctx ctx; | |
60319eb1 | 7126 | int rem, err = 0; |
20a69341 | 7127 | |
e2b750d7 PNA |
7128 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
7129 | genmask, NETLINK_CB(skb).portid); | |
7130 | if (IS_ERR(table)) { | |
7131 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
7132 | return PTR_ERR(table); | |
7133 | } | |
20a69341 | 7134 | |
e2b750d7 | 7135 | set = nft_set_lookup(table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
7136 | if (IS_ERR(set)) |
7137 | return PTR_ERR(set); | |
c88c535b PNA |
7138 | |
7139 | if (!list_empty(&set->bindings) && | |
7140 | (set->flags & (NFT_SET_CONSTANT | NFT_SET_ANONYMOUS))) | |
20a69341 PM |
7141 | return -EBUSY; |
7142 | ||
e2b750d7 PNA |
7143 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
7144 | ||
e6ba7cb6 PNA |
7145 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) |
7146 | return nft_set_flush(&ctx, set, genmask); | |
8411b644 | 7147 | |
20a69341 PM |
7148 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
7149 | err = nft_del_setelem(&ctx, set, attr); | |
f80a612d FFM |
7150 | if (err == -ENOENT && |
7151 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYSETELEM) | |
7152 | continue; | |
7153 | ||
b53c1166 PNA |
7154 | if (err < 0) { |
7155 | NL_SET_BAD_ATTR(extack, attr); | |
60319eb1 | 7156 | break; |
b53c1166 | 7157 | } |
20a69341 | 7158 | } |
60319eb1 | 7159 | return err; |
20a69341 PM |
7160 | } |
7161 | ||
e5009240 PNA |
7162 | /* |
7163 | * Stateful objects | |
7164 | */ | |
7165 | ||
7166 | /** | |
7167 | * nft_register_obj- register nf_tables stateful object type | |
3db86c39 | 7168 | * @obj_type: object type |
e5009240 PNA |
7169 | * |
7170 | * Registers the object type for use with nf_tables. Returns zero on | |
7171 | * success or a negative errno code otherwise. | |
7172 | */ | |
7173 | int nft_register_obj(struct nft_object_type *obj_type) | |
7174 | { | |
7175 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
7176 | return -EINVAL; | |
7177 | ||
7178 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7179 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
7180 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7181 | return 0; | |
7182 | } | |
7183 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
7184 | ||
7185 | /** | |
7186 | * nft_unregister_obj - unregister nf_tables object type | |
3db86c39 | 7187 | * @obj_type: object type |
e5009240 PNA |
7188 | * |
7189 | * Unregisters the object type for use with nf_tables. | |
7190 | */ | |
7191 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
7192 | { | |
7193 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7194 | list_del_rcu(&obj_type->list); | |
7195 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7196 | } | |
7197 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
7198 | ||
4d44175a FW |
7199 | struct nft_object *nft_obj_lookup(const struct net *net, |
7200 | const struct nft_table *table, | |
cac20fcd PNA |
7201 | const struct nlattr *nla, u32 objtype, |
7202 | u8 genmask) | |
e5009240 | 7203 | { |
4d44175a FW |
7204 | struct nft_object_hash_key k = { .table = table }; |
7205 | char search[NFT_OBJ_MAXNAMELEN]; | |
7206 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
7207 | struct nft_object *obj; |
7208 | ||
872f6903 | 7209 | nla_strscpy(search, nla, sizeof(search)); |
4d44175a FW |
7210 | k.name = search; |
7211 | ||
7212 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
7213 | !lockdep_commit_lock_is_held(net)); | |
7214 | ||
7215 | rcu_read_lock(); | |
7216 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
7217 | if (!list) | |
7218 | goto out; | |
7219 | ||
7220 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
7221 | if (objtype == obj->ops->type->type && | |
7222 | nft_active_genmask(obj, genmask)) { | |
7223 | rcu_read_unlock(); | |
e5009240 | 7224 | return obj; |
4d44175a | 7225 | } |
e5009240 | 7226 | } |
4d44175a FW |
7227 | out: |
7228 | rcu_read_unlock(); | |
e5009240 PNA |
7229 | return ERR_PTR(-ENOENT); |
7230 | } | |
cac20fcd | 7231 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 7232 | |
cac20fcd PNA |
7233 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
7234 | const struct nlattr *nla, | |
7235 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
7236 | { |
7237 | struct nft_object *obj; | |
7238 | ||
7239 | list_for_each_entry(obj, &table->objects, list) { | |
7240 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
7241 | objtype == obj->ops->type->type && | |
7242 | nft_active_genmask(obj, genmask)) | |
7243 | return obj; | |
7244 | } | |
7245 | return ERR_PTR(-ENOENT); | |
7246 | } | |
7247 | ||
e5009240 | 7248 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
7249 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
7250 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
7251 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
7252 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
7253 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
7254 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7255 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
b131c964 JGG |
7256 | [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY, |
7257 | .len = NFT_USERDATA_MAXLEN }, | |
e5009240 PNA |
7258 | }; |
7259 | ||
84fba055 FW |
7260 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
7261 | const struct nft_object_type *type, | |
e5009240 PNA |
7262 | const struct nlattr *attr) |
7263 | { | |
5b4c6e38 | 7264 | struct nlattr **tb; |
dfc46034 | 7265 | const struct nft_object_ops *ops; |
e5009240 | 7266 | struct nft_object *obj; |
5b4c6e38 GS |
7267 | int err = -ENOMEM; |
7268 | ||
7269 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
7270 | if (!tb) | |
7271 | goto err1; | |
e5009240 PNA |
7272 | |
7273 | if (attr) { | |
8cb08174 JB |
7274 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
7275 | type->policy, NULL); | |
e5009240 | 7276 | if (err < 0) |
5b4c6e38 | 7277 | goto err2; |
e5009240 PNA |
7278 | } else { |
7279 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
7280 | } | |
7281 | ||
dfc46034 PBG |
7282 | if (type->select_ops) { |
7283 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
7284 | if (IS_ERR(ops)) { | |
7285 | err = PTR_ERR(ops); | |
5b4c6e38 | 7286 | goto err2; |
dfc46034 PBG |
7287 | } |
7288 | } else { | |
7289 | ops = type->ops; | |
7290 | } | |
7291 | ||
e5009240 | 7292 | err = -ENOMEM; |
33758c89 | 7293 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL_ACCOUNT); |
5b4c6e38 GS |
7294 | if (!obj) |
7295 | goto err2; | |
e5009240 | 7296 | |
dfc46034 | 7297 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 7298 | if (err < 0) |
5b4c6e38 | 7299 | goto err3; |
e5009240 | 7300 | |
dfc46034 PBG |
7301 | obj->ops = ops; |
7302 | ||
5b4c6e38 | 7303 | kfree(tb); |
e5009240 | 7304 | return obj; |
5b4c6e38 | 7305 | err3: |
e5009240 | 7306 | kfree(obj); |
5b4c6e38 GS |
7307 | err2: |
7308 | kfree(tb); | |
e5009240 PNA |
7309 | err1: |
7310 | return ERR_PTR(err); | |
7311 | } | |
7312 | ||
7313 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 7314 | struct nft_object *obj, bool reset) |
e5009240 PNA |
7315 | { |
7316 | struct nlattr *nest; | |
7317 | ||
ae0be8de | 7318 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
7319 | if (!nest) |
7320 | goto nla_put_failure; | |
dfc46034 | 7321 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
7322 | goto nla_put_failure; |
7323 | nla_nest_end(skb, nest); | |
7324 | return 0; | |
7325 | ||
7326 | nla_put_failure: | |
7327 | return -1; | |
7328 | } | |
7329 | ||
7330 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
7331 | { | |
7332 | const struct nft_object_type *type; | |
7333 | ||
7334 | list_for_each_entry(type, &nf_tables_objects, list) { | |
7335 | if (objtype == type->type) | |
7336 | return type; | |
7337 | } | |
7338 | return NULL; | |
7339 | } | |
7340 | ||
452238e8 FW |
7341 | static const struct nft_object_type * |
7342 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
7343 | { |
7344 | const struct nft_object_type *type; | |
7345 | ||
7346 | type = __nft_obj_type_get(objtype); | |
7347 | if (type != NULL && try_module_get(type->owner)) | |
7348 | return type; | |
7349 | ||
f102d66b | 7350 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
7351 | #ifdef CONFIG_MODULES |
7352 | if (type == NULL) { | |
eb014de4 | 7353 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
7354 | return ERR_PTR(-EAGAIN); |
7355 | } | |
7356 | #endif | |
7357 | return ERR_PTR(-ENOENT); | |
7358 | } | |
7359 | ||
d62d0ba9 FFM |
7360 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
7361 | const struct nft_object_type *type, | |
7362 | const struct nlattr *attr, | |
7363 | struct nft_object *obj) | |
7364 | { | |
7365 | struct nft_object *newobj; | |
7366 | struct nft_trans *trans; | |
dad3bdee FW |
7367 | int err = -ENOMEM; |
7368 | ||
7369 | if (!try_module_get(type->owner)) | |
7370 | return -ENOENT; | |
d62d0ba9 FFM |
7371 | |
7372 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
7373 | sizeof(struct nft_trans_obj)); | |
7374 | if (!trans) | |
dad3bdee | 7375 | goto err_trans; |
d62d0ba9 FFM |
7376 | |
7377 | newobj = nft_obj_init(ctx, type, attr); | |
7378 | if (IS_ERR(newobj)) { | |
7379 | err = PTR_ERR(newobj); | |
b74ae961 | 7380 | goto err_free_trans; |
d62d0ba9 FFM |
7381 | } |
7382 | ||
7383 | nft_trans_obj(trans) = obj; | |
7384 | nft_trans_obj_update(trans) = true; | |
7385 | nft_trans_obj_newobj(trans) = newobj; | |
0854db2a | 7386 | nft_trans_commit_list_add_tail(ctx->net, trans); |
d62d0ba9 FFM |
7387 | |
7388 | return 0; | |
b74ae961 DC |
7389 | |
7390 | err_free_trans: | |
d62d0ba9 | 7391 | kfree(trans); |
dad3bdee FW |
7392 | err_trans: |
7393 | module_put(type->owner); | |
d62d0ba9 FFM |
7394 | return err; |
7395 | } | |
7396 | ||
7dab8ee3 PNA |
7397 | static int nf_tables_newobj(struct sk_buff *skb, const struct nfnl_info *info, |
7398 | const struct nlattr * const nla[]) | |
e5009240 | 7399 | { |
7dab8ee3 PNA |
7400 | struct netlink_ext_ack *extack = info->extack; |
7401 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7402 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7403 | const struct nft_object_type *type; |
7dab8ee3 | 7404 | struct net *net = info->net; |
e5009240 PNA |
7405 | struct nft_table *table; |
7406 | struct nft_object *obj; | |
7407 | struct nft_ctx ctx; | |
7408 | u32 objtype; | |
7409 | int err; | |
7410 | ||
7411 | if (!nla[NFTA_OBJ_TYPE] || | |
7412 | !nla[NFTA_OBJ_NAME] || | |
7413 | !nla[NFTA_OBJ_DATA]) | |
7414 | return -EINVAL; | |
7415 | ||
6001a930 PNA |
7416 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7417 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7418 | if (IS_ERR(table)) { |
7419 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7420 | return PTR_ERR(table); |
36dd1bcc | 7421 | } |
e5009240 PNA |
7422 | |
7423 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7424 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
7425 | if (IS_ERR(obj)) { |
7426 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
7427 | if (err != -ENOENT) { |
7428 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7429 | return err; |
36dd1bcc | 7430 | } |
1a28ad74 | 7431 | } else { |
7dab8ee3 | 7432 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 7433 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); |
e5009240 | 7434 | return -EEXIST; |
36dd1bcc | 7435 | } |
7dab8ee3 | 7436 | if (info->nlh->nlmsg_flags & NLM_F_REPLACE) |
d62d0ba9 FFM |
7437 | return -EOPNOTSUPP; |
7438 | ||
fd57d0cb | 7439 | type = __nft_obj_type_get(objtype); |
dac7f50a AT |
7440 | if (WARN_ON_ONCE(!type)) |
7441 | return -ENOENT; | |
7442 | ||
7dab8ee3 | 7443 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
d62d0ba9 FFM |
7444 | |
7445 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
7446 | } |
7447 | ||
7dab8ee3 | 7448 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 | 7449 | |
1689f259 PNA |
7450 | if (!nft_use_inc(&table->use)) |
7451 | return -EMFILE; | |
7452 | ||
452238e8 | 7453 | type = nft_obj_type_get(net, objtype); |
1689f259 PNA |
7454 | if (IS_ERR(type)) { |
7455 | err = PTR_ERR(type); | |
7456 | goto err_type; | |
7457 | } | |
e5009240 | 7458 | |
84fba055 | 7459 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
7460 | if (IS_ERR(obj)) { |
7461 | err = PTR_ERR(obj); | |
b131c964 | 7462 | goto err_init; |
e5009240 | 7463 | } |
d152159b | 7464 | obj->key.table = table; |
3ecbfd65 HS |
7465 | obj->handle = nf_tables_alloc_handle(table); |
7466 | ||
33758c89 | 7467 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL_ACCOUNT); |
d152159b | 7468 | if (!obj->key.name) { |
61509575 | 7469 | err = -ENOMEM; |
b131c964 JGG |
7470 | goto err_strdup; |
7471 | } | |
7472 | ||
7473 | if (nla[NFTA_OBJ_USERDATA]) { | |
af0acf22 | 7474 | obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL_ACCOUNT); |
b131c964 JGG |
7475 | if (obj->udata == NULL) |
7476 | goto err_userdata; | |
7477 | ||
85db827a | 7478 | obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]); |
61509575 | 7479 | } |
e5009240 PNA |
7480 | |
7481 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
7482 | if (err < 0) | |
b131c964 | 7483 | goto err_trans; |
e5009240 | 7484 | |
4d44175a FW |
7485 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
7486 | nft_objname_ht_params); | |
7487 | if (err < 0) | |
b131c964 | 7488 | goto err_obj_ht; |
4d44175a | 7489 | |
e5009240 | 7490 | list_add_tail_rcu(&obj->list, &table->objects); |
1689f259 | 7491 | |
e5009240 | 7492 | return 0; |
b131c964 | 7493 | err_obj_ht: |
4d44175a FW |
7494 | /* queued in transaction log */ |
7495 | INIT_LIST_HEAD(&obj->list); | |
7496 | return err; | |
b131c964 | 7497 | err_trans: |
b131c964 | 7498 | kfree(obj->udata); |
85dfd816 PNA |
7499 | err_userdata: |
7500 | kfree(obj->key.name); | |
b131c964 | 7501 | err_strdup: |
dfc46034 | 7502 | if (obj->ops->destroy) |
00bfb320 | 7503 | obj->ops->destroy(&ctx, obj); |
e5009240 | 7504 | kfree(obj); |
b131c964 | 7505 | err_init: |
e5009240 | 7506 | module_put(type->owner); |
1689f259 PNA |
7507 | err_type: |
7508 | nft_use_dec_restore(&table->use); | |
7509 | ||
e5009240 PNA |
7510 | return err; |
7511 | } | |
7512 | ||
7513 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
7514 | u32 portid, u32 seq, int event, u32 flags, | |
7515 | int family, const struct nft_table *table, | |
43da04a5 | 7516 | struct nft_object *obj, bool reset) |
e5009240 | 7517 | { |
e5009240 PNA |
7518 | struct nlmsghdr *nlh; |
7519 | ||
dedb67c4 | 7520 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
19c28b13 PNA |
7521 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
7522 | NFNETLINK_V0, nft_base_seq(net)); | |
7523 | if (!nlh) | |
e5009240 PNA |
7524 | goto nla_put_failure; |
7525 | ||
e5009240 | 7526 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || |
d152159b | 7527 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
3ecbfd65 HS |
7528 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), |
7529 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
7530 | goto nla_put_failure; |
7531 | ||
28339b21 PNA |
7532 | if (event == NFT_MSG_DELOBJ) { |
7533 | nlmsg_end(skb, nlh); | |
7534 | return 0; | |
7535 | } | |
7536 | ||
7537 | if (nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || | |
7538 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || | |
7539 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset)) | |
7540 | goto nla_put_failure; | |
7541 | ||
b131c964 JGG |
7542 | if (obj->udata && |
7543 | nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata)) | |
7544 | goto nla_put_failure; | |
7545 | ||
e5009240 PNA |
7546 | nlmsg_end(skb, nlh); |
7547 | return 0; | |
7548 | ||
7549 | nla_put_failure: | |
7550 | nlmsg_trim(skb, nlh); | |
7551 | return -1; | |
7552 | } | |
7553 | ||
a9fea2a3 | 7554 | struct nft_obj_filter { |
e46abbcc | 7555 | char *table; |
a9fea2a3 PNA |
7556 | u32 type; |
7557 | }; | |
7558 | ||
e5009240 PNA |
7559 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
7560 | { | |
7561 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 7562 | const struct nft_table *table; |
e5009240 | 7563 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 7564 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
7565 | struct net *net = sock_net(skb->sk); |
7566 | int family = nfmsg->nfgen_family; | |
0854db2a | 7567 | struct nftables_pernet *nft_net; |
43da04a5 PNA |
7568 | struct nft_object *obj; |
7569 | bool reset = false; | |
7570 | ||
7571 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
7572 | reset = true; | |
e5009240 PNA |
7573 | |
7574 | rcu_read_lock(); | |
d59d2f82 | 7575 | nft_net = nft_pernet(net); |
34002783 | 7576 | cb->seq = READ_ONCE(nft_net->base_seq); |
e5009240 | 7577 | |
0854db2a | 7578 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 7579 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
7580 | continue; |
7581 | ||
36596dad PNA |
7582 | list_for_each_entry_rcu(obj, &table->objects, list) { |
7583 | if (!nft_is_active(net, obj)) | |
7584 | goto cont; | |
7585 | if (idx < s_idx) | |
7586 | goto cont; | |
7587 | if (idx > s_idx) | |
7588 | memset(&cb->args[1], 0, | |
7589 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 7590 | if (filter && filter->table && |
36596dad PNA |
7591 | strcmp(filter->table, table->name)) |
7592 | goto cont; | |
7593 | if (filter && | |
7594 | filter->type != NFT_OBJECT_UNSPEC && | |
7595 | obj->ops->type->type != filter->type) | |
7596 | goto cont; | |
8e6cf365 | 7597 | if (reset) { |
68df2ed5 | 7598 | char *buf = kasprintf(GFP_ATOMIC, |
c520292f | 7599 | "%s:%u", |
8e6cf365 | 7600 | table->name, |
0854db2a | 7601 | nft_net->base_seq); |
8e6cf365 RGB |
7602 | |
7603 | audit_log_nfcfg(buf, | |
7604 | family, | |
7605 | obj->handle, | |
14224039 | 7606 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7607 | GFP_ATOMIC); |
8e6cf365 RGB |
7608 | kfree(buf); |
7609 | } | |
7610 | ||
36596dad PNA |
7611 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
7612 | cb->nlh->nlmsg_seq, | |
7613 | NFT_MSG_NEWOBJ, | |
7614 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 7615 | table->family, table, |
36596dad PNA |
7616 | obj, reset) < 0) |
7617 | goto done; | |
e5009240 | 7618 | |
36596dad | 7619 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 7620 | cont: |
36596dad | 7621 | idx++; |
e5009240 PNA |
7622 | } |
7623 | } | |
7624 | done: | |
7625 | rcu_read_unlock(); | |
7626 | ||
7627 | cb->args[0] = idx; | |
7628 | return skb->len; | |
7629 | } | |
7630 | ||
90fd131a | 7631 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 7632 | { |
90fd131a FW |
7633 | const struct nlattr * const *nla = cb->data; |
7634 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 7635 | |
90fd131a FW |
7636 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
7637 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
7638 | if (!filter) | |
7639 | return -ENOMEM; | |
7640 | ||
7641 | if (nla[NFTA_OBJ_TABLE]) { | |
7642 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
7643 | if (!filter->table) { | |
7644 | kfree(filter); | |
7645 | return -ENOMEM; | |
7646 | } | |
7647 | } | |
7648 | ||
7649 | if (nla[NFTA_OBJ_TYPE]) | |
7650 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 7651 | } |
a9fea2a3 | 7652 | |
90fd131a | 7653 | cb->data = filter; |
a9fea2a3 PNA |
7654 | return 0; |
7655 | } | |
7656 | ||
90fd131a | 7657 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 7658 | { |
90fd131a | 7659 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 7660 | |
90fd131a FW |
7661 | if (filter) { |
7662 | kfree(filter->table); | |
7663 | kfree(filter); | |
e46abbcc | 7664 | } |
a9fea2a3 | 7665 | |
90fd131a | 7666 | return 0; |
a9fea2a3 PNA |
7667 | } |
7668 | ||
d9adf22a | 7669 | /* called with rcu_read_lock held */ |
797d4980 PNA |
7670 | static int nf_tables_getobj(struct sk_buff *skb, const struct nfnl_info *info, |
7671 | const struct nlattr * const nla[]) | |
e5009240 | 7672 | { |
797d4980 PNA |
7673 | struct netlink_ext_ack *extack = info->extack; |
7674 | u8 genmask = nft_genmask_cur(info->net); | |
ef4b65e5 | 7675 | u8 family = info->nfmsg->nfgen_family; |
e5009240 | 7676 | const struct nft_table *table; |
797d4980 | 7677 | struct net *net = info->net; |
e5009240 PNA |
7678 | struct nft_object *obj; |
7679 | struct sk_buff *skb2; | |
43da04a5 | 7680 | bool reset = false; |
e5009240 PNA |
7681 | u32 objtype; |
7682 | int err; | |
7683 | ||
797d4980 | 7684 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
e5009240 | 7685 | struct netlink_dump_control c = { |
90fd131a | 7686 | .start = nf_tables_dump_obj_start, |
e5009240 | 7687 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 7688 | .done = nf_tables_dump_obj_done, |
d9adf22a | 7689 | .module = THIS_MODULE, |
90fd131a | 7690 | .data = (void *)nla, |
e5009240 | 7691 | }; |
a9fea2a3 | 7692 | |
797d4980 | 7693 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
e5009240 PNA |
7694 | } |
7695 | ||
7696 | if (!nla[NFTA_OBJ_NAME] || | |
7697 | !nla[NFTA_OBJ_TYPE]) | |
7698 | return -EINVAL; | |
7699 | ||
6001a930 | 7700 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, 0); |
36dd1bcc PNA |
7701 | if (IS_ERR(table)) { |
7702 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7703 | return PTR_ERR(table); |
36dd1bcc | 7704 | } |
e5009240 PNA |
7705 | |
7706 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 7707 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
7708 | if (IS_ERR(obj)) { |
7709 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 7710 | return PTR_ERR(obj); |
36dd1bcc | 7711 | } |
e5009240 | 7712 | |
d9adf22a | 7713 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
7714 | if (!skb2) |
7715 | return -ENOMEM; | |
7716 | ||
797d4980 | 7717 | if (NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
43da04a5 PNA |
7718 | reset = true; |
7719 | ||
8e6cf365 | 7720 | if (reset) { |
0854db2a FW |
7721 | const struct nftables_pernet *nft_net; |
7722 | char *buf; | |
7723 | ||
d59d2f82 | 7724 | nft_net = nft_pernet(net); |
0854db2a | 7725 | buf = kasprintf(GFP_ATOMIC, "%s:%u", table->name, nft_net->base_seq); |
8e6cf365 RGB |
7726 | |
7727 | audit_log_nfcfg(buf, | |
7728 | family, | |
7729 | obj->handle, | |
14224039 | 7730 | AUDIT_NFT_OP_OBJ_RESET, |
68df2ed5 | 7731 | GFP_ATOMIC); |
8e6cf365 RGB |
7732 | kfree(buf); |
7733 | } | |
7734 | ||
e5009240 | 7735 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
797d4980 | 7736 | info->nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, |
43da04a5 | 7737 | family, table, obj, reset); |
e5009240 | 7738 | if (err < 0) |
ee921183 | 7739 | goto err_fill_obj_info; |
e5009240 | 7740 | |
ee921183 PNA |
7741 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
7742 | ||
7743 | err_fill_obj_info: | |
e5009240 PNA |
7744 | kfree_skb(skb2); |
7745 | return err; | |
e5009240 PNA |
7746 | } |
7747 | ||
00bfb320 | 7748 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 7749 | { |
dfc46034 | 7750 | if (obj->ops->destroy) |
00bfb320 | 7751 | obj->ops->destroy(ctx, obj); |
e5009240 | 7752 | |
dfc46034 | 7753 | module_put(obj->ops->type->owner); |
d152159b | 7754 | kfree(obj->key.name); |
bc7a7082 | 7755 | kfree(obj->udata); |
e5009240 PNA |
7756 | kfree(obj); |
7757 | } | |
7758 | ||
7dab8ee3 PNA |
7759 | static int nf_tables_delobj(struct sk_buff *skb, const struct nfnl_info *info, |
7760 | const struct nlattr * const nla[]) | |
e5009240 | 7761 | { |
7dab8ee3 PNA |
7762 | struct netlink_ext_ack *extack = info->extack; |
7763 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 7764 | u8 family = info->nfmsg->nfgen_family; |
7dab8ee3 | 7765 | struct net *net = info->net; |
36dd1bcc | 7766 | const struct nlattr *attr; |
e5009240 PNA |
7767 | struct nft_table *table; |
7768 | struct nft_object *obj; | |
7769 | struct nft_ctx ctx; | |
7770 | u32 objtype; | |
7771 | ||
7772 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 7773 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
7774 | return -EINVAL; |
7775 | ||
6001a930 PNA |
7776 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask, |
7777 | NETLINK_CB(skb).portid); | |
36dd1bcc PNA |
7778 | if (IS_ERR(table)) { |
7779 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 7780 | return PTR_ERR(table); |
36dd1bcc | 7781 | } |
e5009240 PNA |
7782 | |
7783 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
7784 | if (nla[NFTA_OBJ_HANDLE]) { |
7785 | attr = nla[NFTA_OBJ_HANDLE]; | |
7786 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
7787 | } else { | |
7788 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 7789 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
7790 | } |
7791 | ||
7792 | if (IS_ERR(obj)) { | |
f80a612d FFM |
7793 | if (PTR_ERR(obj) == -ENOENT && |
7794 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYOBJ) | |
7795 | return 0; | |
7796 | ||
36dd1bcc | 7797 | NL_SET_BAD_ATTR(extack, attr); |
e5009240 | 7798 | return PTR_ERR(obj); |
36dd1bcc PNA |
7799 | } |
7800 | if (obj->use > 0) { | |
7801 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 7802 | return -EBUSY; |
36dd1bcc | 7803 | } |
e5009240 | 7804 | |
7dab8ee3 | 7805 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
e5009240 PNA |
7806 | |
7807 | return nft_delobj(&ctx, obj); | |
7808 | } | |
7809 | ||
d152159b | 7810 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 7811 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6fb721cf | 7812 | u16 flags, int family, int report, gfp_t gfp) |
e5009240 | 7813 | { |
d59d2f82 | 7814 | struct nftables_pernet *nft_net = nft_pernet(net); |
e5009240 PNA |
7815 | struct sk_buff *skb; |
7816 | int err; | |
c520292f | 7817 | char *buf = kasprintf(gfp, "%s:%u", |
0854db2a | 7818 | table->name, nft_net->base_seq); |
8e6cf365 RGB |
7819 | |
7820 | audit_log_nfcfg(buf, | |
7821 | family, | |
7822 | obj->handle, | |
7823 | event == NFT_MSG_NEWOBJ ? | |
c520292f RGB |
7824 | AUDIT_NFT_OP_OBJ_REGISTER : |
7825 | AUDIT_NFT_OP_OBJ_UNREGISTER, | |
68df2ed5 | 7826 | gfp); |
8e6cf365 | 7827 | kfree(buf); |
e5009240 | 7828 | |
2599e989 PNA |
7829 | if (!report && |
7830 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7831 | return; |
e5009240 | 7832 | |
2599e989 | 7833 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
7834 | if (skb == NULL) |
7835 | goto err; | |
7836 | ||
6fb721cf PNA |
7837 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, |
7838 | flags & (NLM_F_CREATE | NLM_F_EXCL), | |
7839 | family, table, obj, false); | |
e5009240 PNA |
7840 | if (err < 0) { |
7841 | kfree_skb(skb); | |
7842 | goto err; | |
7843 | } | |
7844 | ||
0854db2a | 7845 | nft_notify_enqueue(skb, report, &nft_net->notify_list); |
25e94a99 | 7846 | return; |
e5009240 | 7847 | err: |
25e94a99 | 7848 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 7849 | } |
2599e989 PNA |
7850 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
7851 | ||
25e94a99 PNA |
7852 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
7853 | struct nft_object *obj, int event) | |
2599e989 | 7854 | { |
25e94a99 | 7855 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
6fb721cf | 7856 | ctx->flags, ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 7857 | } |
e5009240 | 7858 | |
3b49e2e9 PNA |
7859 | /* |
7860 | * Flow tables | |
7861 | */ | |
7862 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
7863 | { | |
7864 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7865 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
7866 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7867 | } | |
7868 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
7869 | ||
7870 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
7871 | { | |
7872 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
7873 | list_del_rcu(&type->list); | |
7874 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
7875 | } | |
7876 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
7877 | ||
7878 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
7879 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
7880 | .len = NFT_NAME_MAXLEN - 1 }, | |
7881 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
7882 | .len = NFT_NAME_MAXLEN - 1 }, | |
7883 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 7884 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 7885 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
7886 | }; |
7887 | ||
cac20fcd PNA |
7888 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
7889 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
7890 | { |
7891 | struct nft_flowtable *flowtable; | |
7892 | ||
d9adf22a | 7893 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
7894 | if (!nla_strcmp(nla, flowtable->name) && |
7895 | nft_active_genmask(flowtable, genmask)) | |
7896 | return flowtable; | |
7897 | } | |
7898 | return ERR_PTR(-ENOENT); | |
7899 | } | |
cac20fcd | 7900 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 7901 | |
9b05b6e1 LGL |
7902 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
7903 | struct nft_flowtable *flowtable, | |
7904 | enum nft_trans_phase phase) | |
7905 | { | |
7906 | switch (phase) { | |
26b5a571 | 7907 | case NFT_TRANS_PREPARE_ERROR: |
9b05b6e1 LGL |
7908 | case NFT_TRANS_PREPARE: |
7909 | case NFT_TRANS_ABORT: | |
7910 | case NFT_TRANS_RELEASE: | |
1689f259 | 7911 | nft_use_dec(&flowtable->use); |
954d8297 | 7912 | fallthrough; |
9b05b6e1 LGL |
7913 | default: |
7914 | return; | |
7915 | } | |
7916 | } | |
7917 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
7918 | ||
ae0662f8 | 7919 | static struct nft_flowtable * |
cac20fcd PNA |
7920 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
7921 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
7922 | { |
7923 | struct nft_flowtable *flowtable; | |
7924 | ||
7925 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
7926 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
7927 | nft_active_genmask(flowtable, genmask)) | |
7928 | return flowtable; | |
7929 | } | |
7930 | return ERR_PTR(-ENOENT); | |
7931 | } | |
7932 | ||
d9246a53 PNA |
7933 | struct nft_flowtable_hook { |
7934 | u32 num; | |
7935 | int priority; | |
7936 | struct list_head list; | |
7937 | }; | |
7938 | ||
3b49e2e9 PNA |
7939 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
7940 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
7941 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
7942 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
7943 | }; | |
7944 | ||
d9246a53 | 7945 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
8509f62b | 7946 | const struct nlattr * const nla[], |
d9246a53 | 7947 | struct nft_flowtable_hook *flowtable_hook, |
c3c060ad PNA |
7948 | struct nft_flowtable *flowtable, |
7949 | struct netlink_ext_ack *extack, bool add) | |
3b49e2e9 | 7950 | { |
3b49e2e9 | 7951 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 7952 | struct nft_hook *hook; |
3b49e2e9 | 7953 | int hooknum, priority; |
3f0465a9 | 7954 | int err; |
3b49e2e9 | 7955 | |
d9246a53 PNA |
7956 | INIT_LIST_HEAD(&flowtable_hook->list); |
7957 | ||
8509f62b PNA |
7958 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, |
7959 | nla[NFTA_FLOWTABLE_HOOK], | |
8cb08174 | 7960 | nft_flowtable_hook_policy, NULL); |
3b49e2e9 PNA |
7961 | if (err < 0) |
7962 | return err; | |
7963 | ||
5b6743fb PNA |
7964 | if (add) { |
7965 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
8509f62b PNA |
7966 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { |
7967 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
7968 | return -ENOENT; | |
7969 | } | |
3b49e2e9 | 7970 | |
5b6743fb PNA |
7971 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
7972 | if (hooknum != NF_NETDEV_INGRESS) | |
7973 | return -EOPNOTSUPP; | |
7974 | ||
7975 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7976 | ||
7977 | flowtable_hook->priority = priority; | |
7978 | flowtable_hook->num = hooknum; | |
7979 | } else { | |
7980 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
7981 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
7982 | if (hooknum != flowtable->hooknum) | |
7983 | return -EOPNOTSUPP; | |
7984 | } | |
7985 | ||
7986 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
7987 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
7988 | if (priority != flowtable->data.priority) | |
7989 | return -EOPNOTSUPP; | |
7990 | } | |
3b49e2e9 | 7991 | |
5b6743fb PNA |
7992 | flowtable_hook->priority = flowtable->data.priority; |
7993 | flowtable_hook->num = flowtable->hooknum; | |
7994 | } | |
3b49e2e9 | 7995 | |
05abe445 PNA |
7996 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
7997 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
7998 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
c3c060ad PNA |
7999 | &flowtable_hook->list, |
8000 | extack); | |
05abe445 PNA |
8001 | if (err < 0) |
8002 | return err; | |
8003 | } | |
3b49e2e9 | 8004 | |
d9246a53 | 8005 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 8006 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
8007 | hook->ops.hooknum = flowtable_hook->num; |
8008 | hook->ops.priority = flowtable_hook->priority; | |
8009 | hook->ops.priv = &flowtable->data; | |
8010 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
8011 | } |
8012 | ||
3b49e2e9 PNA |
8013 | return err; |
8014 | } | |
8015 | ||
98319cb9 | 8016 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
8017 | { |
8018 | const struct nf_flowtable_type *type; | |
8019 | ||
8020 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 8021 | if (family == type->family) |
3b49e2e9 PNA |
8022 | return type; |
8023 | } | |
8024 | return NULL; | |
8025 | } | |
8026 | ||
452238e8 FW |
8027 | static const struct nf_flowtable_type * |
8028 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
8029 | { |
8030 | const struct nf_flowtable_type *type; | |
8031 | ||
98319cb9 | 8032 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
8033 | if (type != NULL && try_module_get(type->owner)) |
8034 | return type; | |
8035 | ||
f102d66b | 8036 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
8037 | #ifdef CONFIG_MODULES |
8038 | if (type == NULL) { | |
eb014de4 | 8039 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
8040 | return ERR_PTR(-EAGAIN); |
8041 | } | |
8042 | #endif | |
8043 | return ERR_PTR(-ENOENT); | |
8044 | } | |
8045 | ||
5acab914 | 8046 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
8047 | static void nft_unregister_flowtable_hook(struct net *net, |
8048 | struct nft_flowtable *flowtable, | |
8049 | struct nft_hook *hook) | |
8050 | { | |
8051 | nf_unregister_net_hook(net, &hook->ops); | |
8052 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
8053 | FLOW_BLOCK_UNBIND); | |
8054 | } | |
8055 | ||
f9a43007 PNA |
8056 | static void __nft_unregister_flowtable_net_hooks(struct net *net, |
8057 | struct list_head *hook_list, | |
8058 | bool release_netdev) | |
3b49e2e9 | 8059 | { |
f9a43007 | 8060 | struct nft_hook *hook, *next; |
3b49e2e9 | 8061 | |
f9a43007 | 8062 | list_for_each_entry_safe(hook, next, hook_list, list) { |
5acab914 | 8063 | nf_unregister_net_hook(net, &hook->ops); |
f9a43007 PNA |
8064 | if (release_netdev) { |
8065 | list_del(&hook->list); | |
ab5e5c06 | 8066 | kfree_rcu(hook, rcu); |
f9a43007 PNA |
8067 | } |
8068 | } | |
8069 | } | |
8070 | ||
8071 | static void nft_unregister_flowtable_net_hooks(struct net *net, | |
8072 | struct list_head *hook_list) | |
8073 | { | |
8074 | __nft_unregister_flowtable_net_hooks(net, hook_list, false); | |
3f0465a9 PNA |
8075 | } |
8076 | ||
8077 | static int nft_register_flowtable_net_hooks(struct net *net, | |
8078 | struct nft_table *table, | |
f9382669 | 8079 | struct list_head *hook_list, |
3f0465a9 PNA |
8080 | struct nft_flowtable *flowtable) |
8081 | { | |
8082 | struct nft_hook *hook, *hook2, *next; | |
8083 | struct nft_flowtable *ft; | |
8084 | int err, i = 0; | |
8085 | ||
f9382669 | 8086 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 | 8087 | list_for_each_entry(ft, &table->flowtables, list) { |
86fe2c19 PNA |
8088 | if (!nft_is_active_next(net, ft)) |
8089 | continue; | |
8090 | ||
3f0465a9 PNA |
8091 | list_for_each_entry(hook2, &ft->hook_list, list) { |
8092 | if (hook->ops.dev == hook2->ops.dev && | |
8093 | hook->ops.pf == hook2->ops.pf) { | |
77a92189 | 8094 | err = -EEXIST; |
3f0465a9 PNA |
8095 | goto err_unregister_net_hooks; |
8096 | } | |
8097 | } | |
8098 | } | |
3b49e2e9 | 8099 | |
d7c03a9f | 8100 | err = flowtable->data.type->setup(&flowtable->data, |
8101 | hook->ops.dev, | |
8102 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
8103 | if (err < 0) |
8104 | goto err_unregister_net_hooks; | |
8105 | ||
d7c03a9f | 8106 | err = nf_register_net_hook(net, &hook->ops); |
8107 | if (err < 0) { | |
8108 | flowtable->data.type->setup(&flowtable->data, | |
8109 | hook->ops.dev, | |
8110 | FLOW_BLOCK_UNBIND); | |
8111 | goto err_unregister_net_hooks; | |
8112 | } | |
8113 | ||
3f0465a9 | 8114 | i++; |
3b49e2e9 | 8115 | } |
3f0465a9 PNA |
8116 | |
8117 | return 0; | |
8118 | ||
8119 | err_unregister_net_hooks: | |
f9382669 | 8120 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
8121 | if (i-- <= 0) |
8122 | break; | |
8123 | ||
ff4bf2f4 | 8124 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
8125 | list_del_rcu(&hook->list); |
8126 | kfree_rcu(hook, rcu); | |
8127 | } | |
8128 | ||
8129 | return err; | |
3b49e2e9 PNA |
8130 | } |
8131 | ||
cdc32546 | 8132 | static void nft_hooks_destroy(struct list_head *hook_list) |
389a2cbc PNA |
8133 | { |
8134 | struct nft_hook *hook, *next; | |
8135 | ||
8136 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
8137 | list_del_rcu(&hook->list); | |
8138 | kfree_rcu(hook, rcu); | |
8139 | } | |
8140 | } | |
8141 | ||
78d9f48f | 8142 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
c3c060ad PNA |
8143 | struct nft_flowtable *flowtable, |
8144 | struct netlink_ext_ack *extack) | |
78d9f48f PNA |
8145 | { |
8146 | const struct nlattr * const *nla = ctx->nla; | |
8147 | struct nft_flowtable_hook flowtable_hook; | |
8148 | struct nft_hook *hook, *next; | |
8149 | struct nft_trans *trans; | |
8150 | bool unregister = false; | |
7b35582c | 8151 | u32 flags; |
78d9f48f PNA |
8152 | int err; |
8153 | ||
8509f62b PNA |
8154 | err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable, |
8155 | extack, false); | |
78d9f48f PNA |
8156 | if (err < 0) |
8157 | return err; | |
8158 | ||
8159 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
8160 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
8161 | list_del(&hook->list); | |
8162 | kfree(hook); | |
8163 | } | |
8164 | } | |
8165 | ||
7b35582c PNA |
8166 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
8167 | flags = ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
c271cc9f PNA |
8168 | if (flags & ~NFT_FLOWTABLE_MASK) { |
8169 | err = -EOPNOTSUPP; | |
8170 | goto err_flowtable_update_hook; | |
8171 | } | |
7b35582c | 8172 | if ((flowtable->data.flags & NFT_FLOWTABLE_HW_OFFLOAD) ^ |
c271cc9f PNA |
8173 | (flags & NFT_FLOWTABLE_HW_OFFLOAD)) { |
8174 | err = -EOPNOTSUPP; | |
8175 | goto err_flowtable_update_hook; | |
8176 | } | |
7b35582c PNA |
8177 | } else { |
8178 | flags = flowtable->data.flags; | |
8179 | } | |
8180 | ||
78d9f48f PNA |
8181 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, |
8182 | &flowtable_hook.list, flowtable); | |
8183 | if (err < 0) | |
8184 | goto err_flowtable_update_hook; | |
8185 | ||
8186 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
8187 | sizeof(struct nft_trans_flowtable)); | |
8188 | if (!trans) { | |
8189 | unregister = true; | |
8190 | err = -ENOMEM; | |
8191 | goto err_flowtable_update_hook; | |
8192 | } | |
8193 | ||
7b35582c | 8194 | nft_trans_flowtable_flags(trans) = flags; |
78d9f48f PNA |
8195 | nft_trans_flowtable(trans) = flowtable; |
8196 | nft_trans_flowtable_update(trans) = true; | |
8197 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
8198 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
8199 | ||
0854db2a | 8200 | nft_trans_commit_list_add_tail(ctx->net, trans); |
78d9f48f PNA |
8201 | |
8202 | return 0; | |
8203 | ||
8204 | err_flowtable_update_hook: | |
8205 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
8206 | if (unregister) | |
8207 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
8208 | list_del_rcu(&hook->list); | |
8209 | kfree_rcu(hook, rcu); | |
8210 | } | |
8211 | ||
8212 | return err; | |
8213 | ||
8214 | } | |
8215 | ||
7dab8ee3 PNA |
8216 | static int nf_tables_newflowtable(struct sk_buff *skb, |
8217 | const struct nfnl_info *info, | |
8218 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8219 | { |
7dab8ee3 | 8220 | struct netlink_ext_ack *extack = info->extack; |
d9246a53 | 8221 | struct nft_flowtable_hook flowtable_hook; |
7dab8ee3 | 8222 | u8 genmask = nft_genmask_next(info->net); |
ef4b65e5 | 8223 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8224 | const struct nf_flowtable_type *type; |
3f0465a9 PNA |
8225 | struct nft_flowtable *flowtable; |
8226 | struct nft_hook *hook, *next; | |
7dab8ee3 | 8227 | struct net *net = info->net; |
3b49e2e9 PNA |
8228 | struct nft_table *table; |
8229 | struct nft_ctx ctx; | |
3f0465a9 | 8230 | int err; |
3b49e2e9 PNA |
8231 | |
8232 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
8233 | !nla[NFTA_FLOWTABLE_NAME] || | |
8234 | !nla[NFTA_FLOWTABLE_HOOK]) | |
8235 | return -EINVAL; | |
8236 | ||
cac20fcd | 8237 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8238 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
8239 | if (IS_ERR(table)) { |
8240 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 8241 | return PTR_ERR(table); |
36dd1bcc | 8242 | } |
3b49e2e9 | 8243 | |
cac20fcd PNA |
8244 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8245 | genmask); | |
3b49e2e9 PNA |
8246 | if (IS_ERR(flowtable)) { |
8247 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
8248 | if (err != -ENOENT) { |
8249 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 8250 | return err; |
36dd1bcc | 8251 | } |
3b49e2e9 | 8252 | } else { |
7dab8ee3 | 8253 | if (info->nlh->nlmsg_flags & NLM_F_EXCL) { |
36dd1bcc | 8254 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); |
3b49e2e9 | 8255 | return -EEXIST; |
36dd1bcc | 8256 | } |
3b49e2e9 | 8257 | |
7dab8ee3 | 8258 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
78d9f48f | 8259 | |
c3c060ad | 8260 | return nft_flowtable_update(&ctx, info->nlh, flowtable, extack); |
3b49e2e9 PNA |
8261 | } |
8262 | ||
7dab8ee3 | 8263 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
3b49e2e9 | 8264 | |
1689f259 PNA |
8265 | if (!nft_use_inc(&table->use)) |
8266 | return -EMFILE; | |
8267 | ||
33758c89 | 8268 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL_ACCOUNT); |
1689f259 PNA |
8269 | if (!flowtable) { |
8270 | err = -ENOMEM; | |
8271 | goto flowtable_alloc; | |
8272 | } | |
3b49e2e9 PNA |
8273 | |
8274 | flowtable->table = table; | |
3ecbfd65 | 8275 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 8276 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 8277 | |
33758c89 | 8278 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL_ACCOUNT); |
3b49e2e9 PNA |
8279 | if (!flowtable->name) { |
8280 | err = -ENOMEM; | |
8281 | goto err1; | |
8282 | } | |
8283 | ||
452238e8 | 8284 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
8285 | if (IS_ERR(type)) { |
8286 | err = PTR_ERR(type); | |
8287 | goto err2; | |
8288 | } | |
8289 | ||
8bb69f3b PNA |
8290 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
8291 | flowtable->data.flags = | |
8292 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
7e6136f1 PNA |
8293 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) { |
8294 | err = -EOPNOTSUPP; | |
8bb69f3b | 8295 | goto err3; |
7e6136f1 | 8296 | } |
8bb69f3b PNA |
8297 | } |
8298 | ||
8299 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 8300 | flowtable->data.type = type; |
a268de77 | 8301 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
8302 | if (err < 0) |
8303 | goto err3; | |
8304 | ||
8509f62b PNA |
8305 | err = nft_flowtable_parse_hook(&ctx, nla, &flowtable_hook, flowtable, |
8306 | extack, true); | |
3b49e2e9 | 8307 | if (err < 0) |
a268de77 | 8308 | goto err4; |
3b49e2e9 | 8309 | |
d9246a53 PNA |
8310 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
8311 | flowtable->data.priority = flowtable_hook.priority; | |
8312 | flowtable->hooknum = flowtable_hook.num; | |
8313 | ||
f9382669 PNA |
8314 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
8315 | &flowtable->hook_list, | |
8316 | flowtable); | |
2d285f26 | 8317 | if (err < 0) { |
cdc32546 | 8318 | nft_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 8319 | goto err4; |
2d285f26 | 8320 | } |
3b49e2e9 PNA |
8321 | |
8322 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
8323 | if (err < 0) | |
3f0465a9 | 8324 | goto err5; |
3b49e2e9 PNA |
8325 | |
8326 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
3b49e2e9 PNA |
8327 | |
8328 | return 0; | |
a268de77 | 8329 | err5: |
3f0465a9 | 8330 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 8331 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
8332 | list_del_rcu(&hook->list); |
8333 | kfree_rcu(hook, rcu); | |
8334 | } | |
a268de77 FF |
8335 | err4: |
8336 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
8337 | err3: |
8338 | module_put(type->owner); | |
8339 | err2: | |
8340 | kfree(flowtable->name); | |
8341 | err1: | |
8342 | kfree(flowtable); | |
1689f259 PNA |
8343 | flowtable_alloc: |
8344 | nft_use_dec_restore(&table->use); | |
8345 | ||
3b49e2e9 PNA |
8346 | return err; |
8347 | } | |
8348 | ||
3003055f PNA |
8349 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
8350 | { | |
8351 | struct nft_hook *this, *next; | |
8352 | ||
8353 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
8354 | list_del(&this->list); | |
8355 | kfree(this); | |
8356 | } | |
8357 | } | |
8358 | ||
abadb2f8 | 8359 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
c3c060ad PNA |
8360 | struct nft_flowtable *flowtable, |
8361 | struct netlink_ext_ack *extack) | |
abadb2f8 PNA |
8362 | { |
8363 | const struct nlattr * const *nla = ctx->nla; | |
8364 | struct nft_flowtable_hook flowtable_hook; | |
b6d9014a | 8365 | LIST_HEAD(flowtable_del_list); |
3003055f | 8366 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
8367 | struct nft_trans *trans; |
8368 | int err; | |
8369 | ||
8509f62b PNA |
8370 | err = nft_flowtable_parse_hook(ctx, nla, &flowtable_hook, flowtable, |
8371 | extack, false); | |
abadb2f8 PNA |
8372 | if (err < 0) |
8373 | return err; | |
8374 | ||
3003055f | 8375 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
8376 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
8377 | if (!hook) { | |
8378 | err = -ENOENT; | |
8379 | goto err_flowtable_del_hook; | |
8380 | } | |
b6d9014a | 8381 | list_move(&hook->list, &flowtable_del_list); |
abadb2f8 PNA |
8382 | } |
8383 | ||
8384 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
8385 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
8386 | if (!trans) { |
8387 | err = -ENOMEM; | |
8388 | goto err_flowtable_del_hook; | |
8389 | } | |
abadb2f8 PNA |
8390 | |
8391 | nft_trans_flowtable(trans) = flowtable; | |
8392 | nft_trans_flowtable_update(trans) = true; | |
8393 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
b6d9014a | 8394 | list_splice(&flowtable_del_list, &nft_trans_flowtable_hooks(trans)); |
3003055f | 8395 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 | 8396 | |
0854db2a | 8397 | nft_trans_commit_list_add_tail(ctx->net, trans); |
abadb2f8 PNA |
8398 | |
8399 | return 0; | |
8400 | ||
8401 | err_flowtable_del_hook: | |
b6d9014a | 8402 | list_splice(&flowtable_del_list, &flowtable->hook_list); |
3003055f | 8403 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
8404 | |
8405 | return err; | |
8406 | } | |
8407 | ||
7dab8ee3 PNA |
8408 | static int nf_tables_delflowtable(struct sk_buff *skb, |
8409 | const struct nfnl_info *info, | |
8410 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8411 | { |
7dab8ee3 PNA |
8412 | struct netlink_ext_ack *extack = info->extack; |
8413 | u8 genmask = nft_genmask_next(info->net); | |
ef4b65e5 | 8414 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8415 | struct nft_flowtable *flowtable; |
7dab8ee3 | 8416 | struct net *net = info->net; |
36dd1bcc | 8417 | const struct nlattr *attr; |
3b49e2e9 PNA |
8418 | struct nft_table *table; |
8419 | struct nft_ctx ctx; | |
8420 | ||
e603ea4b PNA |
8421 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
8422 | (!nla[NFTA_FLOWTABLE_NAME] && | |
8423 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
8424 | return -EINVAL; | |
8425 | ||
cac20fcd | 8426 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8427 | genmask, NETLINK_CB(skb).portid); |
36dd1bcc PNA |
8428 | if (IS_ERR(table)) { |
8429 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 8430 | return PTR_ERR(table); |
36dd1bcc | 8431 | } |
3b49e2e9 | 8432 | |
36dd1bcc PNA |
8433 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
8434 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
8435 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
8436 | } else { | |
8437 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
8438 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
8439 | } | |
8440 | ||
8441 | if (IS_ERR(flowtable)) { | |
f80a612d FFM |
8442 | if (PTR_ERR(flowtable) == -ENOENT && |
8443 | NFNL_MSG_TYPE(info->nlh->nlmsg_type) == NFT_MSG_DESTROYFLOWTABLE) | |
8444 | return 0; | |
8445 | ||
36dd1bcc PNA |
8446 | NL_SET_BAD_ATTR(extack, attr); |
8447 | return PTR_ERR(flowtable); | |
8448 | } | |
abadb2f8 | 8449 | |
7dab8ee3 | 8450 | nft_ctx_init(&ctx, net, skb, info->nlh, family, table, NULL, nla); |
abadb2f8 PNA |
8451 | |
8452 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
c3c060ad | 8453 | return nft_delflowtable_hook(&ctx, flowtable, extack); |
abadb2f8 | 8454 | |
36dd1bcc PNA |
8455 | if (flowtable->use > 0) { |
8456 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 8457 | return -EBUSY; |
36dd1bcc | 8458 | } |
3b49e2e9 | 8459 | |
3b49e2e9 PNA |
8460 | return nft_delflowtable(&ctx, flowtable); |
8461 | } | |
8462 | ||
8463 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
8464 | u32 portid, u32 seq, int event, | |
8465 | u32 flags, int family, | |
c42d8bda PNA |
8466 | struct nft_flowtable *flowtable, |
8467 | struct list_head *hook_list) | |
3b49e2e9 PNA |
8468 | { |
8469 | struct nlattr *nest, *nest_devs; | |
3f0465a9 | 8470 | struct nft_hook *hook; |
3b49e2e9 | 8471 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
8472 | |
8473 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
19c28b13 PNA |
8474 | nlh = nfnl_msg_put(skb, portid, seq, event, flags, family, |
8475 | NFNETLINK_V0, nft_base_seq(net)); | |
8476 | if (!nlh) | |
3b49e2e9 PNA |
8477 | goto nla_put_failure; |
8478 | ||
3b49e2e9 PNA |
8479 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || |
8480 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 | 8481 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), |
28339b21 PNA |
8482 | NFTA_FLOWTABLE_PAD)) |
8483 | goto nla_put_failure; | |
8484 | ||
8485 | if (event == NFT_MSG_DELFLOWTABLE && !hook_list) { | |
8486 | nlmsg_end(skb, nlh); | |
8487 | return 0; | |
8488 | } | |
8489 | ||
8490 | if (nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || | |
8bb69f3b | 8491 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) |
3b49e2e9 PNA |
8492 | goto nla_put_failure; |
8493 | ||
ae0be8de | 8494 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
8495 | if (!nest) |
8496 | goto nla_put_failure; | |
3b49e2e9 | 8497 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 8498 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
8499 | goto nla_put_failure; |
8500 | ||
ae0be8de | 8501 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
8502 | if (!nest_devs) |
8503 | goto nla_put_failure; | |
8504 | ||
28339b21 PNA |
8505 | if (!hook_list) |
8506 | hook_list = &flowtable->hook_list; | |
8507 | ||
c42d8bda | 8508 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 8509 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
8510 | goto nla_put_failure; |
8511 | } | |
8512 | nla_nest_end(skb, nest_devs); | |
8513 | nla_nest_end(skb, nest); | |
8514 | ||
8515 | nlmsg_end(skb, nlh); | |
8516 | return 0; | |
8517 | ||
8518 | nla_put_failure: | |
8519 | nlmsg_trim(skb, nlh); | |
8520 | return -1; | |
8521 | } | |
8522 | ||
8523 | struct nft_flowtable_filter { | |
8524 | char *table; | |
8525 | }; | |
8526 | ||
8527 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
8528 | struct netlink_callback *cb) | |
8529 | { | |
8530 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
8531 | struct nft_flowtable_filter *filter = cb->data; | |
8532 | unsigned int idx = 0, s_idx = cb->args[0]; | |
8533 | struct net *net = sock_net(skb->sk); | |
8534 | int family = nfmsg->nfgen_family; | |
8535 | struct nft_flowtable *flowtable; | |
0854db2a | 8536 | struct nftables_pernet *nft_net; |
3b49e2e9 PNA |
8537 | const struct nft_table *table; |
8538 | ||
8539 | rcu_read_lock(); | |
d59d2f82 | 8540 | nft_net = nft_pernet(net); |
34002783 | 8541 | cb->seq = READ_ONCE(nft_net->base_seq); |
3b49e2e9 | 8542 | |
0854db2a | 8543 | list_for_each_entry_rcu(table, &nft_net->tables, list) { |
98319cb9 | 8544 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
8545 | continue; |
8546 | ||
36596dad PNA |
8547 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
8548 | if (!nft_is_active(net, flowtable)) | |
8549 | goto cont; | |
8550 | if (idx < s_idx) | |
8551 | goto cont; | |
8552 | if (idx > s_idx) | |
8553 | memset(&cb->args[1], 0, | |
8554 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 8555 | if (filter && filter->table && |
36596dad PNA |
8556 | strcmp(filter->table, table->name)) |
8557 | goto cont; | |
3b49e2e9 | 8558 | |
36596dad PNA |
8559 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
8560 | cb->nlh->nlmsg_seq, | |
8561 | NFT_MSG_NEWFLOWTABLE, | |
8562 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda | 8563 | table->family, |
28339b21 | 8564 | flowtable, NULL) < 0) |
36596dad | 8565 | goto done; |
3b49e2e9 | 8566 | |
36596dad | 8567 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 8568 | cont: |
36596dad | 8569 | idx++; |
3b49e2e9 PNA |
8570 | } |
8571 | } | |
8572 | done: | |
8573 | rcu_read_unlock(); | |
8574 | ||
8575 | cb->args[0] = idx; | |
8576 | return skb->len; | |
8577 | } | |
8578 | ||
90fd131a | 8579 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 8580 | { |
90fd131a FW |
8581 | const struct nlattr * const *nla = cb->data; |
8582 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 8583 | |
90fd131a FW |
8584 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
8585 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
8586 | if (!filter) | |
8587 | return -ENOMEM; | |
3b49e2e9 | 8588 | |
90fd131a FW |
8589 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
8590 | GFP_ATOMIC); | |
8591 | if (!filter->table) { | |
8592 | kfree(filter); | |
8593 | return -ENOMEM; | |
8594 | } | |
8595 | } | |
3b49e2e9 | 8596 | |
90fd131a | 8597 | cb->data = filter; |
3b49e2e9 PNA |
8598 | return 0; |
8599 | } | |
8600 | ||
90fd131a | 8601 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 8602 | { |
90fd131a | 8603 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 8604 | |
3b49e2e9 | 8605 | if (!filter) |
90fd131a | 8606 | return 0; |
3b49e2e9 | 8607 | |
90fd131a FW |
8608 | kfree(filter->table); |
8609 | kfree(filter); | |
8610 | ||
8611 | return 0; | |
3b49e2e9 PNA |
8612 | } |
8613 | ||
d9adf22a | 8614 | /* called with rcu_read_lock held */ |
797d4980 PNA |
8615 | static int nf_tables_getflowtable(struct sk_buff *skb, |
8616 | const struct nfnl_info *info, | |
8617 | const struct nlattr * const nla[]) | |
3b49e2e9 | 8618 | { |
797d4980 | 8619 | u8 genmask = nft_genmask_cur(info->net); |
ef4b65e5 | 8620 | u8 family = info->nfmsg->nfgen_family; |
3b49e2e9 | 8621 | struct nft_flowtable *flowtable; |
3b49e2e9 | 8622 | const struct nft_table *table; |
797d4980 | 8623 | struct net *net = info->net; |
3b49e2e9 PNA |
8624 | struct sk_buff *skb2; |
8625 | int err; | |
8626 | ||
797d4980 | 8627 | if (info->nlh->nlmsg_flags & NLM_F_DUMP) { |
3b49e2e9 | 8628 | struct netlink_dump_control c = { |
90fd131a | 8629 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
8630 | .dump = nf_tables_dump_flowtable, |
8631 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 8632 | .module = THIS_MODULE, |
90fd131a | 8633 | .data = (void *)nla, |
3b49e2e9 PNA |
8634 | }; |
8635 | ||
797d4980 | 8636 | return nft_netlink_dump_start_rcu(info->sk, skb, info->nlh, &c); |
3b49e2e9 PNA |
8637 | } |
8638 | ||
8639 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
8640 | return -EINVAL; | |
8641 | ||
cac20fcd | 8642 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6001a930 | 8643 | genmask, 0); |
3b49e2e9 PNA |
8644 | if (IS_ERR(table)) |
8645 | return PTR_ERR(table); | |
8646 | ||
cac20fcd PNA |
8647 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
8648 | genmask); | |
03a0120f | 8649 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
8650 | return PTR_ERR(flowtable); |
8651 | ||
d9adf22a | 8652 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
8653 | if (!skb2) |
8654 | return -ENOMEM; | |
8655 | ||
8656 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
797d4980 | 8657 | info->nlh->nlmsg_seq, |
3b49e2e9 | 8658 | NFT_MSG_NEWFLOWTABLE, 0, family, |
28339b21 | 8659 | flowtable, NULL); |
3b49e2e9 | 8660 | if (err < 0) |
ee921183 | 8661 | goto err_fill_flowtable_info; |
3b49e2e9 | 8662 | |
ee921183 PNA |
8663 | return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid); |
8664 | ||
8665 | err_fill_flowtable_info: | |
3b49e2e9 PNA |
8666 | kfree_skb(skb2); |
8667 | return err; | |
8668 | } | |
8669 | ||
8670 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
8671 | struct nft_flowtable *flowtable, | |
28339b21 | 8672 | struct list_head *hook_list, int event) |
3b49e2e9 | 8673 | { |
d59d2f82 | 8674 | struct nftables_pernet *nft_net = nft_pernet(ctx->net); |
3b49e2e9 | 8675 | struct sk_buff *skb; |
6fb721cf | 8676 | u16 flags = 0; |
3b49e2e9 PNA |
8677 | int err; |
8678 | ||
dceababa | 8679 | if (!ctx->report && |
3b49e2e9 PNA |
8680 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) |
8681 | return; | |
8682 | ||
8683 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
8684 | if (skb == NULL) | |
8685 | goto err; | |
8686 | ||
6fb721cf PNA |
8687 | if (ctx->flags & (NLM_F_CREATE | NLM_F_EXCL)) |
8688 | flags |= ctx->flags & (NLM_F_CREATE | NLM_F_EXCL); | |
8689 | ||
3b49e2e9 | 8690 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, |
6fb721cf | 8691 | ctx->seq, event, flags, |
c42d8bda | 8692 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
8693 | if (err < 0) { |
8694 | kfree_skb(skb); | |
8695 | goto err; | |
8696 | } | |
8697 | ||
0854db2a | 8698 | nft_notify_enqueue(skb, ctx->report, &nft_net->notify_list); |
3b49e2e9 PNA |
8699 | return; |
8700 | err: | |
8701 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
8702 | } | |
8703 | ||
3b49e2e9 PNA |
8704 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
8705 | { | |
3f0465a9 PNA |
8706 | struct nft_hook *hook, *next; |
8707 | ||
5acab914 | 8708 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 8709 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
8710 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
8711 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
8712 | list_del_rcu(&hook->list); |
8713 | kfree(hook); | |
8714 | } | |
3b49e2e9 | 8715 | kfree(flowtable->name); |
3b49e2e9 | 8716 | module_put(flowtable->data.type->owner); |
a12486eb | 8717 | kfree(flowtable); |
3b49e2e9 PNA |
8718 | } |
8719 | ||
84d7fce6 PNA |
8720 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
8721 | u32 portid, u32 seq) | |
8722 | { | |
d59d2f82 | 8723 | struct nftables_pernet *nft_net = nft_pernet(net); |
84d7fce6 | 8724 | struct nlmsghdr *nlh; |
784b4e61 | 8725 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 8726 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 | 8727 | |
19c28b13 PNA |
8728 | nlh = nfnl_msg_put(skb, portid, seq, event, 0, AF_UNSPEC, |
8729 | NFNETLINK_V0, nft_base_seq(net)); | |
8730 | if (!nlh) | |
84d7fce6 PNA |
8731 | goto nla_put_failure; |
8732 | ||
0854db2a | 8733 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(nft_net->base_seq)) || |
784b4e61 PS |
8734 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || |
8735 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
8736 | goto nla_put_failure; |
8737 | ||
053c095a JB |
8738 | nlmsg_end(skb, nlh); |
8739 | return 0; | |
84d7fce6 PNA |
8740 | |
8741 | nla_put_failure: | |
8742 | nlmsg_trim(skb, nlh); | |
8743 | return -EMSGSIZE; | |
8744 | } | |
8745 | ||
3b49e2e9 PNA |
8746 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
8747 | struct nft_flowtable *flowtable) | |
8748 | { | |
3f0465a9 | 8749 | struct nft_hook *hook; |
3b49e2e9 | 8750 | |
3f0465a9 PNA |
8751 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
8752 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
8753 | continue; |
8754 | ||
5acab914 | 8755 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 8756 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
8757 | list_del_rcu(&hook->list); |
8758 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
8759 | break; |
8760 | } | |
8761 | } | |
8762 | ||
8763 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
8764 | unsigned long event, void *ptr) | |
8765 | { | |
8766 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
8767 | struct nft_flowtable *flowtable; | |
0854db2a | 8768 | struct nftables_pernet *nft_net; |
3b49e2e9 | 8769 | struct nft_table *table; |
0a2cf5ee | 8770 | struct net *net; |
3b49e2e9 PNA |
8771 | |
8772 | if (event != NETDEV_UNREGISTER) | |
8773 | return 0; | |
8774 | ||
6a48de01 | 8775 | net = dev_net(dev); |
d59d2f82 | 8776 | nft_net = nft_pernet(net); |
0854db2a FW |
8777 | mutex_lock(&nft_net->commit_mutex); |
8778 | list_for_each_entry(table, &nft_net->tables, list) { | |
36596dad PNA |
8779 | list_for_each_entry(flowtable, &table->flowtables, list) { |
8780 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
8781 | } |
8782 | } | |
0854db2a | 8783 | mutex_unlock(&nft_net->commit_mutex); |
6a48de01 | 8784 | |
3b49e2e9 PNA |
8785 | return NOTIFY_DONE; |
8786 | } | |
8787 | ||
8788 | static struct notifier_block nf_tables_flowtable_notifier = { | |
8789 | .notifier_call = nf_tables_flowtable_event, | |
8790 | }; | |
8791 | ||
25e94a99 PNA |
8792 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
8793 | int event) | |
84d7fce6 PNA |
8794 | { |
8795 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
8796 | struct sk_buff *skb2; | |
8797 | int err; | |
8798 | ||
dceababa | 8799 | if (!nlmsg_report(nlh) && |
84d7fce6 | 8800 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 8801 | return; |
84d7fce6 | 8802 | |
84d7fce6 PNA |
8803 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
8804 | if (skb2 == NULL) | |
8805 | goto err; | |
8806 | ||
8807 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
8808 | nlh->nlmsg_seq); | |
8809 | if (err < 0) { | |
8810 | kfree_skb(skb2); | |
8811 | goto err; | |
8812 | } | |
8813 | ||
25e94a99 PNA |
8814 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8815 | nlmsg_report(nlh), GFP_KERNEL); | |
8816 | return; | |
84d7fce6 | 8817 | err: |
25e94a99 PNA |
8818 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
8819 | -ENOBUFS); | |
84d7fce6 PNA |
8820 | } |
8821 | ||
797d4980 PNA |
8822 | static int nf_tables_getgen(struct sk_buff *skb, const struct nfnl_info *info, |
8823 | const struct nlattr * const nla[]) | |
84d7fce6 | 8824 | { |
84d7fce6 PNA |
8825 | struct sk_buff *skb2; |
8826 | int err; | |
8827 | ||
d9adf22a | 8828 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
8829 | if (skb2 == NULL) |
8830 | return -ENOMEM; | |
8831 | ||
797d4980 PNA |
8832 | err = nf_tables_fill_gen_info(skb2, info->net, NETLINK_CB(skb).portid, |
8833 | info->nlh->nlmsg_seq); | |
84d7fce6 | 8834 | if (err < 0) |
ee921183 | 8835 | goto err_fill_gen_info; |
84d7fce6 | 8836 | |
797d4980 | 8837 | return nfnetlink_unicast(skb2, info->net, NETLINK_CB(skb).portid); |
ee921183 PNA |
8838 | |
8839 | err_fill_gen_info: | |
84d7fce6 PNA |
8840 | kfree_skb(skb2); |
8841 | return err; | |
8842 | } | |
8843 | ||
96518518 PM |
8844 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
8845 | [NFT_MSG_NEWTABLE] = { | |
50f2db9e PNA |
8846 | .call = nf_tables_newtable, |
8847 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8848 | .attr_count = NFTA_TABLE_MAX, |
8849 | .policy = nft_table_policy, | |
8850 | }, | |
8851 | [NFT_MSG_GETTABLE] = { | |
50f2db9e PNA |
8852 | .call = nf_tables_gettable, |
8853 | .type = NFNL_CB_RCU, | |
96518518 PM |
8854 | .attr_count = NFTA_TABLE_MAX, |
8855 | .policy = nft_table_policy, | |
8856 | }, | |
8857 | [NFT_MSG_DELTABLE] = { | |
50f2db9e PNA |
8858 | .call = nf_tables_deltable, |
8859 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8860 | .attr_count = NFTA_TABLE_MAX, |
8861 | .policy = nft_table_policy, | |
8862 | }, | |
f80a612d FFM |
8863 | [NFT_MSG_DESTROYTABLE] = { |
8864 | .call = nf_tables_deltable, | |
8865 | .type = NFNL_CB_BATCH, | |
8866 | .attr_count = NFTA_TABLE_MAX, | |
8867 | .policy = nft_table_policy, | |
8868 | }, | |
96518518 | 8869 | [NFT_MSG_NEWCHAIN] = { |
50f2db9e PNA |
8870 | .call = nf_tables_newchain, |
8871 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8872 | .attr_count = NFTA_CHAIN_MAX, |
8873 | .policy = nft_chain_policy, | |
8874 | }, | |
8875 | [NFT_MSG_GETCHAIN] = { | |
50f2db9e PNA |
8876 | .call = nf_tables_getchain, |
8877 | .type = NFNL_CB_RCU, | |
96518518 PM |
8878 | .attr_count = NFTA_CHAIN_MAX, |
8879 | .policy = nft_chain_policy, | |
8880 | }, | |
8881 | [NFT_MSG_DELCHAIN] = { | |
50f2db9e PNA |
8882 | .call = nf_tables_delchain, |
8883 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8884 | .attr_count = NFTA_CHAIN_MAX, |
8885 | .policy = nft_chain_policy, | |
8886 | }, | |
f80a612d FFM |
8887 | [NFT_MSG_DESTROYCHAIN] = { |
8888 | .call = nf_tables_delchain, | |
8889 | .type = NFNL_CB_BATCH, | |
8890 | .attr_count = NFTA_CHAIN_MAX, | |
8891 | .policy = nft_chain_policy, | |
8892 | }, | |
96518518 | 8893 | [NFT_MSG_NEWRULE] = { |
50f2db9e PNA |
8894 | .call = nf_tables_newrule, |
8895 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8896 | .attr_count = NFTA_RULE_MAX, |
8897 | .policy = nft_rule_policy, | |
8898 | }, | |
8899 | [NFT_MSG_GETRULE] = { | |
50f2db9e PNA |
8900 | .call = nf_tables_getrule, |
8901 | .type = NFNL_CB_RCU, | |
96518518 PM |
8902 | .attr_count = NFTA_RULE_MAX, |
8903 | .policy = nft_rule_policy, | |
8904 | }, | |
8daa8fde PS |
8905 | [NFT_MSG_GETRULE_RESET] = { |
8906 | .call = nf_tables_getrule, | |
8907 | .type = NFNL_CB_RCU, | |
8908 | .attr_count = NFTA_RULE_MAX, | |
8909 | .policy = nft_rule_policy, | |
8910 | }, | |
96518518 | 8911 | [NFT_MSG_DELRULE] = { |
50f2db9e PNA |
8912 | .call = nf_tables_delrule, |
8913 | .type = NFNL_CB_BATCH, | |
96518518 PM |
8914 | .attr_count = NFTA_RULE_MAX, |
8915 | .policy = nft_rule_policy, | |
8916 | }, | |
f80a612d FFM |
8917 | [NFT_MSG_DESTROYRULE] = { |
8918 | .call = nf_tables_delrule, | |
8919 | .type = NFNL_CB_BATCH, | |
8920 | .attr_count = NFTA_RULE_MAX, | |
8921 | .policy = nft_rule_policy, | |
8922 | }, | |
20a69341 | 8923 | [NFT_MSG_NEWSET] = { |
50f2db9e PNA |
8924 | .call = nf_tables_newset, |
8925 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8926 | .attr_count = NFTA_SET_MAX, |
8927 | .policy = nft_set_policy, | |
8928 | }, | |
8929 | [NFT_MSG_GETSET] = { | |
50f2db9e PNA |
8930 | .call = nf_tables_getset, |
8931 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8932 | .attr_count = NFTA_SET_MAX, |
8933 | .policy = nft_set_policy, | |
8934 | }, | |
8935 | [NFT_MSG_DELSET] = { | |
50f2db9e PNA |
8936 | .call = nf_tables_delset, |
8937 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8938 | .attr_count = NFTA_SET_MAX, |
8939 | .policy = nft_set_policy, | |
8940 | }, | |
f80a612d FFM |
8941 | [NFT_MSG_DESTROYSET] = { |
8942 | .call = nf_tables_delset, | |
8943 | .type = NFNL_CB_BATCH, | |
8944 | .attr_count = NFTA_SET_MAX, | |
8945 | .policy = nft_set_policy, | |
8946 | }, | |
20a69341 | 8947 | [NFT_MSG_NEWSETELEM] = { |
50f2db9e PNA |
8948 | .call = nf_tables_newsetelem, |
8949 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8950 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8951 | .policy = nft_set_elem_list_policy, | |
8952 | }, | |
8953 | [NFT_MSG_GETSETELEM] = { | |
50f2db9e PNA |
8954 | .call = nf_tables_getsetelem, |
8955 | .type = NFNL_CB_RCU, | |
20a69341 PM |
8956 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8957 | .policy = nft_set_elem_list_policy, | |
8958 | }, | |
079cd633 PS |
8959 | [NFT_MSG_GETSETELEM_RESET] = { |
8960 | .call = nf_tables_getsetelem, | |
8961 | .type = NFNL_CB_RCU, | |
8962 | .attr_count = NFTA_SET_ELEM_LIST_MAX, | |
8963 | .policy = nft_set_elem_list_policy, | |
8964 | }, | |
20a69341 | 8965 | [NFT_MSG_DELSETELEM] = { |
50f2db9e PNA |
8966 | .call = nf_tables_delsetelem, |
8967 | .type = NFNL_CB_BATCH, | |
20a69341 PM |
8968 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
8969 | .policy = nft_set_elem_list_policy, | |
8970 | }, | |
f80a612d FFM |
8971 | [NFT_MSG_DESTROYSETELEM] = { |
8972 | .call = nf_tables_delsetelem, | |
8973 | .type = NFNL_CB_BATCH, | |
8974 | .attr_count = NFTA_SET_ELEM_LIST_MAX, | |
8975 | .policy = nft_set_elem_list_policy, | |
8976 | }, | |
84d7fce6 | 8977 | [NFT_MSG_GETGEN] = { |
50f2db9e PNA |
8978 | .call = nf_tables_getgen, |
8979 | .type = NFNL_CB_RCU, | |
84d7fce6 | 8980 | }, |
e5009240 | 8981 | [NFT_MSG_NEWOBJ] = { |
50f2db9e PNA |
8982 | .call = nf_tables_newobj, |
8983 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8984 | .attr_count = NFTA_OBJ_MAX, |
8985 | .policy = nft_obj_policy, | |
8986 | }, | |
8987 | [NFT_MSG_GETOBJ] = { | |
50f2db9e PNA |
8988 | .call = nf_tables_getobj, |
8989 | .type = NFNL_CB_RCU, | |
e5009240 PNA |
8990 | .attr_count = NFTA_OBJ_MAX, |
8991 | .policy = nft_obj_policy, | |
8992 | }, | |
8993 | [NFT_MSG_DELOBJ] = { | |
50f2db9e PNA |
8994 | .call = nf_tables_delobj, |
8995 | .type = NFNL_CB_BATCH, | |
e5009240 PNA |
8996 | .attr_count = NFTA_OBJ_MAX, |
8997 | .policy = nft_obj_policy, | |
8998 | }, | |
f80a612d FFM |
8999 | [NFT_MSG_DESTROYOBJ] = { |
9000 | .call = nf_tables_delobj, | |
9001 | .type = NFNL_CB_BATCH, | |
9002 | .attr_count = NFTA_OBJ_MAX, | |
9003 | .policy = nft_obj_policy, | |
9004 | }, | |
43da04a5 | 9005 | [NFT_MSG_GETOBJ_RESET] = { |
50f2db9e PNA |
9006 | .call = nf_tables_getobj, |
9007 | .type = NFNL_CB_RCU, | |
43da04a5 PNA |
9008 | .attr_count = NFTA_OBJ_MAX, |
9009 | .policy = nft_obj_policy, | |
9010 | }, | |
3b49e2e9 | 9011 | [NFT_MSG_NEWFLOWTABLE] = { |
50f2db9e PNA |
9012 | .call = nf_tables_newflowtable, |
9013 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
9014 | .attr_count = NFTA_FLOWTABLE_MAX, |
9015 | .policy = nft_flowtable_policy, | |
9016 | }, | |
9017 | [NFT_MSG_GETFLOWTABLE] = { | |
50f2db9e PNA |
9018 | .call = nf_tables_getflowtable, |
9019 | .type = NFNL_CB_RCU, | |
3b49e2e9 PNA |
9020 | .attr_count = NFTA_FLOWTABLE_MAX, |
9021 | .policy = nft_flowtable_policy, | |
9022 | }, | |
9023 | [NFT_MSG_DELFLOWTABLE] = { | |
50f2db9e PNA |
9024 | .call = nf_tables_delflowtable, |
9025 | .type = NFNL_CB_BATCH, | |
3b49e2e9 PNA |
9026 | .attr_count = NFTA_FLOWTABLE_MAX, |
9027 | .policy = nft_flowtable_policy, | |
9028 | }, | |
f80a612d FFM |
9029 | [NFT_MSG_DESTROYFLOWTABLE] = { |
9030 | .call = nf_tables_delflowtable, | |
9031 | .type = NFNL_CB_BATCH, | |
9032 | .attr_count = NFTA_FLOWTABLE_MAX, | |
9033 | .policy = nft_flowtable_policy, | |
9034 | }, | |
96518518 PM |
9035 | }; |
9036 | ||
a654de8f PNA |
9037 | static int nf_tables_validate(struct net *net) |
9038 | { | |
d59d2f82 | 9039 | struct nftables_pernet *nft_net = nft_pernet(net); |
a654de8f PNA |
9040 | struct nft_table *table; |
9041 | ||
00c320f9 FW |
9042 | list_for_each_entry(table, &nft_net->tables, list) { |
9043 | switch (table->validate_state) { | |
9044 | case NFT_VALIDATE_SKIP: | |
9045 | continue; | |
9046 | case NFT_VALIDATE_NEED: | |
9047 | nft_validate_state_update(table, NFT_VALIDATE_DO); | |
9048 | fallthrough; | |
9049 | case NFT_VALIDATE_DO: | |
a654de8f PNA |
9050 | if (nft_table_validate(net, table) < 0) |
9051 | return -EAGAIN; | |
00c320f9 FW |
9052 | |
9053 | nft_validate_state_update(table, NFT_VALIDATE_SKIP); | |
a654de8f | 9054 | } |
9a32e985 | 9055 | |
a654de8f PNA |
9056 | break; |
9057 | } | |
9058 | ||
9059 | return 0; | |
9060 | } | |
9061 | ||
66293c46 FW |
9062 | /* a drop policy has to be deferred until all rules have been activated, |
9063 | * otherwise a large ruleset that contains a drop-policy base chain will | |
9064 | * cause all packets to get dropped until the full transaction has been | |
9065 | * processed. | |
9066 | * | |
9067 | * We defer the drop policy until the transaction has been finalized. | |
9068 | */ | |
9069 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
9070 | { | |
9071 | struct nft_base_chain *basechain; | |
9072 | ||
9073 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
9074 | return; | |
9075 | ||
9076 | if (!nft_is_base_chain(trans->ctx.chain)) | |
9077 | return; | |
9078 | ||
9079 | basechain = nft_base_chain(trans->ctx.chain); | |
9080 | basechain->policy = NF_DROP; | |
9081 | } | |
9082 | ||
91c7b38d PNA |
9083 | static void nft_chain_commit_update(struct nft_trans *trans) |
9084 | { | |
9085 | struct nft_base_chain *basechain; | |
9086 | ||
1b2470e5 FW |
9087 | if (nft_trans_chain_name(trans)) { |
9088 | rhltable_remove(&trans->ctx.table->chains_ht, | |
9089 | &trans->ctx.chain->rhlhead, | |
9090 | nft_chain_ht_params); | |
d71efb59 | 9091 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
9092 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
9093 | trans->ctx.chain->name, | |
9094 | &trans->ctx.chain->rhlhead, | |
9095 | nft_chain_ht_params); | |
9096 | } | |
91c7b38d | 9097 | |
f323d954 | 9098 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
9099 | return; |
9100 | ||
53315ac6 FW |
9101 | nft_chain_stats_replace(trans); |
9102 | ||
91c7b38d | 9103 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
9104 | |
9105 | switch (nft_trans_chain_policy(trans)) { | |
9106 | case NF_DROP: | |
9107 | case NF_ACCEPT: | |
9108 | basechain->policy = nft_trans_chain_policy(trans); | |
9109 | break; | |
9110 | } | |
9111 | } | |
9112 | ||
d62d0ba9 FFM |
9113 | static void nft_obj_commit_update(struct nft_trans *trans) |
9114 | { | |
9115 | struct nft_object *newobj; | |
9116 | struct nft_object *obj; | |
9117 | ||
9118 | obj = nft_trans_obj(trans); | |
9119 | newobj = nft_trans_obj_newobj(trans); | |
9120 | ||
9fedd894 FFM |
9121 | if (obj->ops->update) |
9122 | obj->ops->update(obj, newobj); | |
d62d0ba9 | 9123 | |
dad3bdee | 9124 | nft_obj_destroy(&trans->ctx, newobj); |
d62d0ba9 FFM |
9125 | } |
9126 | ||
2f99aa31 | 9127 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 9128 | { |
c7c32e72 PNA |
9129 | switch (trans->msg_type) { |
9130 | case NFT_MSG_DELTABLE: | |
f80a612d | 9131 | case NFT_MSG_DESTROYTABLE: |
c7c32e72 PNA |
9132 | nf_tables_table_destroy(&trans->ctx); |
9133 | break; | |
9f8aac0b | 9134 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 9135 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
9136 | kfree(nft_trans_chain_name(trans)); |
9137 | break; | |
c7c32e72 | 9138 | case NFT_MSG_DELCHAIN: |
f80a612d | 9139 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
9140 | if (nft_trans_chain_update(trans)) |
9141 | nft_hooks_destroy(&nft_trans_chain_hooks(trans)); | |
9142 | else | |
9143 | nf_tables_chain_destroy(&trans->ctx); | |
c7c32e72 PNA |
9144 | break; |
9145 | case NFT_MSG_DELRULE: | |
f80a612d | 9146 | case NFT_MSG_DESTROYRULE: |
c7c32e72 PNA |
9147 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); |
9148 | break; | |
9149 | case NFT_MSG_DELSET: | |
f80a612d | 9150 | case NFT_MSG_DESTROYSET: |
0c2a85ed | 9151 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 9152 | break; |
61edafbb | 9153 | case NFT_MSG_DELSETELEM: |
f80a612d | 9154 | case NFT_MSG_DESTROYSETELEM: |
3453c927 PNA |
9155 | nf_tables_set_elem_destroy(&trans->ctx, |
9156 | nft_trans_elem_set(trans), | |
59105446 | 9157 | nft_trans_elem(trans).priv); |
61edafbb | 9158 | break; |
e5009240 | 9159 | case NFT_MSG_DELOBJ: |
f80a612d | 9160 | case NFT_MSG_DESTROYOBJ: |
00bfb320 | 9161 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 9162 | break; |
3b49e2e9 | 9163 | case NFT_MSG_DELFLOWTABLE: |
f80a612d | 9164 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 9165 | if (nft_trans_flowtable_update(trans)) |
cdc32546 | 9166 | nft_hooks_destroy(&nft_trans_flowtable_hooks(trans)); |
abadb2f8 PNA |
9167 | else |
9168 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 9169 | break; |
c7c32e72 | 9170 | } |
0935d558 FW |
9171 | |
9172 | if (trans->put_net) | |
9173 | put_net(trans->ctx.net); | |
9174 | ||
c7c32e72 PNA |
9175 | kfree(trans); |
9176 | } | |
9177 | ||
0935d558 | 9178 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
9179 | { |
9180 | struct nft_trans *trans, *next; | |
0935d558 FW |
9181 | LIST_HEAD(head); |
9182 | ||
9183 | spin_lock(&nf_tables_destroy_list_lock); | |
9184 | list_splice_init(&nf_tables_destroy_list, &head); | |
9185 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 9186 | |
0935d558 | 9187 | if (list_empty(&head)) |
2f99aa31 FW |
9188 | return; |
9189 | ||
9190 | synchronize_rcu(); | |
9191 | ||
0935d558 | 9192 | list_for_each_entry_safe(trans, next, &head, list) { |
938154b9 | 9193 | nft_trans_list_del(trans); |
2f99aa31 FW |
9194 | nft_commit_release(trans); |
9195 | } | |
9196 | } | |
9197 | ||
ffe8923f FW |
9198 | void nf_tables_trans_destroy_flush_work(void) |
9199 | { | |
9200 | flush_work(&trans_destroy_work); | |
9201 | } | |
9202 | EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work); | |
9203 | ||
ed5f85d4 PNA |
9204 | static bool nft_expr_reduce(struct nft_regs_track *track, |
9205 | const struct nft_expr *expr) | |
9206 | { | |
9e539c5b | 9207 | return false; |
ed5f85d4 PNA |
9208 | } |
9209 | ||
0cbc06b3 FW |
9210 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
9211 | { | |
2c865a8a | 9212 | const struct nft_expr *expr, *last; |
12e4ecfa | 9213 | struct nft_regs_track track = {}; |
2c865a8a PNA |
9214 | unsigned int size, data_size; |
9215 | void *data, *data_boundary; | |
9216 | struct nft_rule_dp *prule; | |
0cbc06b3 | 9217 | struct nft_rule *rule; |
0cbc06b3 FW |
9218 | |
9219 | /* already handled or inactive chain? */ | |
2c865a8a | 9220 | if (chain->blob_next || !nft_is_active_next(net, chain)) |
0cbc06b3 FW |
9221 | return 0; |
9222 | ||
63045bfd | 9223 | data_size = 0; |
10377d42 | 9224 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
9225 | if (nft_is_active_next(net, rule)) { |
9226 | data_size += sizeof(*prule) + rule->dlen; | |
9227 | if (data_size > INT_MAX) | |
9228 | return -ENOMEM; | |
9229 | } | |
0cbc06b3 FW |
9230 | } |
9231 | ||
e38fbfa9 | 9232 | chain->blob_next = nf_tables_chain_alloc_rules(chain, data_size); |
2c865a8a | 9233 | if (!chain->blob_next) |
0cbc06b3 FW |
9234 | return -ENOMEM; |
9235 | ||
2c865a8a PNA |
9236 | data = (void *)chain->blob_next->data; |
9237 | data_boundary = data + data_size; | |
9238 | size = 0; | |
9239 | ||
10377d42 | 9240 | list_for_each_entry(rule, &chain->rules, list) { |
2c865a8a PNA |
9241 | if (!nft_is_active_next(net, rule)) |
9242 | continue; | |
9243 | ||
9244 | prule = (struct nft_rule_dp *)data; | |
9245 | data += offsetof(struct nft_rule_dp, data); | |
9246 | if (WARN_ON_ONCE(data > data_boundary)) | |
9247 | return -ENOMEM; | |
9248 | ||
12e4ecfa | 9249 | size = 0; |
fe75e84a | 9250 | track.last = nft_expr_last(rule); |
2c865a8a | 9251 | nft_rule_for_each_expr(expr, last, rule) { |
12e4ecfa PNA |
9252 | track.cur = expr; |
9253 | ||
ed5f85d4 | 9254 | if (nft_expr_reduce(&track, expr)) { |
12e4ecfa PNA |
9255 | expr = track.cur; |
9256 | continue; | |
9257 | } | |
9258 | ||
08e42a0d | 9259 | if (WARN_ON_ONCE(data + size + expr->ops->size > data_boundary)) |
2c865a8a PNA |
9260 | return -ENOMEM; |
9261 | ||
9262 | memcpy(data + size, expr, expr->ops->size); | |
9263 | size += expr->ops->size; | |
9264 | } | |
9265 | if (WARN_ON_ONCE(size >= 1 << 12)) | |
9266 | return -ENOMEM; | |
9267 | ||
9268 | prule->handle = rule->handle; | |
9269 | prule->dlen = size; | |
9270 | prule->is_last = 0; | |
9271 | ||
9272 | data += size; | |
9273 | size = 0; | |
9274 | chain->blob_next->size += (unsigned long)(data - (void *)prule); | |
0cbc06b3 FW |
9275 | } |
9276 | ||
2c865a8a PNA |
9277 | if (WARN_ON_ONCE(data > data_boundary)) |
9278 | return -ENOMEM; | |
9279 | ||
e38fbfa9 FW |
9280 | prule = (struct nft_rule_dp *)data; |
9281 | nft_last_rule(chain, prule); | |
2c865a8a | 9282 | |
0cbc06b3 FW |
9283 | return 0; |
9284 | } | |
9285 | ||
9286 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
9287 | { | |
d59d2f82 | 9288 | struct nftables_pernet *nft_net = nft_pernet(net); |
0cbc06b3 FW |
9289 | struct nft_trans *trans, *next; |
9290 | ||
0854db2a | 9291 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 FW |
9292 | struct nft_chain *chain = trans->ctx.chain; |
9293 | ||
9294 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
9295 | trans->msg_type == NFT_MSG_DELRULE) { | |
2c865a8a PNA |
9296 | kvfree(chain->blob_next); |
9297 | chain->blob_next = NULL; | |
0cbc06b3 FW |
9298 | } |
9299 | } | |
9300 | } | |
9301 | ||
e38fbfa9 | 9302 | static void __nf_tables_commit_chain_free_rules(struct rcu_head *h) |
0cbc06b3 | 9303 | { |
e38fbfa9 | 9304 | struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h); |
0cbc06b3 | 9305 | |
e38fbfa9 | 9306 | kvfree(l->blob); |
0cbc06b3 FW |
9307 | } |
9308 | ||
2c865a8a | 9309 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule_blob *blob) |
0cbc06b3 | 9310 | { |
e38fbfa9 | 9311 | struct nft_rule_dp_last *last; |
0cbc06b3 | 9312 | |
e38fbfa9 FW |
9313 | /* last rule trailer is after end marker */ |
9314 | last = (void *)blob + sizeof(*blob) + blob->size; | |
9315 | last->blob = blob; | |
0cbc06b3 | 9316 | |
e38fbfa9 | 9317 | call_rcu(&last->h, __nf_tables_commit_chain_free_rules); |
0cbc06b3 FW |
9318 | } |
9319 | ||
0fb39bbe | 9320 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 | 9321 | { |
2c865a8a | 9322 | struct nft_rule_blob *g0, *g1; |
0cbc06b3 FW |
9323 | bool next_genbit; |
9324 | ||
9325 | next_genbit = nft_gencursor_next(net); | |
9326 | ||
2c865a8a | 9327 | g0 = rcu_dereference_protected(chain->blob_gen_0, |
f102d66b | 9328 | lockdep_commit_lock_is_held(net)); |
2c865a8a | 9329 | g1 = rcu_dereference_protected(chain->blob_gen_1, |
f102d66b | 9330 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
9331 | |
9332 | /* No changes to this chain? */ | |
2c865a8a | 9333 | if (chain->blob_next == NULL) { |
0cbc06b3 FW |
9334 | /* chain had no change in last or next generation */ |
9335 | if (g0 == g1) | |
9336 | return; | |
9337 | /* | |
9338 | * chain had no change in this generation; make sure next | |
9339 | * one uses same rules as current generation. | |
9340 | */ | |
9341 | if (next_genbit) { | |
2c865a8a | 9342 | rcu_assign_pointer(chain->blob_gen_1, g0); |
0cbc06b3 FW |
9343 | nf_tables_commit_chain_free_rules_old(g1); |
9344 | } else { | |
2c865a8a | 9345 | rcu_assign_pointer(chain->blob_gen_0, g1); |
0cbc06b3 FW |
9346 | nf_tables_commit_chain_free_rules_old(g0); |
9347 | } | |
9348 | ||
9349 | return; | |
9350 | } | |
9351 | ||
9352 | if (next_genbit) | |
2c865a8a | 9353 | rcu_assign_pointer(chain->blob_gen_1, chain->blob_next); |
0cbc06b3 | 9354 | else |
2c865a8a | 9355 | rcu_assign_pointer(chain->blob_gen_0, chain->blob_next); |
0cbc06b3 | 9356 | |
2c865a8a | 9357 | chain->blob_next = NULL; |
0cbc06b3 FW |
9358 | |
9359 | if (g0 == g1) | |
9360 | return; | |
9361 | ||
9362 | if (next_genbit) | |
9363 | nf_tables_commit_chain_free_rules_old(g1); | |
9364 | else | |
9365 | nf_tables_commit_chain_free_rules_old(g0); | |
9366 | } | |
9367 | ||
d152159b FW |
9368 | static void nft_obj_del(struct nft_object *obj) |
9369 | { | |
4d44175a | 9370 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
9371 | list_del_rcu(&obj->list); |
9372 | } | |
9373 | ||
d0e2c7de | 9374 | void nft_chain_del(struct nft_chain *chain) |
1b2470e5 FW |
9375 | { |
9376 | struct nft_table *table = chain->table; | |
9377 | ||
9378 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
9379 | nft_chain_ht_params)); | |
9380 | list_del_rcu(&chain->list); | |
9381 | } | |
9382 | ||
5f68718b PNA |
9383 | static void nft_trans_gc_setelem_remove(struct nft_ctx *ctx, |
9384 | struct nft_trans_gc *trans) | |
9385 | { | |
9386 | void **priv = trans->priv; | |
9387 | unsigned int i; | |
9388 | ||
9389 | for (i = 0; i < trans->count; i++) { | |
9390 | struct nft_set_elem elem = { | |
9391 | .priv = priv[i], | |
9392 | }; | |
9393 | ||
9394 | nft_setelem_data_deactivate(ctx->net, trans->set, &elem); | |
9395 | nft_setelem_remove(ctx->net, trans->set, &elem); | |
9396 | } | |
9397 | } | |
9398 | ||
9399 | void nft_trans_gc_destroy(struct nft_trans_gc *trans) | |
9400 | { | |
9401 | nft_set_put(trans->set); | |
9402 | put_net(trans->net); | |
9403 | kfree(trans); | |
9404 | } | |
9405 | ||
9406 | static void nft_trans_gc_trans_free(struct rcu_head *rcu) | |
9407 | { | |
9408 | struct nft_set_elem elem = {}; | |
9409 | struct nft_trans_gc *trans; | |
9410 | struct nft_ctx ctx = {}; | |
9411 | unsigned int i; | |
9412 | ||
9413 | trans = container_of(rcu, struct nft_trans_gc, rcu); | |
9414 | ctx.net = read_pnet(&trans->set->net); | |
9415 | ||
9416 | for (i = 0; i < trans->count; i++) { | |
9417 | elem.priv = trans->priv[i]; | |
9418 | if (!nft_setelem_is_catchall(trans->set, &elem)) | |
9419 | atomic_dec(&trans->set->nelems); | |
9420 | ||
9421 | nf_tables_set_elem_destroy(&ctx, trans->set, elem.priv); | |
9422 | } | |
9423 | ||
9424 | nft_trans_gc_destroy(trans); | |
9425 | } | |
9426 | ||
9427 | static bool nft_trans_gc_work_done(struct nft_trans_gc *trans) | |
9428 | { | |
9429 | struct nftables_pernet *nft_net; | |
9430 | struct nft_ctx ctx = {}; | |
9431 | ||
9432 | nft_net = nft_pernet(trans->net); | |
9433 | ||
9434 | mutex_lock(&nft_net->commit_mutex); | |
9435 | ||
9436 | /* Check for race with transaction, otherwise this batch refers to | |
9437 | * stale objects that might not be there anymore. Skip transaction if | |
9438 | * set has been destroyed from control plane transaction in case gc | |
9439 | * worker loses race. | |
9440 | */ | |
9441 | if (READ_ONCE(nft_net->gc_seq) != trans->seq || trans->set->dead) { | |
9442 | mutex_unlock(&nft_net->commit_mutex); | |
9443 | return false; | |
9444 | } | |
9445 | ||
9446 | ctx.net = trans->net; | |
9447 | ctx.table = trans->set->table; | |
9448 | ||
9449 | nft_trans_gc_setelem_remove(&ctx, trans); | |
9450 | mutex_unlock(&nft_net->commit_mutex); | |
9451 | ||
9452 | return true; | |
9453 | } | |
9454 | ||
9455 | static void nft_trans_gc_work(struct work_struct *work) | |
9456 | { | |
9457 | struct nft_trans_gc *trans, *next; | |
9458 | LIST_HEAD(trans_gc_list); | |
9459 | ||
9460 | spin_lock(&nf_tables_destroy_list_lock); | |
9461 | list_splice_init(&nf_tables_gc_list, &trans_gc_list); | |
9462 | spin_unlock(&nf_tables_destroy_list_lock); | |
9463 | ||
9464 | list_for_each_entry_safe(trans, next, &trans_gc_list, list) { | |
9465 | list_del(&trans->list); | |
9466 | if (!nft_trans_gc_work_done(trans)) { | |
9467 | nft_trans_gc_destroy(trans); | |
9468 | continue; | |
9469 | } | |
9470 | call_rcu(&trans->rcu, nft_trans_gc_trans_free); | |
9471 | } | |
9472 | } | |
9473 | ||
9474 | struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set, | |
9475 | unsigned int gc_seq, gfp_t gfp) | |
9476 | { | |
9477 | struct net *net = read_pnet(&set->net); | |
9478 | struct nft_trans_gc *trans; | |
9479 | ||
9480 | trans = kzalloc(sizeof(*trans), gfp); | |
9481 | if (!trans) | |
9482 | return NULL; | |
9483 | ||
02c6c244 PNA |
9484 | trans->net = maybe_get_net(net); |
9485 | if (!trans->net) { | |
9486 | kfree(trans); | |
9487 | return NULL; | |
9488 | } | |
9489 | ||
5f68718b PNA |
9490 | refcount_inc(&set->refs); |
9491 | trans->set = set; | |
5f68718b PNA |
9492 | trans->seq = gc_seq; |
9493 | ||
9494 | return trans; | |
9495 | } | |
9496 | ||
9497 | void nft_trans_gc_elem_add(struct nft_trans_gc *trans, void *priv) | |
9498 | { | |
9499 | trans->priv[trans->count++] = priv; | |
9500 | } | |
9501 | ||
9502 | static void nft_trans_gc_queue_work(struct nft_trans_gc *trans) | |
9503 | { | |
9504 | spin_lock(&nf_tables_gc_list_lock); | |
9505 | list_add_tail(&trans->list, &nf_tables_gc_list); | |
9506 | spin_unlock(&nf_tables_gc_list_lock); | |
9507 | ||
9508 | schedule_work(&trans_gc_work); | |
9509 | } | |
9510 | ||
9511 | static int nft_trans_gc_space(struct nft_trans_gc *trans) | |
9512 | { | |
9513 | return NFT_TRANS_GC_BATCHCOUNT - trans->count; | |
9514 | } | |
9515 | ||
9516 | struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc, | |
9517 | unsigned int gc_seq, gfp_t gfp) | |
9518 | { | |
9519 | if (nft_trans_gc_space(gc)) | |
9520 | return gc; | |
9521 | ||
9522 | nft_trans_gc_queue_work(gc); | |
9523 | ||
9524 | return nft_trans_gc_alloc(gc->set, gc_seq, gfp); | |
9525 | } | |
9526 | ||
9527 | void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans) | |
9528 | { | |
9529 | if (trans->count == 0) { | |
9530 | nft_trans_gc_destroy(trans); | |
9531 | return; | |
9532 | } | |
9533 | ||
9534 | nft_trans_gc_queue_work(trans); | |
9535 | } | |
9536 | ||
9537 | struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp) | |
9538 | { | |
9539 | if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net))) | |
9540 | return NULL; | |
9541 | ||
9542 | if (nft_trans_gc_space(gc)) | |
9543 | return gc; | |
9544 | ||
9545 | call_rcu(&gc->rcu, nft_trans_gc_trans_free); | |
9546 | ||
9547 | return nft_trans_gc_alloc(gc->set, 0, gfp); | |
9548 | } | |
9549 | ||
9550 | void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans) | |
9551 | { | |
9552 | WARN_ON_ONCE(!lockdep_commit_lock_is_held(trans->net)); | |
9553 | ||
9554 | if (trans->count == 0) { | |
9555 | nft_trans_gc_destroy(trans); | |
9556 | return; | |
9557 | } | |
9558 | ||
9559 | call_rcu(&trans->rcu, nft_trans_gc_trans_free); | |
9560 | } | |
9561 | ||
9562 | struct nft_trans_gc *nft_trans_gc_catchall(struct nft_trans_gc *gc, | |
9563 | unsigned int gc_seq) | |
9564 | { | |
9565 | struct nft_set_elem_catchall *catchall; | |
9566 | const struct nft_set *set = gc->set; | |
9567 | struct nft_set_ext *ext; | |
9568 | ||
9569 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
9570 | ext = nft_set_elem_ext(set, catchall->elem); | |
9571 | ||
9572 | if (!nft_set_elem_expired(ext)) | |
9573 | continue; | |
9574 | if (nft_set_elem_is_dead(ext)) | |
9575 | goto dead_elem; | |
9576 | ||
9577 | nft_set_elem_dead(ext); | |
9578 | dead_elem: | |
9579 | gc = nft_trans_gc_queue_async(gc, gc_seq, GFP_ATOMIC); | |
9580 | if (!gc) | |
9581 | return NULL; | |
9582 | ||
9583 | nft_trans_gc_elem_add(gc, catchall->elem); | |
9584 | } | |
9585 | ||
9586 | return gc; | |
9587 | } | |
9588 | ||
eb014de4 PNA |
9589 | static void nf_tables_module_autoload_cleanup(struct net *net) |
9590 | { | |
d59d2f82 | 9591 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
9592 | struct nft_module_request *req, *next; |
9593 | ||
0854db2a FW |
9594 | WARN_ON_ONCE(!list_empty(&nft_net->commit_list)); |
9595 | list_for_each_entry_safe(req, next, &nft_net->module_list, list) { | |
eb014de4 PNA |
9596 | WARN_ON_ONCE(!req->done); |
9597 | list_del(&req->list); | |
9598 | kfree(req); | |
9599 | } | |
9600 | } | |
9601 | ||
0935d558 FW |
9602 | static void nf_tables_commit_release(struct net *net) |
9603 | { | |
d59d2f82 | 9604 | struct nftables_pernet *nft_net = nft_pernet(net); |
0935d558 FW |
9605 | struct nft_trans *trans; |
9606 | ||
9607 | /* all side effects have to be made visible. | |
9608 | * For example, if a chain named 'foo' has been deleted, a | |
9609 | * new transaction must not find it anymore. | |
9610 | * | |
9611 | * Memory reclaim happens asynchronously from work queue | |
9612 | * to prevent expensive synchronize_rcu() in commit phase. | |
9613 | */ | |
0854db2a | 9614 | if (list_empty(&nft_net->commit_list)) { |
eb014de4 | 9615 | nf_tables_module_autoload_cleanup(net); |
0854db2a | 9616 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9617 | return; |
9618 | } | |
9619 | ||
0854db2a | 9620 | trans = list_last_entry(&nft_net->commit_list, |
0935d558 FW |
9621 | struct nft_trans, list); |
9622 | get_net(trans->ctx.net); | |
9623 | WARN_ON_ONCE(trans->put_net); | |
9624 | ||
9625 | trans->put_net = true; | |
9626 | spin_lock(&nf_tables_destroy_list_lock); | |
0854db2a | 9627 | list_splice_tail_init(&nft_net->commit_list, &nf_tables_destroy_list); |
0935d558 FW |
9628 | spin_unlock(&nf_tables_destroy_list_lock); |
9629 | ||
eb014de4 | 9630 | nf_tables_module_autoload_cleanup(net); |
0935d558 | 9631 | schedule_work(&trans_destroy_work); |
ffe8923f | 9632 | |
0854db2a | 9633 | mutex_unlock(&nft_net->commit_mutex); |
0935d558 FW |
9634 | } |
9635 | ||
67cc570e PNA |
9636 | static void nft_commit_notify(struct net *net, u32 portid) |
9637 | { | |
d59d2f82 | 9638 | struct nftables_pernet *nft_net = nft_pernet(net); |
67cc570e PNA |
9639 | struct sk_buff *batch_skb = NULL, *nskb, *skb; |
9640 | unsigned char *data; | |
9641 | int len; | |
9642 | ||
0854db2a | 9643 | list_for_each_entry_safe(skb, nskb, &nft_net->notify_list, list) { |
67cc570e PNA |
9644 | if (!batch_skb) { |
9645 | new_batch: | |
9646 | batch_skb = skb; | |
9647 | len = NLMSG_GOODSIZE - skb->len; | |
9648 | list_del(&skb->list); | |
9649 | continue; | |
9650 | } | |
9651 | len -= skb->len; | |
9652 | if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) { | |
9653 | data = skb_put(batch_skb, skb->len); | |
9654 | memcpy(data, skb->data, skb->len); | |
9655 | list_del(&skb->list); | |
9656 | kfree_skb(skb); | |
9657 | continue; | |
9658 | } | |
9659 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9660 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9661 | goto new_batch; | |
9662 | } | |
9663 | ||
9664 | if (batch_skb) { | |
9665 | nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES, | |
9666 | NFT_CB(batch_skb).report, GFP_KERNEL); | |
9667 | } | |
9668 | ||
0854db2a | 9669 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); |
67cc570e PNA |
9670 | } |
9671 | ||
c520292f RGB |
9672 | static int nf_tables_commit_audit_alloc(struct list_head *adl, |
9673 | struct nft_table *table) | |
9674 | { | |
9675 | struct nft_audit_data *adp; | |
9676 | ||
9677 | list_for_each_entry(adp, adl, list) { | |
9678 | if (adp->table == table) | |
9679 | return 0; | |
9680 | } | |
9681 | adp = kzalloc(sizeof(*adp), GFP_KERNEL); | |
9682 | if (!adp) | |
9683 | return -ENOMEM; | |
9684 | adp->table = table; | |
9685 | list_add(&adp->list, adl); | |
9686 | return 0; | |
9687 | } | |
9688 | ||
cfbe3650 DM |
9689 | static void nf_tables_commit_audit_free(struct list_head *adl) |
9690 | { | |
9691 | struct nft_audit_data *adp, *adn; | |
9692 | ||
9693 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9694 | list_del(&adp->list); | |
9695 | kfree(adp); | |
9696 | } | |
9697 | } | |
9698 | ||
c520292f RGB |
9699 | static void nf_tables_commit_audit_collect(struct list_head *adl, |
9700 | struct nft_table *table, u32 op) | |
9701 | { | |
9702 | struct nft_audit_data *adp; | |
9703 | ||
9704 | list_for_each_entry(adp, adl, list) { | |
9705 | if (adp->table == table) | |
9706 | goto found; | |
9707 | } | |
dadf33c9 | 9708 | WARN_ONCE(1, "table=%s not expected in commit list", table->name); |
c520292f RGB |
9709 | return; |
9710 | found: | |
9711 | adp->entries++; | |
9712 | if (!adp->op || adp->op > op) | |
9713 | adp->op = op; | |
9714 | } | |
9715 | ||
9716 | #define AUNFTABLENAMELEN (NFT_TABLE_MAXNAMELEN + 22) | |
9717 | ||
9718 | static void nf_tables_commit_audit_log(struct list_head *adl, u32 generation) | |
9719 | { | |
9720 | struct nft_audit_data *adp, *adn; | |
9721 | char aubuf[AUNFTABLENAMELEN]; | |
9722 | ||
9723 | list_for_each_entry_safe(adp, adn, adl, list) { | |
9724 | snprintf(aubuf, AUNFTABLENAMELEN, "%s:%u", adp->table->name, | |
9725 | generation); | |
9726 | audit_log_nfcfg(aubuf, adp->table->family, adp->entries, | |
9727 | nft2audit_op[adp->op], GFP_KERNEL); | |
9728 | list_del(&adp->list); | |
9729 | kfree(adp); | |
9730 | } | |
9731 | } | |
9732 | ||
212ed75d PNA |
9733 | static void nft_set_commit_update(struct list_head *set_update_list) |
9734 | { | |
9735 | struct nft_set *set, *next; | |
9736 | ||
9737 | list_for_each_entry_safe(set, next, set_update_list, pending_update) { | |
9738 | list_del_init(&set->pending_update); | |
9739 | ||
9740 | if (!set->ops->commit) | |
9741 | continue; | |
9742 | ||
9743 | set->ops->commit(set); | |
9744 | } | |
9745 | } | |
9746 | ||
6a33d8b7 PNA |
9747 | static unsigned int nft_gc_seq_begin(struct nftables_pernet *nft_net) |
9748 | { | |
9749 | unsigned int gc_seq; | |
9750 | ||
9751 | /* Bump gc counter, it becomes odd, this is the busy mark. */ | |
9752 | gc_seq = READ_ONCE(nft_net->gc_seq); | |
9753 | WRITE_ONCE(nft_net->gc_seq, ++gc_seq); | |
9754 | ||
9755 | return gc_seq; | |
9756 | } | |
9757 | ||
9758 | static void nft_gc_seq_end(struct nftables_pernet *nft_net, unsigned int gc_seq) | |
9759 | { | |
9760 | WRITE_ONCE(nft_net->gc_seq, ++gc_seq); | |
9761 | } | |
9762 | ||
5913beaf | 9763 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 9764 | { |
d59d2f82 | 9765 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 9766 | struct nft_trans *trans, *next; |
5f68718b | 9767 | unsigned int base_seq, gc_seq; |
212ed75d | 9768 | LIST_HEAD(set_update_list); |
a3716e70 | 9769 | struct nft_trans_elem *te; |
0cbc06b3 FW |
9770 | struct nft_chain *chain; |
9771 | struct nft_table *table; | |
c520292f | 9772 | LIST_HEAD(adl); |
c9626a2c | 9773 | int err; |
37082f93 | 9774 | |
0854db2a FW |
9775 | if (list_empty(&nft_net->commit_list)) { |
9776 | mutex_unlock(&nft_net->commit_mutex); | |
b8b27498 FW |
9777 | return 0; |
9778 | } | |
9779 | ||
938154b9 PNA |
9780 | list_for_each_entry(trans, &nft_net->binding_list, binding_list) { |
9781 | switch (trans->msg_type) { | |
9782 | case NFT_MSG_NEWSET: | |
9783 | if (!nft_trans_set_update(trans) && | |
9784 | nft_set_is_anonymous(nft_trans_set(trans)) && | |
9785 | !nft_trans_set_bound(trans)) { | |
9786 | pr_warn_once("nftables ruleset with unbound set\n"); | |
9787 | return -EINVAL; | |
9788 | } | |
9789 | break; | |
62e1e94b PNA |
9790 | case NFT_MSG_NEWCHAIN: |
9791 | if (!nft_trans_chain_update(trans) && | |
9792 | nft_chain_binding(nft_trans_chain(trans)) && | |
9793 | !nft_trans_chain_bound(trans)) { | |
9794 | pr_warn_once("nftables ruleset with unbound chain\n"); | |
9795 | return -EINVAL; | |
9796 | } | |
9797 | break; | |
938154b9 PNA |
9798 | } |
9799 | } | |
9800 | ||
a654de8f PNA |
9801 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
9802 | if (nf_tables_validate(net) < 0) | |
9803 | return -EAGAIN; | |
9804 | ||
c9626a2c PNA |
9805 | err = nft_flow_rule_offload_commit(net); |
9806 | if (err < 0) | |
9807 | return err; | |
9808 | ||
0cbc06b3 | 9809 | /* 1. Allocate space for next generation rules_gen_X[] */ |
0854db2a | 9810 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
0cbc06b3 | 9811 | int ret; |
37082f93 | 9812 | |
c520292f RGB |
9813 | ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); |
9814 | if (ret) { | |
9815 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9816 | nf_tables_commit_audit_free(&adl); |
c520292f RGB |
9817 | return ret; |
9818 | } | |
0cbc06b3 FW |
9819 | if (trans->msg_type == NFT_MSG_NEWRULE || |
9820 | trans->msg_type == NFT_MSG_DELRULE) { | |
9821 | chain = trans->ctx.chain; | |
9822 | ||
9823 | ret = nf_tables_commit_chain_prepare(net, chain); | |
9824 | if (ret < 0) { | |
9825 | nf_tables_commit_chain_prepare_cancel(net); | |
cfbe3650 | 9826 | nf_tables_commit_audit_free(&adl); |
0cbc06b3 FW |
9827 | return ret; |
9828 | } | |
9829 | } | |
9830 | } | |
37082f93 | 9831 | |
0cbc06b3 | 9832 | /* step 2. Make rules_gen_X visible to packet path */ |
0854db2a | 9833 | list_for_each_entry(table, &nft_net->tables, list) { |
0fb39bbe FW |
9834 | list_for_each_entry(chain, &table->chains, list) |
9835 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
9836 | } |
9837 | ||
9838 | /* | |
9839 | * Bump generation counter, invalidate any dump in progress. | |
9840 | * Cannot fail after this point. | |
37082f93 | 9841 | */ |
34002783 PNA |
9842 | base_seq = READ_ONCE(nft_net->base_seq); |
9843 | while (++base_seq == 0) | |
0854db2a | 9844 | ; |
0cbc06b3 | 9845 | |
34002783 PNA |
9846 | WRITE_ONCE(nft_net->base_seq, base_seq); |
9847 | ||
6a33d8b7 | 9848 | gc_seq = nft_gc_seq_begin(nft_net); |
5f68718b | 9849 | |
0cbc06b3 FW |
9850 | /* step 3. Start new generation, rules_gen_X now in use. */ |
9851 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 | 9852 | |
0854db2a | 9853 | list_for_each_entry_safe(trans, next, &nft_net->commit_list, list) { |
c520292f RGB |
9854 | nf_tables_commit_audit_collect(&adl, trans->ctx.table, |
9855 | trans->msg_type); | |
b380e5c7 | 9856 | switch (trans->msg_type) { |
55dd6f93 PNA |
9857 | case NFT_MSG_NEWTABLE: |
9858 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
9859 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
9860 | nft_trans_destroy(trans); | |
9861 | break; | |
9862 | } | |
9863 | if (trans->ctx.table->flags & NFT_TABLE_F_DORMANT) | |
0ce7cf41 PNA |
9864 | nf_tables_table_disable(net, trans->ctx.table); |
9865 | ||
179d9ba5 | 9866 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; |
55dd6f93 | 9867 | } else { |
f2a6d766 | 9868 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 9869 | } |
35151d84 | 9870 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
9871 | nft_trans_destroy(trans); |
9872 | break; | |
9873 | case NFT_MSG_DELTABLE: | |
f80a612d | 9874 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 9875 | list_del_rcu(&trans->ctx.table->list); |
f80a612d | 9876 | nf_tables_table_notify(&trans->ctx, trans->msg_type); |
55dd6f93 | 9877 | break; |
91c7b38d | 9878 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 9879 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 9880 | nft_chain_commit_update(trans); |
b9703ed4 PNA |
9881 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, |
9882 | &nft_trans_chain_hooks(trans)); | |
9883 | list_splice(&nft_trans_chain_hooks(trans), | |
9884 | &nft_trans_basechain(trans)->hook_list); | |
9f8aac0b FW |
9885 | /* trans destroyed after rcu grace period */ |
9886 | } else { | |
66293c46 | 9887 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 9888 | nft_clear(net, trans->ctx.chain); |
b9703ed4 | 9889 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN, NULL); |
9f8aac0b FW |
9890 | nft_trans_destroy(trans); |
9891 | } | |
91c7b38d PNA |
9892 | break; |
9893 | case NFT_MSG_DELCHAIN: | |
f80a612d | 9894 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
9895 | if (nft_trans_chain_update(trans)) { |
9896 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN, | |
9897 | &nft_trans_chain_hooks(trans)); | |
9898 | nft_netdev_unregister_hooks(net, | |
9899 | &nft_trans_chain_hooks(trans), | |
9900 | true); | |
9901 | } else { | |
9902 | nft_chain_del(trans->ctx.chain); | |
9903 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN, | |
9904 | NULL); | |
9905 | nf_tables_unregister_hook(trans->ctx.net, | |
9906 | trans->ctx.table, | |
9907 | trans->ctx.chain); | |
9908 | } | |
91c7b38d | 9909 | break; |
b380e5c7 | 9910 | case NFT_MSG_NEWRULE: |
889f7ee7 | 9911 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 9912 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 9913 | nft_trans_rule(trans), |
35151d84 | 9914 | NFT_MSG_NEWRULE); |
9dd732e0 PNA |
9915 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
9916 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
9917 | ||
37082f93 | 9918 | nft_trans_destroy(trans); |
b380e5c7 PNA |
9919 | break; |
9920 | case NFT_MSG_DELRULE: | |
f80a612d | 9921 | case NFT_MSG_DESTROYRULE: |
b380e5c7 | 9922 | list_del_rcu(&nft_trans_rule(trans)->list); |
35151d84 PNA |
9923 | nf_tables_rule_notify(&trans->ctx, |
9924 | nft_trans_rule(trans), | |
f80a612d | 9925 | trans->msg_type); |
f6ac8585 PNA |
9926 | nft_rule_expr_deactivate(&trans->ctx, |
9927 | nft_trans_rule(trans), | |
9928 | NFT_TRANS_COMMIT); | |
26b5934f PNA |
9929 | |
9930 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) | |
9931 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 | 9932 | break; |
958bee14 | 9933 | case NFT_MSG_NEWSET: |
123b9961 PNA |
9934 | if (nft_trans_set_update(trans)) { |
9935 | struct nft_set *set = nft_trans_set(trans); | |
4fefee57 | 9936 | |
123b9961 PNA |
9937 | WRITE_ONCE(set->timeout, nft_trans_set_timeout(trans)); |
9938 | WRITE_ONCE(set->gc_int, nft_trans_set_gc_int(trans)); | |
96b2ef9b FW |
9939 | |
9940 | if (nft_trans_set_size(trans)) | |
9941 | WRITE_ONCE(set->size, nft_trans_set_size(trans)); | |
123b9961 PNA |
9942 | } else { |
9943 | nft_clear(net, nft_trans_set(trans)); | |
9944 | /* This avoids hitting -EBUSY when deleting the table | |
9945 | * from the transaction. | |
9946 | */ | |
9947 | if (nft_set_is_anonymous(nft_trans_set(trans)) && | |
9948 | !list_empty(&nft_trans_set(trans)->bindings)) | |
1689f259 | 9949 | nft_use_dec(&trans->ctx.table->use); |
123b9961 | 9950 | } |
958bee14 | 9951 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 9952 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
9953 | nft_trans_destroy(trans); |
9954 | break; | |
9955 | case NFT_MSG_DELSET: | |
f80a612d | 9956 | case NFT_MSG_DESTROYSET: |
5f68718b | 9957 | nft_trans_set(trans)->dead = 1; |
37a9cc52 | 9958 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 9959 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
f80a612d | 9960 | trans->msg_type, GFP_KERNEL); |
958bee14 | 9961 | break; |
60319eb1 | 9962 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
9963 | te = (struct nft_trans_elem *)trans->data; |
9964 | ||
aaa31047 | 9965 | nft_setelem_activate(net, te->set, &te->elem); |
cc02e457 PM |
9966 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9967 | &te->elem, | |
6fb721cf | 9968 | NFT_MSG_NEWSETELEM); |
212ed75d PNA |
9969 | if (te->set->ops->commit && |
9970 | list_empty(&te->set->pending_update)) { | |
9971 | list_add_tail(&te->set->pending_update, | |
9972 | &set_update_list); | |
9973 | } | |
60319eb1 PNA |
9974 | nft_trans_destroy(trans); |
9975 | break; | |
9976 | case NFT_MSG_DELSETELEM: | |
f80a612d | 9977 | case NFT_MSG_DESTROYSETELEM: |
a3716e70 | 9978 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 9979 | |
a3716e70 PNA |
9980 | nf_tables_setelem_notify(&trans->ctx, te->set, |
9981 | &te->elem, | |
f80a612d | 9982 | trans->msg_type); |
aaa31047 PNA |
9983 | nft_setelem_remove(net, te->set, &te->elem); |
9984 | if (!nft_setelem_is_catchall(te->set, &te->elem)) { | |
9985 | atomic_dec(&te->set->nelems); | |
9986 | te->set->ndeact--; | |
9987 | } | |
212ed75d PNA |
9988 | if (te->set->ops->commit && |
9989 | list_empty(&te->set->pending_update)) { | |
9990 | list_add_tail(&te->set->pending_update, | |
9991 | &set_update_list); | |
9992 | } | |
60319eb1 | 9993 | break; |
e5009240 | 9994 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
9995 | if (nft_trans_obj_update(trans)) { |
9996 | nft_obj_commit_update(trans); | |
9997 | nf_tables_obj_notify(&trans->ctx, | |
9998 | nft_trans_obj(trans), | |
9999 | NFT_MSG_NEWOBJ); | |
10000 | } else { | |
10001 | nft_clear(net, nft_trans_obj(trans)); | |
10002 | nf_tables_obj_notify(&trans->ctx, | |
10003 | nft_trans_obj(trans), | |
10004 | NFT_MSG_NEWOBJ); | |
10005 | nft_trans_destroy(trans); | |
10006 | } | |
e5009240 PNA |
10007 | break; |
10008 | case NFT_MSG_DELOBJ: | |
f80a612d | 10009 | case NFT_MSG_DESTROYOBJ: |
d152159b | 10010 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 | 10011 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
f80a612d | 10012 | trans->msg_type); |
e5009240 | 10013 | break; |
3b49e2e9 | 10014 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 10015 | if (nft_trans_flowtable_update(trans)) { |
7b35582c PNA |
10016 | nft_trans_flowtable(trans)->data.flags = |
10017 | nft_trans_flowtable_flags(trans); | |
78d9f48f PNA |
10018 | nf_tables_flowtable_notify(&trans->ctx, |
10019 | nft_trans_flowtable(trans), | |
10020 | &nft_trans_flowtable_hooks(trans), | |
10021 | NFT_MSG_NEWFLOWTABLE); | |
10022 | list_splice(&nft_trans_flowtable_hooks(trans), | |
10023 | &nft_trans_flowtable(trans)->hook_list); | |
10024 | } else { | |
10025 | nft_clear(net, nft_trans_flowtable(trans)); | |
10026 | nf_tables_flowtable_notify(&trans->ctx, | |
10027 | nft_trans_flowtable(trans), | |
28339b21 | 10028 | NULL, |
78d9f48f PNA |
10029 | NFT_MSG_NEWFLOWTABLE); |
10030 | } | |
3b49e2e9 PNA |
10031 | nft_trans_destroy(trans); |
10032 | break; | |
10033 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 10034 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 10035 | if (nft_trans_flowtable_update(trans)) { |
abadb2f8 PNA |
10036 | nf_tables_flowtable_notify(&trans->ctx, |
10037 | nft_trans_flowtable(trans), | |
10038 | &nft_trans_flowtable_hooks(trans), | |
f80a612d | 10039 | trans->msg_type); |
abadb2f8 PNA |
10040 | nft_unregister_flowtable_net_hooks(net, |
10041 | &nft_trans_flowtable_hooks(trans)); | |
10042 | } else { | |
10043 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
10044 | nf_tables_flowtable_notify(&trans->ctx, | |
10045 | nft_trans_flowtable(trans), | |
28339b21 | 10046 | NULL, |
f80a612d | 10047 | trans->msg_type); |
abadb2f8 PNA |
10048 | nft_unregister_flowtable_net_hooks(net, |
10049 | &nft_trans_flowtable(trans)->hook_list); | |
10050 | } | |
3b49e2e9 | 10051 | break; |
37082f93 | 10052 | } |
37082f93 PNA |
10053 | } |
10054 | ||
212ed75d PNA |
10055 | nft_set_commit_update(&set_update_list); |
10056 | ||
67cc570e | 10057 | nft_commit_notify(net, NETLINK_CB(skb).portid); |
84d7fce6 | 10058 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0854db2a | 10059 | nf_tables_commit_audit_log(&adl, nft_net->base_seq); |
5f68718b | 10060 | |
6a33d8b7 | 10061 | nft_gc_seq_end(nft_net, gc_seq); |
0935d558 | 10062 | nf_tables_commit_release(net); |
37082f93 PNA |
10063 | |
10064 | return 0; | |
10065 | } | |
10066 | ||
eb014de4 PNA |
10067 | static void nf_tables_module_autoload(struct net *net) |
10068 | { | |
d59d2f82 | 10069 | struct nftables_pernet *nft_net = nft_pernet(net); |
eb014de4 PNA |
10070 | struct nft_module_request *req, *next; |
10071 | LIST_HEAD(module_list); | |
10072 | ||
0854db2a FW |
10073 | list_splice_init(&nft_net->module_list, &module_list); |
10074 | mutex_unlock(&nft_net->commit_mutex); | |
eb014de4 | 10075 | list_for_each_entry_safe(req, next, &module_list, list) { |
1d305ba4 FW |
10076 | request_module("%s", req->module); |
10077 | req->done = true; | |
eb014de4 | 10078 | } |
0854db2a FW |
10079 | mutex_lock(&nft_net->commit_mutex); |
10080 | list_splice(&module_list, &nft_net->module_list); | |
eb014de4 PNA |
10081 | } |
10082 | ||
b326dd37 | 10083 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 10084 | { |
c7c32e72 PNA |
10085 | switch (trans->msg_type) { |
10086 | case NFT_MSG_NEWTABLE: | |
10087 | nf_tables_table_destroy(&trans->ctx); | |
10088 | break; | |
10089 | case NFT_MSG_NEWCHAIN: | |
b9703ed4 PNA |
10090 | if (nft_trans_chain_update(trans)) |
10091 | nft_hooks_destroy(&nft_trans_chain_hooks(trans)); | |
10092 | else | |
10093 | nf_tables_chain_destroy(&trans->ctx); | |
c7c32e72 PNA |
10094 | break; |
10095 | case NFT_MSG_NEWRULE: | |
10096 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
10097 | break; | |
10098 | case NFT_MSG_NEWSET: | |
0c2a85ed | 10099 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 10100 | break; |
61edafbb PM |
10101 | case NFT_MSG_NEWSETELEM: |
10102 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 10103 | nft_trans_elem(trans).priv, true); |
61edafbb | 10104 | break; |
e5009240 | 10105 | case NFT_MSG_NEWOBJ: |
00bfb320 | 10106 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 10107 | break; |
3b49e2e9 | 10108 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f | 10109 | if (nft_trans_flowtable_update(trans)) |
cdc32546 | 10110 | nft_hooks_destroy(&nft_trans_flowtable_hooks(trans)); |
78d9f48f PNA |
10111 | else |
10112 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 10113 | break; |
c7c32e72 PNA |
10114 | } |
10115 | kfree(trans); | |
10116 | } | |
10117 | ||
212ed75d PNA |
10118 | static void nft_set_abort_update(struct list_head *set_update_list) |
10119 | { | |
10120 | struct nft_set *set, *next; | |
10121 | ||
10122 | list_for_each_entry_safe(set, next, set_update_list, pending_update) { | |
10123 | list_del_init(&set->pending_update); | |
10124 | ||
10125 | if (!set->ops->abort) | |
10126 | continue; | |
10127 | ||
10128 | set->ops->abort(set); | |
10129 | } | |
10130 | } | |
10131 | ||
c0391b6a | 10132 | static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) |
37082f93 | 10133 | { |
d59d2f82 | 10134 | struct nftables_pernet *nft_net = nft_pernet(net); |
37082f93 | 10135 | struct nft_trans *trans, *next; |
212ed75d | 10136 | LIST_HEAD(set_update_list); |
02263db0 | 10137 | struct nft_trans_elem *te; |
37082f93 | 10138 | |
c0391b6a PNA |
10139 | if (action == NFNL_ABORT_VALIDATE && |
10140 | nf_tables_validate(net) < 0) | |
10141 | return -EAGAIN; | |
10142 | ||
0854db2a | 10143 | list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list, |
a907e36d | 10144 | list) { |
b380e5c7 | 10145 | switch (trans->msg_type) { |
55dd6f93 PNA |
10146 | case NFT_MSG_NEWTABLE: |
10147 | if (nft_trans_table_update(trans)) { | |
179d9ba5 PNA |
10148 | if (!(trans->ctx.table->flags & __NFT_TABLE_F_UPDATE)) { |
10149 | nft_trans_destroy(trans); | |
10150 | break; | |
10151 | } | |
10152 | if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_DORMANT) { | |
0ce7cf41 | 10153 | nf_tables_table_disable(net, trans->ctx.table); |
179d9ba5 PNA |
10154 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; |
10155 | } else if (trans->ctx.table->flags & __NFT_TABLE_F_WAS_AWAKEN) { | |
10156 | trans->ctx.table->flags &= ~NFT_TABLE_F_DORMANT; | |
10157 | } | |
10158 | trans->ctx.table->flags &= ~__NFT_TABLE_F_UPDATE; | |
55dd6f93 PNA |
10159 | nft_trans_destroy(trans); |
10160 | } else { | |
e688a7f8 | 10161 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
10162 | } |
10163 | break; | |
10164 | case NFT_MSG_DELTABLE: | |
f80a612d | 10165 | case NFT_MSG_DESTROYTABLE: |
f2a6d766 | 10166 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
10167 | nft_trans_destroy(trans); |
10168 | break; | |
91c7b38d PNA |
10169 | case NFT_MSG_NEWCHAIN: |
10170 | if (nft_trans_chain_update(trans)) { | |
b9703ed4 PNA |
10171 | nft_netdev_unregister_hooks(net, |
10172 | &nft_trans_chain_hooks(trans), | |
10173 | true); | |
982f4051 | 10174 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 10175 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
10176 | nft_trans_destroy(trans); |
10177 | } else { | |
4bedf9ee | 10178 | if (nft_trans_chain_bound(trans)) { |
d0e2c7de PNA |
10179 | nft_trans_destroy(trans); |
10180 | break; | |
10181 | } | |
1689f259 | 10182 | nft_use_dec_restore(&trans->ctx.table->use); |
1b2470e5 | 10183 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
10184 | nf_tables_unregister_hook(trans->ctx.net, |
10185 | trans->ctx.table, | |
10186 | trans->ctx.chain); | |
91c7b38d PNA |
10187 | } |
10188 | break; | |
10189 | case NFT_MSG_DELCHAIN: | |
f80a612d | 10190 | case NFT_MSG_DESTROYCHAIN: |
7d937b10 PNA |
10191 | if (nft_trans_chain_update(trans)) { |
10192 | list_splice(&nft_trans_chain_hooks(trans), | |
10193 | &nft_trans_basechain(trans)->hook_list); | |
10194 | } else { | |
1689f259 | 10195 | nft_use_inc_restore(&trans->ctx.table->use); |
7d937b10 PNA |
10196 | nft_clear(trans->ctx.net, trans->ctx.chain); |
10197 | } | |
91c7b38d PNA |
10198 | nft_trans_destroy(trans); |
10199 | break; | |
b380e5c7 | 10200 | case NFT_MSG_NEWRULE: |
4bedf9ee PNA |
10201 | if (nft_trans_rule_bound(trans)) { |
10202 | nft_trans_destroy(trans); | |
10203 | break; | |
10204 | } | |
1689f259 | 10205 | nft_use_dec_restore(&trans->ctx.chain->use); |
b380e5c7 | 10206 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
10207 | nft_rule_expr_deactivate(&trans->ctx, |
10208 | nft_trans_rule(trans), | |
10209 | NFT_TRANS_ABORT); | |
3c5e4462 PNA |
10210 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
10211 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
b380e5c7 PNA |
10212 | break; |
10213 | case NFT_MSG_DELRULE: | |
f80a612d | 10214 | case NFT_MSG_DESTROYRULE: |
1689f259 | 10215 | nft_use_inc_restore(&trans->ctx.chain->use); |
889f7ee7 | 10216 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 10217 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
3c5e4462 PNA |
10218 | if (trans->ctx.chain->flags & NFT_CHAIN_HW_OFFLOAD) |
10219 | nft_flow_rule_destroy(nft_trans_flow_rule(trans)); | |
10220 | ||
37082f93 | 10221 | nft_trans_destroy(trans); |
b380e5c7 | 10222 | break; |
958bee14 | 10223 | case NFT_MSG_NEWSET: |
123b9961 PNA |
10224 | if (nft_trans_set_update(trans)) { |
10225 | nft_trans_destroy(trans); | |
10226 | break; | |
10227 | } | |
1689f259 | 10228 | nft_use_dec_restore(&trans->ctx.table->use); |
6a0a8d10 | 10229 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
10230 | nft_trans_destroy(trans); |
10231 | break; | |
10232 | } | |
10233 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
10234 | break; |
10235 | case NFT_MSG_DELSET: | |
f80a612d | 10236 | case NFT_MSG_DESTROYSET: |
1689f259 | 10237 | nft_use_inc_restore(&trans->ctx.table->use); |
37a9cc52 | 10238 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
628bd3e4 PNA |
10239 | if (nft_trans_set(trans)->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
10240 | nft_map_activate(&trans->ctx, nft_trans_set(trans)); | |
10241 | ||
958bee14 PNA |
10242 | nft_trans_destroy(trans); |
10243 | break; | |
60319eb1 | 10244 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 10245 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
10246 | nft_trans_destroy(trans); |
10247 | break; | |
10248 | } | |
02263db0 | 10249 | te = (struct nft_trans_elem *)trans->data; |
aaa31047 PNA |
10250 | nft_setelem_remove(net, te->set, &te->elem); |
10251 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
10252 | atomic_dec(&te->set->nelems); | |
212ed75d PNA |
10253 | |
10254 | if (te->set->ops->abort && | |
10255 | list_empty(&te->set->pending_update)) { | |
10256 | list_add_tail(&te->set->pending_update, | |
10257 | &set_update_list); | |
10258 | } | |
60319eb1 PNA |
10259 | break; |
10260 | case NFT_MSG_DELSETELEM: | |
f80a612d | 10261 | case NFT_MSG_DESTROYSETELEM: |
cc02e457 PM |
10262 | te = (struct nft_trans_elem *)trans->data; |
10263 | ||
f8bb7889 | 10264 | nft_setelem_data_activate(net, te->set, &te->elem); |
aaa31047 PNA |
10265 | nft_setelem_activate(net, te->set, &te->elem); |
10266 | if (!nft_setelem_is_catchall(te->set, &te->elem)) | |
10267 | te->set->ndeact--; | |
cc02e457 | 10268 | |
212ed75d PNA |
10269 | if (te->set->ops->abort && |
10270 | list_empty(&te->set->pending_update)) { | |
10271 | list_add_tail(&te->set->pending_update, | |
10272 | &set_update_list); | |
10273 | } | |
e5009240 PNA |
10274 | nft_trans_destroy(trans); |
10275 | break; | |
10276 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 | 10277 | if (nft_trans_obj_update(trans)) { |
dad3bdee | 10278 | nft_obj_destroy(&trans->ctx, nft_trans_obj_newobj(trans)); |
d62d0ba9 FFM |
10279 | nft_trans_destroy(trans); |
10280 | } else { | |
1689f259 | 10281 | nft_use_dec_restore(&trans->ctx.table->use); |
d62d0ba9 FFM |
10282 | nft_obj_del(nft_trans_obj(trans)); |
10283 | } | |
e5009240 PNA |
10284 | break; |
10285 | case NFT_MSG_DELOBJ: | |
f80a612d | 10286 | case NFT_MSG_DESTROYOBJ: |
1689f259 | 10287 | nft_use_inc_restore(&trans->ctx.table->use); |
e5009240 | 10288 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); |
60319eb1 PNA |
10289 | nft_trans_destroy(trans); |
10290 | break; | |
3b49e2e9 | 10291 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
10292 | if (nft_trans_flowtable_update(trans)) { |
10293 | nft_unregister_flowtable_net_hooks(net, | |
10294 | &nft_trans_flowtable_hooks(trans)); | |
10295 | } else { | |
1689f259 | 10296 | nft_use_dec_restore(&trans->ctx.table->use); |
78d9f48f PNA |
10297 | list_del_rcu(&nft_trans_flowtable(trans)->list); |
10298 | nft_unregister_flowtable_net_hooks(net, | |
10299 | &nft_trans_flowtable(trans)->hook_list); | |
10300 | } | |
3b49e2e9 PNA |
10301 | break; |
10302 | case NFT_MSG_DELFLOWTABLE: | |
f80a612d | 10303 | case NFT_MSG_DESTROYFLOWTABLE: |
abadb2f8 | 10304 | if (nft_trans_flowtable_update(trans)) { |
b6d9014a PNA |
10305 | list_splice(&nft_trans_flowtable_hooks(trans), |
10306 | &nft_trans_flowtable(trans)->hook_list); | |
abadb2f8 | 10307 | } else { |
1689f259 | 10308 | nft_use_inc_restore(&trans->ctx.table->use); |
abadb2f8 PNA |
10309 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); |
10310 | } | |
3b49e2e9 PNA |
10311 | nft_trans_destroy(trans); |
10312 | break; | |
37082f93 | 10313 | } |
37082f93 PNA |
10314 | } |
10315 | ||
212ed75d PNA |
10316 | nft_set_abort_update(&set_update_list); |
10317 | ||
b326dd37 PNA |
10318 | synchronize_rcu(); |
10319 | ||
a1cee076 | 10320 | list_for_each_entry_safe_reverse(trans, next, |
0854db2a | 10321 | &nft_net->commit_list, list) { |
938154b9 | 10322 | nft_trans_list_del(trans); |
b326dd37 | 10323 | nf_tables_abort_release(trans); |
37082f93 PNA |
10324 | } |
10325 | ||
c0391b6a | 10326 | if (action == NFNL_ABORT_AUTOLOAD) |
eb014de4 PNA |
10327 | nf_tables_module_autoload(net); |
10328 | else | |
10329 | nf_tables_module_autoload_cleanup(net); | |
10330 | ||
37082f93 PNA |
10331 | return 0; |
10332 | } | |
10333 | ||
c0391b6a PNA |
10334 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, |
10335 | enum nfnl_abort_action action) | |
71ad00c5 | 10336 | { |
d59d2f82 | 10337 | struct nftables_pernet *nft_net = nft_pernet(net); |
c0391b6a | 10338 | int ret = __nf_tables_abort(net, action); |
f102d66b | 10339 | |
0854db2a | 10340 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
10341 | |
10342 | return ret; | |
71ad00c5 FW |
10343 | } |
10344 | ||
74e8bcd2 PNA |
10345 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
10346 | { | |
d59d2f82 | 10347 | struct nftables_pernet *nft_net = nft_pernet(net); |
f102d66b FW |
10348 | bool genid_ok; |
10349 | ||
0854db2a | 10350 | mutex_lock(&nft_net->commit_mutex); |
f102d66b | 10351 | |
0854db2a | 10352 | genid_ok = genid == 0 || nft_net->base_seq == genid; |
f102d66b | 10353 | if (!genid_ok) |
0854db2a | 10354 | mutex_unlock(&nft_net->commit_mutex); |
f102d66b FW |
10355 | |
10356 | /* else, commit mutex has to be released by commit or abort function */ | |
10357 | return genid_ok; | |
74e8bcd2 PNA |
10358 | } |
10359 | ||
96518518 PM |
10360 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
10361 | .name = "nf_tables", | |
10362 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
10363 | .cb_count = NFT_MSG_MAX, | |
10364 | .cb = nf_tables_cb, | |
0628b123 PNA |
10365 | .commit = nf_tables_commit, |
10366 | .abort = nf_tables_abort, | |
74e8bcd2 | 10367 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 10368 | .owner = THIS_MODULE, |
96518518 PM |
10369 | }; |
10370 | ||
7210e4e3 | 10371 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 10372 | enum nft_chain_types type) |
7210e4e3 PNA |
10373 | { |
10374 | const struct nft_base_chain *basechain; | |
10375 | ||
f323d954 | 10376 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
10377 | basechain = nft_base_chain(chain); |
10378 | if (basechain->type->type != type) | |
10379 | return -EOPNOTSUPP; | |
10380 | } | |
10381 | return 0; | |
10382 | } | |
10383 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
10384 | ||
75e8d06d PNA |
10385 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
10386 | unsigned int hook_flags) | |
10387 | { | |
10388 | struct nft_base_chain *basechain; | |
10389 | ||
f323d954 | 10390 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
10391 | basechain = nft_base_chain(chain); |
10392 | ||
c974a3a3 | 10393 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
10394 | return 0; |
10395 | ||
10396 | return -EOPNOTSUPP; | |
10397 | } | |
10398 | ||
10399 | return 0; | |
10400 | } | |
10401 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
10402 | ||
20a69341 PM |
10403 | /* |
10404 | * Loop detection - walk through the ruleset beginning at the destination chain | |
10405 | * of a new jump until either the source chain is reached (loop) or all | |
10406 | * reachable chains have been traversed. | |
10407 | * | |
10408 | * The loop check is performed whenever a new jump verdict is added to an | |
10409 | * expression or verdict map or a verdict map is bound to a new chain. | |
10410 | */ | |
10411 | ||
10412 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
10413 | const struct nft_chain *chain); | |
10414 | ||
6387aa6e PNA |
10415 | static int nft_check_loops(const struct nft_ctx *ctx, |
10416 | const struct nft_set_ext *ext) | |
10417 | { | |
10418 | const struct nft_data *data; | |
10419 | int ret; | |
10420 | ||
10421 | data = nft_set_ext_data(ext); | |
10422 | switch (data->verdict.code) { | |
10423 | case NFT_JUMP: | |
10424 | case NFT_GOTO: | |
10425 | ret = nf_tables_check_loops(ctx, data->verdict.chain); | |
10426 | break; | |
10427 | default: | |
10428 | ret = 0; | |
10429 | break; | |
10430 | } | |
10431 | ||
10432 | return ret; | |
10433 | } | |
10434 | ||
20a69341 | 10435 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, |
de70185d | 10436 | struct nft_set *set, |
20a69341 | 10437 | const struct nft_set_iter *iter, |
de70185d | 10438 | struct nft_set_elem *elem) |
20a69341 | 10439 | { |
fe2811eb | 10440 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
fe2811eb PM |
10441 | |
10442 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
10443 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
10444 | return 0; |
10445 | ||
6387aa6e | 10446 | return nft_check_loops(ctx, ext); |
20a69341 PM |
10447 | } |
10448 | ||
aaa31047 PNA |
10449 | static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
10450 | struct nft_set *set) | |
10451 | { | |
10452 | u8 genmask = nft_genmask_next(ctx->net); | |
10453 | struct nft_set_elem_catchall *catchall; | |
10454 | struct nft_set_ext *ext; | |
10455 | int ret = 0; | |
10456 | ||
10457 | list_for_each_entry_rcu(catchall, &set->catchall_list, list) { | |
10458 | ext = nft_set_elem_ext(set, catchall->elem); | |
10459 | if (!nft_set_elem_active(ext, genmask)) | |
10460 | continue; | |
10461 | ||
10462 | ret = nft_check_loops(ctx, ext); | |
10463 | if (ret < 0) | |
10464 | return ret; | |
10465 | } | |
10466 | ||
10467 | return ret; | |
10468 | } | |
10469 | ||
20a69341 PM |
10470 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
10471 | const struct nft_chain *chain) | |
10472 | { | |
10473 | const struct nft_rule *rule; | |
10474 | const struct nft_expr *expr, *last; | |
de70185d | 10475 | struct nft_set *set; |
20a69341 PM |
10476 | struct nft_set_binding *binding; |
10477 | struct nft_set_iter iter; | |
20a69341 PM |
10478 | |
10479 | if (ctx->chain == chain) | |
10480 | return -ELOOP; | |
10481 | ||
10482 | list_for_each_entry(rule, &chain->rules, list) { | |
10483 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
10484 | struct nft_immediate_expr *priv; |
10485 | const struct nft_data *data; | |
0ca743a5 PNA |
10486 | int err; |
10487 | ||
a654de8f | 10488 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
10489 | continue; |
10490 | ||
a654de8f PNA |
10491 | priv = nft_expr_priv(expr); |
10492 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 10493 | continue; |
20a69341 | 10494 | |
a654de8f | 10495 | data = &priv->data; |
1ca2e170 | 10496 | switch (data->verdict.code) { |
20a69341 PM |
10497 | case NFT_JUMP: |
10498 | case NFT_GOTO: | |
1ca2e170 PM |
10499 | err = nf_tables_check_loops(ctx, |
10500 | data->verdict.chain); | |
20a69341 PM |
10501 | if (err < 0) |
10502 | return err; | |
c2168e6b | 10503 | break; |
20a69341 PM |
10504 | default: |
10505 | break; | |
10506 | } | |
10507 | } | |
10508 | } | |
10509 | ||
10510 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
10511 | if (!nft_is_active_next(ctx->net, set)) |
10512 | continue; | |
20a69341 PM |
10513 | if (!(set->flags & NFT_SET_MAP) || |
10514 | set->dtype != NFT_DATA_VERDICT) | |
10515 | continue; | |
10516 | ||
10517 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
10518 | if (!(binding->flags & NFT_SET_MAP) || |
10519 | binding->chain != chain) | |
20a69341 PM |
10520 | continue; |
10521 | ||
8588ac09 | 10522 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
10523 | iter.skip = 0; |
10524 | iter.count = 0; | |
10525 | iter.err = 0; | |
10526 | iter.fn = nf_tables_loop_check_setelem; | |
10527 | ||
10528 | set->ops->walk(ctx, set, &iter); | |
aaa31047 PNA |
10529 | if (!iter.err) |
10530 | iter.err = nft_set_catchall_loops(ctx, set); | |
10531 | ||
20a69341 PM |
10532 | if (iter.err < 0) |
10533 | return iter.err; | |
10534 | } | |
10535 | } | |
10536 | ||
10537 | return 0; | |
10538 | } | |
10539 | ||
36b701fa LGL |
10540 | /** |
10541 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
10542 | * | |
10543 | * @attr: netlink attribute to fetch value from | |
10544 | * @max: maximum value to be stored in dest | |
10545 | * @dest: pointer to the variable | |
10546 | * | |
10547 | * Parse, check and store a given u32 netlink attribute into variable. | |
10548 | * This function returns -ERANGE if the value goes over maximum value. | |
10549 | * Otherwise a 0 is returned and the attribute value is stored in the | |
10550 | * destination variable. | |
10551 | */ | |
f1d505bb | 10552 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 10553 | { |
09525a09 | 10554 | u32 val; |
36b701fa LGL |
10555 | |
10556 | val = ntohl(nla_get_be32(attr)); | |
10557 | if (val > max) | |
10558 | return -ERANGE; | |
10559 | ||
10560 | *dest = val; | |
10561 | return 0; | |
10562 | } | |
10563 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
10564 | ||
6c6f9f31 | 10565 | static int nft_parse_register(const struct nlattr *attr, u32 *preg) |
b1c96ed3 | 10566 | { |
49499c3e PM |
10567 | unsigned int reg; |
10568 | ||
10569 | reg = ntohl(nla_get_be32(attr)); | |
10570 | switch (reg) { | |
10571 | case NFT_REG_VERDICT...NFT_REG_4: | |
6e1acfa3 PNA |
10572 | *preg = reg * NFT_REG_SIZE / NFT_REG32_SIZE; |
10573 | break; | |
10574 | case NFT_REG32_00...NFT_REG32_15: | |
10575 | *preg = reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
10576 | break; | |
49499c3e | 10577 | default: |
6e1acfa3 | 10578 | return -ERANGE; |
49499c3e | 10579 | } |
6e1acfa3 PNA |
10580 | |
10581 | return 0; | |
b1c96ed3 | 10582 | } |
b1c96ed3 | 10583 | |
49499c3e PM |
10584 | /** |
10585 | * nft_dump_register - dump a register value to a netlink attribute | |
10586 | * | |
10587 | * @skb: socket buffer | |
10588 | * @attr: attribute number | |
10589 | * @reg: register number | |
10590 | * | |
10591 | * Construct a netlink attribute containing the register number. For | |
10592 | * compatibility reasons, register numbers being a multiple of 4 are | |
10593 | * translated to the corresponding 128 bit register numbers. | |
10594 | */ | |
b1c96ed3 PM |
10595 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
10596 | { | |
49499c3e PM |
10597 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
10598 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
10599 | else | |
10600 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
10601 | ||
b1c96ed3 PM |
10602 | return nla_put_be32(skb, attr, htonl(reg)); |
10603 | } | |
10604 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
10605 | ||
4f16d25c | 10606 | static int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 10607 | { |
49499c3e | 10608 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 10609 | return -EINVAL; |
d07db988 PM |
10610 | if (len == 0) |
10611 | return -EINVAL; | |
c593642c | 10612 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 10613 | return -ERANGE; |
49499c3e | 10614 | |
96518518 PM |
10615 | return 0; |
10616 | } | |
4f16d25c PNA |
10617 | |
10618 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len) | |
10619 | { | |
10620 | u32 reg; | |
10621 | int err; | |
10622 | ||
6e1acfa3 PNA |
10623 | err = nft_parse_register(attr, ®); |
10624 | if (err < 0) | |
10625 | return err; | |
10626 | ||
4f16d25c PNA |
10627 | err = nft_validate_register_load(reg, len); |
10628 | if (err < 0) | |
10629 | return err; | |
10630 | ||
10631 | *sreg = reg; | |
10632 | return 0; | |
10633 | } | |
10634 | EXPORT_SYMBOL_GPL(nft_parse_register_load); | |
96518518 | 10635 | |
345023b0 PNA |
10636 | static int nft_validate_register_store(const struct nft_ctx *ctx, |
10637 | enum nft_registers reg, | |
10638 | const struct nft_data *data, | |
10639 | enum nft_data_types type, | |
10640 | unsigned int len) | |
96518518 | 10641 | { |
20a69341 PM |
10642 | int err; |
10643 | ||
96518518 PM |
10644 | switch (reg) { |
10645 | case NFT_REG_VERDICT: | |
58f40ab6 | 10646 | if (type != NFT_DATA_VERDICT) |
96518518 | 10647 | return -EINVAL; |
20a69341 | 10648 | |
58f40ab6 | 10649 | if (data != NULL && |
1ca2e170 PM |
10650 | (data->verdict.code == NFT_GOTO || |
10651 | data->verdict.code == NFT_JUMP)) { | |
10652 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
10653 | if (err < 0) |
10654 | return err; | |
20a69341 PM |
10655 | } |
10656 | ||
96518518 PM |
10657 | return 0; |
10658 | default: | |
49499c3e | 10659 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 10660 | return -EINVAL; |
45d9bcda PM |
10661 | if (len == 0) |
10662 | return -EINVAL; | |
49499c3e | 10663 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 10664 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 10665 | return -ERANGE; |
27e6d201 | 10666 | |
96518518 PM |
10667 | if (data != NULL && type != NFT_DATA_VALUE) |
10668 | return -EINVAL; | |
10669 | return 0; | |
10670 | } | |
10671 | } | |
345023b0 PNA |
10672 | |
10673 | int nft_parse_register_store(const struct nft_ctx *ctx, | |
10674 | const struct nlattr *attr, u8 *dreg, | |
10675 | const struct nft_data *data, | |
10676 | enum nft_data_types type, unsigned int len) | |
10677 | { | |
10678 | int err; | |
10679 | u32 reg; | |
10680 | ||
6e1acfa3 PNA |
10681 | err = nft_parse_register(attr, ®); |
10682 | if (err < 0) | |
10683 | return err; | |
10684 | ||
345023b0 PNA |
10685 | err = nft_validate_register_store(ctx, reg, data, type, len); |
10686 | if (err < 0) | |
10687 | return err; | |
10688 | ||
10689 | *dreg = reg; | |
10690 | return 0; | |
10691 | } | |
10692 | EXPORT_SYMBOL_GPL(nft_parse_register_store); | |
96518518 PM |
10693 | |
10694 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
10695 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
10696 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
10697 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
51d70f18 | 10698 | [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
10699 | }; |
10700 | ||
10701 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
10702 | struct nft_data_desc *desc, const struct nlattr *nla) | |
10703 | { | |
664b0f8c | 10704 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
10705 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
10706 | struct nft_chain *chain; | |
10707 | int err; | |
10708 | ||
8cb08174 JB |
10709 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
10710 | nft_verdict_policy, NULL); | |
96518518 PM |
10711 | if (err < 0) |
10712 | return err; | |
10713 | ||
10714 | if (!tb[NFTA_VERDICT_CODE]) | |
10715 | return -EINVAL; | |
ddbd8be6 FW |
10716 | |
10717 | /* zero padding hole for memcmp */ | |
10718 | memset(data, 0, sizeof(*data)); | |
1ca2e170 | 10719 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 10720 | |
1ca2e170 | 10721 | switch (data->verdict.code) { |
e0abdadc | 10722 | default: |
1ca2e170 | 10723 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
10724 | case NF_ACCEPT: |
10725 | case NF_DROP: | |
10726 | case NF_QUEUE: | |
10727 | break; | |
10728 | default: | |
10729 | return -EINVAL; | |
10730 | } | |
954d8297 | 10731 | fallthrough; |
96518518 PM |
10732 | case NFT_CONTINUE: |
10733 | case NFT_BREAK: | |
10734 | case NFT_RETURN: | |
96518518 PM |
10735 | break; |
10736 | case NFT_JUMP: | |
10737 | case NFT_GOTO: | |
51d70f18 PNA |
10738 | if (tb[NFTA_VERDICT_CHAIN]) { |
10739 | chain = nft_chain_lookup(ctx->net, ctx->table, | |
10740 | tb[NFTA_VERDICT_CHAIN], | |
10741 | genmask); | |
10742 | } else if (tb[NFTA_VERDICT_CHAIN_ID]) { | |
95f466d2 | 10743 | chain = nft_chain_lookup_byid(ctx->net, ctx->table, |
515ad530 TLSC |
10744 | tb[NFTA_VERDICT_CHAIN_ID], |
10745 | genmask); | |
51d70f18 PNA |
10746 | if (IS_ERR(chain)) |
10747 | return PTR_ERR(chain); | |
10748 | } else { | |
96518518 | 10749 | return -EINVAL; |
51d70f18 PNA |
10750 | } |
10751 | ||
96518518 PM |
10752 | if (IS_ERR(chain)) |
10753 | return PTR_ERR(chain); | |
f323d954 | 10754 | if (nft_is_base_chain(chain)) |
96518518 | 10755 | return -EOPNOTSUPP; |
e02f0d39 PNA |
10756 | if (nft_chain_is_bound(chain)) |
10757 | return -EINVAL; | |
f323ef3a PNA |
10758 | if (desc->flags & NFT_DATA_DESC_SETELEM && |
10759 | chain->flags & NFT_CHAIN_BINDING) | |
10760 | return -EINVAL; | |
1689f259 PNA |
10761 | if (!nft_use_inc(&chain->use)) |
10762 | return -EMFILE; | |
96518518 | 10763 | |
1ca2e170 | 10764 | data->verdict.chain = chain; |
96518518 | 10765 | break; |
96518518 PM |
10766 | } |
10767 | ||
4c4ed074 | 10768 | desc->len = sizeof(data->verdict); |
341b6941 | 10769 | |
96518518 PM |
10770 | return 0; |
10771 | } | |
10772 | ||
10773 | static void nft_verdict_uninit(const struct nft_data *data) | |
10774 | { | |
d0e2c7de | 10775 | struct nft_chain *chain; |
d0e2c7de | 10776 | |
1ca2e170 | 10777 | switch (data->verdict.code) { |
96518518 PM |
10778 | case NFT_JUMP: |
10779 | case NFT_GOTO: | |
d0e2c7de | 10780 | chain = data->verdict.chain; |
1689f259 | 10781 | nft_use_dec(&chain->use); |
96518518 PM |
10782 | break; |
10783 | } | |
10784 | } | |
10785 | ||
33d5a7b1 | 10786 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
10787 | { |
10788 | struct nlattr *nest; | |
10789 | ||
ae0be8de | 10790 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
10791 | if (!nest) |
10792 | goto nla_put_failure; | |
10793 | ||
33d5a7b1 | 10794 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
10795 | goto nla_put_failure; |
10796 | ||
33d5a7b1 | 10797 | switch (v->code) { |
96518518 PM |
10798 | case NFT_JUMP: |
10799 | case NFT_GOTO: | |
1ca2e170 | 10800 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 10801 | v->chain->name)) |
96518518 PM |
10802 | goto nla_put_failure; |
10803 | } | |
10804 | nla_nest_end(skb, nest); | |
10805 | return 0; | |
10806 | ||
10807 | nla_put_failure: | |
10808 | return -1; | |
10809 | } | |
10810 | ||
d0a11fc3 | 10811 | static int nft_value_init(const struct nft_ctx *ctx, |
341b6941 PNA |
10812 | struct nft_data *data, struct nft_data_desc *desc, |
10813 | const struct nlattr *nla) | |
96518518 PM |
10814 | { |
10815 | unsigned int len; | |
10816 | ||
10817 | len = nla_len(nla); | |
10818 | if (len == 0) | |
10819 | return -EINVAL; | |
341b6941 | 10820 | if (len > desc->size) |
96518518 | 10821 | return -EOVERFLOW; |
341b6941 PNA |
10822 | if (desc->len) { |
10823 | if (len != desc->len) | |
10824 | return -EINVAL; | |
10825 | } else { | |
10826 | desc->len = len; | |
10827 | } | |
96518518 | 10828 | |
d0a11fc3 | 10829 | nla_memcpy(data->data, nla, len); |
341b6941 | 10830 | |
96518518 PM |
10831 | return 0; |
10832 | } | |
10833 | ||
10834 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
10835 | unsigned int len) | |
10836 | { | |
10837 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
10838 | } | |
10839 | ||
10840 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 10841 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
10842 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
10843 | }; | |
10844 | ||
10845 | /** | |
10846 | * nft_data_init - parse nf_tables data netlink attributes | |
10847 | * | |
10848 | * @ctx: context of the expression using the data | |
10849 | * @data: destination struct nft_data | |
10850 | * @desc: data description | |
10851 | * @nla: netlink attribute containing data | |
10852 | * | |
10853 | * Parse the netlink data attributes and initialize a struct nft_data. | |
10854 | * The type and length of data are returned in the data description. | |
10855 | * | |
10856 | * The caller can indicate that it only wants to accept data of type | |
10857 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
10858 | */ | |
341b6941 | 10859 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
96518518 PM |
10860 | struct nft_data_desc *desc, const struct nlattr *nla) |
10861 | { | |
10862 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
10863 | int err; | |
10864 | ||
341b6941 PNA |
10865 | if (WARN_ON_ONCE(!desc->size)) |
10866 | return -EINVAL; | |
10867 | ||
8cb08174 JB |
10868 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
10869 | nft_data_policy, NULL); | |
96518518 PM |
10870 | if (err < 0) |
10871 | return err; | |
10872 | ||
341b6941 PNA |
10873 | if (tb[NFTA_DATA_VALUE]) { |
10874 | if (desc->type != NFT_DATA_VALUE) | |
10875 | return -EINVAL; | |
10876 | ||
10877 | err = nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); | |
10878 | } else if (tb[NFTA_DATA_VERDICT] && ctx != NULL) { | |
10879 | if (desc->type != NFT_DATA_VERDICT) | |
10880 | return -EINVAL; | |
10881 | ||
10882 | err = nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
10883 | } else { | |
10884 | err = -EINVAL; | |
10885 | } | |
10886 | ||
10887 | return err; | |
96518518 PM |
10888 | } |
10889 | EXPORT_SYMBOL_GPL(nft_data_init); | |
10890 | ||
10891 | /** | |
59105446 | 10892 | * nft_data_release - release a nft_data item |
96518518 PM |
10893 | * |
10894 | * @data: struct nft_data to release | |
10895 | * @type: type of data | |
10896 | * | |
10897 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
10898 | * all others need to be released by calling this function. | |
10899 | */ | |
59105446 | 10900 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 10901 | { |
960bd2c2 | 10902 | if (type < NFT_DATA_VERDICT) |
96518518 | 10903 | return; |
960bd2c2 | 10904 | switch (type) { |
96518518 PM |
10905 | case NFT_DATA_VERDICT: |
10906 | return nft_verdict_uninit(data); | |
10907 | default: | |
10908 | WARN_ON(1); | |
10909 | } | |
10910 | } | |
59105446 | 10911 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
10912 | |
10913 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
10914 | enum nft_data_types type, unsigned int len) | |
10915 | { | |
10916 | struct nlattr *nest; | |
10917 | int err; | |
10918 | ||
ae0be8de | 10919 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
10920 | if (nest == NULL) |
10921 | return -1; | |
10922 | ||
10923 | switch (type) { | |
10924 | case NFT_DATA_VALUE: | |
10925 | err = nft_value_dump(skb, data, len); | |
10926 | break; | |
10927 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 10928 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
10929 | break; |
10930 | default: | |
10931 | err = -EINVAL; | |
10932 | WARN_ON(1); | |
10933 | } | |
10934 | ||
10935 | nla_nest_end(skb, nest); | |
10936 | return err; | |
10937 | } | |
10938 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
10939 | ||
5ebe0b0e PNA |
10940 | int __nft_release_basechain(struct nft_ctx *ctx) |
10941 | { | |
10942 | struct nft_rule *rule, *nr; | |
10943 | ||
fa5950e4 FW |
10944 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
10945 | return 0; | |
5ebe0b0e | 10946 | |
c974a3a3 | 10947 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
10948 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
10949 | list_del(&rule->list); | |
1689f259 | 10950 | nft_use_dec(&ctx->chain->use); |
bb7b40ae | 10951 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 10952 | } |
1b2470e5 | 10953 | nft_chain_del(ctx->chain); |
1689f259 | 10954 | nft_use_dec(&ctx->table->use); |
43a605f2 | 10955 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
10956 | |
10957 | return 0; | |
10958 | } | |
10959 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
10960 | ||
00dfe9be PNA |
10961 | static void __nft_release_hook(struct net *net, struct nft_table *table) |
10962 | { | |
6069da44 | 10963 | struct nft_flowtable *flowtable; |
00dfe9be PNA |
10964 | struct nft_chain *chain; |
10965 | ||
10966 | list_for_each_entry(chain, &table->chains, list) | |
f9a43007 | 10967 | __nf_tables_unregister_hook(net, table, chain, true); |
6069da44 | 10968 | list_for_each_entry(flowtable, &table->flowtables, list) |
f9a43007 PNA |
10969 | __nft_unregister_flowtable_net_hooks(net, &flowtable->hook_list, |
10970 | true); | |
00dfe9be PNA |
10971 | } |
10972 | ||
767d1216 PNA |
10973 | static void __nft_release_hooks(struct net *net) |
10974 | { | |
d59d2f82 | 10975 | struct nftables_pernet *nft_net = nft_pernet(net); |
767d1216 | 10976 | struct nft_table *table; |
767d1216 | 10977 | |
0854db2a | 10978 | list_for_each_entry(table, &nft_net->tables, list) { |
2888b080 PNA |
10979 | if (nft_table_has_owner(table)) |
10980 | continue; | |
10981 | ||
00dfe9be | 10982 | __nft_release_hook(net, table); |
2888b080 | 10983 | } |
767d1216 PNA |
10984 | } |
10985 | ||
fd020332 | 10986 | static void __nft_release_table(struct net *net, struct nft_table *table) |
df05ef87 | 10987 | { |
3b49e2e9 | 10988 | struct nft_flowtable *flowtable, *nf; |
df05ef87 | 10989 | struct nft_chain *chain, *nc; |
e5009240 | 10990 | struct nft_object *obj, *ne; |
df05ef87 PNA |
10991 | struct nft_rule *rule, *nr; |
10992 | struct nft_set *set, *ns; | |
10993 | struct nft_ctx ctx = { | |
10994 | .net = net, | |
43a605f2 | 10995 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
10996 | }; |
10997 | ||
fd020332 PNA |
10998 | ctx.family = table->family; |
10999 | ctx.table = table; | |
11000 | list_for_each_entry(chain, &table->chains, list) { | |
751d460c PNA |
11001 | if (nft_chain_is_bound(chain)) |
11002 | continue; | |
11003 | ||
fd020332 PNA |
11004 | ctx.chain = chain; |
11005 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
11006 | list_del(&rule->list); | |
1689f259 | 11007 | nft_use_dec(&chain->use); |
fd020332 | 11008 | nf_tables_rule_release(&ctx, rule); |
df05ef87 | 11009 | } |
df05ef87 | 11010 | } |
fd020332 PNA |
11011 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
11012 | list_del(&flowtable->list); | |
1689f259 | 11013 | nft_use_dec(&table->use); |
fd020332 PNA |
11014 | nf_tables_flowtable_destroy(flowtable); |
11015 | } | |
11016 | list_for_each_entry_safe(set, ns, &table->sets, list) { | |
11017 | list_del(&set->list); | |
1689f259 | 11018 | nft_use_dec(&table->use); |
628bd3e4 PNA |
11019 | if (set->flags & (NFT_SET_MAP | NFT_SET_OBJECT)) |
11020 | nft_map_deactivate(&ctx, set); | |
11021 | ||
fd020332 PNA |
11022 | nft_set_destroy(&ctx, set); |
11023 | } | |
11024 | list_for_each_entry_safe(obj, ne, &table->objects, list) { | |
11025 | nft_obj_del(obj); | |
1689f259 | 11026 | nft_use_dec(&table->use); |
fd020332 PNA |
11027 | nft_obj_destroy(&ctx, obj); |
11028 | } | |
11029 | list_for_each_entry_safe(chain, nc, &table->chains, list) { | |
11030 | ctx.chain = chain; | |
11031 | nft_chain_del(chain); | |
1689f259 | 11032 | nft_use_dec(&table->use); |
fd020332 PNA |
11033 | nf_tables_chain_destroy(&ctx); |
11034 | } | |
fd020332 PNA |
11035 | nf_tables_table_destroy(&ctx); |
11036 | } | |
11037 | ||
bd1777b3 | 11038 | static void __nft_release_tables(struct net *net) |
fd020332 | 11039 | { |
d59d2f82 | 11040 | struct nftables_pernet *nft_net = nft_pernet(net); |
fd020332 PNA |
11041 | struct nft_table *table, *nt; |
11042 | ||
0854db2a | 11043 | list_for_each_entry_safe(table, nt, &nft_net->tables, list) { |
bd1777b3 | 11044 | if (nft_table_has_owner(table)) |
6001a930 PNA |
11045 | continue; |
11046 | ||
a499b03b FW |
11047 | list_del(&table->list); |
11048 | ||
fd020332 | 11049 | __nft_release_table(net, table); |
6001a930 PNA |
11050 | } |
11051 | } | |
11052 | ||
11053 | static int nft_rcv_nl_event(struct notifier_block *this, unsigned long event, | |
11054 | void *ptr) | |
11055 | { | |
a499b03b | 11056 | struct nft_table *table, *to_delete[8]; |
0854db2a | 11057 | struct nftables_pernet *nft_net; |
6001a930 | 11058 | struct netlink_notify *n = ptr; |
6001a930 | 11059 | struct net *net = n->net; |
a499b03b FW |
11060 | unsigned int deleted; |
11061 | bool restart = false; | |
6a33d8b7 | 11062 | unsigned int gc_seq; |
6001a930 PNA |
11063 | |
11064 | if (event != NETLINK_URELEASE || n->protocol != NETLINK_NETFILTER) | |
11065 | return NOTIFY_DONE; | |
11066 | ||
d59d2f82 | 11067 | nft_net = nft_pernet(net); |
a499b03b | 11068 | deleted = 0; |
0854db2a | 11069 | mutex_lock(&nft_net->commit_mutex); |
6a33d8b7 PNA |
11070 | |
11071 | gc_seq = nft_gc_seq_begin(nft_net); | |
11072 | ||
d4bc8271 PNA |
11073 | if (!list_empty(&nf_tables_destroy_list)) |
11074 | rcu_barrier(); | |
a499b03b | 11075 | again: |
0854db2a | 11076 | list_for_each_entry(table, &nft_net->tables, list) { |
6001a930 PNA |
11077 | if (nft_table_has_owner(table) && |
11078 | n->portid == table->nlpid) { | |
11079 | __nft_release_hook(net, table); | |
a499b03b FW |
11080 | list_del_rcu(&table->list); |
11081 | to_delete[deleted++] = table; | |
11082 | if (deleted >= ARRAY_SIZE(to_delete)) | |
11083 | break; | |
6001a930 PNA |
11084 | } |
11085 | } | |
a499b03b FW |
11086 | if (deleted) { |
11087 | restart = deleted >= ARRAY_SIZE(to_delete); | |
6001a930 | 11088 | synchronize_rcu(); |
a499b03b FW |
11089 | while (deleted) |
11090 | __nft_release_table(net, to_delete[--deleted]); | |
11091 | ||
11092 | if (restart) | |
11093 | goto again; | |
6001a930 | 11094 | } |
6a33d8b7 PNA |
11095 | nft_gc_seq_end(nft_net, gc_seq); |
11096 | ||
0854db2a | 11097 | mutex_unlock(&nft_net->commit_mutex); |
6001a930 PNA |
11098 | |
11099 | return NOTIFY_DONE; | |
df05ef87 PNA |
11100 | } |
11101 | ||
6001a930 PNA |
11102 | static struct notifier_block nft_nl_notifier = { |
11103 | .notifier_call = nft_rcv_nl_event, | |
11104 | }; | |
11105 | ||
dd4cbef7 PNA |
11106 | static int __net_init nf_tables_init_net(struct net *net) |
11107 | { | |
d59d2f82 | 11108 | struct nftables_pernet *nft_net = nft_pernet(net); |
0854db2a FW |
11109 | |
11110 | INIT_LIST_HEAD(&nft_net->tables); | |
11111 | INIT_LIST_HEAD(&nft_net->commit_list); | |
938154b9 | 11112 | INIT_LIST_HEAD(&nft_net->binding_list); |
0854db2a FW |
11113 | INIT_LIST_HEAD(&nft_net->module_list); |
11114 | INIT_LIST_HEAD(&nft_net->notify_list); | |
11115 | mutex_init(&nft_net->commit_mutex); | |
11116 | nft_net->base_seq = 1; | |
5f68718b | 11117 | nft_net->gc_seq = 0; |
a654de8f | 11118 | |
dd4cbef7 PNA |
11119 | return 0; |
11120 | } | |
11121 | ||
767d1216 PNA |
11122 | static void __net_exit nf_tables_pre_exit_net(struct net *net) |
11123 | { | |
3923b1e4 PNA |
11124 | struct nftables_pernet *nft_net = nft_pernet(net); |
11125 | ||
11126 | mutex_lock(&nft_net->commit_mutex); | |
767d1216 | 11127 | __nft_release_hooks(net); |
3923b1e4 | 11128 | mutex_unlock(&nft_net->commit_mutex); |
767d1216 PNA |
11129 | } |
11130 | ||
dd4cbef7 PNA |
11131 | static void __net_exit nf_tables_exit_net(struct net *net) |
11132 | { | |
d59d2f82 | 11133 | struct nftables_pernet *nft_net = nft_pernet(net); |
6a33d8b7 | 11134 | unsigned int gc_seq; |
0854db2a FW |
11135 | |
11136 | mutex_lock(&nft_net->commit_mutex); | |
6a33d8b7 PNA |
11137 | |
11138 | gc_seq = nft_gc_seq_begin(nft_net); | |
11139 | ||
03c1f1ef SY |
11140 | if (!list_empty(&nft_net->commit_list) || |
11141 | !list_empty(&nft_net->module_list)) | |
c0391b6a | 11142 | __nf_tables_abort(net, NFNL_ABORT_NONE); |
6a33d8b7 | 11143 | |
bd1777b3 | 11144 | __nft_release_tables(net); |
6a33d8b7 PNA |
11145 | |
11146 | nft_gc_seq_end(nft_net, gc_seq); | |
11147 | ||
0854db2a FW |
11148 | mutex_unlock(&nft_net->commit_mutex); |
11149 | WARN_ON_ONCE(!list_empty(&nft_net->tables)); | |
11150 | WARN_ON_ONCE(!list_empty(&nft_net->module_list)); | |
11151 | WARN_ON_ONCE(!list_empty(&nft_net->notify_list)); | |
dd4cbef7 PNA |
11152 | } |
11153 | ||
5f68718b PNA |
11154 | static void nf_tables_exit_batch(struct list_head *net_exit_list) |
11155 | { | |
11156 | flush_work(&trans_gc_work); | |
11157 | } | |
11158 | ||
99633ab2 | 11159 | static struct pernet_operations nf_tables_net_ops = { |
767d1216 PNA |
11160 | .init = nf_tables_init_net, |
11161 | .pre_exit = nf_tables_pre_exit_net, | |
11162 | .exit = nf_tables_exit_net, | |
5f68718b | 11163 | .exit_batch = nf_tables_exit_batch, |
0854db2a FW |
11164 | .id = &nf_tables_net_id, |
11165 | .size = sizeof(struct nftables_pernet), | |
99633ab2 PNA |
11166 | }; |
11167 | ||
96518518 PM |
11168 | static int __init nf_tables_module_init(void) |
11169 | { | |
11170 | int err; | |
11171 | ||
d209df3e FW |
11172 | err = register_pernet_subsys(&nf_tables_net_ops); |
11173 | if (err < 0) | |
11174 | return err; | |
11175 | ||
11176 | err = nft_chain_filter_init(); | |
11177 | if (err < 0) | |
6001a930 | 11178 | goto err_chain_filter; |
02c7b25e | 11179 | |
96518518 PM |
11180 | err = nf_tables_core_module_init(); |
11181 | if (err < 0) | |
6001a930 | 11182 | goto err_core_module; |
96518518 | 11183 | |
d209df3e | 11184 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 11185 | if (err < 0) |
6001a930 | 11186 | goto err_netdev_notifier; |
96518518 | 11187 | |
4d44175a FW |
11188 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
11189 | if (err < 0) | |
6001a930 | 11190 | goto err_rht_objname; |
4d44175a | 11191 | |
06d392cb | 11192 | err = nft_offload_init(); |
11193 | if (err < 0) | |
6001a930 PNA |
11194 | goto err_offload; |
11195 | ||
11196 | err = netlink_register_notifier(&nft_nl_notifier); | |
11197 | if (err < 0) | |
11198 | goto err_netlink_notifier; | |
06d392cb | 11199 | |
d209df3e FW |
11200 | /* must be last */ |
11201 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
11202 | if (err < 0) | |
6001a930 | 11203 | goto err_nfnl_subsys; |
3b49e2e9 | 11204 | |
c1deb065 | 11205 | nft_chain_route_init(); |
3474a2c6 | 11206 | |
d209df3e | 11207 | return err; |
6001a930 PNA |
11208 | |
11209 | err_nfnl_subsys: | |
11210 | netlink_unregister_notifier(&nft_nl_notifier); | |
11211 | err_netlink_notifier: | |
06d392cb | 11212 | nft_offload_exit(); |
6001a930 | 11213 | err_offload: |
4d44175a | 11214 | rhltable_destroy(&nft_objname_ht); |
6001a930 | 11215 | err_rht_objname: |
d209df3e | 11216 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
6001a930 | 11217 | err_netdev_notifier: |
96518518 | 11218 | nf_tables_core_module_exit(); |
6001a930 | 11219 | err_core_module: |
d209df3e | 11220 | nft_chain_filter_fini(); |
6001a930 | 11221 | err_chain_filter: |
d209df3e | 11222 | unregister_pernet_subsys(&nf_tables_net_ops); |
96518518 PM |
11223 | return err; |
11224 | } | |
11225 | ||
11226 | static void __exit nf_tables_module_exit(void) | |
11227 | { | |
11228 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
6001a930 | 11229 | netlink_unregister_notifier(&nft_nl_notifier); |
06d392cb | 11230 | nft_offload_exit(); |
3b49e2e9 | 11231 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 11232 | nft_chain_filter_fini(); |
c1deb065 | 11233 | nft_chain_route_fini(); |
71ad00c5 | 11234 | unregister_pernet_subsys(&nf_tables_net_ops); |
5f68718b | 11235 | cancel_work_sync(&trans_gc_work); |
0935d558 | 11236 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 11237 | rcu_barrier(); |
4d44175a | 11238 | rhltable_destroy(&nft_objname_ht); |
96518518 | 11239 | nf_tables_core_module_exit(); |
96518518 PM |
11240 | } |
11241 | ||
11242 | module_init(nf_tables_module_init); | |
11243 | module_exit(nf_tables_module_exit); | |
11244 | ||
11245 | MODULE_LICENSE("GPL"); | |
11246 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
11247 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |