]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nf_tables_api.c
netfilter: nft_dynset: report EOPNOTSUPP on missing set feature
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nf_tables_api.c
CommitLineData
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
96518518 28static LIST_HEAD(nf_tables_expressions);
e5009240 29static LIST_HEAD(nf_tables_objects);
3b49e2e9 30static LIST_HEAD(nf_tables_flowtables);
0935d558
FW
31static LIST_HEAD(nf_tables_destroy_list);
32static DEFINE_SPINLOCK(nf_tables_destroy_list_lock);
3ecbfd65 33static u64 table_handle;
96518518 34
a654de8f
PNA
35enum {
36 NFT_VALIDATE_SKIP = 0,
37 NFT_VALIDATE_NEED,
38 NFT_VALIDATE_DO,
39};
40
4d44175a
FW
41static struct rhltable nft_objname_ht;
42
1b2470e5
FW
43static u32 nft_chain_hash(const void *data, u32 len, u32 seed);
44static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed);
45static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *);
46
4d44175a
FW
47static u32 nft_objname_hash(const void *data, u32 len, u32 seed);
48static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed);
49static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *);
50
1b2470e5
FW
51static const struct rhashtable_params nft_chain_ht_params = {
52 .head_offset = offsetof(struct nft_chain, rhlhead),
53 .key_offset = offsetof(struct nft_chain, name),
54 .hashfn = nft_chain_hash,
55 .obj_hashfn = nft_chain_hash_obj,
56 .obj_cmpfn = nft_chain_hash_cmp,
1b2470e5
FW
57 .automatic_shrinking = true,
58};
59
4d44175a
FW
60static const struct rhashtable_params nft_objname_ht_params = {
61 .head_offset = offsetof(struct nft_object, rhlhead),
62 .key_offset = offsetof(struct nft_object, key),
63 .hashfn = nft_objname_hash,
64 .obj_hashfn = nft_objname_hash_obj,
65 .obj_cmpfn = nft_objname_hash_cmp,
66 .automatic_shrinking = true,
67};
68
a654de8f
PNA
69static void nft_validate_state_update(struct net *net, u8 new_validate_state)
70{
71 switch (net->nft.validate_state) {
72 case NFT_VALIDATE_SKIP:
73 WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO);
74 break;
75 case NFT_VALIDATE_NEED:
76 break;
77 case NFT_VALIDATE_DO:
78 if (new_validate_state == NFT_VALIDATE_NEED)
79 return;
80 }
81
82 net->nft.validate_state = new_validate_state;
83}
0935d558
FW
84static void nf_tables_trans_destroy_work(struct work_struct *w);
85static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
a654de8f 86
7c95f6d8 87static void nft_ctx_init(struct nft_ctx *ctx,
633c9a84 88 struct net *net,
7c95f6d8
PNA
89 const struct sk_buff *skb,
90 const struct nlmsghdr *nlh,
36596dad 91 u8 family,
7c95f6d8
PNA
92 struct nft_table *table,
93 struct nft_chain *chain,
94 const struct nlattr * const *nla)
95{
633c9a84 96 ctx->net = net;
36596dad 97 ctx->family = family;
26b2f552 98 ctx->level = 0;
128ad332
PNA
99 ctx->table = table;
100 ctx->chain = chain;
101 ctx->nla = nla;
102 ctx->portid = NETLINK_CB(skb).portid;
103 ctx->report = nlmsg_report(nlh);
c9626a2c 104 ctx->flags = nlh->nlmsg_flags;
128ad332 105 ctx->seq = nlh->nlmsg_seq;
7c95f6d8
PNA
106}
107
8411b644
PNA
108static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
109 int msg_type, u32 size, gfp_t gfp)
1081d11b
PNA
110{
111 struct nft_trans *trans;
112
8411b644 113 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
1081d11b
PNA
114 if (trans == NULL)
115 return NULL;
116
b380e5c7 117 trans->msg_type = msg_type;
1081d11b
PNA
118 trans->ctx = *ctx;
119
120 return trans;
121}
122
8411b644
PNA
123static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
124 int msg_type, u32 size)
125{
126 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
127}
128
1081d11b
PNA
129static void nft_trans_destroy(struct nft_trans *trans)
130{
131 list_del(&trans->list);
132 kfree(trans);
133}
134
f6ac8585
PNA
135static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set)
136{
137 struct net *net = ctx->net;
138 struct nft_trans *trans;
139
140 if (!nft_set_is_anonymous(set))
141 return;
142
143 list_for_each_entry_reverse(trans, &net->nft.commit_list, list) {
6a0a8d10
PNA
144 switch (trans->msg_type) {
145 case NFT_MSG_NEWSET:
146 if (nft_trans_set(trans) == set)
147 nft_trans_set_bound(trans) = true;
148 break;
149 case NFT_MSG_NEWSETELEM:
150 if (nft_trans_elem_set(trans) == set)
151 nft_trans_elem_set_bound(trans) = true;
f6ac8585
PNA
152 break;
153 }
154 }
155}
156
d54725cd
PNA
157static int nft_netdev_register_hooks(struct net *net,
158 struct list_head *hook_list)
159{
160 struct nft_hook *hook;
161 int err, j;
162
163 j = 0;
164 list_for_each_entry(hook, hook_list, list) {
165 err = nf_register_net_hook(net, &hook->ops);
166 if (err < 0)
167 goto err_register;
168
169 j++;
170 }
171 return 0;
172
173err_register:
174 list_for_each_entry(hook, hook_list, list) {
175 if (j-- <= 0)
176 break;
177
178 nf_unregister_net_hook(net, &hook->ops);
179 }
180 return err;
181}
182
183static void nft_netdev_unregister_hooks(struct net *net,
184 struct list_head *hook_list)
185{
186 struct nft_hook *hook;
187
188 list_for_each_entry(hook, hook_list, list)
189 nf_unregister_net_hook(net, &hook->ops);
190}
191
c974a3a3
PNA
192static int nf_tables_register_hook(struct net *net,
193 const struct nft_table *table,
194 struct nft_chain *chain)
d8ee8f7c 195{
d54725cd 196 struct nft_base_chain *basechain;
a37061a6 197 const struct nf_hook_ops *ops;
ae6153b5 198
d8ee8f7c 199 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 200 !nft_is_base_chain(chain))
d8ee8f7c
PNA
201 return 0;
202
4e25ceb8
FW
203 basechain = nft_base_chain(chain);
204 ops = &basechain->ops;
ae6153b5 205
4e25ceb8
FW
206 if (basechain->type->ops_register)
207 return basechain->type->ops_register(net, ops);
208
d3519cb8 209 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
1e9451cb
FW
210 return nft_netdev_register_hooks(net, &basechain->hook_list);
211
212 return nf_register_net_hook(net, &basechain->ops);
d8ee8f7c
PNA
213}
214
c974a3a3
PNA
215static void nf_tables_unregister_hook(struct net *net,
216 const struct nft_table *table,
217 struct nft_chain *chain)
c5598794 218{
d54725cd 219 struct nft_base_chain *basechain;
4e25ceb8
FW
220 const struct nf_hook_ops *ops;
221
d8ee8f7c 222 if (table->flags & NFT_TABLE_F_DORMANT ||
f323d954 223 !nft_is_base_chain(chain))
d8ee8f7c 224 return;
4e25ceb8
FW
225 basechain = nft_base_chain(chain);
226 ops = &basechain->ops;
d8ee8f7c 227
4e25ceb8
FW
228 if (basechain->type->ops_unregister)
229 return basechain->type->ops_unregister(net, ops);
230
d3519cb8 231 if (nft_base_chain_netdev(table->family, basechain->ops.hooknum))
1e9451cb
FW
232 nft_netdev_unregister_hooks(net, &basechain->hook_list);
233 else
234 nf_unregister_net_hook(net, &basechain->ops);
c5598794
AB
235}
236
ee01d542
AB
237static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
238{
239 struct nft_trans *trans;
240
241 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
242 if (trans == NULL)
243 return -ENOMEM;
244
245 if (msg_type == NFT_MSG_NEWTABLE)
f2a6d766 246 nft_activate_next(ctx->net, ctx->table);
ee01d542
AB
247
248 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
249 return 0;
250}
251
252static int nft_deltable(struct nft_ctx *ctx)
253{
254 int err;
255
256 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
257 if (err < 0)
258 return err;
259
f2a6d766 260 nft_deactivate_next(ctx->net, ctx->table);
ee01d542
AB
261 return err;
262}
263
66293c46 264static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
ee01d542
AB
265{
266 struct nft_trans *trans;
267
268 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
269 if (trans == NULL)
66293c46 270 return ERR_PTR(-ENOMEM);
ee01d542 271
74cccc3d 272 if (msg_type == NFT_MSG_NEWCHAIN) {
664b0f8c 273 nft_activate_next(ctx->net, ctx->chain);
ee01d542 274
74cccc3d
PNA
275 if (ctx->nla[NFTA_CHAIN_ID]) {
276 nft_trans_chain_id(trans) =
277 ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID]));
278 }
279 }
280
ee01d542 281 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
66293c46 282 return trans;
ee01d542
AB
283}
284
285static int nft_delchain(struct nft_ctx *ctx)
286{
66293c46 287 struct nft_trans *trans;
ee01d542 288
66293c46
FW
289 trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
290 if (IS_ERR(trans))
291 return PTR_ERR(trans);
ee01d542
AB
292
293 ctx->table->use--;
664b0f8c 294 nft_deactivate_next(ctx->net, ctx->chain);
ee01d542 295
66293c46 296 return 0;
ee01d542
AB
297}
298
bb7b40ae
PNA
299static void nft_rule_expr_activate(const struct nft_ctx *ctx,
300 struct nft_rule *rule)
301{
302 struct nft_expr *expr;
303
304 expr = nft_expr_first(rule);
31cc578a 305 while (nft_expr_more(rule, expr)) {
bb7b40ae
PNA
306 if (expr->ops->activate)
307 expr->ops->activate(ctx, expr);
308
309 expr = nft_expr_next(expr);
310 }
311}
312
313static void nft_rule_expr_deactivate(const struct nft_ctx *ctx,
f6ac8585
PNA
314 struct nft_rule *rule,
315 enum nft_trans_phase phase)
bb7b40ae
PNA
316{
317 struct nft_expr *expr;
318
319 expr = nft_expr_first(rule);
31cc578a 320 while (nft_expr_more(rule, expr)) {
bb7b40ae 321 if (expr->ops->deactivate)
f6ac8585 322 expr->ops->deactivate(ctx, expr, phase);
bb7b40ae
PNA
323
324 expr = nft_expr_next(expr);
325 }
326}
327
ee01d542
AB
328static int
329nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
330{
331 /* You cannot delete the same rule twice */
889f7ee7
PNA
332 if (nft_is_active_next(ctx->net, rule)) {
333 nft_deactivate_next(ctx->net, rule);
ee01d542
AB
334 ctx->chain->use--;
335 return 0;
336 }
337 return -ENOENT;
338}
339
340static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
341 struct nft_rule *rule)
342{
343 struct nft_trans *trans;
344
345 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
346 if (trans == NULL)
347 return NULL;
348
1a94e38d
PNA
349 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
350 nft_trans_rule_id(trans) =
351 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
352 }
ee01d542
AB
353 nft_trans_rule(trans) = rule;
354 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
355
356 return trans;
357}
358
359static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
360{
63b48c73 361 struct nft_flow_rule *flow;
ee01d542
AB
362 struct nft_trans *trans;
363 int err;
364
365 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
366 if (trans == NULL)
367 return -ENOMEM;
368
63b48c73
PNA
369 if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) {
370 flow = nft_flow_rule_create(ctx->net, rule);
371 if (IS_ERR(flow)) {
372 nft_trans_destroy(trans);
373 return PTR_ERR(flow);
374 }
375
376 nft_trans_flow_rule(trans) = flow;
377 }
378
ee01d542
AB
379 err = nf_tables_delrule_deactivate(ctx, rule);
380 if (err < 0) {
381 nft_trans_destroy(trans);
382 return err;
383 }
f6ac8585 384 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE);
ee01d542
AB
385
386 return 0;
387}
388
389static int nft_delrule_by_chain(struct nft_ctx *ctx)
390{
391 struct nft_rule *rule;
392 int err;
393
394 list_for_each_entry(rule, &ctx->chain->rules, list) {
23b7ca4f
PNA
395 if (!nft_is_active_next(ctx->net, rule))
396 continue;
397
ee01d542
AB
398 err = nft_delrule(ctx, rule);
399 if (err < 0)
400 return err;
401 }
402 return 0;
403}
404
cd5125d8 405static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type,
ee01d542
AB
406 struct nft_set *set)
407{
408 struct nft_trans *trans;
409
410 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
411 if (trans == NULL)
412 return -ENOMEM;
413
414 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
415 nft_trans_set_id(trans) =
416 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
37a9cc52 417 nft_activate_next(ctx->net, set);
ee01d542
AB
418 }
419 nft_trans_set(trans) = set;
420 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
421
422 return 0;
423}
424
cd5125d8 425static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set)
ee01d542
AB
426{
427 int err;
428
429 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
430 if (err < 0)
431 return err;
432
37a9cc52 433 nft_deactivate_next(ctx->net, set);
ee01d542
AB
434 ctx->table->use--;
435
436 return err;
437}
438
e5009240
PNA
439static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
440 struct nft_object *obj)
441{
442 struct nft_trans *trans;
443
444 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
445 if (trans == NULL)
446 return -ENOMEM;
447
448 if (msg_type == NFT_MSG_NEWOBJ)
449 nft_activate_next(ctx->net, obj);
450
451 nft_trans_obj(trans) = obj;
452 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
453
454 return 0;
455}
456
457static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
458{
459 int err;
460
461 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
462 if (err < 0)
463 return err;
464
465 nft_deactivate_next(ctx->net, obj);
466 ctx->table->use--;
467
468 return err;
469}
470
3b49e2e9
PNA
471static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type,
472 struct nft_flowtable *flowtable)
473{
474 struct nft_trans *trans;
475
476 trans = nft_trans_alloc(ctx, msg_type,
477 sizeof(struct nft_trans_flowtable));
478 if (trans == NULL)
479 return -ENOMEM;
480
481 if (msg_type == NFT_MSG_NEWFLOWTABLE)
482 nft_activate_next(ctx->net, flowtable);
483
484 nft_trans_flowtable(trans) = flowtable;
485 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
486
487 return 0;
488}
489
490static int nft_delflowtable(struct nft_ctx *ctx,
491 struct nft_flowtable *flowtable)
492{
493 int err;
494
495 err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable);
496 if (err < 0)
497 return err;
498
499 nft_deactivate_next(ctx->net, flowtable);
500 ctx->table->use--;
501
502 return err;
503}
504
96518518
PM
505/*
506 * Tables
507 */
508
36596dad 509static struct nft_table *nft_table_lookup(const struct net *net,
f2a6d766 510 const struct nlattr *nla,
36596dad 511 u8 family, u8 genmask)
96518518
PM
512{
513 struct nft_table *table;
514
cac20fcd
PNA
515 if (nla == NULL)
516 return ERR_PTR(-EINVAL);
517
0a6a9515
QC
518 list_for_each_entry_rcu(table, &net->nft.tables, list,
519 lockdep_is_held(&net->nft.commit_mutex)) {
f2a6d766 520 if (!nla_strcmp(nla, table->name) &&
98319cb9 521 table->family == family &&
f2a6d766 522 nft_active_genmask(table, genmask))
96518518
PM
523 return table;
524 }
cac20fcd
PNA
525
526 return ERR_PTR(-ENOENT);
96518518
PM
527}
528
3ecbfd65
HS
529static struct nft_table *nft_table_lookup_byhandle(const struct net *net,
530 const struct nlattr *nla,
531 u8 genmask)
532{
533 struct nft_table *table;
534
535 list_for_each_entry(table, &net->nft.tables, list) {
536 if (be64_to_cpu(nla_get_be64(nla)) == table->handle &&
537 nft_active_genmask(table, genmask))
538 return table;
539 }
3ecbfd65
HS
540
541 return ERR_PTR(-ENOENT);
542}
543
96518518
PM
544static inline u64 nf_tables_alloc_handle(struct nft_table *table)
545{
546 return ++table->hgenerator;
547}
548
32537e91 549static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX];
9370761c 550
82603549
PNA
551static const struct nft_chain_type *
552__nft_chain_type_get(u8 family, enum nft_chain_types type)
553{
554 if (family >= NFPROTO_NUMPROTO ||
555 type >= NFT_CHAIN_T_MAX)
556 return NULL;
557
558 return chain_type[family][type];
559}
560
32537e91 561static const struct nft_chain_type *
1ea26cca 562__nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family)
9370761c 563{
82603549 564 const struct nft_chain_type *type;
9370761c
PNA
565 int i;
566
baae3e62 567 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
82603549
PNA
568 type = __nft_chain_type_get(family, i);
569 if (!type)
570 continue;
571 if (!nla_strcmp(nla, type->name))
572 return type;
9370761c 573 }
baae3e62 574 return NULL;
9370761c
PNA
575}
576
eb014de4
PNA
577struct nft_module_request {
578 struct list_head list;
579 char module[MODULE_NAME_LEN];
580 bool done;
581};
582
452238e8 583#ifdef CONFIG_MODULES
35b7ee34
AL
584static __printf(2, 3) int nft_request_module(struct net *net, const char *fmt,
585 ...)
452238e8
FW
586{
587 char module_name[MODULE_NAME_LEN];
eb014de4 588 struct nft_module_request *req;
452238e8
FW
589 va_list args;
590 int ret;
591
452238e8
FW
592 va_start(args, fmt);
593 ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
594 va_end(args);
9332d27d 595 if (ret >= MODULE_NAME_LEN)
eb014de4 596 return 0;
452238e8 597
eb014de4
PNA
598 list_for_each_entry(req, &net->nft.module_list, list) {
599 if (!strcmp(req->module, module_name)) {
600 if (req->done)
601 return 0;
ec7470b8 602
eb014de4
PNA
603 /* A request to load this module already exists. */
604 return -EAGAIN;
605 }
606 }
607
608 req = kmalloc(sizeof(*req), GFP_KERNEL);
609 if (!req)
610 return -ENOMEM;
611
612 req->done = false;
613 strlcpy(req->module, module_name, MODULE_NAME_LEN);
614 list_add_tail(&req->list, &net->nft.module_list);
615
616 return -EAGAIN;
452238e8
FW
617}
618#endif
619
f102d66b
FW
620static void lockdep_nfnl_nft_mutex_not_held(void)
621{
622#ifdef CONFIG_PROVE_LOCKING
c0700dfa
FW
623 if (debug_locks)
624 WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES));
f102d66b
FW
625#endif
626}
627
32537e91 628static const struct nft_chain_type *
452238e8
FW
629nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla,
630 u8 family, bool autoload)
9370761c 631{
32537e91 632 const struct nft_chain_type *type;
9370761c 633
1ea26cca 634 type = __nf_tables_chain_type_lookup(nla, family);
93b0806f
PM
635 if (type != NULL)
636 return type;
f102d66b
FW
637
638 lockdep_nfnl_nft_mutex_not_held();
9370761c 639#ifdef CONFIG_MODULES
93b0806f 640 if (autoload) {
eb014de4
PNA
641 if (nft_request_module(net, "nft-chain-%u-%.*s", family,
642 nla_len(nla),
643 (const char *)nla_data(nla)) == -EAGAIN)
93b0806f 644 return ERR_PTR(-EAGAIN);
9370761c
PNA
645 }
646#endif
93b0806f 647 return ERR_PTR(-ENOENT);
9370761c
PNA
648}
649
96518518 650static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
1cae565e
PNA
651 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
652 .len = NFT_TABLE_MAXNAMELEN - 1 },
9ddf6323 653 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
3ecbfd65 654 [NFTA_TABLE_HANDLE] = { .type = NLA_U64 },
7a81575b
JGG
655 [NFTA_TABLE_USERDATA] = { .type = NLA_BINARY,
656 .len = NFT_USERDATA_MAXLEN }
96518518
PM
657};
658
84d7fce6
PNA
659static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
660 u32 portid, u32 seq, int event, u32 flags,
661 int family, const struct nft_table *table)
96518518
PM
662{
663 struct nlmsghdr *nlh;
664 struct nfgenmsg *nfmsg;
665
dedb67c4 666 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518
PM
667 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
668 if (nlh == NULL)
669 goto nla_put_failure;
670
671 nfmsg = nlmsg_data(nlh);
672 nfmsg->nfgen_family = family;
673 nfmsg->version = NFNETLINK_V0;
84d7fce6 674 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518 675
9ddf6323 676 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
d8bcc768 677 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
3ecbfd65
HS
678 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) ||
679 nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle),
680 NFTA_TABLE_PAD))
96518518
PM
681 goto nla_put_failure;
682
7a81575b
JGG
683 if (table->udata) {
684 if (nla_put(skb, NFTA_TABLE_USERDATA, table->udlen, table->udata))
685 goto nla_put_failure;
686 }
687
053c095a
JB
688 nlmsg_end(skb, nlh);
689 return 0;
96518518
PM
690
691nla_put_failure:
692 nlmsg_trim(skb, nlh);
693 return -1;
694}
695
67cc570e
PNA
696struct nftnl_skb_parms {
697 bool report;
698};
699#define NFT_CB(skb) (*(struct nftnl_skb_parms*)&((skb)->cb))
700
701static void nft_notify_enqueue(struct sk_buff *skb, bool report,
702 struct list_head *notify_list)
703{
704 NFT_CB(skb).report = report;
705 list_add_tail(&skb->list, notify_list);
706}
707
25e94a99 708static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
96518518
PM
709{
710 struct sk_buff *skb;
96518518 711 int err;
8e6cf365
RGB
712 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;?:0",
713 ctx->table->name, ctx->table->handle);
714
715 audit_log_nfcfg(buf,
716 ctx->family,
717 ctx->table->use,
718 event == NFT_MSG_NEWTABLE ?
719 AUDIT_NFT_OP_TABLE_REGISTER :
14224039
RGB
720 AUDIT_NFT_OP_TABLE_UNREGISTER,
721 GFP_KERNEL);
8e6cf365 722 kfree(buf);
96518518 723
128ad332
PNA
724 if (!ctx->report &&
725 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 726 return;
96518518 727
96518518
PM
728 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
729 if (skb == NULL)
730 goto err;
731
84d7fce6 732 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 733 event, 0, ctx->family, ctx->table);
96518518
PM
734 if (err < 0) {
735 kfree_skb(skb);
736 goto err;
737 }
738
67cc570e 739 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 740 return;
96518518 741err:
25e94a99 742 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
743}
744
745static int nf_tables_dump_tables(struct sk_buff *skb,
746 struct netlink_callback *cb)
747{
748 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
96518518
PM
749 const struct nft_table *table;
750 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 751 struct net *net = sock_net(skb->sk);
96518518
PM
752 int family = nfmsg->nfgen_family;
753
e688a7f8 754 rcu_read_lock();
38e029f1
PNA
755 cb->seq = net->nft.base_seq;
756
36596dad 757 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 758 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
759 continue;
760
36596dad
PNA
761 if (idx < s_idx)
762 goto cont;
763 if (idx > s_idx)
764 memset(&cb->args[1], 0,
765 sizeof(cb->args) - sizeof(cb->args[0]));
766 if (!nft_is_active(net, table))
767 continue;
768 if (nf_tables_fill_table_info(skb, net,
769 NETLINK_CB(cb->skb).portid,
770 cb->nlh->nlmsg_seq,
771 NFT_MSG_NEWTABLE, NLM_F_MULTI,
98319cb9 772 table->family, table) < 0)
36596dad
PNA
773 goto done;
774
775 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 776cont:
36596dad 777 idx++;
96518518
PM
778 }
779done:
e688a7f8 780 rcu_read_unlock();
96518518
PM
781 cb->args[0] = idx;
782 return skb->len;
783}
784
d9adf22a
FW
785static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb,
786 const struct nlmsghdr *nlh,
787 struct netlink_dump_control *c)
788{
789 int err;
790
791 if (!try_module_get(THIS_MODULE))
792 return -EINVAL;
793
794 rcu_read_unlock();
795 err = netlink_dump_start(nlsk, skb, nlh, c);
796 rcu_read_lock();
797 module_put(THIS_MODULE);
798
799 return err;
800}
801
802/* called with rcu_read_lock held */
7b8002a1
PNA
803static int nf_tables_gettable(struct net *net, struct sock *nlsk,
804 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
805 const struct nlattr * const nla[],
806 struct netlink_ext_ack *extack)
96518518
PM
807{
808 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 809 u8 genmask = nft_genmask_cur(net);
96518518
PM
810 const struct nft_table *table;
811 struct sk_buff *skb2;
812 int family = nfmsg->nfgen_family;
813 int err;
814
815 if (nlh->nlmsg_flags & NLM_F_DUMP) {
816 struct netlink_dump_control c = {
817 .dump = nf_tables_dump_tables,
d9adf22a 818 .module = THIS_MODULE,
96518518 819 };
d9adf22a
FW
820
821 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
822 }
823
cac20fcd 824 table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask);
36dd1bcc
PNA
825 if (IS_ERR(table)) {
826 NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]);
96518518 827 return PTR_ERR(table);
36dd1bcc 828 }
96518518 829
d9adf22a 830 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
831 if (!skb2)
832 return -ENOMEM;
833
84d7fce6 834 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
835 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
836 family, table);
837 if (err < 0)
ee921183 838 goto err_fill_table_info;
96518518 839
ee921183 840 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 841
ee921183 842err_fill_table_info:
96518518
PM
843 kfree_skb(skb2);
844 return err;
845}
846
c9c17211 847static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt)
10435c11
F
848{
849 struct nft_chain *chain;
850 u32 i = 0;
851
852 list_for_each_entry(chain, &table->chains, list) {
853 if (!nft_is_active_next(net, chain))
854 continue;
f323d954 855 if (!nft_is_base_chain(chain))
10435c11
F
856 continue;
857
858 if (cnt && i++ == cnt)
859 break;
860
1e9451cb 861 nf_tables_unregister_hook(net, table, chain);
10435c11
F
862 }
863}
864
c9c17211 865static int nf_tables_table_enable(struct net *net, struct nft_table *table)
9ddf6323
PNA
866{
867 struct nft_chain *chain;
868 int err, i = 0;
869
870 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
871 if (!nft_is_active_next(net, chain))
872 continue;
f323d954 873 if (!nft_is_base_chain(chain))
d2012975
PNA
874 continue;
875
1e9451cb 876 err = nf_tables_register_hook(net, table, chain);
9ddf6323 877 if (err < 0)
d54725cd 878 goto err_register_hooks;
9ddf6323
PNA
879
880 i++;
881 }
882 return 0;
d54725cd
PNA
883
884err_register_hooks:
10435c11 885 if (i)
c9c17211 886 nft_table_disable(net, table, i);
9ddf6323
PNA
887 return err;
888}
889
c9c17211 890static void nf_tables_table_disable(struct net *net, struct nft_table *table)
9ddf6323 891{
c9c17211 892 nft_table_disable(net, table, 0);
9ddf6323
PNA
893}
894
e1aaca93 895static int nf_tables_updtable(struct nft_ctx *ctx)
9ddf6323 896{
55dd6f93 897 struct nft_trans *trans;
e1aaca93 898 u32 flags;
55dd6f93 899 int ret = 0;
9ddf6323 900
e1aaca93
PNA
901 if (!ctx->nla[NFTA_TABLE_FLAGS])
902 return 0;
9ddf6323 903
e1aaca93
PNA
904 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
905 if (flags & ~NFT_TABLE_F_DORMANT)
906 return -EINVAL;
907
63283dd2
PNA
908 if (flags == ctx->table->flags)
909 return 0;
910
55dd6f93
PNA
911 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
912 sizeof(struct nft_trans_table));
913 if (trans == NULL)
914 return -ENOMEM;
9ddf6323 915
e1aaca93
PNA
916 if ((flags & NFT_TABLE_F_DORMANT) &&
917 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
55dd6f93 918 nft_trans_table_enable(trans) = false;
e1aaca93
PNA
919 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
920 ctx->table->flags & NFT_TABLE_F_DORMANT) {
1e9451cb 921 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
c9c17211 922 ret = nf_tables_table_enable(ctx->net, ctx->table);
1e9451cb 923 if (ret >= 0)
55dd6f93 924 nft_trans_table_enable(trans) = true;
1e9451cb
FW
925 else
926 ctx->table->flags |= NFT_TABLE_F_DORMANT;
9ddf6323 927 }
e1aaca93
PNA
928 if (ret < 0)
929 goto err;
9ddf6323 930
55dd6f93
PNA
931 nft_trans_table_update(trans) = true;
932 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
933 return 0;
9ddf6323 934err:
55dd6f93 935 nft_trans_destroy(trans);
9ddf6323
PNA
936 return ret;
937}
938
1b2470e5
FW
939static u32 nft_chain_hash(const void *data, u32 len, u32 seed)
940{
941 const char *name = data;
942
943 return jhash(name, strlen(name), seed);
944}
945
946static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed)
947{
948 const struct nft_chain *chain = data;
949
950 return nft_chain_hash(chain->name, 0, seed);
951}
952
953static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg,
954 const void *ptr)
955{
956 const struct nft_chain *chain = ptr;
957 const char *name = arg->key;
958
959 return strcmp(chain->name, name);
960}
961
4d44175a
FW
962static u32 nft_objname_hash(const void *data, u32 len, u32 seed)
963{
964 const struct nft_object_hash_key *k = data;
965
966 seed ^= hash_ptr(k->table, 32);
967
968 return jhash(k->name, strlen(k->name), seed);
969}
970
971static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed)
972{
973 const struct nft_object *obj = data;
974
975 return nft_objname_hash(&obj->key, 0, seed);
976}
977
978static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg,
979 const void *ptr)
980{
981 const struct nft_object_hash_key *k = arg->key;
982 const struct nft_object *obj = ptr;
983
984 if (obj->key.table != k->table)
985 return -1;
986
987 return strcmp(obj->key.name, k->name);
988}
989
633c9a84
PNA
990static int nf_tables_newtable(struct net *net, struct sock *nlsk,
991 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
992 const struct nlattr * const nla[],
993 struct netlink_ext_ack *extack)
96518518
PM
994{
995 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 996 u8 genmask = nft_genmask_next(net);
96518518 997 int family = nfmsg->nfgen_family;
36dd1bcc
PNA
998 const struct nlattr *attr;
999 struct nft_table *table;
e1aaca93 1000 struct nft_ctx ctx;
7a81575b 1001 u32 flags = 0;
55dd6f93 1002 int err;
96518518 1003
f102d66b 1004 lockdep_assert_held(&net->nft.commit_mutex);
36dd1bcc
PNA
1005 attr = nla[NFTA_TABLE_NAME];
1006 table = nft_table_lookup(net, attr, family, genmask);
96518518
PM
1007 if (IS_ERR(table)) {
1008 if (PTR_ERR(table) != -ENOENT)
1009 return PTR_ERR(table);
1a28ad74 1010 } else {
36dd1bcc
PNA
1011 if (nlh->nlmsg_flags & NLM_F_EXCL) {
1012 NL_SET_BAD_ATTR(extack, attr);
96518518 1013 return -EEXIST;
36dd1bcc 1014 }
96518518
PM
1015 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1016 return -EOPNOTSUPP;
e1aaca93 1017
98319cb9 1018 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e1aaca93 1019 return nf_tables_updtable(&ctx);
96518518
PM
1020 }
1021
c5c1f975
PM
1022 if (nla[NFTA_TABLE_FLAGS]) {
1023 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
1024 if (flags & ~NFT_TABLE_F_DORMANT)
1025 return -EINVAL;
1026 }
1027
ffdb210e 1028 err = -ENOMEM;
1cae565e 1029 table = kzalloc(sizeof(*table), GFP_KERNEL);
ffdb210e 1030 if (table == NULL)
98319cb9 1031 goto err_kzalloc;
96518518 1032
36dd1bcc 1033 table->name = nla_strdup(attr, GFP_KERNEL);
e46abbcc 1034 if (table->name == NULL)
98319cb9 1035 goto err_strdup;
e46abbcc 1036
7a81575b 1037 if (nla[NFTA_TABLE_USERDATA]) {
85db827a 1038 table->udata = nla_memdup(nla[NFTA_TABLE_USERDATA], GFP_KERNEL);
7a81575b
JGG
1039 if (table->udata == NULL)
1040 goto err_table_udata;
1041
85db827a 1042 table->udlen = nla_len(nla[NFTA_TABLE_USERDATA]);
7a81575b
JGG
1043 }
1044
1b2470e5
FW
1045 err = rhltable_init(&table->chains_ht, &nft_chain_ht_params);
1046 if (err)
1047 goto err_chain_ht;
1048
96518518 1049 INIT_LIST_HEAD(&table->chains);
20a69341 1050 INIT_LIST_HEAD(&table->sets);
e5009240 1051 INIT_LIST_HEAD(&table->objects);
3b49e2e9 1052 INIT_LIST_HEAD(&table->flowtables);
98319cb9 1053 table->family = family;
c5c1f975 1054 table->flags = flags;
3ecbfd65 1055 table->handle = ++table_handle;
9ddf6323 1056
98319cb9 1057 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
55dd6f93 1058 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
ffdb210e 1059 if (err < 0)
98319cb9 1060 goto err_trans;
ffdb210e 1061
36596dad 1062 list_add_tail_rcu(&table->list, &net->nft.tables);
96518518 1063 return 0;
98319cb9 1064err_trans:
1b2470e5
FW
1065 rhltable_destroy(&table->chains_ht);
1066err_chain_ht:
7a81575b
JGG
1067 kfree(table->udata);
1068err_table_udata:
e46abbcc 1069 kfree(table->name);
98319cb9 1070err_strdup:
ffdb210e 1071 kfree(table);
98319cb9 1072err_kzalloc:
ffdb210e 1073 return err;
96518518
PM
1074}
1075
b9ac12ef
AB
1076static int nft_flush_table(struct nft_ctx *ctx)
1077{
3b49e2e9 1078 struct nft_flowtable *flowtable, *nft;
b9ac12ef 1079 struct nft_chain *chain, *nc;
e5009240 1080 struct nft_object *obj, *ne;
b9ac12ef 1081 struct nft_set *set, *ns;
3b49e2e9 1082 int err;
b9ac12ef 1083
a2f18db0 1084 list_for_each_entry(chain, &ctx->table->chains, list) {
664b0f8c
PNA
1085 if (!nft_is_active_next(ctx->net, chain))
1086 continue;
1087
d0e2c7de
PNA
1088 if (nft_chain_is_bound(chain))
1089 continue;
1090
b9ac12ef
AB
1091 ctx->chain = chain;
1092
1093 err = nft_delrule_by_chain(ctx);
1094 if (err < 0)
1095 goto out;
b9ac12ef
AB
1096 }
1097
1098 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
37a9cc52
PNA
1099 if (!nft_is_active_next(ctx->net, set))
1100 continue;
1101
408070d6 1102 if (nft_set_is_anonymous(set) &&
b9ac12ef
AB
1103 !list_empty(&set->bindings))
1104 continue;
1105
1106 err = nft_delset(ctx, set);
1107 if (err < 0)
1108 goto out;
1109 }
1110
3b49e2e9 1111 list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) {
335178d5
FW
1112 if (!nft_is_active_next(ctx->net, flowtable))
1113 continue;
1114
3b49e2e9
PNA
1115 err = nft_delflowtable(ctx, flowtable);
1116 if (err < 0)
1117 goto out;
1118 }
1119
e5009240 1120 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
335178d5
FW
1121 if (!nft_is_active_next(ctx->net, obj))
1122 continue;
1123
e5009240
PNA
1124 err = nft_delobj(ctx, obj);
1125 if (err < 0)
1126 goto out;
1127 }
1128
a2f18db0 1129 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
664b0f8c
PNA
1130 if (!nft_is_active_next(ctx->net, chain))
1131 continue;
1132
d0e2c7de
PNA
1133 if (nft_chain_is_bound(chain))
1134 continue;
1135
a2f18db0
PNA
1136 ctx->chain = chain;
1137
1138 err = nft_delchain(ctx);
1139 if (err < 0)
1140 goto out;
1141 }
1142
b9ac12ef
AB
1143 err = nft_deltable(ctx);
1144out:
1145 return err;
1146}
1147
1148static int nft_flush(struct nft_ctx *ctx, int family)
1149{
b9ac12ef
AB
1150 struct nft_table *table, *nt;
1151 const struct nlattr * const *nla = ctx->nla;
1152 int err = 0;
1153
36596dad 1154 list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) {
98319cb9 1155 if (family != AF_UNSPEC && table->family != family)
b9ac12ef
AB
1156 continue;
1157
98319cb9 1158 ctx->family = table->family;
f2a6d766 1159
36596dad
PNA
1160 if (!nft_is_active_next(ctx->net, table))
1161 continue;
b9ac12ef 1162
36596dad
PNA
1163 if (nla[NFTA_TABLE_NAME] &&
1164 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
1165 continue;
b9ac12ef 1166
36596dad
PNA
1167 ctx->table = table;
1168
1169 err = nft_flush_table(ctx);
1170 if (err < 0)
1171 goto out;
b9ac12ef
AB
1172 }
1173out:
1174 return err;
1175}
1176
633c9a84
PNA
1177static int nf_tables_deltable(struct net *net, struct sock *nlsk,
1178 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
1179 const struct nlattr * const nla[],
1180 struct netlink_ext_ack *extack)
96518518
PM
1181{
1182 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 1183 u8 genmask = nft_genmask_next(net);
ee01d542 1184 int family = nfmsg->nfgen_family;
36dd1bcc
PNA
1185 const struct nlattr *attr;
1186 struct nft_table *table;
55dd6f93 1187 struct nft_ctx ctx;
96518518 1188
36596dad 1189 nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla);
3ecbfd65
HS
1190 if (family == AF_UNSPEC ||
1191 (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE]))
b9ac12ef
AB
1192 return nft_flush(&ctx, family);
1193
36dd1bcc
PNA
1194 if (nla[NFTA_TABLE_HANDLE]) {
1195 attr = nla[NFTA_TABLE_HANDLE];
1196 table = nft_table_lookup_byhandle(net, attr, genmask);
1197 } else {
1198 attr = nla[NFTA_TABLE_NAME];
1199 table = nft_table_lookup(net, attr, family, genmask);
1200 }
3ecbfd65 1201
36dd1bcc
PNA
1202 if (IS_ERR(table)) {
1203 NL_SET_BAD_ATTR(extack, attr);
96518518 1204 return PTR_ERR(table);
36dd1bcc 1205 }
96518518 1206
a8278400
PNA
1207 if (nlh->nlmsg_flags & NLM_F_NONREC &&
1208 table->use > 0)
1209 return -EBUSY;
1210
98319cb9 1211 ctx.family = family;
b9ac12ef 1212 ctx.table = table;
55dd6f93 1213
b9ac12ef 1214 return nft_flush_table(&ctx);
96518518
PM
1215}
1216
55dd6f93
PNA
1217static void nf_tables_table_destroy(struct nft_ctx *ctx)
1218{
fa5950e4
FW
1219 if (WARN_ON(ctx->table->use > 0))
1220 return;
4fefee57 1221
1b2470e5 1222 rhltable_destroy(&ctx->table->chains_ht);
e46abbcc 1223 kfree(ctx->table->name);
bc7a7082 1224 kfree(ctx->table->udata);
55dd6f93 1225 kfree(ctx->table);
55dd6f93
PNA
1226}
1227
cc07eeb0 1228void nft_register_chain_type(const struct nft_chain_type *ctype)
96518518 1229{
96518518 1230 nfnl_lock(NFNL_SUBSYS_NFTABLES);
82603549 1231 if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) {
cc07eeb0
PNA
1232 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1233 return;
96518518 1234 }
9370761c 1235 chain_type[ctype->family][ctype->type] = ctype;
96518518 1236 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
96518518 1237}
9370761c 1238EXPORT_SYMBOL_GPL(nft_register_chain_type);
96518518 1239
32537e91 1240void nft_unregister_chain_type(const struct nft_chain_type *ctype)
96518518 1241{
96518518 1242 nfnl_lock(NFNL_SUBSYS_NFTABLES);
9370761c 1243 chain_type[ctype->family][ctype->type] = NULL;
96518518
PM
1244 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1245}
9370761c 1246EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
96518518
PM
1247
1248/*
1249 * Chains
1250 */
1251
1252static struct nft_chain *
cac20fcd 1253nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask)
96518518
PM
1254{
1255 struct nft_chain *chain;
1256
1257 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
1258 if (chain->handle == handle &&
1259 nft_active_genmask(chain, genmask))
96518518
PM
1260 return chain;
1261 }
1262
1263 return ERR_PTR(-ENOENT);
1264}
1265
4d44175a 1266static bool lockdep_commit_lock_is_held(const struct net *net)
f102d66b
FW
1267{
1268#ifdef CONFIG_PROVE_LOCKING
1269 return lockdep_is_held(&net->nft.commit_mutex);
1270#else
1271 return true;
1272#endif
1273}
1274
1275static struct nft_chain *nft_chain_lookup(struct net *net,
1276 struct nft_table *table,
cac20fcd 1277 const struct nlattr *nla, u8 genmask)
96518518 1278{
1b2470e5
FW
1279 char search[NFT_CHAIN_MAXNAMELEN + 1];
1280 struct rhlist_head *tmp, *list;
96518518
PM
1281 struct nft_chain *chain;
1282
1283 if (nla == NULL)
1284 return ERR_PTR(-EINVAL);
1285
872f6903 1286 nla_strscpy(search, nla, sizeof(search));
96518518 1287
1b2470e5 1288 WARN_ON(!rcu_read_lock_held() &&
f102d66b 1289 !lockdep_commit_lock_is_held(net));
1b2470e5
FW
1290
1291 chain = ERR_PTR(-ENOENT);
1292 rcu_read_lock();
1293 list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params);
1294 if (!list)
1295 goto out_unlock;
1296
1297 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
1298 if (nft_active_genmask(chain, genmask))
1299 goto out_unlock;
1300 }
1301 chain = ERR_PTR(-ENOENT);
1302out_unlock:
1303 rcu_read_unlock();
1304 return chain;
96518518
PM
1305}
1306
1307static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
b2fbd044
LZ
1308 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
1309 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
1310 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
1311 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
1312 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1313 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
0ca743a5 1314 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
9332d27d
FW
1315 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING,
1316 .len = NFT_MODULE_AUTOLOAD_LIMIT },
0ca743a5 1317 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
c9626a2c 1318 [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 },
74cccc3d 1319 [NFTA_CHAIN_ID] = { .type = NLA_U32 },
002f2176
JGG
1320 [NFTA_CHAIN_USERDATA] = { .type = NLA_BINARY,
1321 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1322};
1323
1324static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
1325 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
1326 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
2cbce139
PNA
1327 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
1328 .len = IFNAMSIZ - 1 },
96518518
PM
1329};
1330
0ca743a5
PNA
1331static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
1332{
1333 struct nft_stats *cpu_stats, total;
1334 struct nlattr *nest;
ce355e20
ED
1335 unsigned int seq;
1336 u64 pkts, bytes;
0ca743a5
PNA
1337 int cpu;
1338
edbd82c5
FW
1339 if (!stats)
1340 return 0;
1341
0ca743a5
PNA
1342 memset(&total, 0, sizeof(total));
1343 for_each_possible_cpu(cpu) {
1344 cpu_stats = per_cpu_ptr(stats, cpu);
ce355e20
ED
1345 do {
1346 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1347 pkts = cpu_stats->pkts;
1348 bytes = cpu_stats->bytes;
1349 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
1350 total.pkts += pkts;
1351 total.bytes += bytes;
0ca743a5 1352 }
ae0be8de 1353 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS);
0ca743a5
PNA
1354 if (nest == NULL)
1355 goto nla_put_failure;
1356
b46f6ded
ND
1357 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
1358 NFTA_COUNTER_PAD) ||
1359 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
1360 NFTA_COUNTER_PAD))
0ca743a5
PNA
1361 goto nla_put_failure;
1362
1363 nla_nest_end(skb, nest);
1364 return 0;
1365
1366nla_put_failure:
1367 return -ENOSPC;
1368}
1369
d54725cd
PNA
1370static int nft_dump_basechain_hook(struct sk_buff *skb, int family,
1371 const struct nft_base_chain *basechain)
1372{
1373 const struct nf_hook_ops *ops = &basechain->ops;
1374 struct nft_hook *hook, *first = NULL;
1375 struct nlattr *nest, *nest_devs;
1376 int n = 0;
1377
1378 nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK);
1379 if (nest == NULL)
1380 goto nla_put_failure;
1381 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1382 goto nla_put_failure;
1383 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1384 goto nla_put_failure;
1385
d3519cb8 1386 if (nft_base_chain_netdev(family, ops->hooknum)) {
d54725cd
PNA
1387 nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS);
1388 list_for_each_entry(hook, &basechain->hook_list, list) {
1389 if (!first)
1390 first = hook;
1391
1392 if (nla_put_string(skb, NFTA_DEVICE_NAME,
1393 hook->ops.dev->name))
1394 goto nla_put_failure;
1395 n++;
1396 }
1397 nla_nest_end(skb, nest_devs);
1398
1399 if (n == 1 &&
1400 nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name))
1401 goto nla_put_failure;
1402 }
1403 nla_nest_end(skb, nest);
1404
1405 return 0;
1406nla_put_failure:
1407 return -1;
1408}
1409
84d7fce6
PNA
1410static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
1411 u32 portid, u32 seq, int event, u32 flags,
1412 int family, const struct nft_table *table,
96518518
PM
1413 const struct nft_chain *chain)
1414{
1415 struct nlmsghdr *nlh;
1416 struct nfgenmsg *nfmsg;
1417
dedb67c4 1418 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518
PM
1419 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
1420 if (nlh == NULL)
1421 goto nla_put_failure;
1422
1423 nfmsg = nlmsg_data(nlh);
1424 nfmsg->nfgen_family = family;
1425 nfmsg->version = NFNETLINK_V0;
84d7fce6 1426 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
1427
1428 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
1429 goto nla_put_failure;
b46f6ded
ND
1430 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1431 NFTA_CHAIN_PAD))
96518518
PM
1432 goto nla_put_failure;
1433 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
1434 goto nla_put_failure;
1435
f323d954 1436 if (nft_is_base_chain(chain)) {
0ca743a5 1437 const struct nft_base_chain *basechain = nft_base_chain(chain);
edbd82c5 1438 struct nft_stats __percpu *stats;
0ca743a5 1439
d54725cd 1440 if (nft_dump_basechain_hook(skb, family, basechain))
2cbce139 1441 goto nla_put_failure;
9370761c 1442
0ca743a5
PNA
1443 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1444 htonl(basechain->policy)))
1445 goto nla_put_failure;
1446
baae3e62
PM
1447 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1448 goto nla_put_failure;
0ca743a5 1449
edbd82c5
FW
1450 stats = rcu_dereference_check(basechain->stats,
1451 lockdep_commit_lock_is_held(net));
1452 if (nft_dump_stats(skb, stats))
0ca743a5 1453 goto nla_put_failure;
96518518
PM
1454 }
1455
d0e2c7de
PNA
1456 if (chain->flags &&
1457 nla_put_be32(skb, NFTA_CHAIN_FLAGS, htonl(chain->flags)))
1458 goto nla_put_failure;
1459
0ca743a5
PNA
1460 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1461 goto nla_put_failure;
1462
002f2176
JGG
1463 if (chain->udata &&
1464 nla_put(skb, NFTA_CHAIN_USERDATA, chain->udlen, chain->udata))
1465 goto nla_put_failure;
1466
053c095a
JB
1467 nlmsg_end(skb, nlh);
1468 return 0;
96518518
PM
1469
1470nla_put_failure:
1471 nlmsg_trim(skb, nlh);
1472 return -1;
1473}
1474
25e94a99 1475static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
96518518
PM
1476{
1477 struct sk_buff *skb;
96518518 1478 int err;
8e6cf365
RGB
1479 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
1480 ctx->table->name, ctx->table->handle,
1481 ctx->chain->name, ctx->chain->handle);
1482
1483 audit_log_nfcfg(buf,
1484 ctx->family,
1485 ctx->chain->use,
1486 event == NFT_MSG_NEWCHAIN ?
1487 AUDIT_NFT_OP_CHAIN_REGISTER :
14224039
RGB
1488 AUDIT_NFT_OP_CHAIN_UNREGISTER,
1489 GFP_KERNEL);
8e6cf365 1490 kfree(buf);
96518518 1491
128ad332
PNA
1492 if (!ctx->report &&
1493 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 1494 return;
96518518 1495
96518518
PM
1496 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1497 if (skb == NULL)
1498 goto err;
1499
84d7fce6 1500 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 1501 event, 0, ctx->family, ctx->table,
35151d84 1502 ctx->chain);
96518518
PM
1503 if (err < 0) {
1504 kfree_skb(skb);
1505 goto err;
1506 }
1507
67cc570e 1508 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 1509 return;
96518518 1510err:
25e94a99 1511 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
1512}
1513
1514static int nf_tables_dump_chains(struct sk_buff *skb,
1515 struct netlink_callback *cb)
1516{
1517 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
96518518
PM
1518 const struct nft_table *table;
1519 const struct nft_chain *chain;
1520 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1521 struct net *net = sock_net(skb->sk);
96518518
PM
1522 int family = nfmsg->nfgen_family;
1523
e688a7f8 1524 rcu_read_lock();
38e029f1
PNA
1525 cb->seq = net->nft.base_seq;
1526
36596dad 1527 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 1528 if (family != NFPROTO_UNSPEC && family != table->family)
96518518
PM
1529 continue;
1530
36596dad
PNA
1531 list_for_each_entry_rcu(chain, &table->chains, list) {
1532 if (idx < s_idx)
1533 goto cont;
1534 if (idx > s_idx)
1535 memset(&cb->args[1], 0,
1536 sizeof(cb->args) - sizeof(cb->args[0]));
1537 if (!nft_is_active(net, chain))
1538 continue;
1539 if (nf_tables_fill_chain_info(skb, net,
1540 NETLINK_CB(cb->skb).portid,
1541 cb->nlh->nlmsg_seq,
1542 NFT_MSG_NEWCHAIN,
1543 NLM_F_MULTI,
98319cb9 1544 table->family, table,
36596dad
PNA
1545 chain) < 0)
1546 goto done;
38e029f1 1547
36596dad 1548 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518 1549cont:
36596dad 1550 idx++;
96518518
PM
1551 }
1552 }
1553done:
e688a7f8 1554 rcu_read_unlock();
96518518
PM
1555 cb->args[0] = idx;
1556 return skb->len;
1557}
1558
d9adf22a 1559/* called with rcu_read_lock held */
7b8002a1
PNA
1560static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1561 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
1562 const struct nlattr * const nla[],
1563 struct netlink_ext_ack *extack)
96518518
PM
1564{
1565 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 1566 u8 genmask = nft_genmask_cur(net);
96518518 1567 const struct nft_chain *chain;
1b2470e5 1568 struct nft_table *table;
96518518
PM
1569 struct sk_buff *skb2;
1570 int family = nfmsg->nfgen_family;
1571 int err;
1572
1573 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1574 struct netlink_dump_control c = {
1575 .dump = nf_tables_dump_chains,
d9adf22a 1576 .module = THIS_MODULE,
96518518 1577 };
d9adf22a
FW
1578
1579 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
1580 }
1581
cac20fcd 1582 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
1583 if (IS_ERR(table)) {
1584 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 1585 return PTR_ERR(table);
36dd1bcc 1586 }
96518518 1587
f102d66b 1588 chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask);
36dd1bcc
PNA
1589 if (IS_ERR(chain)) {
1590 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
96518518 1591 return PTR_ERR(chain);
36dd1bcc 1592 }
96518518 1593
d9adf22a 1594 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
1595 if (!skb2)
1596 return -ENOMEM;
1597
84d7fce6 1598 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
1599 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1600 family, table, chain);
1601 if (err < 0)
ee921183 1602 goto err_fill_chain_info;
96518518 1603
ee921183 1604 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 1605
ee921183 1606err_fill_chain_info:
96518518
PM
1607 kfree_skb(skb2);
1608 return err;
1609}
1610
0ca743a5
PNA
1611static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1612 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1613 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1614};
1615
ff3cd7b3 1616static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
0ca743a5
PNA
1617{
1618 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1619 struct nft_stats __percpu *newstats;
1620 struct nft_stats *stats;
1621 int err;
1622
8cb08174
JB
1623 err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr,
1624 nft_counter_policy, NULL);
0ca743a5 1625 if (err < 0)
ff3cd7b3 1626 return ERR_PTR(err);
0ca743a5
PNA
1627
1628 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
ff3cd7b3 1629 return ERR_PTR(-EINVAL);
0ca743a5 1630
ce355e20 1631 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
0ca743a5 1632 if (newstats == NULL)
ff3cd7b3 1633 return ERR_PTR(-ENOMEM);
0ca743a5
PNA
1634
1635 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1636 * are not exposed to userspace.
1637 */
e8781f70 1638 preempt_disable();
0ca743a5
PNA
1639 stats = this_cpu_ptr(newstats);
1640 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1641 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
e8781f70 1642 preempt_enable();
0ca743a5 1643
ff3cd7b3
PNA
1644 return newstats;
1645}
1646
53315ac6 1647static void nft_chain_stats_replace(struct nft_trans *trans)
ff3cd7b3 1648{
53315ac6 1649 struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain);
0befd061 1650
53315ac6 1651 if (!nft_trans_chain_stats(trans))
b88825de
PNA
1652 return;
1653
b685b534
PM
1654 nft_trans_chain_stats(trans) =
1655 rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans),
1656 lockdep_commit_lock_is_held(trans->ctx.net));
53315ac6
FW
1657
1658 if (!nft_trans_chain_stats(trans))
bbb8c61f 1659 static_branch_inc(&nft_counters_enabled);
0ca743a5
PNA
1660}
1661
0cbc06b3
FW
1662static void nf_tables_chain_free_chain_rules(struct nft_chain *chain)
1663{
1664 struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0);
1665 struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1);
1666
1667 if (g0 != g1)
1668 kvfree(g1);
1669 kvfree(g0);
1670
1671 /* should be NULL either via abort or via successful commit */
1672 WARN_ON_ONCE(chain->rules_next);
1673 kvfree(chain->rules_next);
1674}
1675
d0e2c7de 1676void nf_tables_chain_destroy(struct nft_ctx *ctx)
91c7b38d 1677{
43a605f2 1678 struct nft_chain *chain = ctx->chain;
d54725cd 1679 struct nft_hook *hook, *next;
43a605f2 1680
fa5950e4
FW
1681 if (WARN_ON(chain->use > 0))
1682 return;
91c7b38d 1683
0cbc06b3
FW
1684 /* no concurrent access possible anymore */
1685 nf_tables_chain_free_chain_rules(chain);
1686
f323d954 1687 if (nft_is_base_chain(chain)) {
2cbce139
PNA
1688 struct nft_base_chain *basechain = nft_base_chain(chain);
1689
d3519cb8 1690 if (nft_base_chain_netdev(ctx->family, basechain->ops.hooknum)) {
d54725cd
PNA
1691 list_for_each_entry_safe(hook, next,
1692 &basechain->hook_list, list) {
1693 list_del_rcu(&hook->list);
1694 kfree_rcu(hook, rcu);
1695 }
1696 }
2cbce139 1697 module_put(basechain->type->owner);
4c05ec47 1698 if (rcu_access_pointer(basechain->stats)) {
9f08ea84 1699 static_branch_dec(&nft_counters_enabled);
4c05ec47
TY
1700 free_percpu(rcu_dereference_raw(basechain->stats));
1701 }
b7263e07 1702 kfree(chain->name);
002f2176 1703 kfree(chain->udata);
2cbce139 1704 kfree(basechain);
91c7b38d 1705 } else {
b7263e07 1706 kfree(chain->name);
002f2176 1707 kfree(chain->udata);
91c7b38d
PNA
1708 kfree(chain);
1709 }
1710}
1711
3f0465a9
PNA
1712static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
1713 const struct nlattr *attr)
1714{
1715 struct net_device *dev;
1716 char ifname[IFNAMSIZ];
1717 struct nft_hook *hook;
1718 int err;
1719
1720 hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL);
1721 if (!hook) {
1722 err = -ENOMEM;
1723 goto err_hook_alloc;
1724 }
1725
872f6903 1726 nla_strscpy(ifname, attr, IFNAMSIZ);
42f1c271
PNA
1727 /* nf_tables_netdev_event() is called under rtnl_mutex, this is
1728 * indirectly serializing all the other holders of the commit_mutex with
1729 * the rtnl_mutex.
1730 */
3f0465a9
PNA
1731 dev = __dev_get_by_name(net, ifname);
1732 if (!dev) {
1733 err = -ENOENT;
1734 goto err_hook_dev;
1735 }
1736 hook->ops.dev = dev;
abadb2f8 1737 hook->inactive = false;
3f0465a9
PNA
1738
1739 return hook;
1740
1741err_hook_dev:
1742 kfree(hook);
1743err_hook_alloc:
1744 return ERR_PTR(err);
1745}
1746
abadb2f8
PNA
1747static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,
1748 const struct nft_hook *this)
b75a3e83
PNA
1749{
1750 struct nft_hook *hook;
1751
1752 list_for_each_entry(hook, hook_list, list) {
1753 if (this->ops.dev == hook->ops.dev)
abadb2f8 1754 return hook;
b75a3e83
PNA
1755 }
1756
abadb2f8 1757 return NULL;
b75a3e83
PNA
1758}
1759
3f0465a9
PNA
1760static int nf_tables_parse_netdev_hooks(struct net *net,
1761 const struct nlattr *attr,
1762 struct list_head *hook_list)
1763{
1764 struct nft_hook *hook, *next;
1765 const struct nlattr *tmp;
1766 int rem, n = 0, err;
1767
1768 nla_for_each_nested(tmp, attr, rem) {
1769 if (nla_type(tmp) != NFTA_DEVICE_NAME) {
1770 err = -EINVAL;
1771 goto err_hook;
1772 }
1773
1774 hook = nft_netdev_hook_alloc(net, tmp);
1775 if (IS_ERR(hook)) {
1776 err = PTR_ERR(hook);
1777 goto err_hook;
1778 }
b75a3e83 1779 if (nft_hook_list_find(hook_list, hook)) {
cd77e75b 1780 kfree(hook);
b75a3e83
PNA
1781 err = -EEXIST;
1782 goto err_hook;
1783 }
3f0465a9
PNA
1784 list_add_tail(&hook->list, hook_list);
1785 n++;
1786
cb662ac6 1787 if (n == NFT_NETDEVICE_MAX) {
3f0465a9
PNA
1788 err = -EFBIG;
1789 goto err_hook;
1790 }
1791 }
3f0465a9
PNA
1792
1793 return 0;
1794
1795err_hook:
1796 list_for_each_entry_safe(hook, next, hook_list, list) {
1797 list_del(&hook->list);
1798 kfree(hook);
1799 }
1800 return err;
1801}
1802
508f8ccd
PNA
1803struct nft_chain_hook {
1804 u32 num;
84ba7dd7 1805 s32 priority;
32537e91 1806 const struct nft_chain_type *type;
d54725cd 1807 struct list_head list;
508f8ccd
PNA
1808};
1809
d54725cd
PNA
1810static int nft_chain_parse_netdev(struct net *net,
1811 struct nlattr *tb[],
1812 struct list_head *hook_list)
1813{
1814 struct nft_hook *hook;
1815 int err;
1816
1817 if (tb[NFTA_HOOK_DEV]) {
1818 hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
1819 if (IS_ERR(hook))
1820 return PTR_ERR(hook);
1821
1822 list_add_tail(&hook->list, hook_list);
1823 } else if (tb[NFTA_HOOK_DEVS]) {
1824 err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS],
1825 hook_list);
1826 if (err < 0)
1827 return err;
05abe445
PNA
1828
1829 if (list_empty(hook_list))
1830 return -EINVAL;
d54725cd
PNA
1831 } else {
1832 return -EINVAL;
1833 }
1834
1835 return 0;
1836}
1837
508f8ccd
PNA
1838static int nft_chain_parse_hook(struct net *net,
1839 const struct nlattr * const nla[],
36596dad 1840 struct nft_chain_hook *hook, u8 family,
445509eb 1841 bool autoload)
508f8ccd
PNA
1842{
1843 struct nlattr *ha[NFTA_HOOK_MAX + 1];
32537e91 1844 const struct nft_chain_type *type;
508f8ccd
PNA
1845 int err;
1846
f102d66b
FW
1847 lockdep_assert_held(&net->nft.commit_mutex);
1848 lockdep_nfnl_nft_mutex_not_held();
1849
8cb08174
JB
1850 err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX,
1851 nla[NFTA_CHAIN_HOOK],
1852 nft_hook_policy, NULL);
508f8ccd
PNA
1853 if (err < 0)
1854 return err;
1855
1856 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1857 ha[NFTA_HOOK_PRIORITY] == NULL)
1858 return -EINVAL;
1859
1860 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
508f8ccd
PNA
1861 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1862
82603549
PNA
1863 type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT);
1864 if (!type)
1865 return -EOPNOTSUPP;
1866
508f8ccd 1867 if (nla[NFTA_CHAIN_TYPE]) {
452238e8 1868 type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE],
445509eb 1869 family, autoload);
508f8ccd
PNA
1870 if (IS_ERR(type))
1871 return PTR_ERR(type);
1872 }
d25e2e93 1873 if (hook->num >= NFT_MAX_HOOKS || !(type->hook_mask & (1 << hook->num)))
508f8ccd 1874 return -EOPNOTSUPP;
84ba7dd7
FW
1875
1876 if (type->type == NFT_CHAIN_T_NAT &&
1877 hook->priority <= NF_IP_PRI_CONNTRACK)
1878 return -EOPNOTSUPP;
1879
508f8ccd
PNA
1880 if (!try_module_get(type->owner))
1881 return -ENOENT;
1882
1883 hook->type = type;
1884
d54725cd 1885 INIT_LIST_HEAD(&hook->list);
d3519cb8 1886 if (nft_base_chain_netdev(family, hook->num)) {
d54725cd
PNA
1887 err = nft_chain_parse_netdev(net, ha, &hook->list);
1888 if (err < 0) {
508f8ccd 1889 module_put(type->owner);
d54725cd 1890 return err;
508f8ccd 1891 }
d54725cd 1892 } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) {
508f8ccd
PNA
1893 module_put(type->owner);
1894 return -EOPNOTSUPP;
1895 }
1896
1897 return 0;
1898}
1899
1900static void nft_chain_release_hook(struct nft_chain_hook *hook)
1901{
d54725cd
PNA
1902 struct nft_hook *h, *next;
1903
1904 list_for_each_entry_safe(h, next, &hook->list, list) {
1905 list_del(&h->list);
1906 kfree(h);
1907 }
508f8ccd 1908 module_put(hook->type->owner);
508f8ccd
PNA
1909}
1910
0cbc06b3
FW
1911struct nft_rules_old {
1912 struct rcu_head h;
1913 struct nft_rule **start;
1914};
1915
1916static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain,
1917 unsigned int alloc)
1918{
1919 if (alloc > INT_MAX)
1920 return NULL;
1921
1922 alloc += 1; /* NULL, ends rules */
1923 if (sizeof(struct nft_rule *) > INT_MAX / alloc)
1924 return NULL;
1925
1926 alloc *= sizeof(struct nft_rule *);
1927 alloc += sizeof(struct nft_rules_old);
1928
1929 return kvmalloc(alloc, GFP_KERNEL);
1930}
1931
d54725cd
PNA
1932static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family,
1933 const struct nft_chain_hook *hook,
1934 struct nft_chain *chain)
1935{
1936 ops->pf = family;
1937 ops->hooknum = hook->num;
1938 ops->priority = hook->priority;
1939 ops->priv = chain;
1940 ops->hook = hook->type->hooks[ops->hooknum];
1941}
1942
1943static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,
1944 struct nft_chain_hook *hook, u32 flags)
1945{
1946 struct nft_chain *chain;
1947 struct nft_hook *h;
1948
1949 basechain->type = hook->type;
1950 INIT_LIST_HEAD(&basechain->hook_list);
1951 chain = &basechain->chain;
1952
d3519cb8 1953 if (nft_base_chain_netdev(family, hook->num)) {
d54725cd
PNA
1954 list_splice_init(&hook->list, &basechain->hook_list);
1955 list_for_each_entry(h, &basechain->hook_list, list)
1956 nft_basechain_hook_init(&h->ops, family, hook, chain);
1957
1958 basechain->ops.hooknum = hook->num;
1959 basechain->ops.priority = hook->priority;
1960 } else {
1961 nft_basechain_hook_init(&basechain->ops, family, hook, chain);
1962 }
1963
67c49de4 1964 chain->flags |= NFT_CHAIN_BASE | flags;
d54725cd
PNA
1965 basechain->policy = NF_ACCEPT;
1966 if (chain->flags & NFT_CHAIN_HW_OFFLOAD &&
1967 nft_chain_offload_priority(basechain) < 0)
1968 return -EOPNOTSUPP;
1969
1970 flow_block_init(&basechain->flow_block);
1971
1972 return 0;
1973}
1974
04b7db41
PNA
1975static int nft_chain_add(struct nft_table *table, struct nft_chain *chain)
1976{
1977 int err;
1978
1979 err = rhltable_insert_key(&table->chains_ht, chain->name,
1980 &chain->rhlhead, nft_chain_ht_params);
1981 if (err)
1982 return err;
1983
1984 list_add_tail_rcu(&chain->list, &table->chains);
1985
1986 return 0;
1987}
1988
d0e2c7de
PNA
1989static u64 chain_id;
1990
4035285f 1991static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
c9626a2c 1992 u8 policy, u32 flags)
4035285f
PNA
1993{
1994 const struct nlattr * const *nla = ctx->nla;
1995 struct nft_table *table = ctx->table;
4035285f
PNA
1996 struct nft_base_chain *basechain;
1997 struct nft_stats __percpu *stats;
1998 struct net *net = ctx->net;
d0e2c7de 1999 char name[NFT_NAME_MAXLEN];
66293c46 2000 struct nft_trans *trans;
4035285f 2001 struct nft_chain *chain;
0cbc06b3 2002 struct nft_rule **rules;
4035285f
PNA
2003 int err;
2004
2005 if (table->use == UINT_MAX)
2006 return -EOVERFLOW;
2007
2008 if (nla[NFTA_CHAIN_HOOK]) {
2009 struct nft_chain_hook hook;
4035285f 2010
d0e2c7de
PNA
2011 if (flags & NFT_CHAIN_BINDING)
2012 return -EOPNOTSUPP;
2013
445509eb 2014 err = nft_chain_parse_hook(net, nla, &hook, family, true);
4035285f
PNA
2015 if (err < 0)
2016 return err;
2017
2018 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
2019 if (basechain == NULL) {
2020 nft_chain_release_hook(&hook);
2021 return -ENOMEM;
2022 }
d54725cd 2023 chain = &basechain->chain;
4035285f
PNA
2024
2025 if (nla[NFTA_CHAIN_COUNTERS]) {
2026 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2027 if (IS_ERR(stats)) {
2028 nft_chain_release_hook(&hook);
2029 kfree(basechain);
2030 return PTR_ERR(stats);
2031 }
4c05ec47 2032 rcu_assign_pointer(basechain->stats, stats);
4035285f
PNA
2033 static_branch_inc(&nft_counters_enabled);
2034 }
2035
d54725cd
PNA
2036 err = nft_basechain_init(basechain, family, &hook, flags);
2037 if (err < 0) {
2038 nft_chain_release_hook(&hook);
2039 kfree(basechain);
2040 return err;
2041 }
4035285f 2042 } else {
d0e2c7de
PNA
2043 if (flags & NFT_CHAIN_BASE)
2044 return -EINVAL;
2045 if (flags & NFT_CHAIN_HW_OFFLOAD)
2046 return -EOPNOTSUPP;
2047
4035285f
PNA
2048 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
2049 if (chain == NULL)
2050 return -ENOMEM;
d0e2c7de
PNA
2051
2052 chain->flags = flags;
4035285f 2053 }
43a605f2
PNA
2054 ctx->chain = chain;
2055
4035285f
PNA
2056 INIT_LIST_HEAD(&chain->rules);
2057 chain->handle = nf_tables_alloc_handle(table);
2058 chain->table = table;
d0e2c7de
PNA
2059
2060 if (nla[NFTA_CHAIN_NAME]) {
2061 chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2062 } else {
59136aa3
FW
2063 if (!(flags & NFT_CHAIN_BINDING)) {
2064 err = -EINVAL;
002f2176 2065 goto err_destroy_chain;
59136aa3 2066 }
d0e2c7de
PNA
2067
2068 snprintf(name, sizeof(name), "__chain%llu", ++chain_id);
2069 chain->name = kstrdup(name, GFP_KERNEL);
2070 }
2071
4035285f
PNA
2072 if (!chain->name) {
2073 err = -ENOMEM;
002f2176
JGG
2074 goto err_destroy_chain;
2075 }
2076
2077 if (nla[NFTA_CHAIN_USERDATA]) {
2078 chain->udata = nla_memdup(nla[NFTA_CHAIN_USERDATA], GFP_KERNEL);
2079 if (chain->udata == NULL) {
2080 err = -ENOMEM;
2081 goto err_destroy_chain;
2082 }
2083 chain->udlen = nla_len(nla[NFTA_CHAIN_USERDATA]);
4035285f
PNA
2084 }
2085
0cbc06b3
FW
2086 rules = nf_tables_chain_alloc_rules(chain, 0);
2087 if (!rules) {
2088 err = -ENOMEM;
002f2176 2089 goto err_destroy_chain;
0cbc06b3
FW
2090 }
2091
2092 *rules = NULL;
2093 rcu_assign_pointer(chain->rules_gen_0, rules);
2094 rcu_assign_pointer(chain->rules_gen_1, rules);
2095
c974a3a3 2096 err = nf_tables_register_hook(net, table, chain);
4035285f 2097 if (err < 0)
002f2176 2098 goto err_destroy_chain;
4035285f 2099
66293c46
FW
2100 trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN);
2101 if (IS_ERR(trans)) {
2102 err = PTR_ERR(trans);
002f2176 2103 goto err_unregister_hook;
1b2470e5 2104 }
4035285f 2105
ad652f38 2106 nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET;
66293c46
FW
2107 if (nft_is_base_chain(chain))
2108 nft_trans_chain_policy(trans) = policy;
2109
04b7db41
PNA
2110 err = nft_chain_add(table, chain);
2111 if (err < 0) {
2112 nft_trans_destroy(trans);
002f2176 2113 goto err_unregister_hook;
04b7db41
PNA
2114 }
2115
4035285f 2116 table->use++;
4035285f
PNA
2117
2118 return 0;
002f2176 2119err_unregister_hook:
c974a3a3 2120 nf_tables_unregister_hook(net, table, chain);
002f2176 2121err_destroy_chain:
43a605f2 2122 nf_tables_chain_destroy(ctx);
4035285f
PNA
2123
2124 return err;
2125}
2126
d54725cd
PNA
2127static bool nft_hook_list_equal(struct list_head *hook_list1,
2128 struct list_head *hook_list2)
2129{
2130 struct nft_hook *hook;
2131 int n = 0, m = 0;
2132
2133 n = 0;
2134 list_for_each_entry(hook, hook_list2, list) {
2135 if (!nft_hook_list_find(hook_list1, hook))
2136 return false;
2137
2138 n++;
2139 }
2140 list_for_each_entry(hook, hook_list1, list)
2141 m++;
2142
2143 return n == m;
2144}
2145
c9626a2c 2146static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,
98a381a7
PNA
2147 u32 flags, const struct nlattr *attr,
2148 struct netlink_ext_ack *extack)
2c4a488a
PNA
2149{
2150 const struct nlattr * const *nla = ctx->nla;
2151 struct nft_table *table = ctx->table;
2152 struct nft_chain *chain = ctx->chain;
2c4a488a
PNA
2153 struct nft_base_chain *basechain;
2154 struct nft_stats *stats = NULL;
2155 struct nft_chain_hook hook;
2c4a488a
PNA
2156 struct nf_hook_ops *ops;
2157 struct nft_trans *trans;
c974a3a3 2158 int err;
2c4a488a 2159
c9626a2c
PNA
2160 if (chain->flags ^ flags)
2161 return -EOPNOTSUPP;
2162
2c4a488a 2163 if (nla[NFTA_CHAIN_HOOK]) {
98a381a7
PNA
2164 if (!nft_is_base_chain(chain)) {
2165 NL_SET_BAD_ATTR(extack, attr);
77a92189 2166 return -EEXIST;
98a381a7 2167 }
36596dad 2168 err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family,
445509eb 2169 false);
2c4a488a
PNA
2170 if (err < 0)
2171 return err;
2172
2173 basechain = nft_base_chain(chain);
2174 if (basechain->type != hook.type) {
2175 nft_chain_release_hook(&hook);
98a381a7 2176 NL_SET_BAD_ATTR(extack, attr);
77a92189 2177 return -EEXIST;
2c4a488a
PNA
2178 }
2179
d3519cb8 2180 if (nft_base_chain_netdev(ctx->family, hook.num)) {
d54725cd
PNA
2181 if (!nft_hook_list_equal(&basechain->hook_list,
2182 &hook.list)) {
2183 nft_chain_release_hook(&hook);
98a381a7 2184 NL_SET_BAD_ATTR(extack, attr);
77a92189 2185 return -EEXIST;
d54725cd
PNA
2186 }
2187 } else {
2188 ops = &basechain->ops;
2189 if (ops->hooknum != hook.num ||
2190 ops->priority != hook.priority) {
2191 nft_chain_release_hook(&hook);
98a381a7 2192 NL_SET_BAD_ATTR(extack, attr);
77a92189 2193 return -EEXIST;
d54725cd 2194 }
2c4a488a
PNA
2195 }
2196 nft_chain_release_hook(&hook);
2197 }
2198
2199 if (nla[NFTA_CHAIN_HANDLE] &&
2200 nla[NFTA_CHAIN_NAME]) {
2201 struct nft_chain *chain2;
2202
f102d66b
FW
2203 chain2 = nft_chain_lookup(ctx->net, table,
2204 nla[NFTA_CHAIN_NAME], genmask);
98a381a7
PNA
2205 if (!IS_ERR(chain2)) {
2206 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
0d18779b 2207 return -EEXIST;
98a381a7 2208 }
2c4a488a
PNA
2209 }
2210
2211 if (nla[NFTA_CHAIN_COUNTERS]) {
2212 if (!nft_is_base_chain(chain))
2213 return -EOPNOTSUPP;
2214
2215 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
2216 if (IS_ERR(stats))
2217 return PTR_ERR(stats);
2218 }
2219
c6cc94df 2220 err = -ENOMEM;
2c4a488a
PNA
2221 trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN,
2222 sizeof(struct nft_trans_chain));
c6cc94df
FW
2223 if (trans == NULL)
2224 goto err;
2c4a488a
PNA
2225
2226 nft_trans_chain_stats(trans) = stats;
2227 nft_trans_chain_update(trans) = true;
2228
2229 if (nla[NFTA_CHAIN_POLICY])
2230 nft_trans_chain_policy(trans) = policy;
2231 else
2232 nft_trans_chain_policy(trans) = -1;
2233
c6cc94df
FW
2234 if (nla[NFTA_CHAIN_HANDLE] &&
2235 nla[NFTA_CHAIN_NAME]) {
2236 struct nft_trans *tmp;
2237 char *name;
2238
2239 err = -ENOMEM;
2240 name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL);
2241 if (!name)
2242 goto err;
2243
2244 err = -EEXIST;
2245 list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) {
2246 if (tmp->msg_type == NFT_MSG_NEWCHAIN &&
2247 tmp->ctx.table == table &&
2248 nft_trans_chain_update(tmp) &&
2249 nft_trans_chain_name(tmp) &&
2250 strcmp(name, nft_trans_chain_name(tmp)) == 0) {
98a381a7 2251 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]);
c6cc94df
FW
2252 kfree(name);
2253 goto err;
2254 }
2c4a488a 2255 }
c6cc94df
FW
2256
2257 nft_trans_chain_name(trans) = name;
2c4a488a
PNA
2258 }
2259 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
2260
2261 return 0;
c6cc94df
FW
2262err:
2263 free_percpu(stats);
2264 kfree(trans);
2265 return err;
2c4a488a
PNA
2266}
2267
837830a4
PNA
2268static struct nft_chain *nft_chain_lookup_byid(const struct net *net,
2269 const struct nlattr *nla)
2270{
2271 u32 id = ntohl(nla_get_be32(nla));
2272 struct nft_trans *trans;
2273
2274 list_for_each_entry(trans, &net->nft.commit_list, list) {
2275 struct nft_chain *chain = trans->ctx.chain;
2276
2277 if (trans->msg_type == NFT_MSG_NEWCHAIN &&
2278 id == nft_trans_chain_id(trans))
2279 return chain;
2280 }
2281 return ERR_PTR(-ENOENT);
2282}
2283
633c9a84
PNA
2284static int nf_tables_newchain(struct net *net, struct sock *nlsk,
2285 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
2286 const struct nlattr * const nla[],
2287 struct netlink_ext_ack *extack)
96518518
PM
2288{
2289 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
4035285f
PNA
2290 u8 genmask = nft_genmask_next(net);
2291 int family = nfmsg->nfgen_family;
74cccc3d 2292 struct nft_chain *chain = NULL;
36dd1bcc 2293 const struct nlattr *attr;
96518518 2294 struct nft_table *table;
57de2a0c 2295 u8 policy = NF_ACCEPT;
4035285f 2296 struct nft_ctx ctx;
96518518 2297 u64 handle = 0;
c9626a2c 2298 u32 flags = 0;
96518518 2299
f102d66b
FW
2300 lockdep_assert_held(&net->nft.commit_mutex);
2301
cac20fcd 2302 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
2303 if (IS_ERR(table)) {
2304 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2305 return PTR_ERR(table);
36dd1bcc 2306 }
96518518 2307
96518518 2308 chain = NULL;
36dd1bcc 2309 attr = nla[NFTA_CHAIN_NAME];
96518518
PM
2310
2311 if (nla[NFTA_CHAIN_HANDLE]) {
2312 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
cac20fcd 2313 chain = nft_chain_lookup_byhandle(table, handle, genmask);
36dd1bcc
PNA
2314 if (IS_ERR(chain)) {
2315 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]);
96518518 2316 return PTR_ERR(chain);
36dd1bcc
PNA
2317 }
2318 attr = nla[NFTA_CHAIN_HANDLE];
74cccc3d 2319 } else if (nla[NFTA_CHAIN_NAME]) {
f102d66b 2320 chain = nft_chain_lookup(net, table, attr, genmask);
96518518 2321 if (IS_ERR(chain)) {
36dd1bcc
PNA
2322 if (PTR_ERR(chain) != -ENOENT) {
2323 NL_SET_BAD_ATTR(extack, attr);
96518518 2324 return PTR_ERR(chain);
36dd1bcc 2325 }
96518518
PM
2326 chain = NULL;
2327 }
74cccc3d
PNA
2328 } else if (!nla[NFTA_CHAIN_ID]) {
2329 return -EINVAL;
96518518
PM
2330 }
2331
57de2a0c 2332 if (nla[NFTA_CHAIN_POLICY]) {
f323d954 2333 if (chain != NULL &&
36dd1bcc
PNA
2334 !nft_is_base_chain(chain)) {
2335 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
d6b6cb1d 2336 return -EOPNOTSUPP;
36dd1bcc 2337 }
d6b6cb1d
PNA
2338
2339 if (chain == NULL &&
36dd1bcc
PNA
2340 nla[NFTA_CHAIN_HOOK] == NULL) {
2341 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]);
57de2a0c 2342 return -EOPNOTSUPP;
36dd1bcc 2343 }
57de2a0c 2344
8f46df18 2345 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
57de2a0c
PM
2346 switch (policy) {
2347 case NF_DROP:
2348 case NF_ACCEPT:
2349 break;
2350 default:
2351 return -EINVAL;
2352 }
2353 }
2354
c9626a2c
PNA
2355 if (nla[NFTA_CHAIN_FLAGS])
2356 flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS]));
b717273d
FW
2357 else if (chain)
2358 flags = chain->flags;
c9626a2c 2359
c1f79a2e
PNA
2360 if (flags & ~NFT_CHAIN_FLAGS)
2361 return -EOPNOTSUPP;
2362
98319cb9 2363 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
4035285f 2364
96518518 2365 if (chain != NULL) {
36dd1bcc
PNA
2366 if (nlh->nlmsg_flags & NLM_F_EXCL) {
2367 NL_SET_BAD_ATTR(extack, attr);
96518518 2368 return -EEXIST;
36dd1bcc 2369 }
96518518
PM
2370 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2371 return -EOPNOTSUPP;
2372
67c49de4 2373 flags |= chain->flags & NFT_CHAIN_BASE;
98a381a7
PNA
2374 return nf_tables_updchain(&ctx, genmask, policy, flags, attr,
2375 extack);
96518518
PM
2376 }
2377
c9626a2c 2378 return nf_tables_addchain(&ctx, family, genmask, policy, flags);
96518518
PM
2379}
2380
633c9a84
PNA
2381static int nf_tables_delchain(struct net *net, struct sock *nlsk,
2382 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
2383 const struct nlattr * const nla[],
2384 struct netlink_ext_ack *extack)
96518518
PM
2385{
2386 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 2387 u8 genmask = nft_genmask_next(net);
36dd1bcc
PNA
2388 int family = nfmsg->nfgen_family;
2389 const struct nlattr *attr;
96518518
PM
2390 struct nft_table *table;
2391 struct nft_chain *chain;
9dee1474 2392 struct nft_rule *rule;
91c7b38d 2393 struct nft_ctx ctx;
3ecbfd65 2394 u64 handle;
9dee1474
PNA
2395 u32 use;
2396 int err;
96518518 2397
cac20fcd 2398 table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask);
36dd1bcc
PNA
2399 if (IS_ERR(table)) {
2400 NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]);
96518518 2401 return PTR_ERR(table);
36dd1bcc 2402 }
96518518 2403
3ecbfd65 2404 if (nla[NFTA_CHAIN_HANDLE]) {
36dd1bcc
PNA
2405 attr = nla[NFTA_CHAIN_HANDLE];
2406 handle = be64_to_cpu(nla_get_be64(attr));
cac20fcd 2407 chain = nft_chain_lookup_byhandle(table, handle, genmask);
3ecbfd65 2408 } else {
36dd1bcc 2409 attr = nla[NFTA_CHAIN_NAME];
f102d66b 2410 chain = nft_chain_lookup(net, table, attr, genmask);
3ecbfd65 2411 }
36dd1bcc
PNA
2412 if (IS_ERR(chain)) {
2413 NL_SET_BAD_ATTR(extack, attr);
96518518 2414 return PTR_ERR(chain);
36dd1bcc 2415 }
9dee1474
PNA
2416
2417 if (nlh->nlmsg_flags & NLM_F_NONREC &&
2418 chain->use > 0)
96518518
PM
2419 return -EBUSY;
2420
98319cb9 2421 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0165d932 2422
9dee1474
PNA
2423 use = chain->use;
2424 list_for_each_entry(rule, &chain->rules, list) {
2425 if (!nft_is_active_next(net, rule))
2426 continue;
2427 use--;
2428
2429 err = nft_delrule(&ctx, rule);
2430 if (err < 0)
2431 return err;
2432 }
2433
2434 /* There are rules and elements that are still holding references to us,
2435 * we cannot do a recursive removal in this case.
2436 */
36dd1bcc
PNA
2437 if (use > 0) {
2438 NL_SET_BAD_ATTR(extack, attr);
9dee1474 2439 return -EBUSY;
36dd1bcc 2440 }
9dee1474 2441
ee01d542 2442 return nft_delchain(&ctx);
96518518
PM
2443}
2444
96518518
PM
2445/*
2446 * Expressions
2447 */
2448
2449/**
ef1f7df9 2450 * nft_register_expr - register nf_tables expr type
3db86c39 2451 * @type: expr type
96518518 2452 *
ef1f7df9 2453 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
2454 * success or a negative errno code otherwise.
2455 */
ef1f7df9 2456int nft_register_expr(struct nft_expr_type *type)
96518518
PM
2457{
2458 nfnl_lock(NFNL_SUBSYS_NFTABLES);
758dbcec 2459 if (type->family == NFPROTO_UNSPEC)
e688a7f8 2460 list_add_tail_rcu(&type->list, &nf_tables_expressions);
758dbcec 2461 else
e688a7f8 2462 list_add_rcu(&type->list, &nf_tables_expressions);
96518518
PM
2463 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2464 return 0;
2465}
2466EXPORT_SYMBOL_GPL(nft_register_expr);
2467
2468/**
ef1f7df9 2469 * nft_unregister_expr - unregister nf_tables expr type
3db86c39 2470 * @type: expr type
96518518 2471 *
ef1f7df9 2472 * Unregisters the expr typefor use with nf_tables.
96518518 2473 */
ef1f7df9 2474void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
2475{
2476 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2477 list_del_rcu(&type->list);
96518518
PM
2478 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2479}
2480EXPORT_SYMBOL_GPL(nft_unregister_expr);
2481
64d46806
PM
2482static const struct nft_expr_type *__nft_expr_type_get(u8 family,
2483 struct nlattr *nla)
96518518 2484{
9cff126f 2485 const struct nft_expr_type *type, *candidate = NULL;
96518518 2486
ef1f7df9 2487 list_for_each_entry(type, &nf_tables_expressions, list) {
9cff126f
PNA
2488 if (!nla_strcmp(nla, type->name)) {
2489 if (!type->family && !candidate)
2490 candidate = type;
2491 else if (type->family == family)
2492 candidate = type;
2493 }
96518518 2494 }
9cff126f 2495 return candidate;
96518518
PM
2496}
2497
b9c04ae7
PNA
2498#ifdef CONFIG_MODULES
2499static int nft_expr_type_request_module(struct net *net, u8 family,
2500 struct nlattr *nla)
2501{
eb014de4
PNA
2502 if (nft_request_module(net, "nft-expr-%u-%.*s", family,
2503 nla_len(nla), (char *)nla_data(nla)) == -EAGAIN)
b9c04ae7
PNA
2504 return -EAGAIN;
2505
2506 return 0;
2507}
2508#endif
2509
452238e8
FW
2510static const struct nft_expr_type *nft_expr_type_get(struct net *net,
2511 u8 family,
64d46806 2512 struct nlattr *nla)
96518518 2513{
ef1f7df9 2514 const struct nft_expr_type *type;
96518518
PM
2515
2516 if (nla == NULL)
2517 return ERR_PTR(-EINVAL);
2518
64d46806 2519 type = __nft_expr_type_get(family, nla);
ef1f7df9
PM
2520 if (type != NULL && try_module_get(type->owner))
2521 return type;
96518518 2522
f102d66b 2523 lockdep_nfnl_nft_mutex_not_held();
96518518 2524#ifdef CONFIG_MODULES
ef1f7df9 2525 if (type == NULL) {
b9c04ae7 2526 if (nft_expr_type_request_module(net, family, nla) == -EAGAIN)
64d46806
PM
2527 return ERR_PTR(-EAGAIN);
2528
eb014de4
PNA
2529 if (nft_request_module(net, "nft-expr-%.*s",
2530 nla_len(nla),
2531 (char *)nla_data(nla)) == -EAGAIN)
96518518
PM
2532 return ERR_PTR(-EAGAIN);
2533 }
2534#endif
2535 return ERR_PTR(-ENOENT);
2536}
2537
2538static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
9332d27d
FW
2539 [NFTA_EXPR_NAME] = { .type = NLA_STRING,
2540 .len = NFT_MODULE_AUTOLOAD_LIMIT },
96518518
PM
2541 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
2542};
2543
2544static int nf_tables_fill_expr_info(struct sk_buff *skb,
2545 const struct nft_expr *expr)
2546{
ef1f7df9 2547 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
2548 goto nla_put_failure;
2549
2550 if (expr->ops->dump) {
ae0be8de
MK
2551 struct nlattr *data = nla_nest_start_noflag(skb,
2552 NFTA_EXPR_DATA);
96518518
PM
2553 if (data == NULL)
2554 goto nla_put_failure;
2555 if (expr->ops->dump(skb, expr) < 0)
2556 goto nla_put_failure;
2557 nla_nest_end(skb, data);
2558 }
2559
2560 return skb->len;
2561
2562nla_put_failure:
2563 return -1;
2564};
2565
0b2d8a7b
PM
2566int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
2567 const struct nft_expr *expr)
2568{
2569 struct nlattr *nest;
2570
ae0be8de 2571 nest = nla_nest_start_noflag(skb, attr);
0b2d8a7b
PM
2572 if (!nest)
2573 goto nla_put_failure;
2574 if (nf_tables_fill_expr_info(skb, expr) < 0)
2575 goto nla_put_failure;
2576 nla_nest_end(skb, nest);
2577 return 0;
2578
2579nla_put_failure:
2580 return -1;
2581}
2582
96518518
PM
2583struct nft_expr_info {
2584 const struct nft_expr_ops *ops;
83d9dcba 2585 const struct nlattr *attr;
ef1f7df9 2586 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
2587};
2588
0ca743a5
PNA
2589static int nf_tables_expr_parse(const struct nft_ctx *ctx,
2590 const struct nlattr *nla,
96518518
PM
2591 struct nft_expr_info *info)
2592{
ef1f7df9 2593 const struct nft_expr_type *type;
96518518 2594 const struct nft_expr_ops *ops;
ef1f7df9 2595 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
2596 int err;
2597
8cb08174
JB
2598 err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla,
2599 nft_expr_policy, NULL);
96518518
PM
2600 if (err < 0)
2601 return err;
2602
452238e8 2603 type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
2604 if (IS_ERR(type))
2605 return PTR_ERR(type);
2606
2607 if (tb[NFTA_EXPR_DATA]) {
8cb08174
JB
2608 err = nla_parse_nested_deprecated(info->tb, type->maxattr,
2609 tb[NFTA_EXPR_DATA],
2610 type->policy, NULL);
ef1f7df9
PM
2611 if (err < 0)
2612 goto err1;
2613 } else
2614 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
2615
2616 if (type->select_ops != NULL) {
0ca743a5
PNA
2617 ops = type->select_ops(ctx,
2618 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
2619 if (IS_ERR(ops)) {
2620 err = PTR_ERR(ops);
0ef1efd1
PNA
2621#ifdef CONFIG_MODULES
2622 if (err == -EAGAIN)
eb014de4
PNA
2623 if (nft_expr_type_request_module(ctx->net,
2624 ctx->family,
2625 tb[NFTA_EXPR_NAME]) != -EAGAIN)
2626 err = -ENOENT;
0ef1efd1 2627#endif
ef1f7df9
PM
2628 goto err1;
2629 }
2630 } else
2631 ops = type->ops;
2632
83d9dcba 2633 info->attr = nla;
96518518 2634 info->ops = ops;
83d9dcba 2635
96518518 2636 return 0;
ef1f7df9
PM
2637
2638err1:
2639 module_put(type->owner);
2640 return err;
96518518
PM
2641}
2642
2643static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 2644 const struct nft_expr_info *info,
96518518
PM
2645 struct nft_expr *expr)
2646{
2647 const struct nft_expr_ops *ops = info->ops;
2648 int err;
2649
2650 expr->ops = ops;
2651 if (ops->init) {
ef1f7df9 2652 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
2653 if (err < 0)
2654 goto err1;
2655 }
2656
96518518 2657 return 0;
96518518
PM
2658err1:
2659 expr->ops = NULL;
2660 return err;
2661}
2662
62472bce
PM
2663static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
2664 struct nft_expr *expr)
96518518 2665{
3f3a390d
PNA
2666 const struct nft_expr_type *type = expr->ops->type;
2667
96518518 2668 if (expr->ops->destroy)
62472bce 2669 expr->ops->destroy(ctx, expr);
3f3a390d 2670 module_put(type->owner);
96518518
PM
2671}
2672
795a6d6b
PNA
2673static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
2674 const struct nlattr *nla)
0b2d8a7b
PM
2675{
2676 struct nft_expr_info info;
2677 struct nft_expr *expr;
b8e20400 2678 struct module *owner;
0b2d8a7b
PM
2679 int err;
2680
2681 err = nf_tables_expr_parse(ctx, nla, &info);
2682 if (err < 0)
2683 goto err1;
2684
2685 err = -ENOMEM;
2686 expr = kzalloc(info.ops->size, GFP_KERNEL);
2687 if (expr == NULL)
2688 goto err2;
2689
2690 err = nf_tables_newexpr(ctx, &info, expr);
2691 if (err < 0)
6cafaf47 2692 goto err3;
0b2d8a7b
PM
2693
2694 return expr;
6cafaf47
LZ
2695err3:
2696 kfree(expr);
0b2d8a7b 2697err2:
b8e20400
PNA
2698 owner = info.ops->type->owner;
2699 if (info.ops->type->release_ops)
2700 info.ops->type->release_ops(info.ops);
2701
2702 module_put(owner);
0b2d8a7b
PM
2703err1:
2704 return ERR_PTR(err);
2705}
2706
c604cc69
PNA
2707int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
2708{
2709 int err;
2710
2711 if (src->ops->clone) {
2712 dst->ops = src->ops;
2713 err = src->ops->clone(dst, src);
2714 if (err < 0)
2715 return err;
2716 } else {
2717 memcpy(dst, src, src->ops->size);
2718 }
2719
2720 __module_get(src->ops->type->owner);
2721
2722 return 0;
2723}
2724
0b2d8a7b
PM
2725void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
2726{
2727 nf_tables_expr_destroy(ctx, expr);
2728 kfree(expr);
2729}
2730
96518518
PM
2731/*
2732 * Rules
2733 */
2734
cac20fcd
PNA
2735static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain,
2736 u64 handle)
96518518
PM
2737{
2738 struct nft_rule *rule;
2739
2740 // FIXME: this sucks
d9adf22a 2741 list_for_each_entry_rcu(rule, &chain->rules, list) {
96518518
PM
2742 if (handle == rule->handle)
2743 return rule;
2744 }
2745
2746 return ERR_PTR(-ENOENT);
2747}
2748
cac20fcd
PNA
2749static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain,
2750 const struct nlattr *nla)
96518518
PM
2751{
2752 if (nla == NULL)
2753 return ERR_PTR(-EINVAL);
2754
cac20fcd 2755 return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
96518518
PM
2756}
2757
2758static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
b2fbd044
LZ
2759 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
2760 .len = NFT_TABLE_MAXNAMELEN - 1 },
96518518
PM
2761 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
2762 .len = NFT_CHAIN_MAXNAMELEN - 1 },
2763 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
2764 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
0ca743a5 2765 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 2766 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
2767 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
2768 .len = NFT_USERDATA_MAXLEN },
467697d2 2769 [NFTA_RULE_ID] = { .type = NLA_U32 },
0604628b 2770 [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 },
837830a4 2771 [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
2772};
2773
84d7fce6
PNA
2774static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
2775 u32 portid, u32 seq, int event,
2776 u32 flags, int family,
96518518
PM
2777 const struct nft_table *table,
2778 const struct nft_chain *chain,
2c82c7e7
FW
2779 const struct nft_rule *rule,
2780 const struct nft_rule *prule)
96518518
PM
2781{
2782 struct nlmsghdr *nlh;
2783 struct nfgenmsg *nfmsg;
2784 const struct nft_expr *expr, *next;
2785 struct nlattr *list;
dedb67c4 2786 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
96518518 2787
dedb67c4 2788 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
96518518
PM
2789 if (nlh == NULL)
2790 goto nla_put_failure;
2791
2792 nfmsg = nlmsg_data(nlh);
2793 nfmsg->nfgen_family = family;
2794 nfmsg->version = NFNETLINK_V0;
84d7fce6 2795 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
2796
2797 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
2798 goto nla_put_failure;
2799 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
2800 goto nla_put_failure;
b46f6ded
ND
2801 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
2802 NFTA_RULE_PAD))
96518518
PM
2803 goto nla_put_failure;
2804
2c82c7e7 2805 if (event != NFT_MSG_DELRULE && prule) {
5e948466 2806 if (nla_put_be64(skb, NFTA_RULE_POSITION,
b46f6ded
ND
2807 cpu_to_be64(prule->handle),
2808 NFTA_RULE_PAD))
5e948466
EL
2809 goto nla_put_failure;
2810 }
2811
ae0be8de 2812 list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS);
96518518
PM
2813 if (list == NULL)
2814 goto nla_put_failure;
2815 nft_rule_for_each_expr(expr, next, rule) {
0b2d8a7b 2816 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
96518518 2817 goto nla_put_failure;
96518518
PM
2818 }
2819 nla_nest_end(skb, list);
2820
86f1ec32
PM
2821 if (rule->udata) {
2822 struct nft_userdata *udata = nft_userdata(rule);
2823 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
2824 udata->data) < 0)
2825 goto nla_put_failure;
2826 }
0768b3b3 2827
053c095a
JB
2828 nlmsg_end(skb, nlh);
2829 return 0;
96518518
PM
2830
2831nla_put_failure:
2832 nlmsg_trim(skb, nlh);
2833 return -1;
2834}
2835
25e94a99
PNA
2836static void nf_tables_rule_notify(const struct nft_ctx *ctx,
2837 const struct nft_rule *rule, int event)
96518518
PM
2838{
2839 struct sk_buff *skb;
96518518 2840 int err;
8e6cf365
RGB
2841 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
2842 ctx->table->name, ctx->table->handle,
2843 ctx->chain->name, ctx->chain->handle);
2844
2845 audit_log_nfcfg(buf,
2846 ctx->family,
2847 rule->handle,
2848 event == NFT_MSG_NEWRULE ?
2849 AUDIT_NFT_OP_RULE_REGISTER :
14224039
RGB
2850 AUDIT_NFT_OP_RULE_UNREGISTER,
2851 GFP_KERNEL);
8e6cf365 2852 kfree(buf);
96518518 2853
128ad332
PNA
2854 if (!ctx->report &&
2855 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 2856 return;
96518518 2857
96518518
PM
2858 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2859 if (skb == NULL)
2860 goto err;
2861
84d7fce6 2862 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
36596dad 2863 event, 0, ctx->family, ctx->table,
2c82c7e7 2864 ctx->chain, rule, NULL);
96518518
PM
2865 if (err < 0) {
2866 kfree_skb(skb);
2867 goto err;
2868 }
2869
67cc570e 2870 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 2871 return;
96518518 2872err:
25e94a99 2873 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
96518518
PM
2874}
2875
6e1f760e 2876struct nft_rule_dump_ctx {
e46abbcc 2877 char *table;
b7263e07 2878 char *chain;
6e1f760e
PNA
2879};
2880
241faece
PS
2881static int __nf_tables_dump_rules(struct sk_buff *skb,
2882 unsigned int *idx,
2883 struct netlink_callback *cb,
2884 const struct nft_table *table,
2885 const struct nft_chain *chain)
2886{
2887 struct net *net = sock_net(skb->sk);
2c82c7e7 2888 const struct nft_rule *rule, *prule;
241faece 2889 unsigned int s_idx = cb->args[0];
241faece 2890
2c82c7e7 2891 prule = NULL;
241faece
PS
2892 list_for_each_entry_rcu(rule, &chain->rules, list) {
2893 if (!nft_is_active(net, rule))
2c82c7e7 2894 goto cont_skip;
241faece
PS
2895 if (*idx < s_idx)
2896 goto cont;
2897 if (*idx > s_idx) {
2898 memset(&cb->args[1], 0,
2899 sizeof(cb->args) - sizeof(cb->args[0]));
2900 }
2901 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
2902 cb->nlh->nlmsg_seq,
2903 NFT_MSG_NEWRULE,
2904 NLM_F_MULTI | NLM_F_APPEND,
2905 table->family,
2c82c7e7 2906 table, chain, rule, prule) < 0)
310529e6 2907 return 1;
241faece
PS
2908
2909 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2910cont:
2c82c7e7
FW
2911 prule = rule;
2912cont_skip:
241faece
PS
2913 (*idx)++;
2914 }
310529e6 2915 return 0;
241faece
PS
2916}
2917
96518518
PM
2918static int nf_tables_dump_rules(struct sk_buff *skb,
2919 struct netlink_callback *cb)
2920{
2921 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
6e1f760e 2922 const struct nft_rule_dump_ctx *ctx = cb->data;
241faece 2923 struct nft_table *table;
96518518 2924 const struct nft_chain *chain;
241faece 2925 unsigned int idx = 0;
99633ab2 2926 struct net *net = sock_net(skb->sk);
96518518
PM
2927 int family = nfmsg->nfgen_family;
2928
e688a7f8 2929 rcu_read_lock();
38e029f1
PNA
2930 cb->seq = net->nft.base_seq;
2931
36596dad 2932 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 2933 if (family != NFPROTO_UNSPEC && family != table->family)
36596dad
PNA
2934 continue;
2935
2936 if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0)
96518518
PM
2937 continue;
2938
715849ab 2939 if (ctx && ctx->table && ctx->chain) {
241faece 2940 struct rhlist_head *list, *tmp;
6e1f760e 2941
241faece
PS
2942 list = rhltable_lookup(&table->chains_ht, ctx->chain,
2943 nft_chain_ht_params);
2944 if (!list)
2945 goto done;
2946
2947 rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) {
2948 if (!nft_is_active(net, chain))
2949 continue;
2950 __nf_tables_dump_rules(skb, &idx,
2951 cb, table, chain);
2952 break;
96518518 2953 }
241faece 2954 goto done;
96518518 2955 }
241faece
PS
2956
2957 list_for_each_entry_rcu(chain, &table->chains, list) {
2958 if (__nf_tables_dump_rules(skb, &idx, cb, table, chain))
2959 goto done;
2960 }
2961
2962 if (ctx && ctx->table)
2963 break;
96518518
PM
2964 }
2965done:
e688a7f8 2966 rcu_read_unlock();
310529e6
PS
2967
2968 cb->args[0] = idx;
96518518
PM
2969 return skb->len;
2970}
2971
90fd131a
FW
2972static int nf_tables_dump_rules_start(struct netlink_callback *cb)
2973{
2974 const struct nlattr * const *nla = cb->data;
2975 struct nft_rule_dump_ctx *ctx = NULL;
2976
2977 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
2978 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
2979 if (!ctx)
2980 return -ENOMEM;
2981
2982 if (nla[NFTA_RULE_TABLE]) {
2983 ctx->table = nla_strdup(nla[NFTA_RULE_TABLE],
2984 GFP_ATOMIC);
2985 if (!ctx->table) {
2986 kfree(ctx);
2987 return -ENOMEM;
2988 }
2989 }
2990 if (nla[NFTA_RULE_CHAIN]) {
2991 ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN],
2992 GFP_ATOMIC);
2993 if (!ctx->chain) {
2994 kfree(ctx->table);
2995 kfree(ctx);
2996 return -ENOMEM;
2997 }
2998 }
2999 }
3000
3001 cb->data = ctx;
3002 return 0;
3003}
3004
6e1f760e
PNA
3005static int nf_tables_dump_rules_done(struct netlink_callback *cb)
3006{
e46abbcc
PS
3007 struct nft_rule_dump_ctx *ctx = cb->data;
3008
3009 if (ctx) {
3010 kfree(ctx->table);
b7263e07 3011 kfree(ctx->chain);
e46abbcc
PS
3012 kfree(ctx);
3013 }
6e1f760e
PNA
3014 return 0;
3015}
3016
d9adf22a 3017/* called with rcu_read_lock held */
7b8002a1
PNA
3018static int nf_tables_getrule(struct net *net, struct sock *nlsk,
3019 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3020 const struct nlattr * const nla[],
3021 struct netlink_ext_ack *extack)
96518518
PM
3022{
3023 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3024 u8 genmask = nft_genmask_cur(net);
96518518
PM
3025 const struct nft_chain *chain;
3026 const struct nft_rule *rule;
1b2470e5 3027 struct nft_table *table;
96518518
PM
3028 struct sk_buff *skb2;
3029 int family = nfmsg->nfgen_family;
3030 int err;
3031
3032 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3033 struct netlink_dump_control c = {
90fd131a 3034 .start= nf_tables_dump_rules_start,
96518518 3035 .dump = nf_tables_dump_rules,
6e1f760e 3036 .done = nf_tables_dump_rules_done,
d9adf22a 3037 .module = THIS_MODULE,
90fd131a 3038 .data = (void *)nla,
96518518 3039 };
6e1f760e 3040
d9adf22a 3041 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
96518518
PM
3042 }
3043
cac20fcd 3044 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3045 if (IS_ERR(table)) {
3046 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3047 return PTR_ERR(table);
36dd1bcc 3048 }
96518518 3049
f102d66b 3050 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask);
36dd1bcc
PNA
3051 if (IS_ERR(chain)) {
3052 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
96518518 3053 return PTR_ERR(chain);
36dd1bcc 3054 }
96518518 3055
cac20fcd 3056 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc
PNA
3057 if (IS_ERR(rule)) {
3058 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3059 return PTR_ERR(rule);
36dd1bcc 3060 }
96518518 3061
d9adf22a 3062 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
96518518
PM
3063 if (!skb2)
3064 return -ENOMEM;
3065
84d7fce6 3066 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
96518518 3067 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
2c82c7e7 3068 family, table, chain, rule, NULL);
96518518 3069 if (err < 0)
ee921183 3070 goto err_fill_rule_info;
96518518 3071
ee921183 3072 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
96518518 3073
ee921183 3074err_fill_rule_info:
96518518
PM
3075 kfree_skb(skb2);
3076 return err;
3077}
3078
62472bce
PM
3079static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
3080 struct nft_rule *rule)
96518518 3081{
29e38801 3082 struct nft_expr *expr, *next;
96518518
PM
3083
3084 /*
3085 * Careful: some expressions might not be initialized in case this
3086 * is called on error from nf_tables_newrule().
3087 */
3088 expr = nft_expr_first(rule);
31cc578a 3089 while (nft_expr_more(rule, expr)) {
29e38801 3090 next = nft_expr_next(expr);
62472bce 3091 nf_tables_expr_destroy(ctx, expr);
29e38801 3092 expr = next;
96518518
PM
3093 }
3094 kfree(rule);
3095}
3096
d0e2c7de 3097void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
bb7b40ae 3098{
f6ac8585 3099 nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
bb7b40ae
PNA
3100 nf_tables_rule_destroy(ctx, rule);
3101}
3102
a654de8f
PNA
3103int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain)
3104{
3105 struct nft_expr *expr, *last;
3106 const struct nft_data *data;
3107 struct nft_rule *rule;
3108 int err;
3109
26b2f552
TY
3110 if (ctx->level == NFT_JUMP_STACK_SIZE)
3111 return -EMLINK;
3112
a654de8f
PNA
3113 list_for_each_entry(rule, &chain->rules, list) {
3114 if (!nft_is_active_next(ctx->net, rule))
3115 continue;
3116
3117 nft_rule_for_each_expr(expr, last, rule) {
3118 if (!expr->ops->validate)
3119 continue;
3120
3121 err = expr->ops->validate(ctx, expr, &data);
3122 if (err < 0)
3123 return err;
3124 }
3125 }
3126
3127 return 0;
3128}
3129EXPORT_SYMBOL_GPL(nft_chain_validate);
3130
3131static int nft_table_validate(struct net *net, const struct nft_table *table)
3132{
3133 struct nft_chain *chain;
3134 struct nft_ctx ctx = {
3135 .net = net,
3136 .family = table->family,
3137 };
3138 int err;
3139
3140 list_for_each_entry(chain, &table->chains, list) {
3141 if (!nft_is_base_chain(chain))
3142 continue;
3143
3144 ctx.chain = chain;
3145 err = nft_chain_validate(&ctx, chain);
3146 if (err < 0)
3147 return err;
3148 }
3149
3150 return 0;
3151}
3152
75dd48e2
PS
3153static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3154 const struct nlattr *nla);
3155
1081d11b
PNA
3156#define NFT_RULE_MAXEXPRS 128
3157
633c9a84
PNA
3158static int nf_tables_newrule(struct net *net, struct sock *nlsk,
3159 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3160 const struct nlattr * const nla[],
3161 struct netlink_ext_ack *extack)
96518518
PM
3162{
3163 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3164 u8 genmask = nft_genmask_next(net);
2a43ecf9 3165 struct nft_expr_info *info = NULL;
98319cb9 3166 int family = nfmsg->nfgen_family;
c9626a2c 3167 struct nft_flow_rule *flow;
96518518
PM
3168 struct nft_table *table;
3169 struct nft_chain *chain;
3170 struct nft_rule *rule, *old_rule = NULL;
86f1ec32 3171 struct nft_userdata *udata;
1081d11b 3172 struct nft_trans *trans = NULL;
96518518
PM
3173 struct nft_expr *expr;
3174 struct nft_ctx ctx;
3175 struct nlattr *tmp;
86f1ec32 3176 unsigned int size, i, n, ulen = 0, usize = 0;
96518518 3177 int err, rem;
5e948466 3178 u64 handle, pos_handle;
96518518 3179
f102d66b
FW
3180 lockdep_assert_held(&net->nft.commit_mutex);
3181
cac20fcd 3182 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3183 if (IS_ERR(table)) {
3184 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3185 return PTR_ERR(table);
36dd1bcc 3186 }
96518518 3187
837830a4
PNA
3188 if (nla[NFTA_RULE_CHAIN]) {
3189 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3190 genmask);
3191 if (IS_ERR(chain)) {
3192 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
3193 return PTR_ERR(chain);
3194 }
d0e2c7de
PNA
3195 if (nft_chain_is_bound(chain))
3196 return -EOPNOTSUPP;
3197
837830a4
PNA
3198 } else if (nla[NFTA_RULE_CHAIN_ID]) {
3199 chain = nft_chain_lookup_byid(net, nla[NFTA_RULE_CHAIN_ID]);
3200 if (IS_ERR(chain)) {
3201 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN_ID]);
3202 return PTR_ERR(chain);
3203 }
3204 } else {
3205 return -EINVAL;
36dd1bcc 3206 }
96518518
PM
3207
3208 if (nla[NFTA_RULE_HANDLE]) {
3209 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
cac20fcd 3210 rule = __nft_rule_lookup(chain, handle);
36dd1bcc
PNA
3211 if (IS_ERR(rule)) {
3212 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3213 return PTR_ERR(rule);
36dd1bcc 3214 }
96518518 3215
36dd1bcc
PNA
3216 if (nlh->nlmsg_flags & NLM_F_EXCL) {
3217 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
96518518 3218 return -EEXIST;
36dd1bcc 3219 }
96518518
PM
3220 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3221 old_rule = rule;
3222 else
3223 return -EOPNOTSUPP;
3224 } else {
445509eb
PNA
3225 if (!(nlh->nlmsg_flags & NLM_F_CREATE) ||
3226 nlh->nlmsg_flags & NLM_F_REPLACE)
96518518
PM
3227 return -EINVAL;
3228 handle = nf_tables_alloc_handle(table);
a0a7379e
PNA
3229
3230 if (chain->use == UINT_MAX)
3231 return -EOVERFLOW;
5e948466 3232
447750f2
FW
3233 if (nla[NFTA_RULE_POSITION]) {
3234 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
3235 old_rule = __nft_rule_lookup(chain, pos_handle);
3236 if (IS_ERR(old_rule)) {
3237 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]);
3238 return PTR_ERR(old_rule);
3239 }
75dd48e2
PS
3240 } else if (nla[NFTA_RULE_POSITION_ID]) {
3241 old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]);
3242 if (IS_ERR(old_rule)) {
3243 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]);
3244 return PTR_ERR(old_rule);
3245 }
36dd1bcc 3246 }
5e948466
EL
3247 }
3248
98319cb9 3249 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0ca743a5 3250
96518518
PM
3251 n = 0;
3252 size = 0;
3253 if (nla[NFTA_RULE_EXPRESSIONS]) {
2a43ecf9
FW
3254 info = kvmalloc_array(NFT_RULE_MAXEXPRS,
3255 sizeof(struct nft_expr_info),
3256 GFP_KERNEL);
3257 if (!info)
3258 return -ENOMEM;
3259
96518518
PM
3260 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
3261 err = -EINVAL;
3262 if (nla_type(tmp) != NFTA_LIST_ELEM)
3263 goto err1;
3264 if (n == NFT_RULE_MAXEXPRS)
3265 goto err1;
0ca743a5 3266 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
96518518
PM
3267 if (err < 0)
3268 goto err1;
3269 size += info[n].ops->size;
3270 n++;
3271 }
3272 }
9889840f
PM
3273 /* Check for overflow of dlen field */
3274 err = -EFBIG;
3275 if (size >= 1 << 12)
3276 goto err1;
96518518 3277
86f1ec32 3278 if (nla[NFTA_RULE_USERDATA]) {
0768b3b3 3279 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
86f1ec32
PM
3280 if (ulen > 0)
3281 usize = sizeof(struct nft_userdata) + ulen;
3282 }
0768b3b3 3283
96518518 3284 err = -ENOMEM;
86f1ec32 3285 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
96518518
PM
3286 if (rule == NULL)
3287 goto err1;
3288
889f7ee7 3289 nft_activate_next(net, rule);
0628b123 3290
96518518
PM
3291 rule->handle = handle;
3292 rule->dlen = size;
86f1ec32 3293 rule->udata = ulen ? 1 : 0;
0768b3b3 3294
86f1ec32
PM
3295 if (ulen) {
3296 udata = nft_userdata(rule);
3297 udata->len = ulen - 1;
3298 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
3299 }
96518518 3300
96518518
PM
3301 expr = nft_expr_first(rule);
3302 for (i = 0; i < n; i++) {
3303 err = nf_tables_newexpr(&ctx, &info[i], expr);
83d9dcba
PNA
3304 if (err < 0) {
3305 NL_SET_BAD_ATTR(extack, info[i].attr);
96518518 3306 goto err2;
83d9dcba 3307 }
a654de8f
PNA
3308
3309 if (info[i].ops->validate)
3310 nft_validate_state_update(net, NFT_VALIDATE_NEED);
3311
ef1f7df9 3312 info[i].ops = NULL;
96518518
PM
3313 expr = nft_expr_next(expr);
3314 }
3315
96518518 3316 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
ca089878 3317 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
569ccae6
FW
3318 if (trans == NULL) {
3319 err = -ENOMEM;
3320 goto err2;
3321 }
ca089878
TY
3322 err = nft_delrule(&ctx, old_rule);
3323 if (err < 0) {
3324 nft_trans_destroy(trans);
569ccae6
FW
3325 goto err2;
3326 }
3327
3328 list_add_tail_rcu(&rule->list, &old_rule->list);
3329 } else {
c9626a2c
PNA
3330 trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule);
3331 if (!trans) {
569ccae6
FW
3332 err = -ENOMEM;
3333 goto err2;
3334 }
3335
3336 if (nlh->nlmsg_flags & NLM_F_APPEND) {
3337 if (old_rule)
3338 list_add_rcu(&rule->list, &old_rule->list);
3339 else
3340 list_add_tail_rcu(&rule->list, &chain->rules);
3341 } else {
3342 if (old_rule)
3343 list_add_tail_rcu(&rule->list, &old_rule->list);
3344 else
3345 list_add_rcu(&rule->list, &chain->rules);
3346 }
0628b123 3347 }
2a43ecf9 3348 kvfree(info);
4fefee57 3349 chain->use++;
96518518 3350
a654de8f
PNA
3351 if (net->nft.validate_state == NFT_VALIDATE_DO)
3352 return nft_table_validate(net, table);
3353
c9626a2c 3354 if (chain->flags & NFT_CHAIN_HW_OFFLOAD) {
be2861dc 3355 flow = nft_flow_rule_create(net, rule);
c9626a2c
PNA
3356 if (IS_ERR(flow))
3357 return PTR_ERR(flow);
3358
3359 nft_trans_flow_rule(trans) = flow;
3360 }
3361
a654de8f 3362 return 0;
96518518 3363err2:
bb7b40ae 3364 nf_tables_rule_release(&ctx, rule);
96518518
PM
3365err1:
3366 for (i = 0; i < n; i++) {
b25a31bf 3367 if (info[i].ops) {
ef1f7df9 3368 module_put(info[i].ops->type->owner);
b25a31bf
TY
3369 if (info[i].ops->type->release_ops)
3370 info[i].ops->type->release_ops(info[i].ops);
3371 }
96518518 3372 }
2a43ecf9 3373 kvfree(info);
96518518
PM
3374 return err;
3375}
3376
1a94e38d
PNA
3377static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
3378 const struct nlattr *nla)
3379{
3380 u32 id = ntohl(nla_get_be32(nla));
3381 struct nft_trans *trans;
3382
3383 list_for_each_entry(trans, &net->nft.commit_list, list) {
3384 struct nft_rule *rule = nft_trans_rule(trans);
3385
3386 if (trans->msg_type == NFT_MSG_NEWRULE &&
3387 id == nft_trans_rule_id(trans))
3388 return rule;
3389 }
3390 return ERR_PTR(-ENOENT);
3391}
3392
633c9a84
PNA
3393static int nf_tables_delrule(struct net *net, struct sock *nlsk,
3394 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3395 const struct nlattr * const nla[],
3396 struct netlink_ext_ack *extack)
96518518
PM
3397{
3398 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 3399 u8 genmask = nft_genmask_next(net);
7c95f6d8 3400 struct nft_table *table;
cf9dc09d
PNA
3401 struct nft_chain *chain = NULL;
3402 struct nft_rule *rule;
0628b123
PNA
3403 int family = nfmsg->nfgen_family, err = 0;
3404 struct nft_ctx ctx;
96518518 3405
cac20fcd 3406 table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask);
36dd1bcc
PNA
3407 if (IS_ERR(table)) {
3408 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]);
96518518 3409 return PTR_ERR(table);
36dd1bcc 3410 }
96518518 3411
cf9dc09d 3412 if (nla[NFTA_RULE_CHAIN]) {
f102d66b
FW
3413 chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN],
3414 genmask);
36dd1bcc
PNA
3415 if (IS_ERR(chain)) {
3416 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]);
cf9dc09d 3417 return PTR_ERR(chain);
36dd1bcc 3418 }
d0e2c7de
PNA
3419 if (nft_chain_is_bound(chain))
3420 return -EOPNOTSUPP;
cf9dc09d 3421 }
96518518 3422
98319cb9 3423 nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla);
0628b123 3424
cf9dc09d
PNA
3425 if (chain) {
3426 if (nla[NFTA_RULE_HANDLE]) {
cac20fcd 3427 rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
36dd1bcc
PNA
3428 if (IS_ERR(rule)) {
3429 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]);
cf9dc09d 3430 return PTR_ERR(rule);
36dd1bcc 3431 }
96518518 3432
1a94e38d
PNA
3433 err = nft_delrule(&ctx, rule);
3434 } else if (nla[NFTA_RULE_ID]) {
3435 rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]);
36dd1bcc
PNA
3436 if (IS_ERR(rule)) {
3437 NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]);
1a94e38d 3438 return PTR_ERR(rule);
36dd1bcc 3439 }
1a94e38d 3440
5e266fe7 3441 err = nft_delrule(&ctx, rule);
cf9dc09d 3442 } else {
ce24b721 3443 err = nft_delrule_by_chain(&ctx);
cf9dc09d
PNA
3444 }
3445 } else {
3446 list_for_each_entry(chain, &table->chains, list) {
664b0f8c
PNA
3447 if (!nft_is_active_next(net, chain))
3448 continue;
3449
cf9dc09d 3450 ctx.chain = chain;
ce24b721 3451 err = nft_delrule_by_chain(&ctx);
0628b123
PNA
3452 if (err < 0)
3453 break;
3454 }
3455 }
3456
3457 return err;
3458}
3459
20a69341
PM
3460/*
3461 * Sets
3462 */
e32a4dc6
FW
3463static const struct nft_set_type *nft_set_types[] = {
3464 &nft_set_hash_fast_type,
3465 &nft_set_hash_type,
3466 &nft_set_rhash_type,
3467 &nft_set_bitmap_type,
3468 &nft_set_rbtree_type,
e6abef61 3469#if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
7400b063
SB
3470 &nft_set_pipapo_avx2_type,
3471#endif
e32a4dc6
FW
3472 &nft_set_pipapo_type,
3473};
20a69341 3474
2b664957 3475#define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \
71cc0873
PS
3476 NFT_SET_TIMEOUT | NFT_SET_OBJECT | \
3477 NFT_SET_EVAL)
2b664957 3478
71cc0873 3479static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags)
2b664957 3480{
71cc0873 3481 return (flags & type->features) == (flags & NFT_SET_FEATURES);
2b664957
PNA
3482}
3483
c50b960c
PM
3484/*
3485 * Select a set implementation based on the data characteristics and the
3486 * given policy. The total memory use might not be known if no size is
3487 * given, in that case the amount of memory per element is used.
3488 */
3489static const struct nft_set_ops *
2b664957
PNA
3490nft_select_set_ops(const struct nft_ctx *ctx,
3491 const struct nlattr * const nla[],
c50b960c
PM
3492 const struct nft_set_desc *desc,
3493 enum nft_set_policies policy)
20a69341 3494{
c50b960c
PM
3495 const struct nft_set_ops *ops, *bops;
3496 struct nft_set_estimate est, best;
2b664957
PNA
3497 const struct nft_set_type *type;
3498 u32 flags = 0;
e32a4dc6 3499 int i;
20a69341 3500
f102d66b
FW
3501 lockdep_assert_held(&ctx->net->nft.commit_mutex);
3502 lockdep_nfnl_nft_mutex_not_held();
e32a4dc6 3503
2b664957
PNA
3504 if (nla[NFTA_SET_FLAGS] != NULL)
3505 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
20a69341 3506
55af753c
PNA
3507 bops = NULL;
3508 best.size = ~0;
3509 best.lookup = ~0;
0b5a7874 3510 best.space = ~0;
c50b960c 3511
e32a4dc6
FW
3512 for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) {
3513 type = nft_set_types[i];
71cc0873 3514 ops = &type->ops;
2b664957 3515
71cc0873 3516 if (!nft_set_ops_candidate(type, flags))
20a69341 3517 continue;
2b664957 3518 if (!ops->estimate(desc, flags, &est))
c50b960c
PM
3519 continue;
3520
3521 switch (policy) {
3522 case NFT_SET_POL_PERFORMANCE:
55af753c 3523 if (est.lookup < best.lookup)
c50b960c 3524 break;
644e334e
PNA
3525 if (est.lookup == best.lookup &&
3526 est.space < best.space)
3527 break;
c50b960c
PM
3528 continue;
3529 case NFT_SET_POL_MEMORY:
0b5a7874
PNA
3530 if (!desc->size) {
3531 if (est.space < best.space)
3532 break;
3533 if (est.space == best.space &&
3534 est.lookup < best.lookup)
3535 break;
4f2921ca 3536 } else if (est.size < best.size || !bops) {
c50b960c 3537 break;
0b5a7874 3538 }
c50b960c
PM
3539 continue;
3540 default:
3541 break;
3542 }
3543
c50b960c
PM
3544 bops = ops;
3545 best = est;
20a69341
PM
3546 }
3547
c50b960c
PM
3548 if (bops != NULL)
3549 return bops;
3550
20a69341
PM
3551 return ERR_PTR(-EOPNOTSUPP);
3552}
3553
3554static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
b2fbd044
LZ
3555 [NFTA_SET_TABLE] = { .type = NLA_STRING,
3556 .len = NFT_TABLE_MAXNAMELEN - 1 },
a9bdd836 3557 [NFTA_SET_NAME] = { .type = NLA_STRING,
cb39ad8b 3558 .len = NFT_SET_MAXNAMELEN - 1 },
20a69341
PM
3559 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
3560 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
3561 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
3562 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
3563 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
3564 [NFTA_SET_POLICY] = { .type = NLA_U32 },
3565 [NFTA_SET_DESC] = { .type = NLA_NESTED },
958bee14 3566 [NFTA_SET_ID] = { .type = NLA_U32 },
761da293
PM
3567 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
3568 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
e6d8ecac
CFG
3569 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
3570 .len = NFT_USERDATA_MAXLEN },
8aeff920 3571 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
3ecbfd65 3572 [NFTA_SET_HANDLE] = { .type = NLA_U64 },
65038428 3573 [NFTA_SET_EXPR] = { .type = NLA_NESTED },
48b0ae04 3574 [NFTA_SET_EXPRESSIONS] = { .type = NLA_NESTED },
c50b960c
PM
3575};
3576
3577static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
3578 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
f3a2181e 3579 [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED },
20a69341
PM
3580};
3581
633c9a84 3582static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
3583 const struct sk_buff *skb,
3584 const struct nlmsghdr *nlh,
f2a6d766 3585 const struct nlattr * const nla[],
36dd1bcc 3586 struct netlink_ext_ack *extack,
f2a6d766 3587 u8 genmask)
20a69341
PM
3588{
3589 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
98319cb9 3590 int family = nfmsg->nfgen_family;
7c95f6d8 3591 struct nft_table *table = NULL;
20a69341 3592
20a69341 3593 if (nla[NFTA_SET_TABLE] != NULL) {
cac20fcd
PNA
3594 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family,
3595 genmask);
36dd1bcc
PNA
3596 if (IS_ERR(table)) {
3597 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
20a69341 3598 return PTR_ERR(table);
36dd1bcc 3599 }
20a69341
PM
3600 }
3601
98319cb9 3602 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
20a69341
PM
3603 return 0;
3604}
3605
cac20fcd
PNA
3606static struct nft_set *nft_set_lookup(const struct nft_table *table,
3607 const struct nlattr *nla, u8 genmask)
20a69341
PM
3608{
3609 struct nft_set *set;
3610
3611 if (nla == NULL)
3612 return ERR_PTR(-EINVAL);
3613
d9adf22a 3614 list_for_each_entry_rcu(set, &table->sets, list) {
37a9cc52
PNA
3615 if (!nla_strcmp(nla, set->name) &&
3616 nft_active_genmask(set, genmask))
20a69341
PM
3617 return set;
3618 }
3619 return ERR_PTR(-ENOENT);
3620}
3621
cac20fcd
PNA
3622static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table,
3623 const struct nlattr *nla,
3624 u8 genmask)
3ecbfd65
HS
3625{
3626 struct nft_set *set;
3627
3ecbfd65
HS
3628 list_for_each_entry(set, &table->sets, list) {
3629 if (be64_to_cpu(nla_get_be64(nla)) == set->handle &&
3630 nft_active_genmask(set, genmask))
3631 return set;
3632 }
3633 return ERR_PTR(-ENOENT);
3634}
3635
cac20fcd
PNA
3636static struct nft_set *nft_set_lookup_byid(const struct net *net,
3637 const struct nlattr *nla, u8 genmask)
958bee14
PNA
3638{
3639 struct nft_trans *trans;
3640 u32 id = ntohl(nla_get_be32(nla));
3641
3642 list_for_each_entry(trans, &net->nft.commit_list, list) {
9c7f96fd
AK
3643 if (trans->msg_type == NFT_MSG_NEWSET) {
3644 struct nft_set *set = nft_trans_set(trans);
37a9cc52 3645
9c7f96fd
AK
3646 if (id == nft_trans_set_id(trans) &&
3647 nft_active_genmask(set, genmask))
3648 return set;
3649 }
958bee14
PNA
3650 }
3651 return ERR_PTR(-ENOENT);
3652}
c7a72e3f 3653
10659cba
PNA
3654struct nft_set *nft_set_lookup_global(const struct net *net,
3655 const struct nft_table *table,
3656 const struct nlattr *nla_set_name,
3657 const struct nlattr *nla_set_id,
3658 u8 genmask)
c7a72e3f
PNA
3659{
3660 struct nft_set *set;
3661
cac20fcd 3662 set = nft_set_lookup(table, nla_set_name, genmask);
c7a72e3f
PNA
3663 if (IS_ERR(set)) {
3664 if (!nla_set_id)
3665 return set;
3666
cac20fcd 3667 set = nft_set_lookup_byid(net, nla_set_id, genmask);
c7a72e3f
PNA
3668 }
3669 return set;
3670}
10659cba 3671EXPORT_SYMBOL_GPL(nft_set_lookup_global);
958bee14 3672
20a69341
PM
3673static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
3674 const char *name)
3675{
3676 const struct nft_set *i;
3677 const char *p;
3678 unsigned long *inuse;
60eb1894 3679 unsigned int n = 0, min = 0;
20a69341 3680
38745490 3681 p = strchr(name, '%');
20a69341
PM
3682 if (p != NULL) {
3683 if (p[1] != 'd' || strchr(p + 2, '%'))
3684 return -EINVAL;
3685
3686 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
3687 if (inuse == NULL)
3688 return -ENOMEM;
60eb1894 3689cont:
20a69341 3690 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
3691 int tmp;
3692
37a9cc52
PNA
3693 if (!nft_is_active_next(ctx->net, set))
3694 continue;
14662917 3695 if (!sscanf(i->name, name, &tmp))
20a69341 3696 continue;
60eb1894 3697 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 3698 continue;
14662917 3699
60eb1894 3700 set_bit(tmp - min, inuse);
20a69341
PM
3701 }
3702
53b70287 3703 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
3704 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
3705 min += BITS_PER_BYTE * PAGE_SIZE;
3706 memset(inuse, 0, PAGE_SIZE);
3707 goto cont;
3708 }
20a69341
PM
3709 free_page((unsigned long)inuse);
3710 }
3711
38745490
PS
3712 set->name = kasprintf(GFP_KERNEL, name, min + n);
3713 if (!set->name)
3714 return -ENOMEM;
3715
20a69341 3716 list_for_each_entry(i, &ctx->table->sets, list) {
37a9cc52
PNA
3717 if (!nft_is_active_next(ctx->net, i))
3718 continue;
e63aaaa6
AY
3719 if (!strcmp(set->name, i->name)) {
3720 kfree(set->name);
7fb6f78d 3721 set->name = NULL;
20a69341 3722 return -ENFILE;
e63aaaa6 3723 }
20a69341
PM
3724 }
3725 return 0;
3726}
3727
917d80d3 3728int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result)
8e1102d5
FW
3729{
3730 u64 ms = be64_to_cpu(nla_get_be64(nla));
3731 u64 max = (u64)(~((u64)0));
3732
3733 max = div_u64(max, NSEC_PER_MSEC);
3734 if (ms >= max)
3735 return -ERANGE;
3736
3737 ms *= NSEC_PER_MSEC;
3738 *result = nsecs_to_jiffies64(ms);
3739 return 0;
3740}
3741
917d80d3 3742__be64 nf_jiffies64_to_msecs(u64 input)
8e1102d5 3743{
3b15d09f 3744 return cpu_to_be64(jiffies64_to_msecs(input));
8e1102d5
FW
3745}
3746
f3a2181e
SB
3747static int nf_tables_fill_set_concat(struct sk_buff *skb,
3748 const struct nft_set *set)
3749{
3750 struct nlattr *concat, *field;
3751 int i;
3752
3753 concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT);
3754 if (!concat)
3755 return -ENOMEM;
3756
3757 for (i = 0; i < set->field_count; i++) {
3758 field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
3759 if (!field)
3760 return -ENOMEM;
3761
3762 if (nla_put_be32(skb, NFTA_SET_FIELD_LEN,
3763 htonl(set->field_len[i])))
3764 return -ENOMEM;
3765
3766 nla_nest_end(skb, field);
3767 }
3768
3769 nla_nest_end(skb, concat);
3770
3771 return 0;
3772}
3773
20a69341
PM
3774static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
3775 const struct nft_set *set, u16 event, u16 flags)
3776{
3777 struct nfgenmsg *nfmsg;
3778 struct nlmsghdr *nlh;
128ad332 3779 u32 portid = ctx->portid;
65038428 3780 struct nlattr *nest;
128ad332 3781 u32 seq = ctx->seq;
48b0ae04 3782 int i;
20a69341 3783
dedb67c4 3784 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
20a69341
PM
3785 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3786 flags);
3787 if (nlh == NULL)
3788 goto nla_put_failure;
3789
3790 nfmsg = nlmsg_data(nlh);
36596dad 3791 nfmsg->nfgen_family = ctx->family;
20a69341 3792 nfmsg->version = NFNETLINK_V0;
84d7fce6 3793 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
20a69341
PM
3794
3795 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3796 goto nla_put_failure;
3797 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3798 goto nla_put_failure;
3ecbfd65
HS
3799 if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle),
3800 NFTA_SET_PAD))
3801 goto nla_put_failure;
20a69341
PM
3802 if (set->flags != 0)
3803 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
3804 goto nla_put_failure;
3805
3806 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
3807 goto nla_put_failure;
3808 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
3809 goto nla_put_failure;
3810 if (set->flags & NFT_SET_MAP) {
3811 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
3812 goto nla_put_failure;
3813 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
3814 goto nla_put_failure;
3815 }
8aeff920
PNA
3816 if (set->flags & NFT_SET_OBJECT &&
3817 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
3818 goto nla_put_failure;
20a69341 3819
761da293 3820 if (set->timeout &&
d3e2a111 3821 nla_put_be64(skb, NFTA_SET_TIMEOUT,
8e1102d5 3822 nf_jiffies64_to_msecs(set->timeout),
b46f6ded 3823 NFTA_SET_PAD))
761da293
PM
3824 goto nla_put_failure;
3825 if (set->gc_int &&
3826 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
3827 goto nla_put_failure;
3828
9363dc4b
AB
3829 if (set->policy != NFT_SET_POL_PERFORMANCE) {
3830 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
3831 goto nla_put_failure;
3832 }
3833
6f03bf43
PNA
3834 if (set->udata &&
3835 nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
e6d8ecac
CFG
3836 goto nla_put_failure;
3837
65038428
PNA
3838 nest = nla_nest_start_noflag(skb, NFTA_SET_DESC);
3839 if (!nest)
c50b960c
PM
3840 goto nla_put_failure;
3841 if (set->size &&
3842 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
3843 goto nla_put_failure;
f3a2181e
SB
3844
3845 if (set->field_count > 1 &&
3846 nf_tables_fill_set_concat(skb, set))
3847 goto nla_put_failure;
3848
65038428
PNA
3849 nla_nest_end(skb, nest);
3850
8cfd9b0f 3851 if (set->num_exprs == 1) {
65038428 3852 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR);
8cfd9b0f 3853 if (nf_tables_fill_expr_info(skb, set->exprs[0]) < 0)
65038428
PNA
3854 goto nla_put_failure;
3855
3856 nla_nest_end(skb, nest);
48b0ae04
PNA
3857 } else if (set->num_exprs > 1) {
3858 nest = nla_nest_start_noflag(skb, NFTA_SET_EXPRESSIONS);
3859 if (nest == NULL)
65038428
PNA
3860 goto nla_put_failure;
3861
48b0ae04
PNA
3862 for (i = 0; i < set->num_exprs; i++) {
3863 if (nft_expr_dump(skb, NFTA_LIST_ELEM,
3864 set->exprs[i]) < 0)
3865 goto nla_put_failure;
3866 }
65038428
PNA
3867 nla_nest_end(skb, nest);
3868 }
c50b960c 3869
053c095a
JB
3870 nlmsg_end(skb, nlh);
3871 return 0;
20a69341
PM
3872
3873nla_put_failure:
3874 nlmsg_trim(skb, nlh);
3875 return -1;
3876}
3877
25e94a99
PNA
3878static void nf_tables_set_notify(const struct nft_ctx *ctx,
3879 const struct nft_set *set, int event,
3880 gfp_t gfp_flags)
20a69341
PM
3881{
3882 struct sk_buff *skb;
128ad332 3883 u32 portid = ctx->portid;
20a69341 3884 int err;
8e6cf365
RGB
3885 char *buf = kasprintf(gfp_flags, "%s:%llu;%s:%llu",
3886 ctx->table->name, ctx->table->handle,
3887 set->name, set->handle);
3888
3889 audit_log_nfcfg(buf,
3890 ctx->family,
3891 set->field_count,
3892 event == NFT_MSG_NEWSET ?
3893 AUDIT_NFT_OP_SET_REGISTER :
14224039
RGB
3894 AUDIT_NFT_OP_SET_UNREGISTER,
3895 gfp_flags);
8e6cf365 3896 kfree(buf);
20a69341 3897
128ad332
PNA
3898 if (!ctx->report &&
3899 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
25e94a99 3900 return;
20a69341 3901
31f8441c 3902 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
20a69341
PM
3903 if (skb == NULL)
3904 goto err;
3905
3906 err = nf_tables_fill_set(skb, ctx, set, event, 0);
3907 if (err < 0) {
3908 kfree_skb(skb);
3909 goto err;
3910 }
3911
67cc570e 3912 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 3913 return;
20a69341 3914err:
25e94a99 3915 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
20a69341
PM
3916}
3917
5b96af77 3918static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
c9c8e485
PNA
3919{
3920 const struct nft_set *set;
3921 unsigned int idx, s_idx = cb->args[0];
c9c8e485
PNA
3922 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
3923 struct net *net = sock_net(skb->sk);
5b96af77 3924 struct nft_ctx *ctx = cb->data, ctx_set;
c9c8e485
PNA
3925
3926 if (cb->args[1])
3927 return skb->len;
3928
e688a7f8 3929 rcu_read_lock();
38e029f1
PNA
3930 cb->seq = net->nft.base_seq;
3931
36596dad
PNA
3932 list_for_each_entry_rcu(table, &net->nft.tables, list) {
3933 if (ctx->family != NFPROTO_UNSPEC &&
98319cb9 3934 ctx->family != table->family)
36596dad
PNA
3935 continue;
3936
3937 if (ctx->table && ctx->table != table)
5b96af77
PNA
3938 continue;
3939
36596dad
PNA
3940 if (cur_table) {
3941 if (cur_table != table)
c9c8e485
PNA
3942 continue;
3943
36596dad 3944 cur_table = NULL;
c9c8e485 3945 }
36596dad
PNA
3946 idx = 0;
3947 list_for_each_entry_rcu(set, &table->sets, list) {
3948 if (idx < s_idx)
3949 goto cont;
3950 if (!nft_is_active(net, set))
3951 goto cont;
5b96af77 3952
36596dad
PNA
3953 ctx_set = *ctx;
3954 ctx_set.table = table;
98319cb9 3955 ctx_set.family = table->family;
c9c8e485 3956
36596dad
PNA
3957 if (nf_tables_fill_set(skb, &ctx_set, set,
3958 NFT_MSG_NEWSET,
3959 NLM_F_MULTI) < 0) {
3960 cb->args[0] = idx;
3961 cb->args[2] = (unsigned long) table;
3962 goto done;
c9c8e485 3963 }
36596dad 3964 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
c9c8e485 3965cont:
36596dad 3966 idx++;
c9c8e485 3967 }
36596dad
PNA
3968 if (s_idx)
3969 s_idx = 0;
c9c8e485
PNA
3970 }
3971 cb->args[1] = 1;
3972done:
e688a7f8 3973 rcu_read_unlock();
c9c8e485
PNA
3974 return skb->len;
3975}
3976
90fd131a
FW
3977static int nf_tables_dump_sets_start(struct netlink_callback *cb)
3978{
3979 struct nft_ctx *ctx_dump = NULL;
3980
3981 ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC);
3982 if (ctx_dump == NULL)
3983 return -ENOMEM;
3984
3985 cb->data = ctx_dump;
3986 return 0;
3987}
3988
5b96af77 3989static int nf_tables_dump_sets_done(struct netlink_callback *cb)
20a69341 3990{
5b96af77
PNA
3991 kfree(cb->data);
3992 return 0;
20a69341
PM
3993}
3994
d9adf22a 3995/* called with rcu_read_lock held */
7b8002a1
PNA
3996static int nf_tables_getset(struct net *net, struct sock *nlsk,
3997 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
3998 const struct nlattr * const nla[],
3999 struct netlink_ext_ack *extack)
20a69341 4000{
f2a6d766 4001 u8 genmask = nft_genmask_cur(net);
20a69341
PM
4002 const struct nft_set *set;
4003 struct nft_ctx ctx;
4004 struct sk_buff *skb2;
c9c8e485 4005 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
4006 int err;
4007
01cfa0a4 4008 /* Verify existence before starting dump */
36dd1bcc
PNA
4009 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
4010 genmask);
20a69341
PM
4011 if (err < 0)
4012 return err;
4013
4014 if (nlh->nlmsg_flags & NLM_F_DUMP) {
4015 struct netlink_dump_control c = {
90fd131a 4016 .start = nf_tables_dump_sets_start,
20a69341 4017 .dump = nf_tables_dump_sets,
5b96af77 4018 .done = nf_tables_dump_sets_done,
90fd131a 4019 .data = &ctx,
d9adf22a 4020 .module = THIS_MODULE,
20a69341 4021 };
5b96af77 4022
d9adf22a 4023 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
20a69341
PM
4024 }
4025
c9c8e485
PNA
4026 /* Only accept unspec with dump */
4027 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
4028 return -EAFNOSUPPORT;
eaa2bcd6
PT
4029 if (!nla[NFTA_SET_TABLE])
4030 return -EINVAL;
c9c8e485 4031
cac20fcd 4032 set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
20a69341
PM
4033 if (IS_ERR(set))
4034 return PTR_ERR(set);
4035
d9adf22a 4036 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
20a69341
PM
4037 if (skb2 == NULL)
4038 return -ENOMEM;
4039
4040 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
4041 if (err < 0)
ee921183 4042 goto err_fill_set_info;
20a69341 4043
ee921183 4044 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
20a69341 4045
ee921183 4046err_fill_set_info:
20a69341
PM
4047 kfree_skb(skb2);
4048 return err;
4049}
4050
f3a2181e
SB
4051static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = {
4052 [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 },
4053};
4054
4055static int nft_set_desc_concat_parse(const struct nlattr *attr,
4056 struct nft_set_desc *desc)
4057{
4058 struct nlattr *tb[NFTA_SET_FIELD_MAX + 1];
4059 u32 len;
4060 int err;
4061
4062 err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr,
4063 nft_concat_policy, NULL);
4064 if (err < 0)
4065 return err;
4066
4067 if (!tb[NFTA_SET_FIELD_LEN])
4068 return -EINVAL;
4069
4070 len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN]));
4071
4072 if (len * BITS_PER_BYTE / 32 > NFT_REG32_COUNT)
4073 return -E2BIG;
4074
4075 desc->field_len[desc->field_count++] = len;
4076
4077 return 0;
4078}
4079
4080static int nft_set_desc_concat(struct nft_set_desc *desc,
4081 const struct nlattr *nla)
4082{
4083 struct nlattr *attr;
4084 int rem, err;
4085
4086 nla_for_each_nested(attr, nla, rem) {
4087 if (nla_type(attr) != NFTA_LIST_ELEM)
4088 return -EINVAL;
4089
4090 err = nft_set_desc_concat_parse(attr, desc);
4091 if (err < 0)
4092 return err;
4093 }
4094
4095 return 0;
4096}
4097
f7e840ee 4098static int nf_tables_set_desc_parse(struct nft_set_desc *desc,
c50b960c
PM
4099 const struct nlattr *nla)
4100{
4101 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
4102 int err;
4103
8cb08174
JB
4104 err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla,
4105 nft_set_desc_policy, NULL);
c50b960c
PM
4106 if (err < 0)
4107 return err;
4108
4109 if (da[NFTA_SET_DESC_SIZE] != NULL)
4110 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
f3a2181e
SB
4111 if (da[NFTA_SET_DESC_CONCAT])
4112 err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]);
c50b960c 4113
f3a2181e 4114 return err;
c50b960c
PM
4115}
4116
633c9a84
PNA
4117static int nf_tables_newset(struct net *net, struct sock *nlsk,
4118 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
4119 const struct nlattr * const nla[],
4120 struct netlink_ext_ack *extack)
20a69341
PM
4121{
4122 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 4123 u8 genmask = nft_genmask_next(net);
98319cb9 4124 int family = nfmsg->nfgen_family;
20a69341 4125 const struct nft_set_ops *ops;
65038428 4126 struct nft_expr *expr = NULL;
20a69341
PM
4127 struct nft_table *table;
4128 struct nft_set *set;
4129 struct nft_ctx ctx;
38745490 4130 char *name;
4ef360dd 4131 u64 size;
761da293 4132 u64 timeout;
8aeff920 4133 u32 ktype, dtype, flags, policy, gc_int, objtype;
c50b960c 4134 struct nft_set_desc desc;
e6d8ecac
CFG
4135 unsigned char *udata;
4136 u16 udlen;
20a69341 4137 int err;
f3a2181e 4138 int i;
20a69341
PM
4139
4140 if (nla[NFTA_SET_TABLE] == NULL ||
4141 nla[NFTA_SET_NAME] == NULL ||
958bee14
PNA
4142 nla[NFTA_SET_KEY_LEN] == NULL ||
4143 nla[NFTA_SET_ID] == NULL)
20a69341
PM
4144 return -EINVAL;
4145
c50b960c
PM
4146 memset(&desc, 0, sizeof(desc));
4147
20a69341
PM
4148 ktype = NFT_DATA_VALUE;
4149 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
4150 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
4151 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
4152 return -EINVAL;
4153 }
4154
c50b960c 4155 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
7d740264 4156 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
4157 return -EINVAL;
4158
4159 flags = 0;
4160 if (nla[NFTA_SET_FLAGS] != NULL) {
4161 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4162 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
7c6c6e95 4163 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
8aeff920 4164 NFT_SET_MAP | NFT_SET_EVAL |
ef516e86 4165 NFT_SET_OBJECT | NFT_SET_CONCAT))
d9583cdf 4166 return -EOPNOTSUPP;
8aeff920 4167 /* Only one of these operations is supported */
acab7131
FW
4168 if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) ==
4169 (NFT_SET_MAP | NFT_SET_OBJECT))
4170 return -EOPNOTSUPP;
4171 if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) ==
4172 (NFT_SET_EVAL | NFT_SET_OBJECT))
7c6c6e95 4173 return -EOPNOTSUPP;
20a69341
PM
4174 }
4175
4176 dtype = 0;
20a69341
PM
4177 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
4178 if (!(flags & NFT_SET_MAP))
4179 return -EINVAL;
4180
4181 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
4182 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
4183 dtype != NFT_DATA_VERDICT)
4184 return -EINVAL;
4185
4186 if (dtype != NFT_DATA_VERDICT) {
4187 if (nla[NFTA_SET_DATA_LEN] == NULL)
4188 return -EINVAL;
c50b960c 4189 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
7d740264 4190 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
4191 return -EINVAL;
4192 } else
7d740264 4193 desc.dlen = sizeof(struct nft_verdict);
20a69341
PM
4194 } else if (flags & NFT_SET_MAP)
4195 return -EINVAL;
4196
8aeff920
PNA
4197 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
4198 if (!(flags & NFT_SET_OBJECT))
4199 return -EINVAL;
4200
4201 objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
4202 if (objtype == NFT_OBJECT_UNSPEC ||
4203 objtype > NFT_OBJECT_MAX)
d9583cdf 4204 return -EOPNOTSUPP;
8aeff920
PNA
4205 } else if (flags & NFT_SET_OBJECT)
4206 return -EINVAL;
4207 else
4208 objtype = NFT_OBJECT_UNSPEC;
4209
761da293
PM
4210 timeout = 0;
4211 if (nla[NFTA_SET_TIMEOUT] != NULL) {
4212 if (!(flags & NFT_SET_TIMEOUT))
4213 return -EINVAL;
8e1102d5
FW
4214
4215 err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout);
4216 if (err)
4217 return err;
761da293
PM
4218 }
4219 gc_int = 0;
4220 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
4221 if (!(flags & NFT_SET_TIMEOUT))
4222 return -EINVAL;
4223 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
4224 }
4225
c50b960c
PM
4226 policy = NFT_SET_POL_PERFORMANCE;
4227 if (nla[NFTA_SET_POLICY] != NULL)
4228 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
4229
4230 if (nla[NFTA_SET_DESC] != NULL) {
f7e840ee 4231 err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]);
c50b960c
PM
4232 if (err < 0)
4233 return err;
4234 }
4235
48b0ae04 4236 if (nla[NFTA_SET_EXPR] || nla[NFTA_SET_EXPRESSIONS])
d56aab26
PNA
4237 desc.expr = true;
4238
cac20fcd 4239 table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask);
36dd1bcc
PNA
4240 if (IS_ERR(table)) {
4241 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]);
20a69341 4242 return PTR_ERR(table);
36dd1bcc 4243 }
20a69341 4244
98319cb9 4245 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
20a69341 4246
cac20fcd 4247 set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask);
20a69341 4248 if (IS_ERR(set)) {
36dd1bcc
PNA
4249 if (PTR_ERR(set) != -ENOENT) {
4250 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 4251 return PTR_ERR(set);
36dd1bcc 4252 }
1a28ad74 4253 } else {
36dd1bcc
PNA
4254 if (nlh->nlmsg_flags & NLM_F_EXCL) {
4255 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]);
20a69341 4256 return -EEXIST;
36dd1bcc 4257 }
20a69341
PM
4258 if (nlh->nlmsg_flags & NLM_F_REPLACE)
4259 return -EOPNOTSUPP;
36dd1bcc 4260
20a69341
PM
4261 return 0;
4262 }
4263
4264 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
4265 return -ENOENT;
4266
2b664957 4267 ops = nft_select_set_ops(&ctx, nla, &desc, policy);
20a69341
PM
4268 if (IS_ERR(ops))
4269 return PTR_ERR(ops);
4270
e6d8ecac
CFG
4271 udlen = 0;
4272 if (nla[NFTA_SET_USERDATA])
4273 udlen = nla_len(nla[NFTA_SET_USERDATA]);
4274
20a69341
PM
4275 size = 0;
4276 if (ops->privsize != NULL)
347b408d 4277 size = ops->privsize(nla, &desc);
20a69341 4278
1ff75a3e 4279 set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
24d19826
FW
4280 if (!set)
4281 return -ENOMEM;
20a69341 4282
38745490
PS
4283 name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL);
4284 if (!name) {
4285 err = -ENOMEM;
65038428 4286 goto err_set_name;
38745490
PS
4287 }
4288
20a69341 4289 err = nf_tables_set_alloc_name(&ctx, set, name);
38745490 4290 kfree(name);
20a69341 4291 if (err < 0)
65038428
PNA
4292 goto err_set_alloc_name;
4293
4294 if (nla[NFTA_SET_EXPR]) {
4295 expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]);
4296 if (IS_ERR(expr)) {
4297 err = PTR_ERR(expr);
4298 goto err_set_alloc_name;
4299 }
8cfd9b0f
PNA
4300 set->exprs[0] = expr;
4301 set->num_exprs++;
48b0ae04
PNA
4302 } else if (nla[NFTA_SET_EXPRESSIONS]) {
4303 struct nft_expr *expr;
4304 struct nlattr *tmp;
4305 int left;
4306
4307 i = 0;
4308 nla_for_each_nested(tmp, nla[NFTA_SET_EXPRESSIONS], left) {
4309 if (i == NFT_SET_EXPR_MAX) {
4310 err = -E2BIG;
4311 goto err_set_init;
4312 }
4313 if (nla_type(tmp) != NFTA_LIST_ELEM) {
4314 err = -EINVAL;
4315 goto err_set_init;
4316 }
4317 expr = nft_set_elem_expr_alloc(&ctx, set, tmp);
4318 if (IS_ERR(expr)) {
4319 err = PTR_ERR(expr);
4320 goto err_set_init;
4321 }
4322 set->exprs[i++] = expr;
4323 set->num_exprs++;
4324 }
65038428 4325 }
20a69341 4326
e6d8ecac
CFG
4327 udata = NULL;
4328 if (udlen) {
4329 udata = set->data + size;
4330 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
4331 }
4332
20a69341 4333 INIT_LIST_HEAD(&set->bindings);
3453c927
PNA
4334 set->table = table;
4335 write_pnet(&set->net, net);
20a69341
PM
4336 set->ops = ops;
4337 set->ktype = ktype;
c50b960c 4338 set->klen = desc.klen;
20a69341 4339 set->dtype = dtype;
8aeff920 4340 set->objtype = objtype;
c50b960c 4341 set->dlen = desc.dlen;
20a69341 4342 set->flags = flags;
c50b960c 4343 set->size = desc.size;
9363dc4b 4344 set->policy = policy;
e6d8ecac
CFG
4345 set->udlen = udlen;
4346 set->udata = udata;
761da293
PM
4347 set->timeout = timeout;
4348 set->gc_int = gc_int;
3ecbfd65 4349 set->handle = nf_tables_alloc_handle(table);
20a69341 4350
f3a2181e
SB
4351 set->field_count = desc.field_count;
4352 for (i = 0; i < desc.field_count; i++)
4353 set->field_len[i] = desc.field_len[i];
4354
c50b960c 4355 err = ops->init(set, &desc, nla);
20a69341 4356 if (err < 0)
65038428 4357 goto err_set_init;
20a69341 4358
958bee14 4359 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
20a69341 4360 if (err < 0)
65038428 4361 goto err_set_trans;
20a69341 4362
e688a7f8 4363 list_add_tail_rcu(&set->list, &table->sets);
4fefee57 4364 table->use++;
20a69341
PM
4365 return 0;
4366
65038428 4367err_set_trans:
c17c3cdf 4368 ops->destroy(set);
65038428 4369err_set_init:
8cfd9b0f
PNA
4370 for (i = 0; i < set->num_exprs; i++)
4371 nft_expr_destroy(&ctx, set->exprs[i]);
65038428 4372err_set_alloc_name:
2f6adf48 4373 kfree(set->name);
65038428 4374err_set_name:
1ff75a3e 4375 kvfree(set);
20a69341
PM
4376 return err;
4377}
4378
0c2a85ed 4379static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
20a69341 4380{
8cfd9b0f
PNA
4381 int i;
4382
273fe3f1
PNA
4383 if (WARN_ON(set->use > 0))
4384 return;
4385
8cfd9b0f
PNA
4386 for (i = 0; i < set->num_exprs; i++)
4387 nft_expr_destroy(ctx, set->exprs[i]);
65038428 4388
20a69341 4389 set->ops->destroy(set);
38745490 4390 kfree(set->name);
1ff75a3e 4391 kvfree(set);
20a69341
PM
4392}
4393
633c9a84
PNA
4394static int nf_tables_delset(struct net *net, struct sock *nlsk,
4395 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
4396 const struct nlattr * const nla[],
4397 struct netlink_ext_ack *extack)
20a69341 4398{
c9c8e485 4399 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
f2a6d766 4400 u8 genmask = nft_genmask_next(net);
36dd1bcc 4401 const struct nlattr *attr;
20a69341
PM
4402 struct nft_set *set;
4403 struct nft_ctx ctx;
4404 int err;
4405
ec2c9935
PM
4406 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
4407 return -EAFNOSUPPORT;
20a69341
PM
4408 if (nla[NFTA_SET_TABLE] == NULL)
4409 return -EINVAL;
4410
36dd1bcc
PNA
4411 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack,
4412 genmask);
20a69341
PM
4413 if (err < 0)
4414 return err;
4415
36dd1bcc
PNA
4416 if (nla[NFTA_SET_HANDLE]) {
4417 attr = nla[NFTA_SET_HANDLE];
4418 set = nft_set_lookup_byhandle(ctx.table, attr, genmask);
4419 } else {
4420 attr = nla[NFTA_SET_NAME];
4421 set = nft_set_lookup(ctx.table, attr, genmask);
4422 }
a8278400 4423
36dd1bcc
PNA
4424 if (IS_ERR(set)) {
4425 NL_SET_BAD_ATTR(extack, attr);
4426 return PTR_ERR(set);
4427 }
273fe3f1 4428 if (set->use ||
36dd1bcc
PNA
4429 (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) {
4430 NL_SET_BAD_ATTR(extack, attr);
20a69341 4431 return -EBUSY;
36dd1bcc 4432 }
20a69341 4433
ee01d542 4434 return nft_delset(&ctx, set);
20a69341
PM
4435}
4436
4437static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
de70185d 4438 struct nft_set *set,
20a69341 4439 const struct nft_set_iter *iter,
de70185d 4440 struct nft_set_elem *elem)
20a69341 4441{
fe2811eb 4442 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
4443 enum nft_registers dreg;
4444
4445 dreg = nft_type_to_reg(set->dtype);
1ec10212
PM
4446 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
4447 set->dtype == NFT_DATA_VERDICT ?
4448 NFT_DATA_VERDICT : NFT_DATA_VALUE,
4449 set->dlen);
20a69341
PM
4450}
4451
4452int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
4453 struct nft_set_binding *binding)
4454{
4455 struct nft_set_binding *i;
4456 struct nft_set_iter iter;
4457
273fe3f1
PNA
4458 if (set->use == UINT_MAX)
4459 return -EOVERFLOW;
4460
408070d6 4461 if (!list_empty(&set->bindings) && nft_set_is_anonymous(set))
20a69341
PM
4462 return -EBUSY;
4463
11113e19 4464 if (binding->flags & NFT_SET_MAP) {
20a69341
PM
4465 /* If the set is already bound to the same chain all
4466 * jumps are already validated for that chain.
4467 */
4468 list_for_each_entry(i, &set->bindings, list) {
a4684402 4469 if (i->flags & NFT_SET_MAP &&
11113e19 4470 i->chain == binding->chain)
20a69341
PM
4471 goto bind;
4472 }
4473
8588ac09 4474 iter.genmask = nft_genmask_next(ctx->net);
20a69341
PM
4475 iter.skip = 0;
4476 iter.count = 0;
4477 iter.err = 0;
4478 iter.fn = nf_tables_bind_check_setelem;
4479
4480 set->ops->walk(ctx, set, &iter);
a02f4248 4481 if (iter.err < 0)
20a69341 4482 return iter.err;
20a69341
PM
4483 }
4484bind:
4485 binding->chain = ctx->chain;
e688a7f8 4486 list_add_tail_rcu(&binding->list, &set->bindings);
f6ac8585 4487 nft_set_trans_bind(ctx, set);
273fe3f1 4488 set->use++;
f6ac8585 4489
20a69341
PM
4490 return 0;
4491}
63aea290 4492EXPORT_SYMBOL_GPL(nf_tables_bind_set);
20a69341 4493
3b0a081d
FW
4494static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
4495 struct nft_set_binding *binding, bool event)
20a69341 4496{
e688a7f8 4497 list_del_rcu(&binding->list);
20a69341 4498
f6ac8585 4499 if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) {
cd5125d8 4500 list_del_rcu(&set->list);
f6ac8585
PNA
4501 if (event)
4502 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET,
4503 GFP_KERNEL);
4504 }
20a69341
PM
4505}
4506
273fe3f1
PNA
4507void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
4508 struct nft_set_binding *binding,
4509 enum nft_trans_phase phase)
4510{
4511 switch (phase) {
4512 case NFT_TRANS_PREPARE:
4513 set->use--;
4514 return;
4515 case NFT_TRANS_ABORT:
4516 case NFT_TRANS_RELEASE:
4517 set->use--;
954d8297 4518 fallthrough;
273fe3f1
PNA
4519 default:
4520 nf_tables_unbind_set(ctx, set, binding,
4521 phase == NFT_TRANS_COMMIT);
4522 }
4523}
4524EXPORT_SYMBOL_GPL(nf_tables_deactivate_set);
4525
cd5125d8
FW
4526void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set)
4527{
f6ac8585 4528 if (list_empty(&set->bindings) && nft_set_is_anonymous(set))
0c2a85ed 4529 nft_set_destroy(ctx, set);
cd5125d8
FW
4530}
4531EXPORT_SYMBOL_GPL(nf_tables_destroy_set);
4532
3ac4c07a
PM
4533const struct nft_set_ext_type nft_set_ext_types[] = {
4534 [NFT_SET_EXT_KEY] = {
7d740264 4535 .align = __alignof__(u32),
3ac4c07a
PM
4536 },
4537 [NFT_SET_EXT_DATA] = {
7d740264 4538 .align = __alignof__(u32),
3ac4c07a 4539 },
563125a7
PNA
4540 [NFT_SET_EXT_EXPRESSIONS] = {
4541 .align = __alignof__(struct nft_set_elem_expr),
f25ad2e9 4542 },
8aeff920
PNA
4543 [NFT_SET_EXT_OBJREF] = {
4544 .len = sizeof(struct nft_object *),
4545 .align = __alignof__(struct nft_object *),
4546 },
3ac4c07a
PM
4547 [NFT_SET_EXT_FLAGS] = {
4548 .len = sizeof(u8),
4549 .align = __alignof__(u8),
4550 },
c3e1b005
PM
4551 [NFT_SET_EXT_TIMEOUT] = {
4552 .len = sizeof(u64),
4553 .align = __alignof__(u64),
4554 },
4555 [NFT_SET_EXT_EXPIRATION] = {
8e1102d5
FW
4556 .len = sizeof(u64),
4557 .align = __alignof__(u64),
c3e1b005 4558 },
68e942e8
PM
4559 [NFT_SET_EXT_USERDATA] = {
4560 .len = sizeof(struct nft_userdata),
4561 .align = __alignof__(struct nft_userdata),
4562 },
7b225d0b
PNA
4563 [NFT_SET_EXT_KEY_END] = {
4564 .align = __alignof__(u32),
4565 },
3ac4c07a 4566};
3ac4c07a 4567
20a69341
PM
4568/*
4569 * Set elements
4570 */
4571
4572static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
4573 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
4574 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
4575 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
c3e1b005 4576 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
79ebb5bb 4577 [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
68e942e8
PM
4578 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
4579 .len = NFT_USERDATA_MAXLEN },
467697d2 4580 [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
9332d27d
FW
4581 [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING,
4582 .len = NFT_OBJ_MAXNAMELEN - 1 },
7b225d0b 4583 [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED },
48b0ae04 4584 [NFTA_SET_ELEM_EXPRESSIONS] = { .type = NLA_NESTED },
20a69341
PM
4585};
4586
4587static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
b2fbd044
LZ
4588 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
4589 .len = NFT_TABLE_MAXNAMELEN - 1 },
4590 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
4591 .len = NFT_SET_MAXNAMELEN - 1 },
20a69341 4592 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
958bee14 4593 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
20a69341
PM
4594};
4595
633c9a84 4596static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
4597 const struct sk_buff *skb,
4598 const struct nlmsghdr *nlh,
f2a6d766 4599 const struct nlattr * const nla[],
36dd1bcc 4600 struct netlink_ext_ack *extack,
f2a6d766 4601 u8 genmask)
20a69341
PM
4602{
4603 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
98319cb9 4604 int family = nfmsg->nfgen_family;
7c95f6d8 4605 struct nft_table *table;
20a69341 4606
cac20fcd
PNA
4607 table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family,
4608 genmask);
36dd1bcc
PNA
4609 if (IS_ERR(table)) {
4610 NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]);
20a69341 4611 return PTR_ERR(table);
36dd1bcc 4612 }
20a69341 4613
98319cb9 4614 nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla);
20a69341
PM
4615 return 0;
4616}
4617
563125a7
PNA
4618static int nft_set_elem_expr_dump(struct sk_buff *skb,
4619 const struct nft_set *set,
4620 const struct nft_set_ext *ext)
4621{
4622 struct nft_set_elem_expr *elem_expr;
4623 u32 size, num_exprs = 0;
4624 struct nft_expr *expr;
48b0ae04 4625 struct nlattr *nest;
563125a7
PNA
4626
4627 elem_expr = nft_set_ext_expr(ext);
4628 nft_setelem_expr_foreach(expr, elem_expr, size)
4629 num_exprs++;
4630
4631 if (num_exprs == 1) {
4632 expr = nft_setelem_expr_at(elem_expr, 0);
4633 if (nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, expr) < 0)
4634 return -1;
4635
4636 return 0;
48b0ae04
PNA
4637 } else if (num_exprs > 1) {
4638 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_EXPRESSIONS);
4639 if (nest == NULL)
4640 goto nla_put_failure;
563125a7 4641
48b0ae04
PNA
4642 nft_setelem_expr_foreach(expr, elem_expr, size) {
4643 expr = nft_setelem_expr_at(elem_expr, size);
4644 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
4645 goto nla_put_failure;
4646 }
4647 nla_nest_end(skb, nest);
4648 }
563125a7 4649 return 0;
48b0ae04
PNA
4650
4651nla_put_failure:
4652 return -1;
563125a7
PNA
4653}
4654
20a69341
PM
4655static int nf_tables_fill_setelem(struct sk_buff *skb,
4656 const struct nft_set *set,
4657 const struct nft_set_elem *elem)
4658{
fe2811eb 4659 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
4660 unsigned char *b = skb_tail_pointer(skb);
4661 struct nlattr *nest;
4662
ae0be8de 4663 nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM);
20a69341
PM
4664 if (nest == NULL)
4665 goto nla_put_failure;
4666
fe2811eb
PM
4667 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
4668 NFT_DATA_VALUE, set->klen) < 0)
20a69341
PM
4669 goto nla_put_failure;
4670
7b225d0b
PNA
4671 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) &&
4672 nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext),
4673 NFT_DATA_VALUE, set->klen) < 0)
4674 goto nla_put_failure;
4675
fe2811eb
PM
4676 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
4677 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
20a69341
PM
4678 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
4679 set->dlen) < 0)
4680 goto nla_put_failure;
4681
563125a7
PNA
4682 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS) &&
4683 nft_set_elem_expr_dump(skb, set, ext))
f25ad2e9
PM
4684 goto nla_put_failure;
4685
8aeff920
PNA
4686 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
4687 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
d152159b 4688 (*nft_set_ext_obj(ext))->key.name) < 0)
8aeff920
PNA
4689 goto nla_put_failure;
4690
fe2811eb
PM
4691 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4692 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
4693 htonl(*nft_set_ext_flags(ext))))
4694 goto nla_put_failure;
20a69341 4695
c3e1b005
PM
4696 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
4697 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
8e1102d5 4698 nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)),
b46f6ded 4699 NFTA_SET_ELEM_PAD))
c3e1b005
PM
4700 goto nla_put_failure;
4701
4702 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
8e1102d5 4703 u64 expires, now = get_jiffies_64();
c3e1b005
PM
4704
4705 expires = *nft_set_ext_expiration(ext);
8e1102d5 4706 if (time_before64(now, expires))
c3e1b005
PM
4707 expires -= now;
4708 else
4709 expires = 0;
4710
4711 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
8e1102d5 4712 nf_jiffies64_to_msecs(expires),
b46f6ded 4713 NFTA_SET_ELEM_PAD))
c3e1b005
PM
4714 goto nla_put_failure;
4715 }
4716
68e942e8
PM
4717 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
4718 struct nft_userdata *udata;
4719
4720 udata = nft_set_ext_userdata(ext);
4721 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
4722 udata->len + 1, udata->data))
4723 goto nla_put_failure;
4724 }
4725
20a69341
PM
4726 nla_nest_end(skb, nest);
4727 return 0;
4728
4729nla_put_failure:
4730 nlmsg_trim(skb, b);
4731 return -EMSGSIZE;
4732}
4733
4734struct nft_set_dump_args {
4735 const struct netlink_callback *cb;
4736 struct nft_set_iter iter;
4737 struct sk_buff *skb;
4738};
4739
4740static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
de70185d 4741 struct nft_set *set,
20a69341 4742 const struct nft_set_iter *iter,
de70185d 4743 struct nft_set_elem *elem)
20a69341
PM
4744{
4745 struct nft_set_dump_args *args;
4746
4747 args = container_of(iter, struct nft_set_dump_args, iter);
4748 return nf_tables_fill_setelem(args->skb, set, elem);
4749}
4750
fa803605
LZ
4751struct nft_set_dump_ctx {
4752 const struct nft_set *set;
4753 struct nft_ctx ctx;
4754};
4755
20a69341
PM
4756static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
4757{
fa803605 4758 struct nft_set_dump_ctx *dump_ctx = cb->data;
633c9a84 4759 struct net *net = sock_net(skb->sk);
fa803605 4760 struct nft_table *table;
de70185d 4761 struct nft_set *set;
20a69341 4762 struct nft_set_dump_args args;
fa803605 4763 bool set_found = false;
20a69341
PM
4764 struct nfgenmsg *nfmsg;
4765 struct nlmsghdr *nlh;
4766 struct nlattr *nest;
4767 u32 portid, seq;
fa803605 4768 int event;
20a69341 4769
fa803605 4770 rcu_read_lock();
36596dad
PNA
4771 list_for_each_entry_rcu(table, &net->nft.tables, list) {
4772 if (dump_ctx->ctx.family != NFPROTO_UNSPEC &&
98319cb9 4773 dump_ctx->ctx.family != table->family)
fa803605 4774 continue;
20a69341 4775
36596dad
PNA
4776 if (table != dump_ctx->ctx.table)
4777 continue;
20a69341 4778
36596dad
PNA
4779 list_for_each_entry_rcu(set, &table->sets, list) {
4780 if (set == dump_ctx->set) {
4781 set_found = true;
4782 break;
fa803605 4783 }
fa803605
LZ
4784 }
4785 break;
4786 }
4787
4788 if (!set_found) {
4789 rcu_read_unlock();
4790 return -ENOENT;
4791 }
20a69341 4792
dedb67c4 4793 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
20a69341
PM
4794 portid = NETLINK_CB(cb->skb).portid;
4795 seq = cb->nlh->nlmsg_seq;
4796
4797 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4798 NLM_F_MULTI);
4799 if (nlh == NULL)
4800 goto nla_put_failure;
4801
4802 nfmsg = nlmsg_data(nlh);
98319cb9 4803 nfmsg->nfgen_family = table->family;
20a69341 4804 nfmsg->version = NFNETLINK_V0;
fa803605 4805 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
20a69341 4806
fa803605 4807 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
20a69341
PM
4808 goto nla_put_failure;
4809 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
4810 goto nla_put_failure;
4811
ae0be8de 4812 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
20a69341
PM
4813 if (nest == NULL)
4814 goto nla_put_failure;
4815
8588ac09
PNA
4816 args.cb = cb;
4817 args.skb = skb;
fa803605 4818 args.iter.genmask = nft_genmask_cur(net);
8588ac09
PNA
4819 args.iter.skip = cb->args[0];
4820 args.iter.count = 0;
4821 args.iter.err = 0;
4822 args.iter.fn = nf_tables_dump_setelem;
fa803605
LZ
4823 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
4824 rcu_read_unlock();
20a69341
PM
4825
4826 nla_nest_end(skb, nest);
4827 nlmsg_end(skb, nlh);
4828
4829 if (args.iter.err && args.iter.err != -EMSGSIZE)
4830 return args.iter.err;
4831 if (args.iter.count == cb->args[0])
4832 return 0;
4833
4834 cb->args[0] = args.iter.count;
4835 return skb->len;
4836
4837nla_put_failure:
fa803605 4838 rcu_read_unlock();
20a69341
PM
4839 return -ENOSPC;
4840}
4841
90fd131a
FW
4842static int nf_tables_dump_set_start(struct netlink_callback *cb)
4843{
4844 struct nft_set_dump_ctx *dump_ctx = cb->data;
4845
4846 cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC);
4847
4848 return cb->data ? 0 : -ENOMEM;
4849}
4850
fa803605
LZ
4851static int nf_tables_dump_set_done(struct netlink_callback *cb)
4852{
4853 kfree(cb->data);
4854 return 0;
4855}
4856
d60ce62f
AB
4857static int nf_tables_fill_setelem_info(struct sk_buff *skb,
4858 const struct nft_ctx *ctx, u32 seq,
4859 u32 portid, int event, u16 flags,
4860 const struct nft_set *set,
4861 const struct nft_set_elem *elem)
4862{
4863 struct nfgenmsg *nfmsg;
4864 struct nlmsghdr *nlh;
4865 struct nlattr *nest;
4866 int err;
4867
dedb67c4 4868 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
d60ce62f
AB
4869 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
4870 flags);
4871 if (nlh == NULL)
4872 goto nla_put_failure;
4873
4874 nfmsg = nlmsg_data(nlh);
36596dad 4875 nfmsg->nfgen_family = ctx->family;
d60ce62f 4876 nfmsg->version = NFNETLINK_V0;
84d7fce6 4877 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
d60ce62f
AB
4878
4879 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
4880 goto nla_put_failure;
4881 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
4882 goto nla_put_failure;
4883
ae0be8de 4884 nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
d60ce62f
AB
4885 if (nest == NULL)
4886 goto nla_put_failure;
4887
4888 err = nf_tables_fill_setelem(skb, set, elem);
4889 if (err < 0)
4890 goto nla_put_failure;
4891
4892 nla_nest_end(skb, nest);
4893
053c095a
JB
4894 nlmsg_end(skb, nlh);
4895 return 0;
d60ce62f
AB
4896
4897nla_put_failure:
4898 nlmsg_trim(skb, nlh);
4899 return -1;
4900}
4901
ba0e4d99
PNA
4902static int nft_setelem_parse_flags(const struct nft_set *set,
4903 const struct nlattr *attr, u32 *flags)
4904{
4905 if (attr == NULL)
4906 return 0;
4907
4908 *flags = ntohl(nla_get_be32(attr));
4909 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
4910 return -EINVAL;
4911 if (!(set->flags & NFT_SET_INTERVAL) &&
4912 *flags & NFT_SET_ELEM_INTERVAL_END)
4913 return -EINVAL;
4914
4915 return 0;
4916}
4917
20a1452c
PNA
4918static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set,
4919 struct nft_data *key, struct nlattr *attr)
4920{
4921 struct nft_data_desc desc;
4922 int err;
4923
4924 err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr);
4925 if (err < 0)
4926 return err;
4927
4928 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) {
4929 nft_data_release(key, desc.type);
4930 return -EINVAL;
4931 }
4932
4933 return 0;
4934}
4935
fdb9c405
PNA
4936static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set,
4937 struct nft_data_desc *desc,
4938 struct nft_data *data,
4939 struct nlattr *attr)
4940{
4941 int err;
4942
4943 err = nft_data_init(ctx, data, NFT_DATA_VALUE_MAXLEN, desc, attr);
4944 if (err < 0)
4945 return err;
4946
4947 if (desc->type != NFT_DATA_VERDICT && desc->len != set->dlen) {
4948 nft_data_release(data, desc->type);
4949 return -EINVAL;
4950 }
4951
4952 return 0;
4953}
4954
ba0e4d99
PNA
4955static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set,
4956 const struct nlattr *attr)
4957{
4958 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
ba0e4d99
PNA
4959 struct nft_set_elem elem;
4960 struct sk_buff *skb;
4961 uint32_t flags = 0;
4962 void *priv;
4963 int err;
4964
8cb08174
JB
4965 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
4966 nft_set_elem_policy, NULL);
ba0e4d99
PNA
4967 if (err < 0)
4968 return err;
4969
4970 if (!nla[NFTA_SET_ELEM_KEY])
4971 return -EINVAL;
4972
4973 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
4974 if (err < 0)
4975 return err;
4976
20a1452c
PNA
4977 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
4978 nla[NFTA_SET_ELEM_KEY]);
ba0e4d99
PNA
4979 if (err < 0)
4980 return err;
4981
7b225d0b
PNA
4982 if (nla[NFTA_SET_ELEM_KEY_END]) {
4983 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
4984 nla[NFTA_SET_ELEM_KEY_END]);
4985 if (err < 0)
4986 return err;
4987 }
4988
ba0e4d99
PNA
4989 priv = set->ops->get(ctx->net, set, &elem, flags);
4990 if (IS_ERR(priv))
4991 return PTR_ERR(priv);
4992
4993 elem.priv = priv;
ba0e4d99
PNA
4994
4995 err = -ENOMEM;
d9adf22a 4996 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
ba0e4d99 4997 if (skb == NULL)
ee921183 4998 return err;
ba0e4d99
PNA
4999
5000 err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid,
5001 NFT_MSG_NEWSETELEM, 0, set, &elem);
5002 if (err < 0)
ee921183 5003 goto err_fill_setelem;
ba0e4d99 5004
ee921183 5005 return nfnetlink_unicast(skb, ctx->net, ctx->portid);
ba0e4d99 5006
ee921183 5007err_fill_setelem:
ba0e4d99 5008 kfree_skb(skb);
ee921183 5009 return err;
ba0e4d99
PNA
5010}
5011
d9adf22a 5012/* called with rcu_read_lock held */
ba0e4d99
PNA
5013static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
5014 struct sk_buff *skb, const struct nlmsghdr *nlh,
5015 const struct nlattr * const nla[],
5016 struct netlink_ext_ack *extack)
5017{
5018 u8 genmask = nft_genmask_cur(net);
5019 struct nft_set *set;
5020 struct nlattr *attr;
5021 struct nft_ctx ctx;
5022 int rem, err = 0;
5023
36dd1bcc
PNA
5024 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5025 genmask);
ba0e4d99
PNA
5026 if (err < 0)
5027 return err;
5028
cac20fcd 5029 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
ba0e4d99
PNA
5030 if (IS_ERR(set))
5031 return PTR_ERR(set);
5032
5033 if (nlh->nlmsg_flags & NLM_F_DUMP) {
5034 struct netlink_dump_control c = {
90fd131a 5035 .start = nf_tables_dump_set_start,
ba0e4d99
PNA
5036 .dump = nf_tables_dump_set,
5037 .done = nf_tables_dump_set_done,
d9adf22a 5038 .module = THIS_MODULE,
ba0e4d99 5039 };
90fd131a
FW
5040 struct nft_set_dump_ctx dump_ctx = {
5041 .set = set,
5042 .ctx = ctx,
5043 };
ba0e4d99 5044
90fd131a 5045 c.data = &dump_ctx;
d9adf22a 5046 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
ba0e4d99
PNA
5047 }
5048
5049 if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS])
5050 return -EINVAL;
5051
5052 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
5053 err = nft_get_set_elem(&ctx, set, attr);
5054 if (err < 0)
5055 break;
5056 }
5057
5058 return err;
5059}
5060
25e94a99
PNA
5061static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
5062 const struct nft_set *set,
5063 const struct nft_set_elem *elem,
5064 int event, u16 flags)
d60ce62f 5065{
128ad332
PNA
5066 struct net *net = ctx->net;
5067 u32 portid = ctx->portid;
d60ce62f
AB
5068 struct sk_buff *skb;
5069 int err;
8e6cf365
RGB
5070 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
5071 ctx->table->name, ctx->table->handle,
5072 set->name, set->handle);
5073
5074 audit_log_nfcfg(buf,
5075 ctx->family,
5076 set->handle,
5077 event == NFT_MSG_NEWSETELEM ?
5078 AUDIT_NFT_OP_SETELEM_REGISTER :
14224039
RGB
5079 AUDIT_NFT_OP_SETELEM_UNREGISTER,
5080 GFP_KERNEL);
8e6cf365 5081 kfree(buf);
d60ce62f 5082
128ad332 5083 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 5084 return;
d60ce62f 5085
d60ce62f
AB
5086 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5087 if (skb == NULL)
5088 goto err;
5089
5090 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
5091 set, elem);
5092 if (err < 0) {
5093 kfree_skb(skb);
5094 goto err;
5095 }
5096
67cc570e 5097 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
25e94a99 5098 return;
d60ce62f 5099err:
25e94a99 5100 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
d60ce62f
AB
5101}
5102
60319eb1
PNA
5103static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
5104 int msg_type,
5105 struct nft_set *set)
5106{
5107 struct nft_trans *trans;
5108
5109 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
5110 if (trans == NULL)
5111 return NULL;
5112
5113 nft_trans_elem_set(trans) = set;
5114 return trans;
5115}
5116
a7fc9368
PNA
5117struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
5118 const struct nft_set *set,
5119 const struct nlattr *attr)
5120{
5121 struct nft_expr *expr;
5122 int err;
5123
5124 expr = nft_expr_init(ctx, attr);
5125 if (IS_ERR(expr))
5126 return expr;
5127
5128 err = -EOPNOTSUPP;
5129 if (!(expr->ops->type->flags & NFT_EXPR_STATEFUL))
5130 goto err_set_elem_expr;
5131
5132 if (expr->ops->type->flags & NFT_EXPR_GC) {
5133 if (set->flags & NFT_SET_TIMEOUT)
5134 goto err_set_elem_expr;
5135 if (!set->ops->gc_init)
5136 goto err_set_elem_expr;
5137 set->ops->gc_init(set);
5138 }
5139
5140 return expr;
5141
5142err_set_elem_expr:
5143 nft_expr_destroy(ctx, expr);
5144 return ERR_PTR(err);
5145}
5146
22fe54d5
PM
5147void *nft_set_elem_init(const struct nft_set *set,
5148 const struct nft_set_ext_tmpl *tmpl,
7b225d0b
PNA
5149 const u32 *key, const u32 *key_end,
5150 const u32 *data, u64 timeout, u64 expiration, gfp_t gfp)
fe2811eb
PM
5151{
5152 struct nft_set_ext *ext;
5153 void *elem;
5154
5155 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
5156 if (elem == NULL)
5157 return NULL;
5158
5159 ext = nft_set_elem_ext(set, elem);
5160 nft_set_ext_init(ext, tmpl);
5161
5162 memcpy(nft_set_ext_key(ext), key, set->klen);
7b225d0b
PNA
5163 if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END))
5164 memcpy(nft_set_ext_key_end(ext), key_end, set->klen);
fe2811eb
PM
5165 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5166 memcpy(nft_set_ext_data(ext), data, set->dlen);
79ebb5bb
LGL
5167 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
5168 *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
5169 if (expiration == 0)
5170 *nft_set_ext_expiration(ext) += timeout;
5171 }
c3e1b005
PM
5172 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
5173 *nft_set_ext_timeout(ext) = timeout;
fe2811eb
PM
5174
5175 return elem;
5176}
5177
563125a7
PNA
5178static void __nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5179 struct nft_expr *expr)
475beb9c
PNA
5180{
5181 if (expr->ops->destroy_clone) {
5182 expr->ops->destroy_clone(ctx, expr);
5183 module_put(expr->ops->type->owner);
5184 } else {
5185 nf_tables_expr_destroy(ctx, expr);
5186 }
5187}
5188
563125a7
PNA
5189static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx,
5190 struct nft_set_elem_expr *elem_expr)
5191{
5192 struct nft_expr *expr;
5193 u32 size;
5194
5195 nft_setelem_expr_foreach(expr, elem_expr, size)
5196 __nft_set_elem_expr_destroy(ctx, expr);
5197}
5198
61f9e292
LZ
5199void nft_set_elem_destroy(const struct nft_set *set, void *elem,
5200 bool destroy_expr)
61edafbb
PM
5201{
5202 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3453c927
PNA
5203 struct nft_ctx ctx = {
5204 .net = read_pnet(&set->net),
5205 .family = set->table->family,
5206 };
61edafbb 5207
59105446 5208 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
61edafbb 5209 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
59105446 5210 nft_data_release(nft_set_ext_data(ext), set->dtype);
563125a7 5211 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c 5212 nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext));
371ebcbb 5213
8aeff920
PNA
5214 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5215 (*nft_set_ext_obj(ext))->use--;
61edafbb
PM
5216 kfree(elem);
5217}
5218EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
5219
59105446
PNA
5220/* Only called from commit path, nft_set_elem_deactivate() already deals with
5221 * the refcounting from the preparation phase.
5222 */
3453c927
PNA
5223static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
5224 const struct nft_set *set, void *elem)
59105446
PNA
5225{
5226 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
5227
563125a7 5228 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS))
475beb9c
PNA
5229 nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext));
5230
59105446
PNA
5231 kfree(elem);
5232}
5233
8cfd9b0f
PNA
5234static int nft_set_elem_expr_clone(const struct nft_ctx *ctx,
5235 struct nft_set *set,
5236 struct nft_expr *expr_array[])
5237{
5238 struct nft_expr *expr;
5239 int err, i, k;
5240
5241 for (i = 0; i < set->num_exprs; i++) {
5242 expr = kzalloc(set->exprs[i]->ops->size, GFP_KERNEL);
5243 if (!expr)
5244 goto err_expr;
5245
5246 err = nft_expr_clone(expr, set->exprs[i]);
5247 if (err < 0) {
5248 nft_expr_destroy(ctx, expr);
5249 goto err_expr;
5250 }
5251 expr_array[i] = expr;
5252 }
5253
5254 return 0;
5255
5256err_expr:
161b838e
CIK
5257 for (k = i - 1; k >= 0; k--)
5258 nft_expr_destroy(ctx, expr_array[k]);
8cfd9b0f
PNA
5259
5260 return -ENOMEM;
5261}
5262
563125a7
PNA
5263static void nft_set_elem_expr_setup(const struct nft_set_ext *ext, int i,
5264 struct nft_expr *expr_array[])
5265{
5266 struct nft_set_elem_expr *elem_expr = nft_set_ext_expr(ext);
5267 struct nft_expr *expr = nft_setelem_expr_at(elem_expr, elem_expr->size);
5268
5269 memcpy(expr, expr_array[i], expr_array[i]->ops->size);
5270 elem_expr->size += expr_array[i]->ops->size;
5271 kfree(expr_array[i]);
5272 expr_array[i] = NULL;
5273}
5274
60319eb1 5275static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
c016c7e4 5276 const struct nlattr *attr, u32 nlmsg_flags)
20a69341 5277{
8cfd9b0f 5278 struct nft_expr *expr_array[NFT_SET_EXPR_MAX] = {};
20a69341 5279 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
8aeff920 5280 u8 genmask = nft_genmask_next(ctx->net);
fe2811eb 5281 struct nft_set_ext_tmpl tmpl;
c016c7e4 5282 struct nft_set_ext *ext, *ext2;
20a69341
PM
5283 struct nft_set_elem elem;
5284 struct nft_set_binding *binding;
8aeff920 5285 struct nft_object *obj = NULL;
68e942e8 5286 struct nft_userdata *udata;
20a1452c 5287 struct nft_data_desc desc;
20a69341 5288 enum nft_registers dreg;
60319eb1 5289 struct nft_trans *trans;
563125a7 5290 u32 flags = 0, size = 0;
c3e1b005 5291 u64 timeout;
79ebb5bb 5292 u64 expiration;
8cfd9b0f 5293 int err, i;
68e942e8 5294 u8 ulen;
20a69341 5295
8cb08174
JB
5296 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5297 nft_set_elem_policy, NULL);
20a69341
PM
5298 if (err < 0)
5299 return err;
5300
5301 if (nla[NFTA_SET_ELEM_KEY] == NULL)
5302 return -EINVAL;
5303
fe2811eb
PM
5304 nft_set_ext_prepare(&tmpl);
5305
0e9091d6
PNA
5306 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5307 if (err < 0)
5308 return err;
5309 if (flags != 0)
5310 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
20a69341
PM
5311
5312 if (set->flags & NFT_SET_MAP) {
5313 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
fe2811eb 5314 !(flags & NFT_SET_ELEM_INTERVAL_END))
20a69341
PM
5315 return -EINVAL;
5316 } else {
5317 if (nla[NFTA_SET_ELEM_DATA] != NULL)
5318 return -EINVAL;
5319 }
5320
bffc124b
PNA
5321 if ((flags & NFT_SET_ELEM_INTERVAL_END) &&
5322 (nla[NFTA_SET_ELEM_DATA] ||
5323 nla[NFTA_SET_ELEM_OBJREF] ||
5324 nla[NFTA_SET_ELEM_TIMEOUT] ||
5325 nla[NFTA_SET_ELEM_EXPIRATION] ||
5326 nla[NFTA_SET_ELEM_USERDATA] ||
48b0ae04
PNA
5327 nla[NFTA_SET_ELEM_EXPR] ||
5328 nla[NFTA_SET_ELEM_EXPRESSIONS]))
bffc124b
PNA
5329 return -EINVAL;
5330
c3e1b005
PM
5331 timeout = 0;
5332 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
5333 if (!(set->flags & NFT_SET_TIMEOUT))
5334 return -EINVAL;
8e1102d5
FW
5335 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT],
5336 &timeout);
5337 if (err)
5338 return err;
c3e1b005
PM
5339 } else if (set->flags & NFT_SET_TIMEOUT) {
5340 timeout = set->timeout;
5341 }
5342
79ebb5bb
LGL
5343 expiration = 0;
5344 if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
5345 if (!(set->flags & NFT_SET_TIMEOUT))
5346 return -EINVAL;
5347 err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
5348 &expiration);
5349 if (err)
5350 return err;
5351 }
5352
8cfd9b0f
PNA
5353 if (nla[NFTA_SET_ELEM_EXPR]) {
5354 struct nft_expr *expr;
5355
5356 if (set->num_exprs != 1)
5357 return -EOPNOTSUPP;
5358
40944452
PNA
5359 expr = nft_set_elem_expr_alloc(ctx, set,
5360 nla[NFTA_SET_ELEM_EXPR]);
5361 if (IS_ERR(expr))
5362 return PTR_ERR(expr);
65038428 5363
8cfd9b0f 5364 expr_array[0] = expr;
65038428 5365
8cfd9b0f
PNA
5366 if (set->exprs[0] && set->exprs[0]->ops != expr->ops) {
5367 err = -EOPNOTSUPP;
65038428 5368 goto err_set_elem_expr;
8cfd9b0f 5369 }
48b0ae04
PNA
5370 } else if (nla[NFTA_SET_ELEM_EXPRESSIONS]) {
5371 struct nft_expr *expr;
5372 struct nlattr *tmp;
5373 int left;
65038428 5374
48b0ae04
PNA
5375 if (set->num_exprs == 0)
5376 return -EOPNOTSUPP;
5377
5378 i = 0;
5379 nla_for_each_nested(tmp, nla[NFTA_SET_ELEM_EXPRESSIONS], left) {
5380 if (i == set->num_exprs) {
5381 err = -E2BIG;
5382 goto err_set_elem_expr;
5383 }
5384 if (nla_type(tmp) != NFTA_LIST_ELEM) {
5385 err = -EINVAL;
5386 goto err_set_elem_expr;
5387 }
5388 expr = nft_set_elem_expr_alloc(ctx, set, tmp);
5389 if (IS_ERR(expr)) {
5390 err = PTR_ERR(expr);
5391 goto err_set_elem_expr;
5392 }
5393 expr_array[i] = expr;
5394
5395 if (expr->ops != set->exprs[i]->ops) {
5396 err = -EOPNOTSUPP;
5397 goto err_set_elem_expr;
5398 }
5399 i++;
5400 }
5401 if (set->num_exprs != i) {
5402 err = -EOPNOTSUPP;
65038428 5403 goto err_set_elem_expr;
48b0ae04 5404 }
8cfd9b0f
PNA
5405 } else if (set->num_exprs > 0) {
5406 err = nft_set_elem_expr_clone(ctx, set, expr_array);
5407 if (err < 0)
5408 goto err_set_elem_expr_clone;
40944452
PNA
5409 }
5410
20a1452c
PNA
5411 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5412 nla[NFTA_SET_ELEM_KEY]);
20a69341 5413 if (err < 0)
40944452 5414 goto err_set_elem_expr;
20a69341 5415
20a1452c 5416 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
7b225d0b
PNA
5417
5418 if (nla[NFTA_SET_ELEM_KEY_END]) {
5419 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5420 nla[NFTA_SET_ELEM_KEY_END]);
5421 if (err < 0)
5422 goto err_parse_key;
5423
5424 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
5425 }
5426
c3e1b005
PM
5427 if (timeout > 0) {
5428 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
5429 if (timeout != set->timeout)
5430 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
5431 }
fe2811eb 5432
563125a7
PNA
5433 if (set->num_exprs) {
5434 for (i = 0; i < set->num_exprs; i++)
5435 size += expr_array[i]->ops->size;
5436
5437 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPRESSIONS,
5438 sizeof(struct nft_set_elem_expr) +
5439 size);
5440 }
40944452 5441
8aeff920
PNA
5442 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
5443 if (!(set->flags & NFT_SET_OBJECT)) {
5444 err = -EINVAL;
7b225d0b 5445 goto err_parse_key_end;
8aeff920 5446 }
4d44175a
FW
5447 obj = nft_obj_lookup(ctx->net, ctx->table,
5448 nla[NFTA_SET_ELEM_OBJREF],
cac20fcd 5449 set->objtype, genmask);
8aeff920
PNA
5450 if (IS_ERR(obj)) {
5451 err = PTR_ERR(obj);
7b225d0b 5452 goto err_parse_key_end;
8aeff920
PNA
5453 }
5454 nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
5455 }
5456
20a69341 5457 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
fdb9c405
PNA
5458 err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val,
5459 nla[NFTA_SET_ELEM_DATA]);
20a69341 5460 if (err < 0)
7b225d0b 5461 goto err_parse_key_end;
20a69341 5462
20a69341
PM
5463 dreg = nft_type_to_reg(set->dtype);
5464 list_for_each_entry(binding, &set->bindings, list) {
5465 struct nft_ctx bind_ctx = {
58c78e10 5466 .net = ctx->net,
36596dad 5467 .family = ctx->family,
20a69341 5468 .table = ctx->table,
7c95f6d8 5469 .chain = (struct nft_chain *)binding->chain,
20a69341
PM
5470 };
5471
11113e19
PM
5472 if (!(binding->flags & NFT_SET_MAP))
5473 continue;
5474
1ec10212 5475 err = nft_validate_register_store(&bind_ctx, dreg,
fdb9c405 5476 &elem.data.val,
20a1452c 5477 desc.type, desc.len);
20a69341 5478 if (err < 0)
7b225d0b 5479 goto err_parse_data;
a654de8f 5480
20a1452c 5481 if (desc.type == NFT_DATA_VERDICT &&
fdb9c405
PNA
5482 (elem.data.val.verdict.code == NFT_GOTO ||
5483 elem.data.val.verdict.code == NFT_JUMP))
a654de8f
PNA
5484 nft_validate_state_update(ctx->net,
5485 NFT_VALIDATE_NEED);
20a69341 5486 }
fe2811eb 5487
20a1452c 5488 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len);
20a69341
PM
5489 }
5490
68e942e8
PM
5491 /* The full maximum length of userdata can exceed the maximum
5492 * offset value (U8_MAX) for following extensions, therefor it
5493 * must be the last extension added.
5494 */
5495 ulen = 0;
5496 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
5497 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
5498 if (ulen > 0)
5499 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
5500 ulen);
5501 }
5502
fe2811eb 5503 err = -ENOMEM;
7b225d0b 5504 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
fdb9c405 5505 elem.key_end.val.data, elem.data.val.data,
79ebb5bb 5506 timeout, expiration, GFP_KERNEL);
fe2811eb 5507 if (elem.priv == NULL)
7b225d0b 5508 goto err_parse_data;
fe2811eb
PM
5509
5510 ext = nft_set_elem_ext(set, elem.priv);
5511 if (flags)
5512 *nft_set_ext_flags(ext) = flags;
68e942e8
PM
5513 if (ulen > 0) {
5514 udata = nft_set_ext_userdata(ext);
5515 udata->len = ulen - 1;
5516 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
5517 }
8aeff920
PNA
5518 if (obj) {
5519 *nft_set_ext_obj(ext) = obj;
5520 obj->use++;
5521 }
563125a7
PNA
5522 for (i = 0; i < set->num_exprs; i++)
5523 nft_set_elem_expr_setup(ext, i, expr_array);
fe2811eb 5524
60319eb1
PNA
5525 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
5526 if (trans == NULL)
7b225d0b 5527 goto err_trans;
60319eb1 5528
69086658 5529 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
c016c7e4
PNA
5530 err = set->ops->insert(ctx->net, set, &elem, &ext2);
5531 if (err) {
5532 if (err == -EEXIST) {
9744a6fc
PNA
5533 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
5534 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
5535 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
77a92189 5536 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
7b225d0b 5537 goto err_element_clash;
8aeff920
PNA
5538 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
5539 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
5540 memcmp(nft_set_ext_data(ext),
5541 nft_set_ext_data(ext2), set->dlen) != 0) ||
5542 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
5543 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
5544 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
77a92189 5545 goto err_element_clash;
c016c7e4
PNA
5546 else if (!(nlmsg_flags & NLM_F_EXCL))
5547 err = 0;
8c2d45b2
PNA
5548 } else if (err == -ENOTEMPTY) {
5549 /* ENOTEMPTY reports overlapping between this element
5550 * and an existing one.
5551 */
5552 err = -EEXIST;
c016c7e4 5553 }
7b225d0b 5554 goto err_element_clash;
c016c7e4 5555 }
20a69341 5556
35d0ac90
PNA
5557 if (set->size &&
5558 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
5559 err = -ENFILE;
7b225d0b 5560 goto err_set_full;
35d0ac90
PNA
5561 }
5562
60319eb1 5563 nft_trans_elem(trans) = elem;
46bbafce 5564 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
20a69341
PM
5565 return 0;
5566
7b225d0b 5567err_set_full:
5cb82a38 5568 set->ops->remove(ctx->net, set, &elem);
7b225d0b 5569err_element_clash:
60319eb1 5570 kfree(trans);
7b225d0b 5571err_trans:
b91d9036
TY
5572 if (obj)
5573 obj->use--;
475beb9c
PNA
5574
5575 nf_tables_set_elem_destroy(ctx, set, elem.priv);
7b225d0b 5576err_parse_data:
20a69341 5577 if (nla[NFTA_SET_ELEM_DATA] != NULL)
fdb9c405 5578 nft_data_release(&elem.data.val, desc.type);
7b225d0b
PNA
5579err_parse_key_end:
5580 nft_data_release(&elem.key_end.val, NFT_DATA_VALUE);
5581err_parse_key:
20a1452c 5582 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
40944452 5583err_set_elem_expr:
8cfd9b0f
PNA
5584 for (i = 0; i < set->num_exprs && expr_array[i]; i++)
5585 nft_expr_destroy(ctx, expr_array[i]);
5586err_set_elem_expr_clone:
20a69341
PM
5587 return err;
5588}
5589
633c9a84
PNA
5590static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
5591 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
5592 const struct nlattr * const nla[],
5593 struct netlink_ext_ack *extack)
20a69341 5594{
f2a6d766 5595 u8 genmask = nft_genmask_next(net);
20a69341
PM
5596 const struct nlattr *attr;
5597 struct nft_set *set;
5598 struct nft_ctx ctx;
a654de8f 5599 int rem, err;
20a69341 5600
7d5570ca
PNA
5601 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
5602 return -EINVAL;
5603
36dd1bcc
PNA
5604 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5605 genmask);
20a69341
PM
5606 if (err < 0)
5607 return err;
5608
a3073c17
PNA
5609 set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
5610 nla[NFTA_SET_ELEM_LIST_SET_ID], genmask);
5611 if (IS_ERR(set))
5612 return PTR_ERR(set);
958bee14 5613
20a69341
PM
5614 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
5615 return -EBUSY;
5616
5617 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
c016c7e4 5618 err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags);
35d0ac90 5619 if (err < 0)
a654de8f 5620 return err;
20a69341 5621 }
a654de8f
PNA
5622
5623 if (net->nft.validate_state == NFT_VALIDATE_DO)
5624 return nft_table_validate(net, ctx.table);
5625
5626 return 0;
20a69341
PM
5627}
5628
59105446
PNA
5629/**
5630 * nft_data_hold - hold a nft_data item
5631 *
5632 * @data: struct nft_data to release
5633 * @type: type of data
5634 *
5635 * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded,
5636 * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and
5637 * NFT_GOTO verdicts. This function must be called on active data objects
5638 * from the second phase of the commit protocol.
5639 */
bb7b40ae 5640void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
59105446 5641{
d0e2c7de
PNA
5642 struct nft_chain *chain;
5643 struct nft_rule *rule;
5644
59105446
PNA
5645 if (type == NFT_DATA_VERDICT) {
5646 switch (data->verdict.code) {
5647 case NFT_JUMP:
5648 case NFT_GOTO:
d0e2c7de
PNA
5649 chain = data->verdict.chain;
5650 chain->use++;
5651
5652 if (!nft_chain_is_bound(chain))
5653 break;
5654
5655 chain->table->use++;
5656 list_for_each_entry(rule, &chain->rules, list)
5657 chain->use++;
5658
5659 nft_chain_add(chain->table, chain);
59105446
PNA
5660 break;
5661 }
5662 }
5663}
5664
5665static void nft_set_elem_activate(const struct net *net,
5666 const struct nft_set *set,
5667 struct nft_set_elem *elem)
5668{
5669 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5670
5671 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5672 nft_data_hold(nft_set_ext_data(ext), set->dtype);
5673 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5674 (*nft_set_ext_obj(ext))->use++;
5675}
5676
5677static void nft_set_elem_deactivate(const struct net *net,
5678 const struct nft_set *set,
5679 struct nft_set_elem *elem)
5680{
5681 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5682
5683 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
5684 nft_data_release(nft_set_ext_data(ext), set->dtype);
5685 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
5686 (*nft_set_ext_obj(ext))->use--;
5687}
5688
60319eb1 5689static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
5690 const struct nlattr *attr)
5691{
5692 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3971ca14 5693 struct nft_set_ext_tmpl tmpl;
20a69341 5694 struct nft_set_elem elem;
3971ca14 5695 struct nft_set_ext *ext;
60319eb1 5696 struct nft_trans *trans;
3971ca14
PNA
5697 u32 flags = 0;
5698 void *priv;
20a69341
PM
5699 int err;
5700
8cb08174
JB
5701 err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr,
5702 nft_set_elem_policy, NULL);
20a69341 5703 if (err < 0)
20a1452c 5704 return err;
20a69341 5705
20a69341 5706 if (nla[NFTA_SET_ELEM_KEY] == NULL)
20a1452c 5707 return -EINVAL;
20a69341 5708
3971ca14
PNA
5709 nft_set_ext_prepare(&tmpl);
5710
5711 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
5712 if (err < 0)
5713 return err;
5714 if (flags != 0)
5715 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
5716
20a1452c
PNA
5717 err = nft_setelem_parse_key(ctx, set, &elem.key.val,
5718 nla[NFTA_SET_ELEM_KEY]);
20a69341 5719 if (err < 0)
20a1452c 5720 return err;
20a69341 5721
20a1452c 5722 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen);
3971ca14 5723
7b225d0b
PNA
5724 if (nla[NFTA_SET_ELEM_KEY_END]) {
5725 err = nft_setelem_parse_key(ctx, set, &elem.key_end.val,
5726 nla[NFTA_SET_ELEM_KEY_END]);
5727 if (err < 0)
5728 return err;
5729
5730 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen);
5731 }
5732
3971ca14 5733 err = -ENOMEM;
7b225d0b
PNA
5734 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data,
5735 elem.key_end.val.data, NULL, 0, 0,
5736 GFP_KERNEL);
3971ca14 5737 if (elem.priv == NULL)
20a1452c 5738 goto fail_elem;
3971ca14
PNA
5739
5740 ext = nft_set_elem_ext(set, elem.priv);
5741 if (flags)
5742 *nft_set_ext_flags(ext) = flags;
5743
60319eb1 5744 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
20a1452c
PNA
5745 if (trans == NULL)
5746 goto fail_trans;
20a69341 5747
42a55769 5748 priv = set->ops->deactivate(ctx->net, set, &elem);
3971ca14 5749 if (priv == NULL) {
cc02e457 5750 err = -ENOENT;
20a1452c 5751 goto fail_ops;
cc02e457 5752 }
3971ca14
PNA
5753 kfree(elem.priv);
5754 elem.priv = priv;
cc02e457 5755
59105446
PNA
5756 nft_set_elem_deactivate(ctx->net, set, &elem);
5757
60319eb1 5758 nft_trans_elem(trans) = elem;
46bbafce 5759 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
0dc13625 5760 return 0;
cc02e457 5761
20a1452c 5762fail_ops:
cc02e457 5763 kfree(trans);
20a1452c 5764fail_trans:
3971ca14 5765 kfree(elem.priv);
20a1452c
PNA
5766fail_elem:
5767 nft_data_release(&elem.key.val, NFT_DATA_VALUE);
20a69341
PM
5768 return err;
5769}
5770
8411b644 5771static int nft_flush_set(const struct nft_ctx *ctx,
de70185d 5772 struct nft_set *set,
8411b644 5773 const struct nft_set_iter *iter,
de70185d 5774 struct nft_set_elem *elem)
8411b644
PNA
5775{
5776 struct nft_trans *trans;
5777 int err;
5778
5779 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
5780 sizeof(struct nft_trans_elem), GFP_ATOMIC);
5781 if (!trans)
5782 return -ENOMEM;
5783
1ba1c414 5784 if (!set->ops->flush(ctx->net, set, elem->priv)) {
8411b644
PNA
5785 err = -ENOENT;
5786 goto err1;
5787 }
b2c11e4b 5788 set->ndeact++;
8411b644 5789
7acfda53 5790 nft_set_elem_deactivate(ctx->net, set, elem);
de70185d
PNA
5791 nft_trans_elem_set(trans) = set;
5792 nft_trans_elem(trans) = *elem;
8411b644
PNA
5793 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
5794
5795 return 0;
5796err1:
5797 kfree(trans);
5798 return err;
5799}
5800
633c9a84
PNA
5801static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
5802 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
5803 const struct nlattr * const nla[],
5804 struct netlink_ext_ack *extack)
20a69341 5805{
f2a6d766 5806 u8 genmask = nft_genmask_next(net);
20a69341
PM
5807 const struct nlattr *attr;
5808 struct nft_set *set;
5809 struct nft_ctx ctx;
60319eb1 5810 int rem, err = 0;
20a69341 5811
36dd1bcc
PNA
5812 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack,
5813 genmask);
20a69341
PM
5814 if (err < 0)
5815 return err;
5816
cac20fcd 5817 set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask);
20a69341
PM
5818 if (IS_ERR(set))
5819 return PTR_ERR(set);
5820 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
5821 return -EBUSY;
5822
8411b644 5823 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
baa2d42c
PNA
5824 struct nft_set_iter iter = {
5825 .genmask = genmask,
5826 .fn = nft_flush_set,
8411b644 5827 };
baa2d42c 5828 set->ops->walk(&ctx, set, &iter);
8411b644 5829
baa2d42c 5830 return iter.err;
8411b644
PNA
5831 }
5832
20a69341
PM
5833 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
5834 err = nft_del_setelem(&ctx, set, attr);
5835 if (err < 0)
60319eb1 5836 break;
4fefee57 5837
3dd0673a 5838 set->ndeact++;
20a69341 5839 }
60319eb1 5840 return err;
20a69341
PM
5841}
5842
cfed7e1b
PM
5843void nft_set_gc_batch_release(struct rcu_head *rcu)
5844{
5845 struct nft_set_gc_batch *gcb;
5846 unsigned int i;
5847
5848 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
5849 for (i = 0; i < gcb->head.cnt; i++)
61f9e292 5850 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
cfed7e1b
PM
5851 kfree(gcb);
5852}
cfed7e1b
PM
5853
5854struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
5855 gfp_t gfp)
5856{
5857 struct nft_set_gc_batch *gcb;
5858
5859 gcb = kzalloc(sizeof(*gcb), gfp);
5860 if (gcb == NULL)
5861 return gcb;
5862 gcb->head.set = set;
5863 return gcb;
5864}
cfed7e1b 5865
e5009240
PNA
5866/*
5867 * Stateful objects
5868 */
5869
5870/**
5871 * nft_register_obj- register nf_tables stateful object type
3db86c39 5872 * @obj_type: object type
e5009240
PNA
5873 *
5874 * Registers the object type for use with nf_tables. Returns zero on
5875 * success or a negative errno code otherwise.
5876 */
5877int nft_register_obj(struct nft_object_type *obj_type)
5878{
5879 if (obj_type->type == NFT_OBJECT_UNSPEC)
5880 return -EINVAL;
5881
5882 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5883 list_add_rcu(&obj_type->list, &nf_tables_objects);
5884 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5885 return 0;
5886}
5887EXPORT_SYMBOL_GPL(nft_register_obj);
5888
5889/**
5890 * nft_unregister_obj - unregister nf_tables object type
3db86c39 5891 * @obj_type: object type
e5009240
PNA
5892 *
5893 * Unregisters the object type for use with nf_tables.
5894 */
5895void nft_unregister_obj(struct nft_object_type *obj_type)
5896{
5897 nfnl_lock(NFNL_SUBSYS_NFTABLES);
5898 list_del_rcu(&obj_type->list);
5899 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
5900}
5901EXPORT_SYMBOL_GPL(nft_unregister_obj);
5902
4d44175a
FW
5903struct nft_object *nft_obj_lookup(const struct net *net,
5904 const struct nft_table *table,
cac20fcd
PNA
5905 const struct nlattr *nla, u32 objtype,
5906 u8 genmask)
e5009240 5907{
4d44175a
FW
5908 struct nft_object_hash_key k = { .table = table };
5909 char search[NFT_OBJ_MAXNAMELEN];
5910 struct rhlist_head *tmp, *list;
e5009240
PNA
5911 struct nft_object *obj;
5912
872f6903 5913 nla_strscpy(search, nla, sizeof(search));
4d44175a
FW
5914 k.name = search;
5915
5916 WARN_ON_ONCE(!rcu_read_lock_held() &&
5917 !lockdep_commit_lock_is_held(net));
5918
5919 rcu_read_lock();
5920 list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params);
5921 if (!list)
5922 goto out;
5923
5924 rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) {
5925 if (objtype == obj->ops->type->type &&
5926 nft_active_genmask(obj, genmask)) {
5927 rcu_read_unlock();
e5009240 5928 return obj;
4d44175a 5929 }
e5009240 5930 }
4d44175a
FW
5931out:
5932 rcu_read_unlock();
e5009240
PNA
5933 return ERR_PTR(-ENOENT);
5934}
cac20fcd 5935EXPORT_SYMBOL_GPL(nft_obj_lookup);
e5009240 5936
cac20fcd
PNA
5937static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table,
5938 const struct nlattr *nla,
5939 u32 objtype, u8 genmask)
3ecbfd65
HS
5940{
5941 struct nft_object *obj;
5942
5943 list_for_each_entry(obj, &table->objects, list) {
5944 if (be64_to_cpu(nla_get_be64(nla)) == obj->handle &&
5945 objtype == obj->ops->type->type &&
5946 nft_active_genmask(obj, genmask))
5947 return obj;
5948 }
5949 return ERR_PTR(-ENOENT);
5950}
5951
e5009240 5952static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
b2fbd044
LZ
5953 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
5954 .len = NFT_TABLE_MAXNAMELEN - 1 },
5955 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
5956 .len = NFT_OBJ_MAXNAMELEN - 1 },
e5009240
PNA
5957 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
5958 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
3ecbfd65 5959 [NFTA_OBJ_HANDLE] = { .type = NLA_U64},
b131c964
JGG
5960 [NFTA_OBJ_USERDATA] = { .type = NLA_BINARY,
5961 .len = NFT_USERDATA_MAXLEN },
e5009240
PNA
5962};
5963
84fba055
FW
5964static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
5965 const struct nft_object_type *type,
e5009240
PNA
5966 const struct nlattr *attr)
5967{
5b4c6e38 5968 struct nlattr **tb;
dfc46034 5969 const struct nft_object_ops *ops;
e5009240 5970 struct nft_object *obj;
5b4c6e38
GS
5971 int err = -ENOMEM;
5972
5973 tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL);
5974 if (!tb)
5975 goto err1;
e5009240
PNA
5976
5977 if (attr) {
8cb08174
JB
5978 err = nla_parse_nested_deprecated(tb, type->maxattr, attr,
5979 type->policy, NULL);
e5009240 5980 if (err < 0)
5b4c6e38 5981 goto err2;
e5009240
PNA
5982 } else {
5983 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
5984 }
5985
dfc46034
PBG
5986 if (type->select_ops) {
5987 ops = type->select_ops(ctx, (const struct nlattr * const *)tb);
5988 if (IS_ERR(ops)) {
5989 err = PTR_ERR(ops);
5b4c6e38 5990 goto err2;
dfc46034
PBG
5991 }
5992 } else {
5993 ops = type->ops;
5994 }
5995
e5009240 5996 err = -ENOMEM;
dfc46034 5997 obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL);
5b4c6e38
GS
5998 if (!obj)
5999 goto err2;
e5009240 6000
dfc46034 6001 err = ops->init(ctx, (const struct nlattr * const *)tb, obj);
e5009240 6002 if (err < 0)
5b4c6e38 6003 goto err3;
e5009240 6004
dfc46034
PBG
6005 obj->ops = ops;
6006
5b4c6e38 6007 kfree(tb);
e5009240 6008 return obj;
5b4c6e38 6009err3:
e5009240 6010 kfree(obj);
5b4c6e38
GS
6011err2:
6012 kfree(tb);
e5009240
PNA
6013err1:
6014 return ERR_PTR(err);
6015}
6016
6017static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
43da04a5 6018 struct nft_object *obj, bool reset)
e5009240
PNA
6019{
6020 struct nlattr *nest;
6021
ae0be8de 6022 nest = nla_nest_start_noflag(skb, attr);
e5009240
PNA
6023 if (!nest)
6024 goto nla_put_failure;
dfc46034 6025 if (obj->ops->dump(skb, obj, reset) < 0)
e5009240
PNA
6026 goto nla_put_failure;
6027 nla_nest_end(skb, nest);
6028 return 0;
6029
6030nla_put_failure:
6031 return -1;
6032}
6033
6034static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
6035{
6036 const struct nft_object_type *type;
6037
6038 list_for_each_entry(type, &nf_tables_objects, list) {
6039 if (objtype == type->type)
6040 return type;
6041 }
6042 return NULL;
6043}
6044
452238e8
FW
6045static const struct nft_object_type *
6046nft_obj_type_get(struct net *net, u32 objtype)
e5009240
PNA
6047{
6048 const struct nft_object_type *type;
6049
6050 type = __nft_obj_type_get(objtype);
6051 if (type != NULL && try_module_get(type->owner))
6052 return type;
6053
f102d66b 6054 lockdep_nfnl_nft_mutex_not_held();
e5009240
PNA
6055#ifdef CONFIG_MODULES
6056 if (type == NULL) {
eb014de4 6057 if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN)
e5009240
PNA
6058 return ERR_PTR(-EAGAIN);
6059 }
6060#endif
6061 return ERR_PTR(-ENOENT);
6062}
6063
d62d0ba9
FFM
6064static int nf_tables_updobj(const struct nft_ctx *ctx,
6065 const struct nft_object_type *type,
6066 const struct nlattr *attr,
6067 struct nft_object *obj)
6068{
6069 struct nft_object *newobj;
6070 struct nft_trans *trans;
6071 int err;
6072
6073 trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ,
6074 sizeof(struct nft_trans_obj));
6075 if (!trans)
6076 return -ENOMEM;
6077
6078 newobj = nft_obj_init(ctx, type, attr);
6079 if (IS_ERR(newobj)) {
6080 err = PTR_ERR(newobj);
b74ae961 6081 goto err_free_trans;
d62d0ba9
FFM
6082 }
6083
6084 nft_trans_obj(trans) = obj;
6085 nft_trans_obj_update(trans) = true;
6086 nft_trans_obj_newobj(trans) = newobj;
6087 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
6088
6089 return 0;
b74ae961
DC
6090
6091err_free_trans:
d62d0ba9 6092 kfree(trans);
d62d0ba9
FFM
6093 return err;
6094}
6095
e5009240
PNA
6096static int nf_tables_newobj(struct net *net, struct sock *nlsk,
6097 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
6098 const struct nlattr * const nla[],
6099 struct netlink_ext_ack *extack)
e5009240
PNA
6100{
6101 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6102 const struct nft_object_type *type;
6103 u8 genmask = nft_genmask_next(net);
6104 int family = nfmsg->nfgen_family;
e5009240
PNA
6105 struct nft_table *table;
6106 struct nft_object *obj;
6107 struct nft_ctx ctx;
6108 u32 objtype;
6109 int err;
6110
6111 if (!nla[NFTA_OBJ_TYPE] ||
6112 !nla[NFTA_OBJ_NAME] ||
6113 !nla[NFTA_OBJ_DATA])
6114 return -EINVAL;
6115
cac20fcd 6116 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6117 if (IS_ERR(table)) {
6118 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6119 return PTR_ERR(table);
36dd1bcc 6120 }
e5009240
PNA
6121
6122 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 6123 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
e5009240
PNA
6124 if (IS_ERR(obj)) {
6125 err = PTR_ERR(obj);
36dd1bcc
PNA
6126 if (err != -ENOENT) {
6127 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6128 return err;
36dd1bcc 6129 }
1a28ad74 6130 } else {
36dd1bcc
PNA
6131 if (nlh->nlmsg_flags & NLM_F_EXCL) {
6132 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6133 return -EEXIST;
36dd1bcc 6134 }
d62d0ba9
FFM
6135 if (nlh->nlmsg_flags & NLM_F_REPLACE)
6136 return -EOPNOTSUPP;
6137
fd57d0cb 6138 type = __nft_obj_type_get(objtype);
d62d0ba9
FFM
6139 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
6140
6141 return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj);
e5009240
PNA
6142 }
6143
98319cb9 6144 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e5009240 6145
452238e8 6146 type = nft_obj_type_get(net, objtype);
e5009240
PNA
6147 if (IS_ERR(type))
6148 return PTR_ERR(type);
6149
84fba055 6150 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
e5009240
PNA
6151 if (IS_ERR(obj)) {
6152 err = PTR_ERR(obj);
b131c964 6153 goto err_init;
e5009240 6154 }
d152159b 6155 obj->key.table = table;
3ecbfd65
HS
6156 obj->handle = nf_tables_alloc_handle(table);
6157
d152159b
FW
6158 obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL);
6159 if (!obj->key.name) {
61509575 6160 err = -ENOMEM;
b131c964
JGG
6161 goto err_strdup;
6162 }
6163
6164 if (nla[NFTA_OBJ_USERDATA]) {
85db827a 6165 obj->udata = nla_memdup(nla[NFTA_OBJ_USERDATA], GFP_KERNEL);
b131c964
JGG
6166 if (obj->udata == NULL)
6167 goto err_userdata;
6168
85db827a 6169 obj->udlen = nla_len(nla[NFTA_OBJ_USERDATA]);
61509575 6170 }
e5009240
PNA
6171
6172 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
6173 if (err < 0)
b131c964 6174 goto err_trans;
e5009240 6175
4d44175a
FW
6176 err = rhltable_insert(&nft_objname_ht, &obj->rhlhead,
6177 nft_objname_ht_params);
6178 if (err < 0)
b131c964 6179 goto err_obj_ht;
4d44175a 6180
e5009240
PNA
6181 list_add_tail_rcu(&obj->list, &table->objects);
6182 table->use++;
6183 return 0;
b131c964 6184err_obj_ht:
4d44175a
FW
6185 /* queued in transaction log */
6186 INIT_LIST_HEAD(&obj->list);
6187 return err;
b131c964 6188err_trans:
d152159b 6189 kfree(obj->key.name);
b131c964
JGG
6190err_userdata:
6191 kfree(obj->udata);
6192err_strdup:
dfc46034 6193 if (obj->ops->destroy)
00bfb320 6194 obj->ops->destroy(&ctx, obj);
e5009240 6195 kfree(obj);
b131c964 6196err_init:
e5009240
PNA
6197 module_put(type->owner);
6198 return err;
6199}
6200
6201static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
6202 u32 portid, u32 seq, int event, u32 flags,
6203 int family, const struct nft_table *table,
43da04a5 6204 struct nft_object *obj, bool reset)
e5009240
PNA
6205{
6206 struct nfgenmsg *nfmsg;
6207 struct nlmsghdr *nlh;
6208
dedb67c4 6209 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
e5009240
PNA
6210 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
6211 if (nlh == NULL)
6212 goto nla_put_failure;
6213
6214 nfmsg = nlmsg_data(nlh);
6215 nfmsg->nfgen_family = family;
6216 nfmsg->version = NFNETLINK_V0;
6217 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
6218
6219 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
d152159b 6220 nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) ||
dfc46034 6221 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) ||
e5009240 6222 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
3ecbfd65
HS
6223 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) ||
6224 nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle),
6225 NFTA_OBJ_PAD))
e5009240
PNA
6226 goto nla_put_failure;
6227
b131c964
JGG
6228 if (obj->udata &&
6229 nla_put(skb, NFTA_OBJ_USERDATA, obj->udlen, obj->udata))
6230 goto nla_put_failure;
6231
e5009240
PNA
6232 nlmsg_end(skb, nlh);
6233 return 0;
6234
6235nla_put_failure:
6236 nlmsg_trim(skb, nlh);
6237 return -1;
6238}
6239
a9fea2a3 6240struct nft_obj_filter {
e46abbcc 6241 char *table;
a9fea2a3
PNA
6242 u32 type;
6243};
6244
e5009240
PNA
6245static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
6246{
6247 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
e5009240 6248 const struct nft_table *table;
e5009240 6249 unsigned int idx = 0, s_idx = cb->args[0];
a9fea2a3 6250 struct nft_obj_filter *filter = cb->data;
e5009240
PNA
6251 struct net *net = sock_net(skb->sk);
6252 int family = nfmsg->nfgen_family;
43da04a5
PNA
6253 struct nft_object *obj;
6254 bool reset = false;
6255
6256 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
6257 reset = true;
e5009240
PNA
6258
6259 rcu_read_lock();
6260 cb->seq = net->nft.base_seq;
6261
36596dad 6262 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 6263 if (family != NFPROTO_UNSPEC && family != table->family)
e5009240
PNA
6264 continue;
6265
36596dad
PNA
6266 list_for_each_entry_rcu(obj, &table->objects, list) {
6267 if (!nft_is_active(net, obj))
6268 goto cont;
6269 if (idx < s_idx)
6270 goto cont;
6271 if (idx > s_idx)
6272 memset(&cb->args[1], 0,
6273 sizeof(cb->args) - sizeof(cb->args[0]));
360cc79d 6274 if (filter && filter->table &&
36596dad
PNA
6275 strcmp(filter->table, table->name))
6276 goto cont;
6277 if (filter &&
6278 filter->type != NFT_OBJECT_UNSPEC &&
6279 obj->ops->type->type != filter->type)
6280 goto cont;
a9fea2a3 6281
8e6cf365 6282 if (reset) {
68df2ed5 6283 char *buf = kasprintf(GFP_ATOMIC,
8e6cf365
RGB
6284 "%s:%llu;?:0",
6285 table->name,
6286 table->handle);
6287
6288 audit_log_nfcfg(buf,
6289 family,
6290 obj->handle,
14224039 6291 AUDIT_NFT_OP_OBJ_RESET,
68df2ed5 6292 GFP_ATOMIC);
8e6cf365
RGB
6293 kfree(buf);
6294 }
6295
36596dad
PNA
6296 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
6297 cb->nlh->nlmsg_seq,
6298 NFT_MSG_NEWOBJ,
6299 NLM_F_MULTI | NLM_F_APPEND,
98319cb9 6300 table->family, table,
36596dad
PNA
6301 obj, reset) < 0)
6302 goto done;
e5009240 6303
36596dad 6304 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
e5009240 6305cont:
36596dad 6306 idx++;
e5009240
PNA
6307 }
6308 }
6309done:
6310 rcu_read_unlock();
6311
6312 cb->args[0] = idx;
6313 return skb->len;
6314}
6315
90fd131a 6316static int nf_tables_dump_obj_start(struct netlink_callback *cb)
a9fea2a3 6317{
90fd131a
FW
6318 const struct nlattr * const *nla = cb->data;
6319 struct nft_obj_filter *filter = NULL;
e46abbcc 6320
90fd131a
FW
6321 if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) {
6322 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
6323 if (!filter)
6324 return -ENOMEM;
6325
6326 if (nla[NFTA_OBJ_TABLE]) {
6327 filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC);
6328 if (!filter->table) {
6329 kfree(filter);
6330 return -ENOMEM;
6331 }
6332 }
6333
6334 if (nla[NFTA_OBJ_TYPE])
6335 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
8bea728d 6336 }
a9fea2a3 6337
90fd131a 6338 cb->data = filter;
a9fea2a3
PNA
6339 return 0;
6340}
6341
90fd131a 6342static int nf_tables_dump_obj_done(struct netlink_callback *cb)
a9fea2a3 6343{
90fd131a 6344 struct nft_obj_filter *filter = cb->data;
a9fea2a3 6345
90fd131a
FW
6346 if (filter) {
6347 kfree(filter->table);
6348 kfree(filter);
e46abbcc 6349 }
a9fea2a3 6350
90fd131a 6351 return 0;
a9fea2a3
PNA
6352}
6353
d9adf22a 6354/* called with rcu_read_lock held */
e5009240
PNA
6355static int nf_tables_getobj(struct net *net, struct sock *nlsk,
6356 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
6357 const struct nlattr * const nla[],
6358 struct netlink_ext_ack *extack)
e5009240
PNA
6359{
6360 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6361 u8 genmask = nft_genmask_cur(net);
6362 int family = nfmsg->nfgen_family;
e5009240
PNA
6363 const struct nft_table *table;
6364 struct nft_object *obj;
6365 struct sk_buff *skb2;
43da04a5 6366 bool reset = false;
e5009240
PNA
6367 u32 objtype;
6368 int err;
6369
6370 if (nlh->nlmsg_flags & NLM_F_DUMP) {
6371 struct netlink_dump_control c = {
90fd131a 6372 .start = nf_tables_dump_obj_start,
e5009240 6373 .dump = nf_tables_dump_obj,
a9fea2a3 6374 .done = nf_tables_dump_obj_done,
d9adf22a 6375 .module = THIS_MODULE,
90fd131a 6376 .data = (void *)nla,
e5009240 6377 };
a9fea2a3 6378
d9adf22a 6379 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
e5009240
PNA
6380 }
6381
6382 if (!nla[NFTA_OBJ_NAME] ||
6383 !nla[NFTA_OBJ_TYPE])
6384 return -EINVAL;
6385
cac20fcd 6386 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6387 if (IS_ERR(table)) {
6388 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6389 return PTR_ERR(table);
36dd1bcc 6390 }
e5009240
PNA
6391
6392 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4d44175a 6393 obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask);
36dd1bcc
PNA
6394 if (IS_ERR(obj)) {
6395 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]);
e5009240 6396 return PTR_ERR(obj);
36dd1bcc 6397 }
e5009240 6398
d9adf22a 6399 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
e5009240
PNA
6400 if (!skb2)
6401 return -ENOMEM;
6402
43da04a5
PNA
6403 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
6404 reset = true;
6405
8e6cf365 6406 if (reset) {
14224039 6407 char *buf = kasprintf(GFP_ATOMIC, "%s:%llu;?:0",
8e6cf365
RGB
6408 table->name, table->handle);
6409
6410 audit_log_nfcfg(buf,
6411 family,
6412 obj->handle,
14224039 6413 AUDIT_NFT_OP_OBJ_RESET,
68df2ed5 6414 GFP_ATOMIC);
8e6cf365
RGB
6415 kfree(buf);
6416 }
6417
e5009240
PNA
6418 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
6419 nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
43da04a5 6420 family, table, obj, reset);
e5009240 6421 if (err < 0)
ee921183 6422 goto err_fill_obj_info;
e5009240 6423
ee921183
PNA
6424 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
6425
6426err_fill_obj_info:
e5009240
PNA
6427 kfree_skb(skb2);
6428 return err;
e5009240
PNA
6429}
6430
00bfb320 6431static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)
e5009240 6432{
dfc46034 6433 if (obj->ops->destroy)
00bfb320 6434 obj->ops->destroy(ctx, obj);
e5009240 6435
dfc46034 6436 module_put(obj->ops->type->owner);
d152159b 6437 kfree(obj->key.name);
bc7a7082 6438 kfree(obj->udata);
e5009240
PNA
6439 kfree(obj);
6440}
6441
6442static int nf_tables_delobj(struct net *net, struct sock *nlsk,
04ba724b
PNA
6443 struct sk_buff *skb, const struct nlmsghdr *nlh,
6444 const struct nlattr * const nla[],
6445 struct netlink_ext_ack *extack)
e5009240
PNA
6446{
6447 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
6448 u8 genmask = nft_genmask_next(net);
6449 int family = nfmsg->nfgen_family;
36dd1bcc 6450 const struct nlattr *attr;
e5009240
PNA
6451 struct nft_table *table;
6452 struct nft_object *obj;
6453 struct nft_ctx ctx;
6454 u32 objtype;
6455
6456 if (!nla[NFTA_OBJ_TYPE] ||
3ecbfd65 6457 (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE]))
e5009240
PNA
6458 return -EINVAL;
6459
cac20fcd 6460 table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask);
36dd1bcc
PNA
6461 if (IS_ERR(table)) {
6462 NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]);
e5009240 6463 return PTR_ERR(table);
36dd1bcc 6464 }
e5009240
PNA
6465
6466 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
36dd1bcc
PNA
6467 if (nla[NFTA_OBJ_HANDLE]) {
6468 attr = nla[NFTA_OBJ_HANDLE];
6469 obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask);
6470 } else {
6471 attr = nla[NFTA_OBJ_NAME];
4d44175a 6472 obj = nft_obj_lookup(net, table, attr, objtype, genmask);
36dd1bcc
PNA
6473 }
6474
6475 if (IS_ERR(obj)) {
6476 NL_SET_BAD_ATTR(extack, attr);
e5009240 6477 return PTR_ERR(obj);
36dd1bcc
PNA
6478 }
6479 if (obj->use > 0) {
6480 NL_SET_BAD_ATTR(extack, attr);
e5009240 6481 return -EBUSY;
36dd1bcc 6482 }
e5009240 6483
98319cb9 6484 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
e5009240
PNA
6485
6486 return nft_delobj(&ctx, obj);
6487}
6488
d152159b 6489void nft_obj_notify(struct net *net, const struct nft_table *table,
25e94a99
PNA
6490 struct nft_object *obj, u32 portid, u32 seq, int event,
6491 int family, int report, gfp_t gfp)
e5009240
PNA
6492{
6493 struct sk_buff *skb;
6494 int err;
14224039 6495 char *buf = kasprintf(gfp, "%s:%llu;?:0",
8e6cf365
RGB
6496 table->name, table->handle);
6497
6498 audit_log_nfcfg(buf,
6499 family,
6500 obj->handle,
6501 event == NFT_MSG_NEWOBJ ?
6502 AUDIT_NFT_OP_OBJ_REGISTER :
14224039 6503 AUDIT_NFT_OP_OBJ_UNREGISTER,
68df2ed5 6504 gfp);
8e6cf365 6505 kfree(buf);
e5009240 6506
2599e989
PNA
6507 if (!report &&
6508 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 6509 return;
e5009240 6510
2599e989 6511 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
e5009240
PNA
6512 if (skb == NULL)
6513 goto err;
6514
2599e989
PNA
6515 err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family,
6516 table, obj, false);
e5009240
PNA
6517 if (err < 0) {
6518 kfree_skb(skb);
6519 goto err;
6520 }
6521
67cc570e 6522 nft_notify_enqueue(skb, report, &net->nft.notify_list);
25e94a99 6523 return;
e5009240 6524err:
25e94a99 6525 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
e5009240 6526}
2599e989
PNA
6527EXPORT_SYMBOL_GPL(nft_obj_notify);
6528
25e94a99
PNA
6529static void nf_tables_obj_notify(const struct nft_ctx *ctx,
6530 struct nft_object *obj, int event)
2599e989 6531{
25e94a99 6532 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
36596dad 6533 ctx->family, ctx->report, GFP_KERNEL);
2599e989 6534}
e5009240 6535
3b49e2e9
PNA
6536/*
6537 * Flow tables
6538 */
6539void nft_register_flowtable_type(struct nf_flowtable_type *type)
6540{
6541 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6542 list_add_tail_rcu(&type->list, &nf_tables_flowtables);
6543 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6544}
6545EXPORT_SYMBOL_GPL(nft_register_flowtable_type);
6546
6547void nft_unregister_flowtable_type(struct nf_flowtable_type *type)
6548{
6549 nfnl_lock(NFNL_SUBSYS_NFTABLES);
6550 list_del_rcu(&type->list);
6551 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
6552}
6553EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type);
6554
6555static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = {
6556 [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING,
6557 .len = NFT_NAME_MAXLEN - 1 },
6558 [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING,
6559 .len = NFT_NAME_MAXLEN - 1 },
6560 [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED },
3ecbfd65 6561 [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 },
8bb69f3b 6562 [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 },
3b49e2e9
PNA
6563};
6564
cac20fcd
PNA
6565struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
6566 const struct nlattr *nla, u8 genmask)
3b49e2e9
PNA
6567{
6568 struct nft_flowtable *flowtable;
6569
d9adf22a 6570 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
3b49e2e9
PNA
6571 if (!nla_strcmp(nla, flowtable->name) &&
6572 nft_active_genmask(flowtable, genmask))
6573 return flowtable;
6574 }
6575 return ERR_PTR(-ENOENT);
6576}
cac20fcd 6577EXPORT_SYMBOL_GPL(nft_flowtable_lookup);
3b49e2e9 6578
9b05b6e1
LGL
6579void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
6580 struct nft_flowtable *flowtable,
6581 enum nft_trans_phase phase)
6582{
6583 switch (phase) {
6584 case NFT_TRANS_PREPARE:
6585 case NFT_TRANS_ABORT:
6586 case NFT_TRANS_RELEASE:
6587 flowtable->use--;
954d8297 6588 fallthrough;
9b05b6e1
LGL
6589 default:
6590 return;
6591 }
6592}
6593EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable);
6594
ae0662f8 6595static struct nft_flowtable *
cac20fcd
PNA
6596nft_flowtable_lookup_byhandle(const struct nft_table *table,
6597 const struct nlattr *nla, u8 genmask)
3ecbfd65
HS
6598{
6599 struct nft_flowtable *flowtable;
6600
6601 list_for_each_entry(flowtable, &table->flowtables, list) {
6602 if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle &&
6603 nft_active_genmask(flowtable, genmask))
6604 return flowtable;
6605 }
6606 return ERR_PTR(-ENOENT);
6607}
6608
d9246a53
PNA
6609struct nft_flowtable_hook {
6610 u32 num;
6611 int priority;
6612 struct list_head list;
6613};
6614
3b49e2e9
PNA
6615static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = {
6616 [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 },
6617 [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 },
6618 [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED },
6619};
6620
d9246a53
PNA
6621static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
6622 const struct nlattr *attr,
6623 struct nft_flowtable_hook *flowtable_hook,
5b6743fb 6624 struct nft_flowtable *flowtable, bool add)
3b49e2e9 6625{
3b49e2e9 6626 struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1];
3f0465a9 6627 struct nft_hook *hook;
3b49e2e9 6628 int hooknum, priority;
3f0465a9 6629 int err;
3b49e2e9 6630
d9246a53
PNA
6631 INIT_LIST_HEAD(&flowtable_hook->list);
6632
8cb08174
JB
6633 err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr,
6634 nft_flowtable_hook_policy, NULL);
3b49e2e9
PNA
6635 if (err < 0)
6636 return err;
6637
5b6743fb
PNA
6638 if (add) {
6639 if (!tb[NFTA_FLOWTABLE_HOOK_NUM] ||
6640 !tb[NFTA_FLOWTABLE_HOOK_PRIORITY])
6641 return -EINVAL;
3b49e2e9 6642
5b6743fb
PNA
6643 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
6644 if (hooknum != NF_NETDEV_INGRESS)
6645 return -EOPNOTSUPP;
6646
6647 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
6648
6649 flowtable_hook->priority = priority;
6650 flowtable_hook->num = hooknum;
6651 } else {
6652 if (tb[NFTA_FLOWTABLE_HOOK_NUM]) {
6653 hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM]));
6654 if (hooknum != flowtable->hooknum)
6655 return -EOPNOTSUPP;
6656 }
6657
6658 if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) {
6659 priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY]));
6660 if (priority != flowtable->data.priority)
6661 return -EOPNOTSUPP;
6662 }
3b49e2e9 6663
5b6743fb
PNA
6664 flowtable_hook->priority = flowtable->data.priority;
6665 flowtable_hook->num = flowtable->hooknum;
6666 }
3b49e2e9 6667
05abe445
PNA
6668 if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) {
6669 err = nf_tables_parse_netdev_hooks(ctx->net,
6670 tb[NFTA_FLOWTABLE_HOOK_DEVS],
6671 &flowtable_hook->list);
6672 if (err < 0)
6673 return err;
6674 }
3b49e2e9 6675
d9246a53 6676 list_for_each_entry(hook, &flowtable_hook->list, list) {
3f0465a9 6677 hook->ops.pf = NFPROTO_NETDEV;
5b6743fb
PNA
6678 hook->ops.hooknum = flowtable_hook->num;
6679 hook->ops.priority = flowtable_hook->priority;
6680 hook->ops.priv = &flowtable->data;
6681 hook->ops.hook = flowtable->data.type->hook;
3b49e2e9
PNA
6682 }
6683
3b49e2e9
PNA
6684 return err;
6685}
6686
98319cb9 6687static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
3b49e2e9
PNA
6688{
6689 const struct nf_flowtable_type *type;
6690
6691 list_for_each_entry(type, &nf_tables_flowtables, list) {
98319cb9 6692 if (family == type->family)
3b49e2e9
PNA
6693 return type;
6694 }
6695 return NULL;
6696}
6697
452238e8
FW
6698static const struct nf_flowtable_type *
6699nft_flowtable_type_get(struct net *net, u8 family)
3b49e2e9
PNA
6700{
6701 const struct nf_flowtable_type *type;
6702
98319cb9 6703 type = __nft_flowtable_type_get(family);
3b49e2e9
PNA
6704 if (type != NULL && try_module_get(type->owner))
6705 return type;
6706
f102d66b 6707 lockdep_nfnl_nft_mutex_not_held();
3b49e2e9
PNA
6708#ifdef CONFIG_MODULES
6709 if (type == NULL) {
eb014de4 6710 if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN)
3b49e2e9
PNA
6711 return ERR_PTR(-EAGAIN);
6712 }
6713#endif
6714 return ERR_PTR(-ENOENT);
6715}
6716
5acab914 6717/* Only called from error and netdev event paths. */
ff4bf2f4
PNA
6718static void nft_unregister_flowtable_hook(struct net *net,
6719 struct nft_flowtable *flowtable,
6720 struct nft_hook *hook)
6721{
6722 nf_unregister_net_hook(net, &hook->ops);
6723 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
6724 FLOW_BLOCK_UNBIND);
6725}
6726
3b49e2e9 6727static void nft_unregister_flowtable_net_hooks(struct net *net,
f9382669 6728 struct list_head *hook_list)
3b49e2e9 6729{
3f0465a9 6730 struct nft_hook *hook;
3b49e2e9 6731
f9382669 6732 list_for_each_entry(hook, hook_list, list)
5acab914 6733 nf_unregister_net_hook(net, &hook->ops);
3f0465a9
PNA
6734}
6735
6736static int nft_register_flowtable_net_hooks(struct net *net,
6737 struct nft_table *table,
f9382669 6738 struct list_head *hook_list,
3f0465a9
PNA
6739 struct nft_flowtable *flowtable)
6740{
6741 struct nft_hook *hook, *hook2, *next;
6742 struct nft_flowtable *ft;
6743 int err, i = 0;
6744
f9382669 6745 list_for_each_entry(hook, hook_list, list) {
3f0465a9
PNA
6746 list_for_each_entry(ft, &table->flowtables, list) {
6747 list_for_each_entry(hook2, &ft->hook_list, list) {
6748 if (hook->ops.dev == hook2->ops.dev &&
6749 hook->ops.pf == hook2->ops.pf) {
77a92189 6750 err = -EEXIST;
3f0465a9
PNA
6751 goto err_unregister_net_hooks;
6752 }
6753 }
6754 }
3b49e2e9 6755
d7c03a9f 6756 err = flowtable->data.type->setup(&flowtable->data,
6757 hook->ops.dev,
6758 FLOW_BLOCK_BIND);
3f0465a9
PNA
6759 if (err < 0)
6760 goto err_unregister_net_hooks;
6761
d7c03a9f 6762 err = nf_register_net_hook(net, &hook->ops);
6763 if (err < 0) {
6764 flowtable->data.type->setup(&flowtable->data,
6765 hook->ops.dev,
6766 FLOW_BLOCK_UNBIND);
6767 goto err_unregister_net_hooks;
6768 }
6769
3f0465a9 6770 i++;
3b49e2e9 6771 }
3f0465a9
PNA
6772
6773 return 0;
6774
6775err_unregister_net_hooks:
f9382669 6776 list_for_each_entry_safe(hook, next, hook_list, list) {
3f0465a9
PNA
6777 if (i-- <= 0)
6778 break;
6779
ff4bf2f4 6780 nft_unregister_flowtable_hook(net, flowtable, hook);
3f0465a9
PNA
6781 list_del_rcu(&hook->list);
6782 kfree_rcu(hook, rcu);
6783 }
6784
6785 return err;
3b49e2e9
PNA
6786}
6787
389a2cbc
PNA
6788static void nft_flowtable_hooks_destroy(struct list_head *hook_list)
6789{
6790 struct nft_hook *hook, *next;
6791
6792 list_for_each_entry_safe(hook, next, hook_list, list) {
6793 list_del_rcu(&hook->list);
6794 kfree_rcu(hook, rcu);
6795 }
6796}
6797
78d9f48f
PNA
6798static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,
6799 struct nft_flowtable *flowtable)
6800{
6801 const struct nlattr * const *nla = ctx->nla;
6802 struct nft_flowtable_hook flowtable_hook;
6803 struct nft_hook *hook, *next;
6804 struct nft_trans *trans;
6805 bool unregister = false;
6806 int err;
6807
6808 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 6809 &flowtable_hook, flowtable, false);
78d9f48f
PNA
6810 if (err < 0)
6811 return err;
6812
6813 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
6814 if (nft_hook_list_find(&flowtable->hook_list, hook)) {
6815 list_del(&hook->list);
6816 kfree(hook);
6817 }
6818 }
6819
6820 err = nft_register_flowtable_net_hooks(ctx->net, ctx->table,
6821 &flowtable_hook.list, flowtable);
6822 if (err < 0)
6823 goto err_flowtable_update_hook;
6824
6825 trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE,
6826 sizeof(struct nft_trans_flowtable));
6827 if (!trans) {
6828 unregister = true;
6829 err = -ENOMEM;
6830 goto err_flowtable_update_hook;
6831 }
6832
6833 nft_trans_flowtable(trans) = flowtable;
6834 nft_trans_flowtable_update(trans) = true;
6835 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
6836 list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans));
6837
6838 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
6839
6840 return 0;
6841
6842err_flowtable_update_hook:
6843 list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) {
6844 if (unregister)
6845 nft_unregister_flowtable_hook(ctx->net, flowtable, hook);
6846 list_del_rcu(&hook->list);
6847 kfree_rcu(hook, rcu);
6848 }
6849
6850 return err;
6851
6852}
6853
3b49e2e9
PNA
6854static int nf_tables_newflowtable(struct net *net, struct sock *nlsk,
6855 struct sk_buff *skb,
6856 const struct nlmsghdr *nlh,
6857 const struct nlattr * const nla[],
6858 struct netlink_ext_ack *extack)
6859{
6860 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
d9246a53 6861 struct nft_flowtable_hook flowtable_hook;
3b49e2e9
PNA
6862 const struct nf_flowtable_type *type;
6863 u8 genmask = nft_genmask_next(net);
6864 int family = nfmsg->nfgen_family;
3f0465a9
PNA
6865 struct nft_flowtable *flowtable;
6866 struct nft_hook *hook, *next;
3b49e2e9
PNA
6867 struct nft_table *table;
6868 struct nft_ctx ctx;
3f0465a9 6869 int err;
3b49e2e9
PNA
6870
6871 if (!nla[NFTA_FLOWTABLE_TABLE] ||
6872 !nla[NFTA_FLOWTABLE_NAME] ||
6873 !nla[NFTA_FLOWTABLE_HOOK])
6874 return -EINVAL;
6875
cac20fcd
PNA
6876 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
6877 genmask);
36dd1bcc
PNA
6878 if (IS_ERR(table)) {
6879 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 6880 return PTR_ERR(table);
36dd1bcc 6881 }
3b49e2e9 6882
cac20fcd
PNA
6883 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
6884 genmask);
3b49e2e9
PNA
6885 if (IS_ERR(flowtable)) {
6886 err = PTR_ERR(flowtable);
36dd1bcc
PNA
6887 if (err != -ENOENT) {
6888 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 6889 return err;
36dd1bcc 6890 }
3b49e2e9 6891 } else {
36dd1bcc
PNA
6892 if (nlh->nlmsg_flags & NLM_F_EXCL) {
6893 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]);
3b49e2e9 6894 return -EEXIST;
36dd1bcc 6895 }
3b49e2e9 6896
78d9f48f
PNA
6897 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
6898
6899 return nft_flowtable_update(&ctx, nlh, flowtable);
3b49e2e9
PNA
6900 }
6901
98319cb9 6902 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
3b49e2e9
PNA
6903
6904 flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL);
6905 if (!flowtable)
6906 return -ENOMEM;
6907
6908 flowtable->table = table;
3ecbfd65 6909 flowtable->handle = nf_tables_alloc_handle(table);
3f0465a9 6910 INIT_LIST_HEAD(&flowtable->hook_list);
3ecbfd65 6911
3b49e2e9
PNA
6912 flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL);
6913 if (!flowtable->name) {
6914 err = -ENOMEM;
6915 goto err1;
6916 }
6917
452238e8 6918 type = nft_flowtable_type_get(net, family);
3b49e2e9
PNA
6919 if (IS_ERR(type)) {
6920 err = PTR_ERR(type);
6921 goto err2;
6922 }
6923
8bb69f3b
PNA
6924 if (nla[NFTA_FLOWTABLE_FLAGS]) {
6925 flowtable->data.flags =
6926 ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS]));
cfbd1125 6927 if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK)
8bb69f3b
PNA
6928 goto err3;
6929 }
6930
6931 write_pnet(&flowtable->data.net, net);
3b49e2e9 6932 flowtable->data.type = type;
a268de77 6933 err = type->init(&flowtable->data);
3b49e2e9
PNA
6934 if (err < 0)
6935 goto err3;
6936
d9246a53 6937 err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 6938 &flowtable_hook, flowtable, true);
3b49e2e9 6939 if (err < 0)
a268de77 6940 goto err4;
3b49e2e9 6941
d9246a53
PNA
6942 list_splice(&flowtable_hook.list, &flowtable->hook_list);
6943 flowtable->data.priority = flowtable_hook.priority;
6944 flowtable->hooknum = flowtable_hook.num;
6945
f9382669
PNA
6946 err = nft_register_flowtable_net_hooks(ctx.net, table,
6947 &flowtable->hook_list,
6948 flowtable);
2d285f26 6949 if (err < 0) {
389a2cbc 6950 nft_flowtable_hooks_destroy(&flowtable->hook_list);
3f0465a9 6951 goto err4;
2d285f26 6952 }
3b49e2e9
PNA
6953
6954 err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable);
6955 if (err < 0)
3f0465a9 6956 goto err5;
3b49e2e9
PNA
6957
6958 list_add_tail_rcu(&flowtable->list, &table->flowtables);
6959 table->use++;
6960
6961 return 0;
a268de77 6962err5:
3f0465a9 6963 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
ff4bf2f4 6964 nft_unregister_flowtable_hook(net, flowtable, hook);
3f0465a9
PNA
6965 list_del_rcu(&hook->list);
6966 kfree_rcu(hook, rcu);
6967 }
a268de77
FF
6968err4:
6969 flowtable->data.type->free(&flowtable->data);
3b49e2e9
PNA
6970err3:
6971 module_put(type->owner);
6972err2:
6973 kfree(flowtable->name);
6974err1:
6975 kfree(flowtable);
6976 return err;
6977}
6978
3003055f
PNA
6979static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook)
6980{
6981 struct nft_hook *this, *next;
6982
6983 list_for_each_entry_safe(this, next, &flowtable_hook->list, list) {
6984 list_del(&this->list);
6985 kfree(this);
6986 }
6987}
6988
abadb2f8
PNA
6989static int nft_delflowtable_hook(struct nft_ctx *ctx,
6990 struct nft_flowtable *flowtable)
6991{
6992 const struct nlattr * const *nla = ctx->nla;
6993 struct nft_flowtable_hook flowtable_hook;
3003055f 6994 struct nft_hook *this, *hook;
abadb2f8
PNA
6995 struct nft_trans *trans;
6996 int err;
6997
6998 err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK],
5b6743fb 6999 &flowtable_hook, flowtable, false);
abadb2f8
PNA
7000 if (err < 0)
7001 return err;
7002
3003055f 7003 list_for_each_entry(this, &flowtable_hook.list, list) {
abadb2f8
PNA
7004 hook = nft_hook_list_find(&flowtable->hook_list, this);
7005 if (!hook) {
7006 err = -ENOENT;
7007 goto err_flowtable_del_hook;
7008 }
7009 hook->inactive = true;
abadb2f8
PNA
7010 }
7011
7012 trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE,
7013 sizeof(struct nft_trans_flowtable));
3003055f
PNA
7014 if (!trans) {
7015 err = -ENOMEM;
7016 goto err_flowtable_del_hook;
7017 }
abadb2f8
PNA
7018
7019 nft_trans_flowtable(trans) = flowtable;
7020 nft_trans_flowtable_update(trans) = true;
7021 INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans));
3003055f 7022 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8
PNA
7023
7024 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
7025
7026 return 0;
7027
7028err_flowtable_del_hook:
3003055f
PNA
7029 list_for_each_entry(this, &flowtable_hook.list, list) {
7030 hook = nft_hook_list_find(&flowtable->hook_list, this);
7031 if (!hook)
7032 break;
7033
abadb2f8 7034 hook->inactive = false;
3003055f
PNA
7035 }
7036 nft_flowtable_hook_release(&flowtable_hook);
abadb2f8
PNA
7037
7038 return err;
7039}
7040
3b49e2e9
PNA
7041static int nf_tables_delflowtable(struct net *net, struct sock *nlsk,
7042 struct sk_buff *skb,
7043 const struct nlmsghdr *nlh,
7044 const struct nlattr * const nla[],
7045 struct netlink_ext_ack *extack)
7046{
7047 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7048 u8 genmask = nft_genmask_next(net);
7049 int family = nfmsg->nfgen_family;
7050 struct nft_flowtable *flowtable;
36dd1bcc 7051 const struct nlattr *attr;
3b49e2e9
PNA
7052 struct nft_table *table;
7053 struct nft_ctx ctx;
7054
e603ea4b
PNA
7055 if (!nla[NFTA_FLOWTABLE_TABLE] ||
7056 (!nla[NFTA_FLOWTABLE_NAME] &&
7057 !nla[NFTA_FLOWTABLE_HANDLE]))
7058 return -EINVAL;
7059
cac20fcd
PNA
7060 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7061 genmask);
36dd1bcc
PNA
7062 if (IS_ERR(table)) {
7063 NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]);
3b49e2e9 7064 return PTR_ERR(table);
36dd1bcc 7065 }
3b49e2e9 7066
36dd1bcc
PNA
7067 if (nla[NFTA_FLOWTABLE_HANDLE]) {
7068 attr = nla[NFTA_FLOWTABLE_HANDLE];
7069 flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask);
7070 } else {
7071 attr = nla[NFTA_FLOWTABLE_NAME];
7072 flowtable = nft_flowtable_lookup(table, attr, genmask);
7073 }
7074
7075 if (IS_ERR(flowtable)) {
7076 NL_SET_BAD_ATTR(extack, attr);
7077 return PTR_ERR(flowtable);
7078 }
abadb2f8
PNA
7079
7080 nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla);
7081
7082 if (nla[NFTA_FLOWTABLE_HOOK])
7083 return nft_delflowtable_hook(&ctx, flowtable);
7084
36dd1bcc
PNA
7085 if (flowtable->use > 0) {
7086 NL_SET_BAD_ATTR(extack, attr);
3b49e2e9 7087 return -EBUSY;
36dd1bcc 7088 }
3b49e2e9 7089
3b49e2e9
PNA
7090 return nft_delflowtable(&ctx, flowtable);
7091}
7092
7093static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
7094 u32 portid, u32 seq, int event,
7095 u32 flags, int family,
c42d8bda
PNA
7096 struct nft_flowtable *flowtable,
7097 struct list_head *hook_list)
3b49e2e9
PNA
7098{
7099 struct nlattr *nest, *nest_devs;
7100 struct nfgenmsg *nfmsg;
3f0465a9 7101 struct nft_hook *hook;
3b49e2e9 7102 struct nlmsghdr *nlh;
3b49e2e9
PNA
7103
7104 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
7105 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
7106 if (nlh == NULL)
7107 goto nla_put_failure;
7108
7109 nfmsg = nlmsg_data(nlh);
7110 nfmsg->nfgen_family = family;
7111 nfmsg->version = NFNETLINK_V0;
7112 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
7113
7114 if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) ||
7115 nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) ||
3ecbfd65
HS
7116 nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) ||
7117 nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle),
8bb69f3b
PNA
7118 NFTA_FLOWTABLE_PAD) ||
7119 nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags)))
3b49e2e9
PNA
7120 goto nla_put_failure;
7121
ae0be8de 7122 nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK);
eb895086
KL
7123 if (!nest)
7124 goto nla_put_failure;
3b49e2e9 7125 if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) ||
71a8a63b 7126 nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority)))
3b49e2e9
PNA
7127 goto nla_put_failure;
7128
ae0be8de 7129 nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS);
3b49e2e9
PNA
7130 if (!nest_devs)
7131 goto nla_put_failure;
7132
c42d8bda 7133 list_for_each_entry_rcu(hook, hook_list, list) {
3f0465a9 7134 if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name))
3b49e2e9
PNA
7135 goto nla_put_failure;
7136 }
7137 nla_nest_end(skb, nest_devs);
7138 nla_nest_end(skb, nest);
7139
7140 nlmsg_end(skb, nlh);
7141 return 0;
7142
7143nla_put_failure:
7144 nlmsg_trim(skb, nlh);
7145 return -1;
7146}
7147
7148struct nft_flowtable_filter {
7149 char *table;
7150};
7151
7152static int nf_tables_dump_flowtable(struct sk_buff *skb,
7153 struct netlink_callback *cb)
7154{
7155 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
7156 struct nft_flowtable_filter *filter = cb->data;
7157 unsigned int idx = 0, s_idx = cb->args[0];
7158 struct net *net = sock_net(skb->sk);
7159 int family = nfmsg->nfgen_family;
7160 struct nft_flowtable *flowtable;
3b49e2e9
PNA
7161 const struct nft_table *table;
7162
7163 rcu_read_lock();
7164 cb->seq = net->nft.base_seq;
7165
36596dad 7166 list_for_each_entry_rcu(table, &net->nft.tables, list) {
98319cb9 7167 if (family != NFPROTO_UNSPEC && family != table->family)
3b49e2e9
PNA
7168 continue;
7169
36596dad
PNA
7170 list_for_each_entry_rcu(flowtable, &table->flowtables, list) {
7171 if (!nft_is_active(net, flowtable))
7172 goto cont;
7173 if (idx < s_idx)
7174 goto cont;
7175 if (idx > s_idx)
7176 memset(&cb->args[1], 0,
7177 sizeof(cb->args) - sizeof(cb->args[0]));
360cc79d 7178 if (filter && filter->table &&
36596dad
PNA
7179 strcmp(filter->table, table->name))
7180 goto cont;
3b49e2e9 7181
36596dad
PNA
7182 if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid,
7183 cb->nlh->nlmsg_seq,
7184 NFT_MSG_NEWFLOWTABLE,
7185 NLM_F_MULTI | NLM_F_APPEND,
c42d8bda
PNA
7186 table->family,
7187 flowtable,
7188 &flowtable->hook_list) < 0)
36596dad 7189 goto done;
3b49e2e9 7190
36596dad 7191 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3b49e2e9 7192cont:
36596dad 7193 idx++;
3b49e2e9
PNA
7194 }
7195 }
7196done:
7197 rcu_read_unlock();
7198
7199 cb->args[0] = idx;
7200 return skb->len;
7201}
7202
90fd131a 7203static int nf_tables_dump_flowtable_start(struct netlink_callback *cb)
3b49e2e9 7204{
90fd131a
FW
7205 const struct nlattr * const *nla = cb->data;
7206 struct nft_flowtable_filter *filter = NULL;
3b49e2e9 7207
90fd131a
FW
7208 if (nla[NFTA_FLOWTABLE_TABLE]) {
7209 filter = kzalloc(sizeof(*filter), GFP_ATOMIC);
7210 if (!filter)
7211 return -ENOMEM;
3b49e2e9 7212
90fd131a
FW
7213 filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE],
7214 GFP_ATOMIC);
7215 if (!filter->table) {
7216 kfree(filter);
7217 return -ENOMEM;
7218 }
7219 }
3b49e2e9 7220
90fd131a 7221 cb->data = filter;
3b49e2e9
PNA
7222 return 0;
7223}
7224
90fd131a 7225static int nf_tables_dump_flowtable_done(struct netlink_callback *cb)
3b49e2e9 7226{
90fd131a 7227 struct nft_flowtable_filter *filter = cb->data;
3b49e2e9 7228
3b49e2e9 7229 if (!filter)
90fd131a 7230 return 0;
3b49e2e9 7231
90fd131a
FW
7232 kfree(filter->table);
7233 kfree(filter);
7234
7235 return 0;
3b49e2e9
PNA
7236}
7237
d9adf22a 7238/* called with rcu_read_lock held */
3b49e2e9
PNA
7239static int nf_tables_getflowtable(struct net *net, struct sock *nlsk,
7240 struct sk_buff *skb,
7241 const struct nlmsghdr *nlh,
7242 const struct nlattr * const nla[],
7243 struct netlink_ext_ack *extack)
7244{
7245 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7246 u8 genmask = nft_genmask_cur(net);
7247 int family = nfmsg->nfgen_family;
7248 struct nft_flowtable *flowtable;
3b49e2e9
PNA
7249 const struct nft_table *table;
7250 struct sk_buff *skb2;
7251 int err;
7252
7253 if (nlh->nlmsg_flags & NLM_F_DUMP) {
7254 struct netlink_dump_control c = {
90fd131a 7255 .start = nf_tables_dump_flowtable_start,
3b49e2e9
PNA
7256 .dump = nf_tables_dump_flowtable,
7257 .done = nf_tables_dump_flowtable_done,
d9adf22a 7258 .module = THIS_MODULE,
90fd131a 7259 .data = (void *)nla,
3b49e2e9
PNA
7260 };
7261
d9adf22a 7262 return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c);
3b49e2e9
PNA
7263 }
7264
7265 if (!nla[NFTA_FLOWTABLE_NAME])
7266 return -EINVAL;
7267
cac20fcd
PNA
7268 table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family,
7269 genmask);
3b49e2e9
PNA
7270 if (IS_ERR(table))
7271 return PTR_ERR(table);
7272
cac20fcd
PNA
7273 flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME],
7274 genmask);
03a0120f 7275 if (IS_ERR(flowtable))
3b49e2e9
PNA
7276 return PTR_ERR(flowtable);
7277
d9adf22a 7278 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
3b49e2e9
PNA
7279 if (!skb2)
7280 return -ENOMEM;
7281
7282 err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid,
7283 nlh->nlmsg_seq,
7284 NFT_MSG_NEWFLOWTABLE, 0, family,
c42d8bda 7285 flowtable, &flowtable->hook_list);
3b49e2e9 7286 if (err < 0)
ee921183 7287 goto err_fill_flowtable_info;
3b49e2e9 7288
ee921183
PNA
7289 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7290
7291err_fill_flowtable_info:
3b49e2e9
PNA
7292 kfree_skb(skb2);
7293 return err;
7294}
7295
7296static void nf_tables_flowtable_notify(struct nft_ctx *ctx,
7297 struct nft_flowtable *flowtable,
c42d8bda 7298 struct list_head *hook_list,
3b49e2e9
PNA
7299 int event)
7300{
7301 struct sk_buff *skb;
7302 int err;
8e6cf365
RGB
7303 char *buf = kasprintf(GFP_KERNEL, "%s:%llu;%s:%llu",
7304 flowtable->table->name, flowtable->table->handle,
7305 flowtable->name, flowtable->handle);
7306
7307 audit_log_nfcfg(buf,
7308 ctx->family,
7309 flowtable->hooknum,
7310 event == NFT_MSG_NEWFLOWTABLE ?
7311 AUDIT_NFT_OP_FLOWTABLE_REGISTER :
14224039
RGB
7312 AUDIT_NFT_OP_FLOWTABLE_UNREGISTER,
7313 GFP_KERNEL);
8e6cf365 7314 kfree(buf);
3b49e2e9 7315
dceababa 7316 if (!ctx->report &&
3b49e2e9
PNA
7317 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
7318 return;
7319
7320 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7321 if (skb == NULL)
7322 goto err;
7323
7324 err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid,
7325 ctx->seq, event, 0,
c42d8bda 7326 ctx->family, flowtable, hook_list);
3b49e2e9
PNA
7327 if (err < 0) {
7328 kfree_skb(skb);
7329 goto err;
7330 }
7331
67cc570e 7332 nft_notify_enqueue(skb, ctx->report, &ctx->net->nft.notify_list);
3b49e2e9
PNA
7333 return;
7334err:
7335 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
7336}
7337
3b49e2e9
PNA
7338static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable)
7339{
3f0465a9
PNA
7340 struct nft_hook *hook, *next;
7341
5acab914 7342 flowtable->data.type->free(&flowtable->data);
3f0465a9 7343 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
5acab914
PNA
7344 flowtable->data.type->setup(&flowtable->data, hook->ops.dev,
7345 FLOW_BLOCK_UNBIND);
3f0465a9
PNA
7346 list_del_rcu(&hook->list);
7347 kfree(hook);
7348 }
3b49e2e9 7349 kfree(flowtable->name);
3b49e2e9 7350 module_put(flowtable->data.type->owner);
a12486eb 7351 kfree(flowtable);
3b49e2e9
PNA
7352}
7353
84d7fce6
PNA
7354static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
7355 u32 portid, u32 seq)
7356{
7357 struct nlmsghdr *nlh;
7358 struct nfgenmsg *nfmsg;
784b4e61 7359 char buf[TASK_COMM_LEN];
dedb67c4 7360 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
84d7fce6
PNA
7361
7362 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
7363 if (nlh == NULL)
7364 goto nla_put_failure;
7365
7366 nfmsg = nlmsg_data(nlh);
7367 nfmsg->nfgen_family = AF_UNSPEC;
7368 nfmsg->version = NFNETLINK_V0;
7369 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
7370
784b4e61
PS
7371 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) ||
7372 nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) ||
7373 nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current)))
84d7fce6
PNA
7374 goto nla_put_failure;
7375
053c095a
JB
7376 nlmsg_end(skb, nlh);
7377 return 0;
84d7fce6
PNA
7378
7379nla_put_failure:
7380 nlmsg_trim(skb, nlh);
7381 return -EMSGSIZE;
7382}
7383
3b49e2e9
PNA
7384static void nft_flowtable_event(unsigned long event, struct net_device *dev,
7385 struct nft_flowtable *flowtable)
7386{
3f0465a9 7387 struct nft_hook *hook;
3b49e2e9 7388
3f0465a9
PNA
7389 list_for_each_entry(hook, &flowtable->hook_list, list) {
7390 if (hook->ops.dev != dev)
3b49e2e9
PNA
7391 continue;
7392
5acab914 7393 /* flow_offload_netdev_event() cleans up entries for us. */
ff4bf2f4 7394 nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook);
3f0465a9
PNA
7395 list_del_rcu(&hook->list);
7396 kfree_rcu(hook, rcu);
3b49e2e9
PNA
7397 break;
7398 }
7399}
7400
7401static int nf_tables_flowtable_event(struct notifier_block *this,
7402 unsigned long event, void *ptr)
7403{
7404 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
7405 struct nft_flowtable *flowtable;
7406 struct nft_table *table;
0a2cf5ee 7407 struct net *net;
3b49e2e9
PNA
7408
7409 if (event != NETDEV_UNREGISTER)
7410 return 0;
7411
6a48de01 7412 net = dev_net(dev);
9e619d87 7413 mutex_lock(&net->nft.commit_mutex);
0a2cf5ee 7414 list_for_each_entry(table, &net->nft.tables, list) {
36596dad
PNA
7415 list_for_each_entry(flowtable, &table->flowtables, list) {
7416 nft_flowtable_event(event, dev, flowtable);
3b49e2e9
PNA
7417 }
7418 }
9e619d87 7419 mutex_unlock(&net->nft.commit_mutex);
6a48de01 7420
3b49e2e9
PNA
7421 return NOTIFY_DONE;
7422}
7423
7424static struct notifier_block nf_tables_flowtable_notifier = {
7425 .notifier_call = nf_tables_flowtable_event,
7426};
7427
25e94a99
PNA
7428static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
7429 int event)
84d7fce6
PNA
7430{
7431 struct nlmsghdr *nlh = nlmsg_hdr(skb);
7432 struct sk_buff *skb2;
7433 int err;
7434
8e6cf365 7435 audit_log_nfcfg("?:0;?:0", 0, net->nft.base_seq,
14224039 7436 AUDIT_NFT_OP_GEN_REGISTER, GFP_KERNEL);
8e6cf365 7437
dceababa 7438 if (!nlmsg_report(nlh) &&
84d7fce6 7439 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
25e94a99 7440 return;
84d7fce6 7441
84d7fce6
PNA
7442 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7443 if (skb2 == NULL)
7444 goto err;
7445
7446 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
7447 nlh->nlmsg_seq);
7448 if (err < 0) {
7449 kfree_skb(skb2);
7450 goto err;
7451 }
7452
25e94a99
PNA
7453 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
7454 nlmsg_report(nlh), GFP_KERNEL);
7455 return;
84d7fce6 7456err:
25e94a99
PNA
7457 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
7458 -ENOBUFS);
84d7fce6
PNA
7459}
7460
7b8002a1
PNA
7461static int nf_tables_getgen(struct net *net, struct sock *nlsk,
7462 struct sk_buff *skb, const struct nlmsghdr *nlh,
04ba724b
PNA
7463 const struct nlattr * const nla[],
7464 struct netlink_ext_ack *extack)
84d7fce6 7465{
84d7fce6
PNA
7466 struct sk_buff *skb2;
7467 int err;
7468
d9adf22a 7469 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
84d7fce6
PNA
7470 if (skb2 == NULL)
7471 return -ENOMEM;
7472
7473 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
7474 nlh->nlmsg_seq);
7475 if (err < 0)
ee921183 7476 goto err_fill_gen_info;
84d7fce6 7477
ee921183
PNA
7478 return nfnetlink_unicast(skb2, net, NETLINK_CB(skb).portid);
7479
7480err_fill_gen_info:
84d7fce6
PNA
7481 kfree_skb(skb2);
7482 return err;
7483}
7484
96518518
PM
7485static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
7486 [NFT_MSG_NEWTABLE] = {
55dd6f93 7487 .call_batch = nf_tables_newtable,
96518518
PM
7488 .attr_count = NFTA_TABLE_MAX,
7489 .policy = nft_table_policy,
7490 },
7491 [NFT_MSG_GETTABLE] = {
d9adf22a 7492 .call_rcu = nf_tables_gettable,
96518518
PM
7493 .attr_count = NFTA_TABLE_MAX,
7494 .policy = nft_table_policy,
7495 },
7496 [NFT_MSG_DELTABLE] = {
55dd6f93 7497 .call_batch = nf_tables_deltable,
96518518
PM
7498 .attr_count = NFTA_TABLE_MAX,
7499 .policy = nft_table_policy,
7500 },
7501 [NFT_MSG_NEWCHAIN] = {
91c7b38d 7502 .call_batch = nf_tables_newchain,
96518518
PM
7503 .attr_count = NFTA_CHAIN_MAX,
7504 .policy = nft_chain_policy,
7505 },
7506 [NFT_MSG_GETCHAIN] = {
d9adf22a 7507 .call_rcu = nf_tables_getchain,
96518518
PM
7508 .attr_count = NFTA_CHAIN_MAX,
7509 .policy = nft_chain_policy,
7510 },
7511 [NFT_MSG_DELCHAIN] = {
91c7b38d 7512 .call_batch = nf_tables_delchain,
96518518
PM
7513 .attr_count = NFTA_CHAIN_MAX,
7514 .policy = nft_chain_policy,
7515 },
7516 [NFT_MSG_NEWRULE] = {
0628b123 7517 .call_batch = nf_tables_newrule,
96518518
PM
7518 .attr_count = NFTA_RULE_MAX,
7519 .policy = nft_rule_policy,
7520 },
7521 [NFT_MSG_GETRULE] = {
d9adf22a 7522 .call_rcu = nf_tables_getrule,
96518518
PM
7523 .attr_count = NFTA_RULE_MAX,
7524 .policy = nft_rule_policy,
7525 },
7526 [NFT_MSG_DELRULE] = {
0628b123 7527 .call_batch = nf_tables_delrule,
96518518
PM
7528 .attr_count = NFTA_RULE_MAX,
7529 .policy = nft_rule_policy,
7530 },
20a69341 7531 [NFT_MSG_NEWSET] = {
958bee14 7532 .call_batch = nf_tables_newset,
20a69341
PM
7533 .attr_count = NFTA_SET_MAX,
7534 .policy = nft_set_policy,
7535 },
7536 [NFT_MSG_GETSET] = {
d9adf22a 7537 .call_rcu = nf_tables_getset,
20a69341
PM
7538 .attr_count = NFTA_SET_MAX,
7539 .policy = nft_set_policy,
7540 },
7541 [NFT_MSG_DELSET] = {
958bee14 7542 .call_batch = nf_tables_delset,
20a69341
PM
7543 .attr_count = NFTA_SET_MAX,
7544 .policy = nft_set_policy,
7545 },
7546 [NFT_MSG_NEWSETELEM] = {
958bee14 7547 .call_batch = nf_tables_newsetelem,
20a69341
PM
7548 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7549 .policy = nft_set_elem_list_policy,
7550 },
7551 [NFT_MSG_GETSETELEM] = {
d9adf22a 7552 .call_rcu = nf_tables_getsetelem,
20a69341
PM
7553 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7554 .policy = nft_set_elem_list_policy,
7555 },
7556 [NFT_MSG_DELSETELEM] = {
958bee14 7557 .call_batch = nf_tables_delsetelem,
20a69341
PM
7558 .attr_count = NFTA_SET_ELEM_LIST_MAX,
7559 .policy = nft_set_elem_list_policy,
7560 },
84d7fce6 7561 [NFT_MSG_GETGEN] = {
d9adf22a 7562 .call_rcu = nf_tables_getgen,
84d7fce6 7563 },
e5009240
PNA
7564 [NFT_MSG_NEWOBJ] = {
7565 .call_batch = nf_tables_newobj,
7566 .attr_count = NFTA_OBJ_MAX,
7567 .policy = nft_obj_policy,
7568 },
7569 [NFT_MSG_GETOBJ] = {
d9adf22a 7570 .call_rcu = nf_tables_getobj,
e5009240
PNA
7571 .attr_count = NFTA_OBJ_MAX,
7572 .policy = nft_obj_policy,
7573 },
7574 [NFT_MSG_DELOBJ] = {
7575 .call_batch = nf_tables_delobj,
7576 .attr_count = NFTA_OBJ_MAX,
7577 .policy = nft_obj_policy,
7578 },
43da04a5 7579 [NFT_MSG_GETOBJ_RESET] = {
d9adf22a 7580 .call_rcu = nf_tables_getobj,
43da04a5
PNA
7581 .attr_count = NFTA_OBJ_MAX,
7582 .policy = nft_obj_policy,
7583 },
3b49e2e9
PNA
7584 [NFT_MSG_NEWFLOWTABLE] = {
7585 .call_batch = nf_tables_newflowtable,
7586 .attr_count = NFTA_FLOWTABLE_MAX,
7587 .policy = nft_flowtable_policy,
7588 },
7589 [NFT_MSG_GETFLOWTABLE] = {
d9adf22a 7590 .call_rcu = nf_tables_getflowtable,
3b49e2e9
PNA
7591 .attr_count = NFTA_FLOWTABLE_MAX,
7592 .policy = nft_flowtable_policy,
7593 },
7594 [NFT_MSG_DELFLOWTABLE] = {
7595 .call_batch = nf_tables_delflowtable,
7596 .attr_count = NFTA_FLOWTABLE_MAX,
7597 .policy = nft_flowtable_policy,
7598 },
96518518
PM
7599};
7600
a654de8f
PNA
7601static int nf_tables_validate(struct net *net)
7602{
7603 struct nft_table *table;
7604
7605 switch (net->nft.validate_state) {
7606 case NFT_VALIDATE_SKIP:
7607 break;
7608 case NFT_VALIDATE_NEED:
7609 nft_validate_state_update(net, NFT_VALIDATE_DO);
954d8297 7610 fallthrough;
a654de8f
PNA
7611 case NFT_VALIDATE_DO:
7612 list_for_each_entry(table, &net->nft.tables, list) {
7613 if (nft_table_validate(net, table) < 0)
7614 return -EAGAIN;
7615 }
7616 break;
7617 }
7618
7619 return 0;
7620}
7621
66293c46
FW
7622/* a drop policy has to be deferred until all rules have been activated,
7623 * otherwise a large ruleset that contains a drop-policy base chain will
7624 * cause all packets to get dropped until the full transaction has been
7625 * processed.
7626 *
7627 * We defer the drop policy until the transaction has been finalized.
7628 */
7629static void nft_chain_commit_drop_policy(struct nft_trans *trans)
7630{
7631 struct nft_base_chain *basechain;
7632
7633 if (nft_trans_chain_policy(trans) != NF_DROP)
7634 return;
7635
7636 if (!nft_is_base_chain(trans->ctx.chain))
7637 return;
7638
7639 basechain = nft_base_chain(trans->ctx.chain);
7640 basechain->policy = NF_DROP;
7641}
7642
91c7b38d
PNA
7643static void nft_chain_commit_update(struct nft_trans *trans)
7644{
7645 struct nft_base_chain *basechain;
7646
1b2470e5
FW
7647 if (nft_trans_chain_name(trans)) {
7648 rhltable_remove(&trans->ctx.table->chains_ht,
7649 &trans->ctx.chain->rhlhead,
7650 nft_chain_ht_params);
d71efb59 7651 swap(trans->ctx.chain->name, nft_trans_chain_name(trans));
1b2470e5
FW
7652 rhltable_insert_key(&trans->ctx.table->chains_ht,
7653 trans->ctx.chain->name,
7654 &trans->ctx.chain->rhlhead,
7655 nft_chain_ht_params);
7656 }
91c7b38d 7657
f323d954 7658 if (!nft_is_base_chain(trans->ctx.chain))
91c7b38d
PNA
7659 return;
7660
53315ac6
FW
7661 nft_chain_stats_replace(trans);
7662
91c7b38d 7663 basechain = nft_base_chain(trans->ctx.chain);
91c7b38d
PNA
7664
7665 switch (nft_trans_chain_policy(trans)) {
7666 case NF_DROP:
7667 case NF_ACCEPT:
7668 basechain->policy = nft_trans_chain_policy(trans);
7669 break;
7670 }
7671}
7672
d62d0ba9
FFM
7673static void nft_obj_commit_update(struct nft_trans *trans)
7674{
7675 struct nft_object *newobj;
7676 struct nft_object *obj;
7677
7678 obj = nft_trans_obj(trans);
7679 newobj = nft_trans_obj_newobj(trans);
7680
9fedd894
FFM
7681 if (obj->ops->update)
7682 obj->ops->update(obj, newobj);
d62d0ba9
FFM
7683
7684 kfree(newobj);
7685}
7686
2f99aa31 7687static void nft_commit_release(struct nft_trans *trans)
c7c32e72 7688{
c7c32e72
PNA
7689 switch (trans->msg_type) {
7690 case NFT_MSG_DELTABLE:
7691 nf_tables_table_destroy(&trans->ctx);
7692 break;
9f8aac0b 7693 case NFT_MSG_NEWCHAIN:
53315ac6 7694 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b
FW
7695 kfree(nft_trans_chain_name(trans));
7696 break;
c7c32e72 7697 case NFT_MSG_DELCHAIN:
43a605f2 7698 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
7699 break;
7700 case NFT_MSG_DELRULE:
7701 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
7702 break;
7703 case NFT_MSG_DELSET:
0c2a85ed 7704 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 7705 break;
61edafbb 7706 case NFT_MSG_DELSETELEM:
3453c927
PNA
7707 nf_tables_set_elem_destroy(&trans->ctx,
7708 nft_trans_elem_set(trans),
59105446 7709 nft_trans_elem(trans).priv);
61edafbb 7710 break;
e5009240 7711 case NFT_MSG_DELOBJ:
00bfb320 7712 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 7713 break;
3b49e2e9 7714 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
7715 if (nft_trans_flowtable_update(trans))
7716 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
7717 else
7718 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 7719 break;
c7c32e72 7720 }
0935d558
FW
7721
7722 if (trans->put_net)
7723 put_net(trans->ctx.net);
7724
c7c32e72
PNA
7725 kfree(trans);
7726}
7727
0935d558 7728static void nf_tables_trans_destroy_work(struct work_struct *w)
2f99aa31
FW
7729{
7730 struct nft_trans *trans, *next;
0935d558
FW
7731 LIST_HEAD(head);
7732
7733 spin_lock(&nf_tables_destroy_list_lock);
7734 list_splice_init(&nf_tables_destroy_list, &head);
7735 spin_unlock(&nf_tables_destroy_list_lock);
2f99aa31 7736
0935d558 7737 if (list_empty(&head))
2f99aa31
FW
7738 return;
7739
7740 synchronize_rcu();
7741
0935d558 7742 list_for_each_entry_safe(trans, next, &head, list) {
2f99aa31
FW
7743 list_del(&trans->list);
7744 nft_commit_release(trans);
7745 }
7746}
7747
ffe8923f
FW
7748void nf_tables_trans_destroy_flush_work(void)
7749{
7750 flush_work(&trans_destroy_work);
7751}
7752EXPORT_SYMBOL_GPL(nf_tables_trans_destroy_flush_work);
7753
0cbc06b3
FW
7754static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain)
7755{
7756 struct nft_rule *rule;
7757 unsigned int alloc = 0;
7758 int i;
7759
7760 /* already handled or inactive chain? */
7761 if (chain->rules_next || !nft_is_active_next(net, chain))
7762 return 0;
7763
7764 rule = list_entry(&chain->rules, struct nft_rule, list);
7765 i = 0;
7766
7767 list_for_each_entry_continue(rule, &chain->rules, list) {
7768 if (nft_is_active_next(net, rule))
7769 alloc++;
7770 }
7771
7772 chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc);
7773 if (!chain->rules_next)
7774 return -ENOMEM;
7775
7776 list_for_each_entry_continue(rule, &chain->rules, list) {
7777 if (nft_is_active_next(net, rule))
7778 chain->rules_next[i++] = rule;
7779 }
7780
7781 chain->rules_next[i] = NULL;
7782 return 0;
7783}
7784
7785static void nf_tables_commit_chain_prepare_cancel(struct net *net)
7786{
7787 struct nft_trans *trans, *next;
7788
7789 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
7790 struct nft_chain *chain = trans->ctx.chain;
7791
7792 if (trans->msg_type == NFT_MSG_NEWRULE ||
7793 trans->msg_type == NFT_MSG_DELRULE) {
7794 kvfree(chain->rules_next);
7795 chain->rules_next = NULL;
7796 }
7797 }
7798}
7799
7800static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h)
7801{
7802 struct nft_rules_old *o = container_of(h, struct nft_rules_old, h);
7803
7804 kvfree(o->start);
7805}
7806
7807static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules)
7808{
7809 struct nft_rule **r = rules;
7810 struct nft_rules_old *old;
7811
7812 while (*r)
7813 r++;
7814
7815 r++; /* rcu_head is after end marker */
7816 old = (void *) r;
7817 old->start = rules;
7818
7819 call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old);
7820}
7821
0fb39bbe 7822static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain)
0cbc06b3
FW
7823{
7824 struct nft_rule **g0, **g1;
7825 bool next_genbit;
7826
7827 next_genbit = nft_gencursor_next(net);
7828
7829 g0 = rcu_dereference_protected(chain->rules_gen_0,
f102d66b 7830 lockdep_commit_lock_is_held(net));
0cbc06b3 7831 g1 = rcu_dereference_protected(chain->rules_gen_1,
f102d66b 7832 lockdep_commit_lock_is_held(net));
0cbc06b3
FW
7833
7834 /* No changes to this chain? */
7835 if (chain->rules_next == NULL) {
7836 /* chain had no change in last or next generation */
7837 if (g0 == g1)
7838 return;
7839 /*
7840 * chain had no change in this generation; make sure next
7841 * one uses same rules as current generation.
7842 */
7843 if (next_genbit) {
7844 rcu_assign_pointer(chain->rules_gen_1, g0);
7845 nf_tables_commit_chain_free_rules_old(g1);
7846 } else {
7847 rcu_assign_pointer(chain->rules_gen_0, g1);
7848 nf_tables_commit_chain_free_rules_old(g0);
7849 }
7850
7851 return;
7852 }
7853
7854 if (next_genbit)
7855 rcu_assign_pointer(chain->rules_gen_1, chain->rules_next);
7856 else
7857 rcu_assign_pointer(chain->rules_gen_0, chain->rules_next);
7858
7859 chain->rules_next = NULL;
7860
7861 if (g0 == g1)
7862 return;
7863
7864 if (next_genbit)
7865 nf_tables_commit_chain_free_rules_old(g1);
7866 else
7867 nf_tables_commit_chain_free_rules_old(g0);
7868}
7869
d152159b
FW
7870static void nft_obj_del(struct nft_object *obj)
7871{
4d44175a 7872 rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params);
d152159b
FW
7873 list_del_rcu(&obj->list);
7874}
7875
d0e2c7de 7876void nft_chain_del(struct nft_chain *chain)
1b2470e5
FW
7877{
7878 struct nft_table *table = chain->table;
7879
7880 WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead,
7881 nft_chain_ht_params));
7882 list_del_rcu(&chain->list);
7883}
7884
abadb2f8
PNA
7885static void nft_flowtable_hooks_del(struct nft_flowtable *flowtable,
7886 struct list_head *hook_list)
7887{
7888 struct nft_hook *hook, *next;
7889
7890 list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) {
7891 if (hook->inactive)
7892 list_move(&hook->list, hook_list);
7893 }
7894}
7895
eb014de4
PNA
7896static void nf_tables_module_autoload_cleanup(struct net *net)
7897{
7898 struct nft_module_request *req, *next;
7899
7900 WARN_ON_ONCE(!list_empty(&net->nft.commit_list));
7901 list_for_each_entry_safe(req, next, &net->nft.module_list, list) {
7902 WARN_ON_ONCE(!req->done);
7903 list_del(&req->list);
7904 kfree(req);
7905 }
7906}
7907
0935d558
FW
7908static void nf_tables_commit_release(struct net *net)
7909{
7910 struct nft_trans *trans;
7911
7912 /* all side effects have to be made visible.
7913 * For example, if a chain named 'foo' has been deleted, a
7914 * new transaction must not find it anymore.
7915 *
7916 * Memory reclaim happens asynchronously from work queue
7917 * to prevent expensive synchronize_rcu() in commit phase.
7918 */
7919 if (list_empty(&net->nft.commit_list)) {
eb014de4 7920 nf_tables_module_autoload_cleanup(net);
0935d558
FW
7921 mutex_unlock(&net->nft.commit_mutex);
7922 return;
7923 }
7924
7925 trans = list_last_entry(&net->nft.commit_list,
7926 struct nft_trans, list);
7927 get_net(trans->ctx.net);
7928 WARN_ON_ONCE(trans->put_net);
7929
7930 trans->put_net = true;
7931 spin_lock(&nf_tables_destroy_list_lock);
7932 list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list);
7933 spin_unlock(&nf_tables_destroy_list_lock);
7934
eb014de4 7935 nf_tables_module_autoload_cleanup(net);
0935d558 7936 schedule_work(&trans_destroy_work);
ffe8923f
FW
7937
7938 mutex_unlock(&net->nft.commit_mutex);
0935d558
FW
7939}
7940
67cc570e
PNA
7941static void nft_commit_notify(struct net *net, u32 portid)
7942{
7943 struct sk_buff *batch_skb = NULL, *nskb, *skb;
7944 unsigned char *data;
7945 int len;
7946
7947 list_for_each_entry_safe(skb, nskb, &net->nft.notify_list, list) {
7948 if (!batch_skb) {
7949new_batch:
7950 batch_skb = skb;
7951 len = NLMSG_GOODSIZE - skb->len;
7952 list_del(&skb->list);
7953 continue;
7954 }
7955 len -= skb->len;
7956 if (len > 0 && NFT_CB(skb).report == NFT_CB(batch_skb).report) {
7957 data = skb_put(batch_skb, skb->len);
7958 memcpy(data, skb->data, skb->len);
7959 list_del(&skb->list);
7960 kfree_skb(skb);
7961 continue;
7962 }
7963 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
7964 NFT_CB(batch_skb).report, GFP_KERNEL);
7965 goto new_batch;
7966 }
7967
7968 if (batch_skb) {
7969 nfnetlink_send(batch_skb, net, portid, NFNLGRP_NFTABLES,
7970 NFT_CB(batch_skb).report, GFP_KERNEL);
7971 }
7972
7973 WARN_ON_ONCE(!list_empty(&net->nft.notify_list));
7974}
7975
5913beaf 7976static int nf_tables_commit(struct net *net, struct sk_buff *skb)
37082f93 7977{
37082f93 7978 struct nft_trans *trans, *next;
a3716e70 7979 struct nft_trans_elem *te;
0cbc06b3
FW
7980 struct nft_chain *chain;
7981 struct nft_table *table;
c9626a2c 7982 int err;
37082f93 7983
b8b27498
FW
7984 if (list_empty(&net->nft.commit_list)) {
7985 mutex_unlock(&net->nft.commit_mutex);
7986 return 0;
7987 }
7988
a654de8f
PNA
7989 /* 0. Validate ruleset, otherwise roll back for error reporting. */
7990 if (nf_tables_validate(net) < 0)
7991 return -EAGAIN;
7992
c9626a2c
PNA
7993 err = nft_flow_rule_offload_commit(net);
7994 if (err < 0)
7995 return err;
7996
0cbc06b3
FW
7997 /* 1. Allocate space for next generation rules_gen_X[] */
7998 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
7999 int ret;
37082f93 8000
0cbc06b3
FW
8001 if (trans->msg_type == NFT_MSG_NEWRULE ||
8002 trans->msg_type == NFT_MSG_DELRULE) {
8003 chain = trans->ctx.chain;
8004
8005 ret = nf_tables_commit_chain_prepare(net, chain);
8006 if (ret < 0) {
8007 nf_tables_commit_chain_prepare_cancel(net);
8008 return ret;
8009 }
8010 }
8011 }
37082f93 8012
0cbc06b3
FW
8013 /* step 2. Make rules_gen_X visible to packet path */
8014 list_for_each_entry(table, &net->nft.tables, list) {
0fb39bbe
FW
8015 list_for_each_entry(chain, &table->chains, list)
8016 nf_tables_commit_chain(net, chain);
0cbc06b3
FW
8017 }
8018
8019 /*
8020 * Bump generation counter, invalidate any dump in progress.
8021 * Cannot fail after this point.
37082f93 8022 */
0cbc06b3
FW
8023 while (++net->nft.base_seq == 0);
8024
8025 /* step 3. Start new generation, rules_gen_X now in use. */
8026 net->nft.gencursor = nft_gencursor_next(net);
37082f93
PNA
8027
8028 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
b380e5c7 8029 switch (trans->msg_type) {
55dd6f93
PNA
8030 case NFT_MSG_NEWTABLE:
8031 if (nft_trans_table_update(trans)) {
8032 if (!nft_trans_table_enable(trans)) {
664b0f8c 8033 nf_tables_table_disable(net,
55dd6f93
PNA
8034 trans->ctx.table);
8035 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
8036 }
8037 } else {
f2a6d766 8038 nft_clear(net, trans->ctx.table);
55dd6f93 8039 }
35151d84 8040 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
55dd6f93
PNA
8041 nft_trans_destroy(trans);
8042 break;
8043 case NFT_MSG_DELTABLE:
f2a6d766 8044 list_del_rcu(&trans->ctx.table->list);
35151d84 8045 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
55dd6f93 8046 break;
91c7b38d 8047 case NFT_MSG_NEWCHAIN:
9f8aac0b 8048 if (nft_trans_chain_update(trans)) {
91c7b38d 8049 nft_chain_commit_update(trans);
9f8aac0b
FW
8050 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8051 /* trans destroyed after rcu grace period */
8052 } else {
66293c46 8053 nft_chain_commit_drop_policy(trans);
664b0f8c 8054 nft_clear(net, trans->ctx.chain);
9f8aac0b
FW
8055 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
8056 nft_trans_destroy(trans);
8057 }
91c7b38d
PNA
8058 break;
8059 case NFT_MSG_DELCHAIN:
1b2470e5 8060 nft_chain_del(trans->ctx.chain);
35151d84 8061 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
c974a3a3
PNA
8062 nf_tables_unregister_hook(trans->ctx.net,
8063 trans->ctx.table,
8064 trans->ctx.chain);
91c7b38d 8065 break;
b380e5c7 8066 case NFT_MSG_NEWRULE:
889f7ee7 8067 nft_clear(trans->ctx.net, nft_trans_rule(trans));
35151d84 8068 nf_tables_rule_notify(&trans->ctx,
37082f93 8069 nft_trans_rule(trans),
35151d84 8070 NFT_MSG_NEWRULE);
37082f93 8071 nft_trans_destroy(trans);
b380e5c7
PNA
8072 break;
8073 case NFT_MSG_DELRULE:
8074 list_del_rcu(&nft_trans_rule(trans)->list);
35151d84
PNA
8075 nf_tables_rule_notify(&trans->ctx,
8076 nft_trans_rule(trans),
8077 NFT_MSG_DELRULE);
f6ac8585
PNA
8078 nft_rule_expr_deactivate(&trans->ctx,
8079 nft_trans_rule(trans),
8080 NFT_TRANS_COMMIT);
b380e5c7 8081 break;
958bee14 8082 case NFT_MSG_NEWSET:
37a9cc52 8083 nft_clear(net, nft_trans_set(trans));
4fefee57
PNA
8084 /* This avoids hitting -EBUSY when deleting the table
8085 * from the transaction.
8086 */
408070d6 8087 if (nft_set_is_anonymous(nft_trans_set(trans)) &&
4fefee57
PNA
8088 !list_empty(&nft_trans_set(trans)->bindings))
8089 trans->ctx.table->use--;
8090
958bee14 8091 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 8092 NFT_MSG_NEWSET, GFP_KERNEL);
958bee14
PNA
8093 nft_trans_destroy(trans);
8094 break;
8095 case NFT_MSG_DELSET:
37a9cc52 8096 list_del_rcu(&nft_trans_set(trans)->list);
958bee14 8097 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 8098 NFT_MSG_DELSET, GFP_KERNEL);
958bee14 8099 break;
60319eb1 8100 case NFT_MSG_NEWSETELEM:
cc02e457
PM
8101 te = (struct nft_trans_elem *)trans->data;
8102
42a55769 8103 te->set->ops->activate(net, te->set, &te->elem);
cc02e457
PM
8104 nf_tables_setelem_notify(&trans->ctx, te->set,
8105 &te->elem,
60319eb1
PNA
8106 NFT_MSG_NEWSETELEM, 0);
8107 nft_trans_destroy(trans);
8108 break;
8109 case NFT_MSG_DELSETELEM:
a3716e70 8110 te = (struct nft_trans_elem *)trans->data;
fe2811eb 8111
a3716e70
PNA
8112 nf_tables_setelem_notify(&trans->ctx, te->set,
8113 &te->elem,
60319eb1 8114 NFT_MSG_DELSETELEM, 0);
5cb82a38 8115 te->set->ops->remove(net, te->set, &te->elem);
3dd0673a
PM
8116 atomic_dec(&te->set->nelems);
8117 te->set->ndeact--;
60319eb1 8118 break;
e5009240 8119 case NFT_MSG_NEWOBJ:
d62d0ba9
FFM
8120 if (nft_trans_obj_update(trans)) {
8121 nft_obj_commit_update(trans);
8122 nf_tables_obj_notify(&trans->ctx,
8123 nft_trans_obj(trans),
8124 NFT_MSG_NEWOBJ);
8125 } else {
8126 nft_clear(net, nft_trans_obj(trans));
8127 nf_tables_obj_notify(&trans->ctx,
8128 nft_trans_obj(trans),
8129 NFT_MSG_NEWOBJ);
8130 nft_trans_destroy(trans);
8131 }
e5009240
PNA
8132 break;
8133 case NFT_MSG_DELOBJ:
d152159b 8134 nft_obj_del(nft_trans_obj(trans));
e5009240
PNA
8135 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
8136 NFT_MSG_DELOBJ);
8137 break;
3b49e2e9 8138 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8139 if (nft_trans_flowtable_update(trans)) {
8140 nf_tables_flowtable_notify(&trans->ctx,
8141 nft_trans_flowtable(trans),
8142 &nft_trans_flowtable_hooks(trans),
8143 NFT_MSG_NEWFLOWTABLE);
8144 list_splice(&nft_trans_flowtable_hooks(trans),
8145 &nft_trans_flowtable(trans)->hook_list);
8146 } else {
8147 nft_clear(net, nft_trans_flowtable(trans));
8148 nf_tables_flowtable_notify(&trans->ctx,
8149 nft_trans_flowtable(trans),
8150 &nft_trans_flowtable(trans)->hook_list,
8151 NFT_MSG_NEWFLOWTABLE);
8152 }
3b49e2e9
PNA
8153 nft_trans_destroy(trans);
8154 break;
8155 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
8156 if (nft_trans_flowtable_update(trans)) {
8157 nft_flowtable_hooks_del(nft_trans_flowtable(trans),
8158 &nft_trans_flowtable_hooks(trans));
8159 nf_tables_flowtable_notify(&trans->ctx,
8160 nft_trans_flowtable(trans),
8161 &nft_trans_flowtable_hooks(trans),
8162 NFT_MSG_DELFLOWTABLE);
8163 nft_unregister_flowtable_net_hooks(net,
8164 &nft_trans_flowtable_hooks(trans));
8165 } else {
8166 list_del_rcu(&nft_trans_flowtable(trans)->list);
8167 nf_tables_flowtable_notify(&trans->ctx,
8168 nft_trans_flowtable(trans),
8169 &nft_trans_flowtable(trans)->hook_list,
8170 NFT_MSG_DELFLOWTABLE);
8171 nft_unregister_flowtable_net_hooks(net,
8172 &nft_trans_flowtable(trans)->hook_list);
8173 }
3b49e2e9 8174 break;
37082f93 8175 }
37082f93
PNA
8176 }
8177
67cc570e 8178 nft_commit_notify(net, NETLINK_CB(skb).portid);
84d7fce6 8179 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
0935d558 8180 nf_tables_commit_release(net);
37082f93
PNA
8181
8182 return 0;
8183}
8184
eb014de4
PNA
8185static void nf_tables_module_autoload(struct net *net)
8186{
8187 struct nft_module_request *req, *next;
8188 LIST_HEAD(module_list);
8189
8190 list_splice_init(&net->nft.module_list, &module_list);
8191 mutex_unlock(&net->nft.commit_mutex);
8192 list_for_each_entry_safe(req, next, &module_list, list) {
1d305ba4
FW
8193 request_module("%s", req->module);
8194 req->done = true;
eb014de4
PNA
8195 }
8196 mutex_lock(&net->nft.commit_mutex);
8197 list_splice(&module_list, &net->nft.module_list);
8198}
8199
b326dd37 8200static void nf_tables_abort_release(struct nft_trans *trans)
c7c32e72 8201{
c7c32e72
PNA
8202 switch (trans->msg_type) {
8203 case NFT_MSG_NEWTABLE:
8204 nf_tables_table_destroy(&trans->ctx);
8205 break;
8206 case NFT_MSG_NEWCHAIN:
43a605f2 8207 nf_tables_chain_destroy(&trans->ctx);
c7c32e72
PNA
8208 break;
8209 case NFT_MSG_NEWRULE:
8210 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
8211 break;
8212 case NFT_MSG_NEWSET:
0c2a85ed 8213 nft_set_destroy(&trans->ctx, nft_trans_set(trans));
c7c32e72 8214 break;
61edafbb
PM
8215 case NFT_MSG_NEWSETELEM:
8216 nft_set_elem_destroy(nft_trans_elem_set(trans),
61f9e292 8217 nft_trans_elem(trans).priv, true);
61edafbb 8218 break;
e5009240 8219 case NFT_MSG_NEWOBJ:
00bfb320 8220 nft_obj_destroy(&trans->ctx, nft_trans_obj(trans));
e5009240 8221 break;
3b49e2e9 8222 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8223 if (nft_trans_flowtable_update(trans))
8224 nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans));
8225 else
8226 nf_tables_flowtable_destroy(nft_trans_flowtable(trans));
3b49e2e9 8227 break;
c7c32e72
PNA
8228 }
8229 kfree(trans);
8230}
8231
c0391b6a 8232static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
37082f93 8233{
37082f93 8234 struct nft_trans *trans, *next;
02263db0 8235 struct nft_trans_elem *te;
abadb2f8 8236 struct nft_hook *hook;
37082f93 8237
c0391b6a
PNA
8238 if (action == NFNL_ABORT_VALIDATE &&
8239 nf_tables_validate(net) < 0)
8240 return -EAGAIN;
8241
a907e36d
XL
8242 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
8243 list) {
b380e5c7 8244 switch (trans->msg_type) {
55dd6f93
PNA
8245 case NFT_MSG_NEWTABLE:
8246 if (nft_trans_table_update(trans)) {
8247 if (nft_trans_table_enable(trans)) {
664b0f8c 8248 nf_tables_table_disable(net,
55dd6f93
PNA
8249 trans->ctx.table);
8250 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
8251 }
8252 nft_trans_destroy(trans);
8253 } else {
e688a7f8 8254 list_del_rcu(&trans->ctx.table->list);
55dd6f93
PNA
8255 }
8256 break;
8257 case NFT_MSG_DELTABLE:
f2a6d766 8258 nft_clear(trans->ctx.net, trans->ctx.table);
55dd6f93
PNA
8259 nft_trans_destroy(trans);
8260 break;
91c7b38d
PNA
8261 case NFT_MSG_NEWCHAIN:
8262 if (nft_trans_chain_update(trans)) {
982f4051 8263 free_percpu(nft_trans_chain_stats(trans));
9f8aac0b 8264 kfree(nft_trans_chain_name(trans));
91c7b38d
PNA
8265 nft_trans_destroy(trans);
8266 } else {
d0e2c7de
PNA
8267 if (nft_chain_is_bound(trans->ctx.chain)) {
8268 nft_trans_destroy(trans);
8269 break;
8270 }
4fefee57 8271 trans->ctx.table->use--;
1b2470e5 8272 nft_chain_del(trans->ctx.chain);
c974a3a3
PNA
8273 nf_tables_unregister_hook(trans->ctx.net,
8274 trans->ctx.table,
8275 trans->ctx.chain);
91c7b38d
PNA
8276 }
8277 break;
8278 case NFT_MSG_DELCHAIN:
4fefee57 8279 trans->ctx.table->use++;
664b0f8c 8280 nft_clear(trans->ctx.net, trans->ctx.chain);
91c7b38d
PNA
8281 nft_trans_destroy(trans);
8282 break;
b380e5c7 8283 case NFT_MSG_NEWRULE:
4fefee57 8284 trans->ctx.chain->use--;
b380e5c7 8285 list_del_rcu(&nft_trans_rule(trans)->list);
f6ac8585
PNA
8286 nft_rule_expr_deactivate(&trans->ctx,
8287 nft_trans_rule(trans),
8288 NFT_TRANS_ABORT);
b380e5c7
PNA
8289 break;
8290 case NFT_MSG_DELRULE:
4fefee57 8291 trans->ctx.chain->use++;
889f7ee7 8292 nft_clear(trans->ctx.net, nft_trans_rule(trans));
bb7b40ae 8293 nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans));
37082f93 8294 nft_trans_destroy(trans);
b380e5c7 8295 break;
958bee14 8296 case NFT_MSG_NEWSET:
4fefee57 8297 trans->ctx.table->use--;
6a0a8d10 8298 if (nft_trans_set_bound(trans)) {
40ba1d9b
PNA
8299 nft_trans_destroy(trans);
8300 break;
8301 }
8302 list_del_rcu(&nft_trans_set(trans)->list);
958bee14
PNA
8303 break;
8304 case NFT_MSG_DELSET:
4fefee57 8305 trans->ctx.table->use++;
37a9cc52 8306 nft_clear(trans->ctx.net, nft_trans_set(trans));
958bee14
PNA
8307 nft_trans_destroy(trans);
8308 break;
60319eb1 8309 case NFT_MSG_NEWSETELEM:
6a0a8d10 8310 if (nft_trans_elem_set_bound(trans)) {
40ba1d9b
PNA
8311 nft_trans_destroy(trans);
8312 break;
8313 }
02263db0 8314 te = (struct nft_trans_elem *)trans->data;
5cb82a38 8315 te->set->ops->remove(net, te->set, &te->elem);
3dd0673a 8316 atomic_dec(&te->set->nelems);
60319eb1
PNA
8317 break;
8318 case NFT_MSG_DELSETELEM:
cc02e457
PM
8319 te = (struct nft_trans_elem *)trans->data;
8320
59105446 8321 nft_set_elem_activate(net, te->set, &te->elem);
42a55769 8322 te->set->ops->activate(net, te->set, &te->elem);
3dd0673a 8323 te->set->ndeact--;
cc02e457 8324
e5009240
PNA
8325 nft_trans_destroy(trans);
8326 break;
8327 case NFT_MSG_NEWOBJ:
d62d0ba9
FFM
8328 if (nft_trans_obj_update(trans)) {
8329 kfree(nft_trans_obj_newobj(trans));
8330 nft_trans_destroy(trans);
8331 } else {
8332 trans->ctx.table->use--;
8333 nft_obj_del(nft_trans_obj(trans));
8334 }
e5009240
PNA
8335 break;
8336 case NFT_MSG_DELOBJ:
8337 trans->ctx.table->use++;
8338 nft_clear(trans->ctx.net, nft_trans_obj(trans));
60319eb1
PNA
8339 nft_trans_destroy(trans);
8340 break;
3b49e2e9 8341 case NFT_MSG_NEWFLOWTABLE:
78d9f48f
PNA
8342 if (nft_trans_flowtable_update(trans)) {
8343 nft_unregister_flowtable_net_hooks(net,
8344 &nft_trans_flowtable_hooks(trans));
8345 } else {
8346 trans->ctx.table->use--;
8347 list_del_rcu(&nft_trans_flowtable(trans)->list);
8348 nft_unregister_flowtable_net_hooks(net,
8349 &nft_trans_flowtable(trans)->hook_list);
8350 }
3b49e2e9
PNA
8351 break;
8352 case NFT_MSG_DELFLOWTABLE:
abadb2f8
PNA
8353 if (nft_trans_flowtable_update(trans)) {
8354 list_for_each_entry(hook, &nft_trans_flowtable(trans)->hook_list, list)
8355 hook->inactive = false;
8356 } else {
8357 trans->ctx.table->use++;
8358 nft_clear(trans->ctx.net, nft_trans_flowtable(trans));
8359 }
3b49e2e9
PNA
8360 nft_trans_destroy(trans);
8361 break;
37082f93 8362 }
37082f93
PNA
8363 }
8364
b326dd37
PNA
8365 synchronize_rcu();
8366
a1cee076
PNA
8367 list_for_each_entry_safe_reverse(trans, next,
8368 &net->nft.commit_list, list) {
c7c32e72 8369 list_del(&trans->list);
b326dd37 8370 nf_tables_abort_release(trans);
37082f93
PNA
8371 }
8372
c0391b6a 8373 if (action == NFNL_ABORT_AUTOLOAD)
eb014de4
PNA
8374 nf_tables_module_autoload(net);
8375 else
8376 nf_tables_module_autoload_cleanup(net);
8377
37082f93
PNA
8378 return 0;
8379}
8380
a654de8f
PNA
8381static void nf_tables_cleanup(struct net *net)
8382{
8383 nft_validate_state_update(net, NFT_VALIDATE_SKIP);
8384}
8385
c0391b6a
PNA
8386static int nf_tables_abort(struct net *net, struct sk_buff *skb,
8387 enum nfnl_abort_action action)
71ad00c5 8388{
c0391b6a 8389 int ret = __nf_tables_abort(net, action);
f102d66b
FW
8390
8391 mutex_unlock(&net->nft.commit_mutex);
8392
8393 return ret;
71ad00c5
FW
8394}
8395
74e8bcd2
PNA
8396static bool nf_tables_valid_genid(struct net *net, u32 genid)
8397{
f102d66b
FW
8398 bool genid_ok;
8399
8400 mutex_lock(&net->nft.commit_mutex);
8401
8402 genid_ok = genid == 0 || net->nft.base_seq == genid;
8403 if (!genid_ok)
8404 mutex_unlock(&net->nft.commit_mutex);
8405
8406 /* else, commit mutex has to be released by commit or abort function */
8407 return genid_ok;
74e8bcd2
PNA
8408}
8409
96518518
PM
8410static const struct nfnetlink_subsystem nf_tables_subsys = {
8411 .name = "nf_tables",
8412 .subsys_id = NFNL_SUBSYS_NFTABLES,
8413 .cb_count = NFT_MSG_MAX,
8414 .cb = nf_tables_cb,
0628b123
PNA
8415 .commit = nf_tables_commit,
8416 .abort = nf_tables_abort,
a654de8f 8417 .cleanup = nf_tables_cleanup,
74e8bcd2 8418 .valid_genid = nf_tables_valid_genid,
be2ab5b4 8419 .owner = THIS_MODULE,
96518518
PM
8420};
8421
7210e4e3 8422int nft_chain_validate_dependency(const struct nft_chain *chain,
32537e91 8423 enum nft_chain_types type)
7210e4e3
PNA
8424{
8425 const struct nft_base_chain *basechain;
8426
f323d954 8427 if (nft_is_base_chain(chain)) {
7210e4e3
PNA
8428 basechain = nft_base_chain(chain);
8429 if (basechain->type->type != type)
8430 return -EOPNOTSUPP;
8431 }
8432 return 0;
8433}
8434EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
8435
75e8d06d
PNA
8436int nft_chain_validate_hooks(const struct nft_chain *chain,
8437 unsigned int hook_flags)
8438{
8439 struct nft_base_chain *basechain;
8440
f323d954 8441 if (nft_is_base_chain(chain)) {
75e8d06d
PNA
8442 basechain = nft_base_chain(chain);
8443
c974a3a3 8444 if ((1 << basechain->ops.hooknum) & hook_flags)
75e8d06d
PNA
8445 return 0;
8446
8447 return -EOPNOTSUPP;
8448 }
8449
8450 return 0;
8451}
8452EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
8453
20a69341
PM
8454/*
8455 * Loop detection - walk through the ruleset beginning at the destination chain
8456 * of a new jump until either the source chain is reached (loop) or all
8457 * reachable chains have been traversed.
8458 *
8459 * The loop check is performed whenever a new jump verdict is added to an
8460 * expression or verdict map or a verdict map is bound to a new chain.
8461 */
8462
8463static int nf_tables_check_loops(const struct nft_ctx *ctx,
8464 const struct nft_chain *chain);
8465
8466static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
de70185d 8467 struct nft_set *set,
20a69341 8468 const struct nft_set_iter *iter,
de70185d 8469 struct nft_set_elem *elem)
20a69341 8470{
fe2811eb
PM
8471 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
8472 const struct nft_data *data;
8473
8474 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
8475 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
62f9c8b4
PNA
8476 return 0;
8477
fe2811eb 8478 data = nft_set_ext_data(ext);
1ca2e170 8479 switch (data->verdict.code) {
20a69341
PM
8480 case NFT_JUMP:
8481 case NFT_GOTO:
1ca2e170 8482 return nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
8483 default:
8484 return 0;
8485 }
8486}
8487
8488static int nf_tables_check_loops(const struct nft_ctx *ctx,
8489 const struct nft_chain *chain)
8490{
8491 const struct nft_rule *rule;
8492 const struct nft_expr *expr, *last;
de70185d 8493 struct nft_set *set;
20a69341
PM
8494 struct nft_set_binding *binding;
8495 struct nft_set_iter iter;
20a69341
PM
8496
8497 if (ctx->chain == chain)
8498 return -ELOOP;
8499
8500 list_for_each_entry(rule, &chain->rules, list) {
8501 nft_rule_for_each_expr(expr, last, rule) {
a654de8f
PNA
8502 struct nft_immediate_expr *priv;
8503 const struct nft_data *data;
0ca743a5
PNA
8504 int err;
8505
a654de8f 8506 if (strcmp(expr->ops->type->name, "immediate"))
20a69341
PM
8507 continue;
8508
a654de8f
PNA
8509 priv = nft_expr_priv(expr);
8510 if (priv->dreg != NFT_REG_VERDICT)
0ca743a5 8511 continue;
20a69341 8512
a654de8f 8513 data = &priv->data;
1ca2e170 8514 switch (data->verdict.code) {
20a69341
PM
8515 case NFT_JUMP:
8516 case NFT_GOTO:
1ca2e170
PM
8517 err = nf_tables_check_loops(ctx,
8518 data->verdict.chain);
20a69341
PM
8519 if (err < 0)
8520 return err;
8521 default:
8522 break;
8523 }
8524 }
8525 }
8526
8527 list_for_each_entry(set, &ctx->table->sets, list) {
37a9cc52
PNA
8528 if (!nft_is_active_next(ctx->net, set))
8529 continue;
20a69341
PM
8530 if (!(set->flags & NFT_SET_MAP) ||
8531 set->dtype != NFT_DATA_VERDICT)
8532 continue;
8533
8534 list_for_each_entry(binding, &set->bindings, list) {
11113e19
PM
8535 if (!(binding->flags & NFT_SET_MAP) ||
8536 binding->chain != chain)
20a69341
PM
8537 continue;
8538
8588ac09 8539 iter.genmask = nft_genmask_next(ctx->net);
20a69341
PM
8540 iter.skip = 0;
8541 iter.count = 0;
8542 iter.err = 0;
8543 iter.fn = nf_tables_loop_check_setelem;
8544
8545 set->ops->walk(ctx, set, &iter);
8546 if (iter.err < 0)
8547 return iter.err;
8548 }
8549 }
8550
8551 return 0;
8552}
8553
36b701fa
LGL
8554/**
8555 * nft_parse_u32_check - fetch u32 attribute and check for maximum value
8556 *
8557 * @attr: netlink attribute to fetch value from
8558 * @max: maximum value to be stored in dest
8559 * @dest: pointer to the variable
8560 *
8561 * Parse, check and store a given u32 netlink attribute into variable.
8562 * This function returns -ERANGE if the value goes over maximum value.
8563 * Otherwise a 0 is returned and the attribute value is stored in the
8564 * destination variable.
8565 */
f1d505bb 8566int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
36b701fa 8567{
09525a09 8568 u32 val;
36b701fa
LGL
8569
8570 val = ntohl(nla_get_be32(attr));
8571 if (val > max)
8572 return -ERANGE;
8573
8574 *dest = val;
8575 return 0;
8576}
8577EXPORT_SYMBOL_GPL(nft_parse_u32_check);
8578
49499c3e
PM
8579/**
8580 * nft_parse_register - parse a register value from a netlink attribute
8581 *
8582 * @attr: netlink attribute
8583 *
8584 * Parse and translate a register value from a netlink attribute.
8585 * Registers used to be 128 bit wide, these register numbers will be
8586 * mapped to the corresponding 32 bit register numbers.
8587 */
b1c96ed3
PM
8588unsigned int nft_parse_register(const struct nlattr *attr)
8589{
49499c3e
PM
8590 unsigned int reg;
8591
8592 reg = ntohl(nla_get_be32(attr));
8593 switch (reg) {
8594 case NFT_REG_VERDICT...NFT_REG_4:
8595 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
8596 default:
8597 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
8598 }
b1c96ed3
PM
8599}
8600EXPORT_SYMBOL_GPL(nft_parse_register);
8601
49499c3e
PM
8602/**
8603 * nft_dump_register - dump a register value to a netlink attribute
8604 *
8605 * @skb: socket buffer
8606 * @attr: attribute number
8607 * @reg: register number
8608 *
8609 * Construct a netlink attribute containing the register number. For
8610 * compatibility reasons, register numbers being a multiple of 4 are
8611 * translated to the corresponding 128 bit register numbers.
8612 */
b1c96ed3
PM
8613int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
8614{
49499c3e
PM
8615 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
8616 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
8617 else
8618 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
8619
b1c96ed3
PM
8620 return nla_put_be32(skb, attr, htonl(reg));
8621}
8622EXPORT_SYMBOL_GPL(nft_dump_register);
8623
96518518 8624/**
d07db988 8625 * nft_validate_register_load - validate a load from a register
96518518
PM
8626 *
8627 * @reg: the register number
d07db988 8628 * @len: the length of the data
96518518
PM
8629 *
8630 * Validate that the input register is one of the general purpose
d07db988 8631 * registers and that the length of the load is within the bounds.
96518518 8632 */
d07db988 8633int nft_validate_register_load(enum nft_registers reg, unsigned int len)
96518518 8634{
49499c3e 8635 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
96518518 8636 return -EINVAL;
d07db988
PM
8637 if (len == 0)
8638 return -EINVAL;
c593642c 8639 if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data))
d07db988 8640 return -ERANGE;
49499c3e 8641
96518518
PM
8642 return 0;
8643}
d07db988 8644EXPORT_SYMBOL_GPL(nft_validate_register_load);
96518518 8645
96518518 8646/**
1ec10212 8647 * nft_validate_register_store - validate an expressions' register store
96518518
PM
8648 *
8649 * @ctx: context of the expression performing the load
8650 * @reg: the destination register number
8651 * @data: the data to load
8652 * @type: the data type
45d9bcda 8653 * @len: the length of the data
96518518
PM
8654 *
8655 * Validate that a data load uses the appropriate data type for
45d9bcda
PM
8656 * the destination register and the length is within the bounds.
8657 * A value of NULL for the data means that its runtime gathered
58f40ab6 8658 * data.
96518518 8659 */
1ec10212
PM
8660int nft_validate_register_store(const struct nft_ctx *ctx,
8661 enum nft_registers reg,
8662 const struct nft_data *data,
8663 enum nft_data_types type, unsigned int len)
96518518 8664{
20a69341
PM
8665 int err;
8666
96518518
PM
8667 switch (reg) {
8668 case NFT_REG_VERDICT:
58f40ab6 8669 if (type != NFT_DATA_VERDICT)
96518518 8670 return -EINVAL;
20a69341 8671
58f40ab6 8672 if (data != NULL &&
1ca2e170
PM
8673 (data->verdict.code == NFT_GOTO ||
8674 data->verdict.code == NFT_JUMP)) {
8675 err = nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
8676 if (err < 0)
8677 return err;
20a69341
PM
8678 }
8679
96518518
PM
8680 return 0;
8681 default:
49499c3e 8682 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
27e6d201 8683 return -EINVAL;
45d9bcda
PM
8684 if (len == 0)
8685 return -EINVAL;
49499c3e 8686 if (reg * NFT_REG32_SIZE + len >
c593642c 8687 sizeof_field(struct nft_regs, data))
45d9bcda 8688 return -ERANGE;
27e6d201 8689
96518518
PM
8690 if (data != NULL && type != NFT_DATA_VALUE)
8691 return -EINVAL;
8692 return 0;
8693 }
8694}
1ec10212 8695EXPORT_SYMBOL_GPL(nft_validate_register_store);
96518518
PM
8696
8697static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
8698 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
8699 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
8700 .len = NFT_CHAIN_MAXNAMELEN - 1 },
51d70f18 8701 [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 },
96518518
PM
8702};
8703
8704static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
8705 struct nft_data_desc *desc, const struct nlattr *nla)
8706{
664b0f8c 8707 u8 genmask = nft_genmask_next(ctx->net);
96518518
PM
8708 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
8709 struct nft_chain *chain;
8710 int err;
8711
8cb08174
JB
8712 err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla,
8713 nft_verdict_policy, NULL);
96518518
PM
8714 if (err < 0)
8715 return err;
8716
8717 if (!tb[NFTA_VERDICT_CODE])
8718 return -EINVAL;
1ca2e170 8719 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
96518518 8720
1ca2e170 8721 switch (data->verdict.code) {
e0abdadc 8722 default:
1ca2e170 8723 switch (data->verdict.code & NF_VERDICT_MASK) {
e0abdadc
PM
8724 case NF_ACCEPT:
8725 case NF_DROP:
8726 case NF_QUEUE:
8727 break;
8728 default:
8729 return -EINVAL;
8730 }
954d8297 8731 fallthrough;
96518518
PM
8732 case NFT_CONTINUE:
8733 case NFT_BREAK:
8734 case NFT_RETURN:
96518518
PM
8735 break;
8736 case NFT_JUMP:
8737 case NFT_GOTO:
51d70f18
PNA
8738 if (tb[NFTA_VERDICT_CHAIN]) {
8739 chain = nft_chain_lookup(ctx->net, ctx->table,
8740 tb[NFTA_VERDICT_CHAIN],
8741 genmask);
8742 } else if (tb[NFTA_VERDICT_CHAIN_ID]) {
8743 chain = nft_chain_lookup_byid(ctx->net,
8744 tb[NFTA_VERDICT_CHAIN_ID]);
8745 if (IS_ERR(chain))
8746 return PTR_ERR(chain);
8747 } else {
96518518 8748 return -EINVAL;
51d70f18
PNA
8749 }
8750
96518518
PM
8751 if (IS_ERR(chain))
8752 return PTR_ERR(chain);
f323d954 8753 if (nft_is_base_chain(chain))
96518518
PM
8754 return -EOPNOTSUPP;
8755
96518518 8756 chain->use++;
1ca2e170 8757 data->verdict.chain = chain;
96518518 8758 break;
96518518
PM
8759 }
8760
4c4ed074 8761 desc->len = sizeof(data->verdict);
96518518
PM
8762 desc->type = NFT_DATA_VERDICT;
8763 return 0;
8764}
8765
8766static void nft_verdict_uninit(const struct nft_data *data)
8767{
d0e2c7de
PNA
8768 struct nft_chain *chain;
8769 struct nft_rule *rule;
8770
1ca2e170 8771 switch (data->verdict.code) {
96518518
PM
8772 case NFT_JUMP:
8773 case NFT_GOTO:
d0e2c7de
PNA
8774 chain = data->verdict.chain;
8775 chain->use--;
8776
8777 if (!nft_chain_is_bound(chain))
8778 break;
8779
8780 chain->table->use--;
8781 list_for_each_entry(rule, &chain->rules, list)
8782 chain->use--;
8783
8784 nft_chain_del(chain);
96518518
PM
8785 break;
8786 }
8787}
8788
33d5a7b1 8789int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
96518518
PM
8790{
8791 struct nlattr *nest;
8792
ae0be8de 8793 nest = nla_nest_start_noflag(skb, type);
96518518
PM
8794 if (!nest)
8795 goto nla_put_failure;
8796
33d5a7b1 8797 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
96518518
PM
8798 goto nla_put_failure;
8799
33d5a7b1 8800 switch (v->code) {
96518518
PM
8801 case NFT_JUMP:
8802 case NFT_GOTO:
1ca2e170 8803 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
33d5a7b1 8804 v->chain->name))
96518518
PM
8805 goto nla_put_failure;
8806 }
8807 nla_nest_end(skb, nest);
8808 return 0;
8809
8810nla_put_failure:
8811 return -1;
8812}
8813
d0a11fc3
PM
8814static int nft_value_init(const struct nft_ctx *ctx,
8815 struct nft_data *data, unsigned int size,
96518518
PM
8816 struct nft_data_desc *desc, const struct nlattr *nla)
8817{
8818 unsigned int len;
8819
8820 len = nla_len(nla);
8821 if (len == 0)
8822 return -EINVAL;
d0a11fc3 8823 if (len > size)
96518518
PM
8824 return -EOVERFLOW;
8825
d0a11fc3 8826 nla_memcpy(data->data, nla, len);
96518518
PM
8827 desc->type = NFT_DATA_VALUE;
8828 desc->len = len;
8829 return 0;
8830}
8831
8832static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
8833 unsigned int len)
8834{
8835 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
8836}
8837
8838static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
d0a11fc3 8839 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
96518518
PM
8840 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
8841};
8842
8843/**
8844 * nft_data_init - parse nf_tables data netlink attributes
8845 *
8846 * @ctx: context of the expression using the data
8847 * @data: destination struct nft_data
d0a11fc3 8848 * @size: maximum data length
96518518
PM
8849 * @desc: data description
8850 * @nla: netlink attribute containing data
8851 *
8852 * Parse the netlink data attributes and initialize a struct nft_data.
8853 * The type and length of data are returned in the data description.
8854 *
8855 * The caller can indicate that it only wants to accept data of type
8856 * NFT_DATA_VALUE by passing NULL for the ctx argument.
8857 */
d0a11fc3
PM
8858int nft_data_init(const struct nft_ctx *ctx,
8859 struct nft_data *data, unsigned int size,
96518518
PM
8860 struct nft_data_desc *desc, const struct nlattr *nla)
8861{
8862 struct nlattr *tb[NFTA_DATA_MAX + 1];
8863 int err;
8864
8cb08174
JB
8865 err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla,
8866 nft_data_policy, NULL);
96518518
PM
8867 if (err < 0)
8868 return err;
8869
8870 if (tb[NFTA_DATA_VALUE])
d0a11fc3
PM
8871 return nft_value_init(ctx, data, size, desc,
8872 tb[NFTA_DATA_VALUE]);
96518518
PM
8873 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
8874 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
8875 return -EINVAL;
8876}
8877EXPORT_SYMBOL_GPL(nft_data_init);
8878
8879/**
59105446 8880 * nft_data_release - release a nft_data item
96518518
PM
8881 *
8882 * @data: struct nft_data to release
8883 * @type: type of data
8884 *
8885 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
8886 * all others need to be released by calling this function.
8887 */
59105446 8888void nft_data_release(const struct nft_data *data, enum nft_data_types type)
96518518 8889{
960bd2c2 8890 if (type < NFT_DATA_VERDICT)
96518518 8891 return;
960bd2c2 8892 switch (type) {
96518518
PM
8893 case NFT_DATA_VERDICT:
8894 return nft_verdict_uninit(data);
8895 default:
8896 WARN_ON(1);
8897 }
8898}
59105446 8899EXPORT_SYMBOL_GPL(nft_data_release);
96518518
PM
8900
8901int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
8902 enum nft_data_types type, unsigned int len)
8903{
8904 struct nlattr *nest;
8905 int err;
8906
ae0be8de 8907 nest = nla_nest_start_noflag(skb, attr);
96518518
PM
8908 if (nest == NULL)
8909 return -1;
8910
8911 switch (type) {
8912 case NFT_DATA_VALUE:
8913 err = nft_value_dump(skb, data, len);
8914 break;
8915 case NFT_DATA_VERDICT:
33d5a7b1 8916 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
96518518
PM
8917 break;
8918 default:
8919 err = -EINVAL;
8920 WARN_ON(1);
8921 }
8922
8923 nla_nest_end(skb, nest);
8924 return err;
8925}
8926EXPORT_SYMBOL_GPL(nft_data_dump);
8927
5ebe0b0e
PNA
8928int __nft_release_basechain(struct nft_ctx *ctx)
8929{
8930 struct nft_rule *rule, *nr;
8931
fa5950e4
FW
8932 if (WARN_ON(!nft_is_base_chain(ctx->chain)))
8933 return 0;
5ebe0b0e 8934
c974a3a3 8935 nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain);
5ebe0b0e
PNA
8936 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
8937 list_del(&rule->list);
8938 ctx->chain->use--;
bb7b40ae 8939 nf_tables_rule_release(ctx, rule);
5ebe0b0e 8940 }
1b2470e5 8941 nft_chain_del(ctx->chain);
5ebe0b0e 8942 ctx->table->use--;
43a605f2 8943 nf_tables_chain_destroy(ctx);
5ebe0b0e
PNA
8944
8945 return 0;
8946}
8947EXPORT_SYMBOL_GPL(__nft_release_basechain);
8948
98319cb9 8949static void __nft_release_tables(struct net *net)
df05ef87 8950{
3b49e2e9 8951 struct nft_flowtable *flowtable, *nf;
df05ef87
PNA
8952 struct nft_table *table, *nt;
8953 struct nft_chain *chain, *nc;
e5009240 8954 struct nft_object *obj, *ne;
df05ef87
PNA
8955 struct nft_rule *rule, *nr;
8956 struct nft_set *set, *ns;
8957 struct nft_ctx ctx = {
8958 .net = net,
43a605f2 8959 .family = NFPROTO_NETDEV,
df05ef87
PNA
8960 };
8961
36596dad 8962 list_for_each_entry_safe(table, nt, &net->nft.tables, list) {
98319cb9 8963 ctx.family = table->family;
dd4cbef7 8964
df05ef87 8965 list_for_each_entry(chain, &table->chains, list)
c974a3a3 8966 nf_tables_unregister_hook(net, table, chain);
df05ef87
PNA
8967 /* No packets are walking on these chains anymore. */
8968 ctx.table = table;
8969 list_for_each_entry(chain, &table->chains, list) {
8970 ctx.chain = chain;
8971 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
8972 list_del(&rule->list);
8973 chain->use--;
bb7b40ae 8974 nf_tables_rule_release(&ctx, rule);
df05ef87
PNA
8975 }
8976 }
3b49e2e9
PNA
8977 list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) {
8978 list_del(&flowtable->list);
8979 table->use--;
8980 nf_tables_flowtable_destroy(flowtable);
8981 }
df05ef87
PNA
8982 list_for_each_entry_safe(set, ns, &table->sets, list) {
8983 list_del(&set->list);
8984 table->use--;
0c2a85ed 8985 nft_set_destroy(&ctx, set);
df05ef87 8986 }
e5009240 8987 list_for_each_entry_safe(obj, ne, &table->objects, list) {
d152159b 8988 nft_obj_del(obj);
e5009240 8989 table->use--;
00bfb320 8990 nft_obj_destroy(&ctx, obj);
e5009240 8991 }
df05ef87 8992 list_for_each_entry_safe(chain, nc, &table->chains, list) {
43a605f2 8993 ctx.chain = chain;
1b2470e5 8994 nft_chain_del(chain);
df05ef87 8995 table->use--;
43a605f2 8996 nf_tables_chain_destroy(&ctx);
df05ef87
PNA
8997 }
8998 list_del(&table->list);
8999 nf_tables_table_destroy(&ctx);
9000 }
9001}
9002
dd4cbef7
PNA
9003static int __net_init nf_tables_init_net(struct net *net)
9004{
9005 INIT_LIST_HEAD(&net->nft.tables);
9006 INIT_LIST_HEAD(&net->nft.commit_list);
eb014de4 9007 INIT_LIST_HEAD(&net->nft.module_list);
67cc570e 9008 INIT_LIST_HEAD(&net->nft.notify_list);
f102d66b 9009 mutex_init(&net->nft.commit_mutex);
dd4cbef7 9010 net->nft.base_seq = 1;
a654de8f
PNA
9011 net->nft.validate_state = NFT_VALIDATE_SKIP;
9012
dd4cbef7
PNA
9013 return 0;
9014}
9015
9016static void __net_exit nf_tables_exit_net(struct net *net)
9017{
f102d66b 9018 mutex_lock(&net->nft.commit_mutex);
71ad00c5 9019 if (!list_empty(&net->nft.commit_list))
c0391b6a 9020 __nf_tables_abort(net, NFNL_ABORT_NONE);
98319cb9 9021 __nft_release_tables(net);
f102d66b 9022 mutex_unlock(&net->nft.commit_mutex);
dd4cbef7 9023 WARN_ON_ONCE(!list_empty(&net->nft.tables));
1d305ba4 9024 WARN_ON_ONCE(!list_empty(&net->nft.module_list));
67cc570e 9025 WARN_ON_ONCE(!list_empty(&net->nft.notify_list));
dd4cbef7
PNA
9026}
9027
99633ab2
PNA
9028static struct pernet_operations nf_tables_net_ops = {
9029 .init = nf_tables_init_net,
613d0776 9030 .exit = nf_tables_exit_net,
99633ab2
PNA
9031};
9032
96518518
PM
9033static int __init nf_tables_module_init(void)
9034{
9035 int err;
9036
0935d558 9037 spin_lock_init(&nf_tables_destroy_list_lock);
d209df3e
FW
9038 err = register_pernet_subsys(&nf_tables_net_ops);
9039 if (err < 0)
9040 return err;
9041
9042 err = nft_chain_filter_init();
9043 if (err < 0)
9044 goto err1;
02c7b25e 9045
96518518
PM
9046 err = nf_tables_core_module_init();
9047 if (err < 0)
d209df3e 9048 goto err2;
96518518 9049
d209df3e 9050 err = register_netdevice_notifier(&nf_tables_flowtable_notifier);
96518518 9051 if (err < 0)
d209df3e 9052 goto err3;
96518518 9053
4d44175a
FW
9054 err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params);
9055 if (err < 0)
9056 goto err4;
9057
06d392cb 9058 err = nft_offload_init();
9059 if (err < 0)
9060 goto err5;
9061
d209df3e
FW
9062 /* must be last */
9063 err = nfnetlink_subsys_register(&nf_tables_subsys);
9064 if (err < 0)
06d392cb 9065 goto err6;
3b49e2e9 9066
c1deb065 9067 nft_chain_route_init();
3474a2c6 9068
d209df3e 9069 return err;
06d392cb 9070err6:
9071 nft_offload_exit();
4d44175a
FW
9072err5:
9073 rhltable_destroy(&nft_objname_ht);
d209df3e
FW
9074err4:
9075 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
9076err3:
96518518 9077 nf_tables_core_module_exit();
d209df3e
FW
9078err2:
9079 nft_chain_filter_fini();
9080err1:
9081 unregister_pernet_subsys(&nf_tables_net_ops);
96518518
PM
9082 return err;
9083}
9084
9085static void __exit nf_tables_module_exit(void)
9086{
9087 nfnetlink_subsys_unregister(&nf_tables_subsys);
06d392cb 9088 nft_offload_exit();
3b49e2e9 9089 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
0a2cf5ee 9090 nft_chain_filter_fini();
c1deb065 9091 nft_chain_route_fini();
71ad00c5 9092 unregister_pernet_subsys(&nf_tables_net_ops);
0935d558 9093 cancel_work_sync(&trans_destroy_work);
1b1bc49c 9094 rcu_barrier();
4d44175a 9095 rhltable_destroy(&nft_objname_ht);
96518518 9096 nf_tables_core_module_exit();
96518518
PM
9097}
9098
9099module_init(nf_tables_module_init);
9100module_exit(nf_tables_module_exit);
9101
9102MODULE_LICENSE("GPL");
9103MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
9104MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);