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