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