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