writeback: Fix performance regression in wb_over_bg_thresh()
[deliverable/linux.git] / net / netfilter / nf_tables_api.c
CommitLineData
96518518 1/*
20a69341 2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20#include <net/netfilter/nf_tables.h>
99633ab2 21#include <net/net_namespace.h>
96518518
PM
22#include <net/sock.h>
23
96518518
PM
24static LIST_HEAD(nf_tables_expressions);
25
26/**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
99633ab2 34int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
96518518
PM
35{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 38 list_add_tail_rcu(&afi->list, &net->nft.af_info);
96518518
PM
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
df05ef87
PNA
44static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi);
45
96518518
PM
46/**
47 * nft_unregister_afinfo - unregister nf_tables address family info
48 *
49 * @afi: address family info to unregister
50 *
51 * Unregister the address family for use with nf_tables.
52 */
df05ef87 53void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi)
96518518
PM
54{
55 nfnl_lock(NFNL_SUBSYS_NFTABLES);
df05ef87 56 __nft_release_afinfo(net, afi);
e688a7f8 57 list_del_rcu(&afi->list);
96518518
PM
58 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
59}
60EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
61
99633ab2 62static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
96518518
PM
63{
64 struct nft_af_info *afi;
65
99633ab2 66 list_for_each_entry(afi, &net->nft.af_info, list) {
96518518
PM
67 if (afi->family == family)
68 return afi;
69 }
70 return NULL;
71}
72
99633ab2
PNA
73static struct nft_af_info *
74nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
96518518
PM
75{
76 struct nft_af_info *afi;
77
99633ab2 78 afi = nft_afinfo_lookup(net, family);
96518518
PM
79 if (afi != NULL)
80 return afi;
81#ifdef CONFIG_MODULES
82 if (autoload) {
83 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
84 request_module("nft-afinfo-%u", family);
85 nfnl_lock(NFNL_SUBSYS_NFTABLES);
99633ab2 86 afi = nft_afinfo_lookup(net, family);
96518518
PM
87 if (afi != NULL)
88 return ERR_PTR(-EAGAIN);
89 }
90#endif
91 return ERR_PTR(-EAFNOSUPPORT);
92}
93
7c95f6d8 94static void nft_ctx_init(struct nft_ctx *ctx,
633c9a84 95 struct net *net,
7c95f6d8
PNA
96 const struct sk_buff *skb,
97 const struct nlmsghdr *nlh,
98 struct nft_af_info *afi,
99 struct nft_table *table,
100 struct nft_chain *chain,
101 const struct nlattr * const *nla)
102{
633c9a84 103 ctx->net = net;
128ad332
PNA
104 ctx->afi = afi;
105 ctx->table = table;
106 ctx->chain = chain;
107 ctx->nla = nla;
108 ctx->portid = NETLINK_CB(skb).portid;
109 ctx->report = nlmsg_report(nlh);
110 ctx->seq = nlh->nlmsg_seq;
7c95f6d8
PNA
111}
112
b380e5c7
PNA
113static struct nft_trans *nft_trans_alloc(struct nft_ctx *ctx, int msg_type,
114 u32 size)
1081d11b
PNA
115{
116 struct nft_trans *trans;
117
118 trans = kzalloc(sizeof(struct nft_trans) + size, GFP_KERNEL);
119 if (trans == NULL)
120 return NULL;
121
b380e5c7 122 trans->msg_type = msg_type;
1081d11b
PNA
123 trans->ctx = *ctx;
124
125 return trans;
126}
127
128static void nft_trans_destroy(struct nft_trans *trans)
129{
130 list_del(&trans->list);
131 kfree(trans);
132}
133
5ebe0b0e
PNA
134static int nft_register_basechain(struct nft_base_chain *basechain,
135 unsigned int hook_nops)
d8ee8f7c 136{
fd2ecda0
EB
137 struct net *net = read_pnet(&basechain->pnet);
138
835b8033
PNA
139 if (basechain->flags & NFT_BASECHAIN_DISABLED)
140 return 0;
141
fd2ecda0 142 return nf_register_net_hooks(net, basechain->ops, hook_nops);
d8ee8f7c
PNA
143}
144
5ebe0b0e
PNA
145static void nft_unregister_basechain(struct nft_base_chain *basechain,
146 unsigned int hook_nops)
d8ee8f7c 147{
fd2ecda0
EB
148 struct net *net = read_pnet(&basechain->pnet);
149
835b8033
PNA
150 if (basechain->flags & NFT_BASECHAIN_DISABLED)
151 return;
152
fd2ecda0 153 nf_unregister_net_hooks(net, basechain->ops, hook_nops);
d8ee8f7c
PNA
154}
155
156static int nf_tables_register_hooks(const struct nft_table *table,
157 struct nft_chain *chain,
158 unsigned int hook_nops)
159{
160 if (table->flags & NFT_TABLE_F_DORMANT ||
161 !(chain->flags & NFT_BASE_CHAIN))
162 return 0;
163
164 return nft_register_basechain(nft_base_chain(chain), hook_nops);
165}
166
c5598794 167static void nf_tables_unregister_hooks(const struct nft_table *table,
d8ee8f7c 168 struct nft_chain *chain,
c5598794
AB
169 unsigned int hook_nops)
170{
d8ee8f7c
PNA
171 if (table->flags & NFT_TABLE_F_DORMANT ||
172 !(chain->flags & NFT_BASE_CHAIN))
173 return;
174
175 nft_unregister_basechain(nft_base_chain(chain), hook_nops);
c5598794
AB
176}
177
ee01d542
AB
178/* Internal table flags */
179#define NFT_TABLE_INACTIVE (1 << 15)
180
181static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
182{
183 struct nft_trans *trans;
184
185 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
186 if (trans == NULL)
187 return -ENOMEM;
188
189 if (msg_type == NFT_MSG_NEWTABLE)
190 ctx->table->flags |= NFT_TABLE_INACTIVE;
191
192 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
193 return 0;
194}
195
196static int nft_deltable(struct nft_ctx *ctx)
197{
198 int err;
199
200 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
201 if (err < 0)
202 return err;
203
204 list_del_rcu(&ctx->table->list);
205 return err;
206}
207
208static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
209{
210 struct nft_trans *trans;
211
212 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
213 if (trans == NULL)
214 return -ENOMEM;
215
216 if (msg_type == NFT_MSG_NEWCHAIN)
217 ctx->chain->flags |= NFT_CHAIN_INACTIVE;
218
219 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
220 return 0;
221}
222
223static int nft_delchain(struct nft_ctx *ctx)
224{
225 int err;
226
227 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
228 if (err < 0)
229 return err;
230
231 ctx->table->use--;
232 list_del_rcu(&ctx->chain->list);
233
234 return err;
235}
236
237static inline bool
238nft_rule_is_active(struct net *net, const struct nft_rule *rule)
239{
ea4bd995 240 return (rule->genmask & nft_genmask_cur(net)) == 0;
ee01d542
AB
241}
242
243static inline int
244nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
245{
ea4bd995 246 return (rule->genmask & nft_genmask_next(net)) == 0;
ee01d542
AB
247}
248
249static inline void
250nft_rule_activate_next(struct net *net, struct nft_rule *rule)
251{
252 /* Now inactive, will be active in the future */
ea4bd995 253 rule->genmask = nft_genmask_cur(net);
ee01d542
AB
254}
255
256static inline void
257nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
258{
ea4bd995 259 rule->genmask = nft_genmask_next(net);
ee01d542
AB
260}
261
262static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
263{
ea4bd995 264 rule->genmask &= ~nft_genmask_next(net);
ee01d542
AB
265}
266
267static int
268nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
269{
270 /* You cannot delete the same rule twice */
271 if (nft_rule_is_active_next(ctx->net, rule)) {
272 nft_rule_deactivate_next(ctx->net, rule);
273 ctx->chain->use--;
274 return 0;
275 }
276 return -ENOENT;
277}
278
279static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
280 struct nft_rule *rule)
281{
282 struct nft_trans *trans;
283
284 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
285 if (trans == NULL)
286 return NULL;
287
288 nft_trans_rule(trans) = rule;
289 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
290
291 return trans;
292}
293
294static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
295{
296 struct nft_trans *trans;
297 int err;
298
299 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
300 if (trans == NULL)
301 return -ENOMEM;
302
303 err = nf_tables_delrule_deactivate(ctx, rule);
304 if (err < 0) {
305 nft_trans_destroy(trans);
306 return err;
307 }
308
309 return 0;
310}
311
312static int nft_delrule_by_chain(struct nft_ctx *ctx)
313{
314 struct nft_rule *rule;
315 int err;
316
317 list_for_each_entry(rule, &ctx->chain->rules, list) {
318 err = nft_delrule(ctx, rule);
319 if (err < 0)
320 return err;
321 }
322 return 0;
323}
324
325/* Internal set flag */
326#define NFT_SET_INACTIVE (1 << 15)
327
328static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
329 struct nft_set *set)
330{
331 struct nft_trans *trans;
332
333 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
334 if (trans == NULL)
335 return -ENOMEM;
336
337 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
338 nft_trans_set_id(trans) =
339 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
340 set->flags |= NFT_SET_INACTIVE;
341 }
342 nft_trans_set(trans) = set;
343 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
344
345 return 0;
346}
347
348static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
349{
350 int err;
351
352 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
353 if (err < 0)
354 return err;
355
356 list_del_rcu(&set->list);
357 ctx->table->use--;
358
359 return err;
360}
361
96518518
PM
362/*
363 * Tables
364 */
365
366static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
367 const struct nlattr *nla)
368{
369 struct nft_table *table;
370
371 list_for_each_entry(table, &afi->tables, list) {
372 if (!nla_strcmp(nla, table->name))
373 return table;
374 }
375 return NULL;
376}
377
378static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
9370761c 379 const struct nlattr *nla)
96518518
PM
380{
381 struct nft_table *table;
382
383 if (nla == NULL)
384 return ERR_PTR(-EINVAL);
385
386 table = nft_table_lookup(afi, nla);
387 if (table != NULL)
388 return table;
389
96518518
PM
390 return ERR_PTR(-ENOENT);
391}
392
393static inline u64 nf_tables_alloc_handle(struct nft_table *table)
394{
395 return ++table->hgenerator;
396}
397
2a37d755 398static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
9370761c 399
2a37d755 400static const struct nf_chain_type *
baae3e62 401__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
9370761c
PNA
402{
403 int i;
404
baae3e62 405 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
9370761c
PNA
406 if (chain_type[family][i] != NULL &&
407 !nla_strcmp(nla, chain_type[family][i]->name))
baae3e62 408 return chain_type[family][i];
9370761c 409 }
baae3e62 410 return NULL;
9370761c
PNA
411}
412
2a37d755 413static const struct nf_chain_type *
baae3e62
PM
414nf_tables_chain_type_lookup(const struct nft_af_info *afi,
415 const struct nlattr *nla,
416 bool autoload)
9370761c 417{
2a37d755 418 const struct nf_chain_type *type;
9370761c
PNA
419
420 type = __nf_tables_chain_type_lookup(afi->family, nla);
93b0806f
PM
421 if (type != NULL)
422 return type;
9370761c 423#ifdef CONFIG_MODULES
93b0806f 424 if (autoload) {
9370761c 425 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2fec6bb6
PNA
426 request_module("nft-chain-%u-%.*s", afi->family,
427 nla_len(nla), (const char *)nla_data(nla));
9370761c
PNA
428 nfnl_lock(NFNL_SUBSYS_NFTABLES);
429 type = __nf_tables_chain_type_lookup(afi->family, nla);
93b0806f
PM
430 if (type != NULL)
431 return ERR_PTR(-EAGAIN);
9370761c
PNA
432 }
433#endif
93b0806f 434 return ERR_PTR(-ENOENT);
9370761c
PNA
435}
436
96518518 437static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
1cae565e
PNA
438 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
439 .len = NFT_TABLE_MAXNAMELEN - 1 },
9ddf6323 440 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
96518518
PM
441};
442
84d7fce6
PNA
443static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
444 u32 portid, u32 seq, int event, u32 flags,
445 int family, const struct nft_table *table)
96518518
PM
446{
447 struct nlmsghdr *nlh;
448 struct nfgenmsg *nfmsg;
449
450 event |= NFNL_SUBSYS_NFTABLES << 8;
451 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
452 if (nlh == NULL)
453 goto nla_put_failure;
454
455 nfmsg = nlmsg_data(nlh);
456 nfmsg->nfgen_family = family;
457 nfmsg->version = NFNETLINK_V0;
84d7fce6 458 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518 459
9ddf6323 460 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
d8bcc768
TB
461 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
462 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
96518518
PM
463 goto nla_put_failure;
464
053c095a
JB
465 nlmsg_end(skb, nlh);
466 return 0;
96518518
PM
467
468nla_put_failure:
469 nlmsg_trim(skb, nlh);
470 return -1;
471}
472
35151d84 473static int nf_tables_table_notify(const struct nft_ctx *ctx, int event)
96518518
PM
474{
475 struct sk_buff *skb;
96518518
PM
476 int err;
477
128ad332
PNA
478 if (!ctx->report &&
479 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
96518518
PM
480 return 0;
481
482 err = -ENOBUFS;
483 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
484 if (skb == NULL)
485 goto err;
486
84d7fce6
PNA
487 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
488 event, 0, ctx->afi->family, ctx->table);
96518518
PM
489 if (err < 0) {
490 kfree_skb(skb);
491 goto err;
492 }
493
128ad332
PNA
494 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
495 ctx->report, GFP_KERNEL);
96518518 496err:
128ad332
PNA
497 if (err < 0) {
498 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
499 err);
500 }
96518518
PM
501 return err;
502}
503
504static int nf_tables_dump_tables(struct sk_buff *skb,
505 struct netlink_callback *cb)
506{
507 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
508 const struct nft_af_info *afi;
509 const struct nft_table *table;
510 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 511 struct net *net = sock_net(skb->sk);
96518518
PM
512 int family = nfmsg->nfgen_family;
513
e688a7f8 514 rcu_read_lock();
38e029f1
PNA
515 cb->seq = net->nft.base_seq;
516
e688a7f8 517 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
96518518
PM
518 if (family != NFPROTO_UNSPEC && family != afi->family)
519 continue;
520
e688a7f8 521 list_for_each_entry_rcu(table, &afi->tables, list) {
96518518
PM
522 if (idx < s_idx)
523 goto cont;
524 if (idx > s_idx)
525 memset(&cb->args[1], 0,
526 sizeof(cb->args) - sizeof(cb->args[0]));
84d7fce6 527 if (nf_tables_fill_table_info(skb, net,
96518518
PM
528 NETLINK_CB(cb->skb).portid,
529 cb->nlh->nlmsg_seq,
530 NFT_MSG_NEWTABLE,
531 NLM_F_MULTI,
532 afi->family, table) < 0)
533 goto done;
38e029f1
PNA
534
535 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518
PM
536cont:
537 idx++;
538 }
539 }
540done:
e688a7f8 541 rcu_read_unlock();
96518518
PM
542 cb->args[0] = idx;
543 return skb->len;
544}
545
7b8002a1
PNA
546static int nf_tables_gettable(struct net *net, struct sock *nlsk,
547 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
548 const struct nlattr * const nla[])
549{
550 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
551 const struct nft_af_info *afi;
552 const struct nft_table *table;
553 struct sk_buff *skb2;
554 int family = nfmsg->nfgen_family;
555 int err;
556
557 if (nlh->nlmsg_flags & NLM_F_DUMP) {
558 struct netlink_dump_control c = {
559 .dump = nf_tables_dump_tables,
560 };
561 return netlink_dump_start(nlsk, skb, nlh, &c);
562 }
563
99633ab2 564 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
565 if (IS_ERR(afi))
566 return PTR_ERR(afi);
567
9370761c 568 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
96518518
PM
569 if (IS_ERR(table))
570 return PTR_ERR(table);
55dd6f93
PNA
571 if (table->flags & NFT_TABLE_INACTIVE)
572 return -ENOENT;
96518518
PM
573
574 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
575 if (!skb2)
576 return -ENOMEM;
577
84d7fce6 578 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
579 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
580 family, table);
581 if (err < 0)
582 goto err;
583
584 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
585
586err:
587 kfree_skb(skb2);
588 return err;
589}
590
115a60b1
PM
591static int nf_tables_table_enable(const struct nft_af_info *afi,
592 struct nft_table *table)
9ddf6323
PNA
593{
594 struct nft_chain *chain;
595 int err, i = 0;
596
597 list_for_each_entry(chain, &table->chains, list) {
d2012975
PNA
598 if (!(chain->flags & NFT_BASE_CHAIN))
599 continue;
600
d8ee8f7c 601 err = nft_register_basechain(nft_base_chain(chain), afi->nops);
9ddf6323
PNA
602 if (err < 0)
603 goto err;
604
605 i++;
606 }
607 return 0;
608err:
609 list_for_each_entry(chain, &table->chains, list) {
d2012975
PNA
610 if (!(chain->flags & NFT_BASE_CHAIN))
611 continue;
612
9ddf6323
PNA
613 if (i-- <= 0)
614 break;
615
d8ee8f7c 616 nft_unregister_basechain(nft_base_chain(chain), afi->nops);
9ddf6323
PNA
617 }
618 return err;
619}
620
f75edf5e 621static void nf_tables_table_disable(const struct nft_af_info *afi,
d8ee8f7c 622 struct nft_table *table)
9ddf6323
PNA
623{
624 struct nft_chain *chain;
625
d2012975
PNA
626 list_for_each_entry(chain, &table->chains, list) {
627 if (chain->flags & NFT_BASE_CHAIN)
d8ee8f7c
PNA
628 nft_unregister_basechain(nft_base_chain(chain),
629 afi->nops);
d2012975 630 }
9ddf6323
PNA
631}
632
e1aaca93 633static int nf_tables_updtable(struct nft_ctx *ctx)
9ddf6323 634{
55dd6f93 635 struct nft_trans *trans;
e1aaca93 636 u32 flags;
55dd6f93 637 int ret = 0;
9ddf6323 638
e1aaca93
PNA
639 if (!ctx->nla[NFTA_TABLE_FLAGS])
640 return 0;
9ddf6323 641
e1aaca93
PNA
642 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
643 if (flags & ~NFT_TABLE_F_DORMANT)
644 return -EINVAL;
645
63283dd2
PNA
646 if (flags == ctx->table->flags)
647 return 0;
648
55dd6f93
PNA
649 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
650 sizeof(struct nft_trans_table));
651 if (trans == NULL)
652 return -ENOMEM;
9ddf6323 653
e1aaca93
PNA
654 if ((flags & NFT_TABLE_F_DORMANT) &&
655 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
55dd6f93 656 nft_trans_table_enable(trans) = false;
e1aaca93
PNA
657 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
658 ctx->table->flags & NFT_TABLE_F_DORMANT) {
659 ret = nf_tables_table_enable(ctx->afi, ctx->table);
55dd6f93 660 if (ret >= 0) {
e1aaca93 661 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
55dd6f93 662 nft_trans_table_enable(trans) = true;
9ddf6323 663 }
9ddf6323 664 }
e1aaca93
PNA
665 if (ret < 0)
666 goto err;
9ddf6323 667
55dd6f93
PNA
668 nft_trans_table_update(trans) = true;
669 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
670 return 0;
9ddf6323 671err:
55dd6f93 672 nft_trans_destroy(trans);
9ddf6323
PNA
673 return ret;
674}
675
633c9a84
PNA
676static int nf_tables_newtable(struct net *net, struct sock *nlsk,
677 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
678 const struct nlattr * const nla[])
679{
680 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
681 const struct nlattr *name;
682 struct nft_af_info *afi;
683 struct nft_table *table;
684 int family = nfmsg->nfgen_family;
c5c1f975 685 u32 flags = 0;
e1aaca93 686 struct nft_ctx ctx;
55dd6f93 687 int err;
96518518 688
99633ab2 689 afi = nf_tables_afinfo_lookup(net, family, true);
96518518
PM
690 if (IS_ERR(afi))
691 return PTR_ERR(afi);
692
693 name = nla[NFTA_TABLE_NAME];
9370761c 694 table = nf_tables_table_lookup(afi, name);
96518518
PM
695 if (IS_ERR(table)) {
696 if (PTR_ERR(table) != -ENOENT)
697 return PTR_ERR(table);
698 table = NULL;
699 }
700
701 if (table != NULL) {
55dd6f93
PNA
702 if (table->flags & NFT_TABLE_INACTIVE)
703 return -ENOENT;
96518518
PM
704 if (nlh->nlmsg_flags & NLM_F_EXCL)
705 return -EEXIST;
706 if (nlh->nlmsg_flags & NLM_F_REPLACE)
707 return -EOPNOTSUPP;
e1aaca93 708
633c9a84 709 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
e1aaca93 710 return nf_tables_updtable(&ctx);
96518518
PM
711 }
712
c5c1f975
PM
713 if (nla[NFTA_TABLE_FLAGS]) {
714 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
715 if (flags & ~NFT_TABLE_F_DORMANT)
716 return -EINVAL;
717 }
718
ebddf1a8 719 err = -EAFNOSUPPORT;
7047f9d0 720 if (!try_module_get(afi->owner))
ebddf1a8 721 goto err1;
7047f9d0 722
ffdb210e 723 err = -ENOMEM;
1cae565e 724 table = kzalloc(sizeof(*table), GFP_KERNEL);
ffdb210e 725 if (table == NULL)
ebddf1a8 726 goto err2;
96518518 727
1cae565e 728 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
96518518 729 INIT_LIST_HEAD(&table->chains);
20a69341 730 INIT_LIST_HEAD(&table->sets);
c5c1f975 731 table->flags = flags;
9ddf6323 732
633c9a84 733 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
55dd6f93 734 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
ffdb210e 735 if (err < 0)
ebddf1a8 736 goto err3;
ffdb210e 737
e688a7f8 738 list_add_tail_rcu(&table->list, &afi->tables);
96518518 739 return 0;
ebddf1a8 740err3:
ffdb210e 741 kfree(table);
ebddf1a8 742err2:
ffdb210e 743 module_put(afi->owner);
ebddf1a8 744err1:
ffdb210e 745 return err;
96518518
PM
746}
747
b9ac12ef
AB
748static int nft_flush_table(struct nft_ctx *ctx)
749{
750 int err;
751 struct nft_chain *chain, *nc;
752 struct nft_set *set, *ns;
753
a2f18db0 754 list_for_each_entry(chain, &ctx->table->chains, list) {
b9ac12ef
AB
755 ctx->chain = chain;
756
757 err = nft_delrule_by_chain(ctx);
758 if (err < 0)
759 goto out;
b9ac12ef
AB
760 }
761
762 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
763 if (set->flags & NFT_SET_ANONYMOUS &&
764 !list_empty(&set->bindings))
765 continue;
766
767 err = nft_delset(ctx, set);
768 if (err < 0)
769 goto out;
770 }
771
a2f18db0
PNA
772 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
773 ctx->chain = chain;
774
775 err = nft_delchain(ctx);
776 if (err < 0)
777 goto out;
778 }
779
b9ac12ef
AB
780 err = nft_deltable(ctx);
781out:
782 return err;
783}
784
785static int nft_flush(struct nft_ctx *ctx, int family)
786{
787 struct nft_af_info *afi;
788 struct nft_table *table, *nt;
789 const struct nlattr * const *nla = ctx->nla;
790 int err = 0;
791
792 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
793 if (family != AF_UNSPEC && afi->family != family)
794 continue;
795
796 ctx->afi = afi;
797 list_for_each_entry_safe(table, nt, &afi->tables, list) {
798 if (nla[NFTA_TABLE_NAME] &&
799 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
800 continue;
801
802 ctx->table = table;
803
804 err = nft_flush_table(ctx);
805 if (err < 0)
806 goto out;
807 }
808 }
809out:
810 return err;
811}
812
633c9a84
PNA
813static int nf_tables_deltable(struct net *net, struct sock *nlsk,
814 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
815 const struct nlattr * const nla[])
816{
817 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
818 struct nft_af_info *afi;
819 struct nft_table *table;
ee01d542 820 int family = nfmsg->nfgen_family;
55dd6f93 821 struct nft_ctx ctx;
96518518 822
633c9a84 823 nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla);
b9ac12ef
AB
824 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
825 return nft_flush(&ctx, family);
826
99633ab2 827 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
828 if (IS_ERR(afi))
829 return PTR_ERR(afi);
830
9370761c 831 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
96518518
PM
832 if (IS_ERR(table))
833 return PTR_ERR(table);
96518518 834
b9ac12ef
AB
835 ctx.afi = afi;
836 ctx.table = table;
55dd6f93 837
b9ac12ef 838 return nft_flush_table(&ctx);
96518518
PM
839}
840
55dd6f93
PNA
841static void nf_tables_table_destroy(struct nft_ctx *ctx)
842{
4fefee57
PNA
843 BUG_ON(ctx->table->use > 0);
844
55dd6f93
PNA
845 kfree(ctx->table);
846 module_put(ctx->afi->owner);
847}
848
2a37d755 849int nft_register_chain_type(const struct nf_chain_type *ctype)
96518518 850{
9370761c 851 int err = 0;
96518518
PM
852
853 nfnl_lock(NFNL_SUBSYS_NFTABLES);
9370761c
PNA
854 if (chain_type[ctype->family][ctype->type] != NULL) {
855 err = -EBUSY;
856 goto out;
96518518 857 }
9370761c
PNA
858 chain_type[ctype->family][ctype->type] = ctype;
859out:
96518518
PM
860 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
861 return err;
862}
9370761c 863EXPORT_SYMBOL_GPL(nft_register_chain_type);
96518518 864
2a37d755 865void nft_unregister_chain_type(const struct nf_chain_type *ctype)
96518518 866{
96518518 867 nfnl_lock(NFNL_SUBSYS_NFTABLES);
9370761c 868 chain_type[ctype->family][ctype->type] = NULL;
96518518
PM
869 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
870}
9370761c 871EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
96518518
PM
872
873/*
874 * Chains
875 */
876
877static struct nft_chain *
878nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
879{
880 struct nft_chain *chain;
881
882 list_for_each_entry(chain, &table->chains, list) {
883 if (chain->handle == handle)
884 return chain;
885 }
886
887 return ERR_PTR(-ENOENT);
888}
889
890static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
891 const struct nlattr *nla)
892{
893 struct nft_chain *chain;
894
895 if (nla == NULL)
896 return ERR_PTR(-EINVAL);
897
898 list_for_each_entry(chain, &table->chains, list) {
899 if (!nla_strcmp(nla, chain->name))
900 return chain;
901 }
902
903 return ERR_PTR(-ENOENT);
904}
905
906static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
907 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
908 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
909 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
910 .len = NFT_CHAIN_MAXNAMELEN - 1 },
911 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
0ca743a5 912 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
4c1f7818 913 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
0ca743a5 914 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
96518518
PM
915};
916
917static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
918 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
919 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
2cbce139
PNA
920 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
921 .len = IFNAMSIZ - 1 },
96518518
PM
922};
923
0ca743a5
PNA
924static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
925{
926 struct nft_stats *cpu_stats, total;
927 struct nlattr *nest;
ce355e20
ED
928 unsigned int seq;
929 u64 pkts, bytes;
0ca743a5
PNA
930 int cpu;
931
932 memset(&total, 0, sizeof(total));
933 for_each_possible_cpu(cpu) {
934 cpu_stats = per_cpu_ptr(stats, cpu);
ce355e20
ED
935 do {
936 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
937 pkts = cpu_stats->pkts;
938 bytes = cpu_stats->bytes;
939 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
940 total.pkts += pkts;
941 total.bytes += bytes;
0ca743a5
PNA
942 }
943 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
944 if (nest == NULL)
945 goto nla_put_failure;
946
947 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
948 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
949 goto nla_put_failure;
950
951 nla_nest_end(skb, nest);
952 return 0;
953
954nla_put_failure:
955 return -ENOSPC;
956}
957
84d7fce6
PNA
958static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
959 u32 portid, u32 seq, int event, u32 flags,
960 int family, const struct nft_table *table,
96518518
PM
961 const struct nft_chain *chain)
962{
963 struct nlmsghdr *nlh;
964 struct nfgenmsg *nfmsg;
965
966 event |= NFNL_SUBSYS_NFTABLES << 8;
967 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
968 if (nlh == NULL)
969 goto nla_put_failure;
970
971 nfmsg = nlmsg_data(nlh);
972 nfmsg->nfgen_family = family;
973 nfmsg->version = NFNETLINK_V0;
84d7fce6 974 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
975
976 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
977 goto nla_put_failure;
978 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
979 goto nla_put_failure;
980 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
981 goto nla_put_failure;
982
983 if (chain->flags & NFT_BASE_CHAIN) {
0ca743a5 984 const struct nft_base_chain *basechain = nft_base_chain(chain);
115a60b1 985 const struct nf_hook_ops *ops = &basechain->ops[0];
0ca743a5
PNA
986 struct nlattr *nest;
987
988 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
96518518
PM
989 if (nest == NULL)
990 goto nla_put_failure;
991 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
992 goto nla_put_failure;
993 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
994 goto nla_put_failure;
2cbce139
PNA
995 if (basechain->dev_name[0] &&
996 nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name))
997 goto nla_put_failure;
96518518 998 nla_nest_end(skb, nest);
9370761c 999
0ca743a5
PNA
1000 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1001 htonl(basechain->policy)))
1002 goto nla_put_failure;
1003
baae3e62
PM
1004 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1005 goto nla_put_failure;
0ca743a5
PNA
1006
1007 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
1008 goto nla_put_failure;
96518518
PM
1009 }
1010
0ca743a5
PNA
1011 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1012 goto nla_put_failure;
1013
053c095a
JB
1014 nlmsg_end(skb, nlh);
1015 return 0;
96518518
PM
1016
1017nla_put_failure:
1018 nlmsg_trim(skb, nlh);
1019 return -1;
1020}
1021
35151d84 1022static int nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
96518518
PM
1023{
1024 struct sk_buff *skb;
96518518
PM
1025 int err;
1026
128ad332
PNA
1027 if (!ctx->report &&
1028 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
96518518
PM
1029 return 0;
1030
1031 err = -ENOBUFS;
1032 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1033 if (skb == NULL)
1034 goto err;
1035
84d7fce6
PNA
1036 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1037 event, 0, ctx->afi->family, ctx->table,
35151d84 1038 ctx->chain);
96518518
PM
1039 if (err < 0) {
1040 kfree_skb(skb);
1041 goto err;
1042 }
1043
128ad332
PNA
1044 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1045 ctx->report, GFP_KERNEL);
96518518 1046err:
128ad332
PNA
1047 if (err < 0) {
1048 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1049 err);
1050 }
96518518
PM
1051 return err;
1052}
1053
1054static int nf_tables_dump_chains(struct sk_buff *skb,
1055 struct netlink_callback *cb)
1056{
1057 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1058 const struct nft_af_info *afi;
1059 const struct nft_table *table;
1060 const struct nft_chain *chain;
1061 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1062 struct net *net = sock_net(skb->sk);
96518518
PM
1063 int family = nfmsg->nfgen_family;
1064
e688a7f8 1065 rcu_read_lock();
38e029f1
PNA
1066 cb->seq = net->nft.base_seq;
1067
e688a7f8 1068 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
96518518
PM
1069 if (family != NFPROTO_UNSPEC && family != afi->family)
1070 continue;
1071
e688a7f8
PNA
1072 list_for_each_entry_rcu(table, &afi->tables, list) {
1073 list_for_each_entry_rcu(chain, &table->chains, list) {
96518518
PM
1074 if (idx < s_idx)
1075 goto cont;
1076 if (idx > s_idx)
1077 memset(&cb->args[1], 0,
1078 sizeof(cb->args) - sizeof(cb->args[0]));
84d7fce6
PNA
1079 if (nf_tables_fill_chain_info(skb, net,
1080 NETLINK_CB(cb->skb).portid,
96518518
PM
1081 cb->nlh->nlmsg_seq,
1082 NFT_MSG_NEWCHAIN,
1083 NLM_F_MULTI,
1084 afi->family, table, chain) < 0)
1085 goto done;
38e029f1
PNA
1086
1087 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518
PM
1088cont:
1089 idx++;
1090 }
1091 }
1092 }
1093done:
e688a7f8 1094 rcu_read_unlock();
96518518
PM
1095 cb->args[0] = idx;
1096 return skb->len;
1097}
1098
7b8002a1
PNA
1099static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1100 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
1101 const struct nlattr * const nla[])
1102{
1103 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1104 const struct nft_af_info *afi;
1105 const struct nft_table *table;
1106 const struct nft_chain *chain;
1107 struct sk_buff *skb2;
1108 int family = nfmsg->nfgen_family;
1109 int err;
1110
1111 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1112 struct netlink_dump_control c = {
1113 .dump = nf_tables_dump_chains,
1114 };
1115 return netlink_dump_start(nlsk, skb, nlh, &c);
1116 }
1117
99633ab2 1118 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1119 if (IS_ERR(afi))
1120 return PTR_ERR(afi);
1121
9370761c 1122 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
96518518
PM
1123 if (IS_ERR(table))
1124 return PTR_ERR(table);
55dd6f93
PNA
1125 if (table->flags & NFT_TABLE_INACTIVE)
1126 return -ENOENT;
96518518
PM
1127
1128 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1129 if (IS_ERR(chain))
1130 return PTR_ERR(chain);
91c7b38d
PNA
1131 if (chain->flags & NFT_CHAIN_INACTIVE)
1132 return -ENOENT;
96518518
PM
1133
1134 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1135 if (!skb2)
1136 return -ENOMEM;
1137
84d7fce6 1138 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
1139 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1140 family, table, chain);
1141 if (err < 0)
1142 goto err;
1143
1144 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1145
1146err:
1147 kfree_skb(skb2);
1148 return err;
1149}
1150
0ca743a5
PNA
1151static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1152 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1153 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1154};
1155
ff3cd7b3 1156static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
0ca743a5
PNA
1157{
1158 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1159 struct nft_stats __percpu *newstats;
1160 struct nft_stats *stats;
1161 int err;
1162
1163 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
1164 if (err < 0)
ff3cd7b3 1165 return ERR_PTR(err);
0ca743a5
PNA
1166
1167 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
ff3cd7b3 1168 return ERR_PTR(-EINVAL);
0ca743a5 1169
ce355e20 1170 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
0ca743a5 1171 if (newstats == NULL)
ff3cd7b3 1172 return ERR_PTR(-ENOMEM);
0ca743a5
PNA
1173
1174 /* Restore old counters on this cpu, no problem. Per-cpu statistics
1175 * are not exposed to userspace.
1176 */
e8781f70 1177 preempt_disable();
0ca743a5
PNA
1178 stats = this_cpu_ptr(newstats);
1179 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1180 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
e8781f70 1181 preempt_enable();
0ca743a5 1182
ff3cd7b3
PNA
1183 return newstats;
1184}
1185
1186static void nft_chain_stats_replace(struct nft_base_chain *chain,
1187 struct nft_stats __percpu *newstats)
1188{
b88825de
PNA
1189 if (newstats == NULL)
1190 return;
1191
0ca743a5 1192 if (chain->stats) {
0ca743a5 1193 struct nft_stats __percpu *oldstats =
67a8fc27 1194 nft_dereference(chain->stats);
0ca743a5
PNA
1195
1196 rcu_assign_pointer(chain->stats, newstats);
1197 synchronize_rcu();
1198 free_percpu(oldstats);
1199 } else
1200 rcu_assign_pointer(chain->stats, newstats);
0ca743a5
PNA
1201}
1202
91c7b38d
PNA
1203static void nf_tables_chain_destroy(struct nft_chain *chain)
1204{
1205 BUG_ON(chain->use > 0);
1206
1207 if (chain->flags & NFT_BASE_CHAIN) {
2cbce139
PNA
1208 struct nft_base_chain *basechain = nft_base_chain(chain);
1209
1210 module_put(basechain->type->owner);
1211 free_percpu(basechain->stats);
1212 if (basechain->ops[0].dev != NULL)
1213 dev_put(basechain->ops[0].dev);
1214 kfree(basechain);
91c7b38d
PNA
1215 } else {
1216 kfree(chain);
1217 }
1218}
1219
633c9a84
PNA
1220static int nf_tables_newchain(struct net *net, struct sock *nlsk,
1221 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
1222 const struct nlattr * const nla[])
1223{
1224 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1225 const struct nlattr * uninitialized_var(name);
7c95f6d8 1226 struct nft_af_info *afi;
96518518
PM
1227 struct nft_table *table;
1228 struct nft_chain *chain;
0ca743a5 1229 struct nft_base_chain *basechain = NULL;
96518518
PM
1230 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1231 int family = nfmsg->nfgen_family;
2cbce139 1232 struct net_device *dev = NULL;
57de2a0c 1233 u8 policy = NF_ACCEPT;
96518518 1234 u64 handle = 0;
115a60b1 1235 unsigned int i;
ff3cd7b3 1236 struct nft_stats __percpu *stats;
96518518
PM
1237 int err;
1238 bool create;
91c7b38d 1239 struct nft_ctx ctx;
96518518
PM
1240
1241 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1242
99633ab2 1243 afi = nf_tables_afinfo_lookup(net, family, true);
96518518
PM
1244 if (IS_ERR(afi))
1245 return PTR_ERR(afi);
1246
9370761c 1247 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
96518518
PM
1248 if (IS_ERR(table))
1249 return PTR_ERR(table);
1250
96518518
PM
1251 chain = NULL;
1252 name = nla[NFTA_CHAIN_NAME];
1253
1254 if (nla[NFTA_CHAIN_HANDLE]) {
1255 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1256 chain = nf_tables_chain_lookup_byhandle(table, handle);
1257 if (IS_ERR(chain))
1258 return PTR_ERR(chain);
1259 } else {
1260 chain = nf_tables_chain_lookup(table, name);
1261 if (IS_ERR(chain)) {
1262 if (PTR_ERR(chain) != -ENOENT)
1263 return PTR_ERR(chain);
1264 chain = NULL;
1265 }
1266 }
1267
57de2a0c
PM
1268 if (nla[NFTA_CHAIN_POLICY]) {
1269 if ((chain != NULL &&
d6b6cb1d
PNA
1270 !(chain->flags & NFT_BASE_CHAIN)))
1271 return -EOPNOTSUPP;
1272
1273 if (chain == NULL &&
57de2a0c
PM
1274 nla[NFTA_CHAIN_HOOK] == NULL)
1275 return -EOPNOTSUPP;
1276
8f46df18 1277 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
57de2a0c
PM
1278 switch (policy) {
1279 case NF_DROP:
1280 case NF_ACCEPT:
1281 break;
1282 default:
1283 return -EINVAL;
1284 }
1285 }
1286
96518518 1287 if (chain != NULL) {
91c7b38d
PNA
1288 struct nft_stats *stats = NULL;
1289 struct nft_trans *trans;
1290
1291 if (chain->flags & NFT_CHAIN_INACTIVE)
1292 return -ENOENT;
96518518
PM
1293 if (nlh->nlmsg_flags & NLM_F_EXCL)
1294 return -EEXIST;
1295 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1296 return -EOPNOTSUPP;
1297
1298 if (nla[NFTA_CHAIN_HANDLE] && name &&
1299 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1300 return -EEXIST;
1301
0ca743a5
PNA
1302 if (nla[NFTA_CHAIN_COUNTERS]) {
1303 if (!(chain->flags & NFT_BASE_CHAIN))
1304 return -EOPNOTSUPP;
1305
ff3cd7b3
PNA
1306 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1307 if (IS_ERR(stats))
1308 return PTR_ERR(stats);
0ca743a5
PNA
1309 }
1310
633c9a84 1311 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
91c7b38d
PNA
1312 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1313 sizeof(struct nft_trans_chain));
f5553c19
PNA
1314 if (trans == NULL) {
1315 free_percpu(stats);
91c7b38d 1316 return -ENOMEM;
f5553c19 1317 }
4401a862 1318
91c7b38d
PNA
1319 nft_trans_chain_stats(trans) = stats;
1320 nft_trans_chain_update(trans) = true;
4401a862 1321
91c7b38d
PNA
1322 if (nla[NFTA_CHAIN_POLICY])
1323 nft_trans_chain_policy(trans) = policy;
1324 else
1325 nft_trans_chain_policy(trans) = -1;
96518518 1326
91c7b38d
PNA
1327 if (nla[NFTA_CHAIN_HANDLE] && name) {
1328 nla_strlcpy(nft_trans_chain_name(trans), name,
1329 NFT_CHAIN_MAXNAMELEN);
1330 }
1331 list_add_tail(&trans->list, &net->nft.commit_list);
1332 return 0;
96518518
PM
1333 }
1334
75820676
PM
1335 if (table->use == UINT_MAX)
1336 return -EOVERFLOW;
1337
96518518 1338 if (nla[NFTA_CHAIN_HOOK]) {
2a37d755 1339 const struct nf_chain_type *type;
96518518 1340 struct nf_hook_ops *ops;
9370761c 1341 nf_hookfn *hookfn;
115a60b1 1342 u32 hooknum, priority;
9370761c 1343
baae3e62 1344 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
9370761c
PNA
1345 if (nla[NFTA_CHAIN_TYPE]) {
1346 type = nf_tables_chain_type_lookup(afi,
1347 nla[NFTA_CHAIN_TYPE],
1348 create);
93b0806f
PM
1349 if (IS_ERR(type))
1350 return PTR_ERR(type);
9370761c 1351 }
96518518
PM
1352
1353 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1354 nft_hook_policy);
1355 if (err < 0)
1356 return err;
1357 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1358 ha[NFTA_HOOK_PRIORITY] == NULL)
1359 return -EINVAL;
9370761c
PNA
1360
1361 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1362 if (hooknum >= afi->nhooks)
96518518 1363 return -EINVAL;
115a60b1 1364 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
96518518 1365
baae3e62 1366 if (!(type->hook_mask & (1 << hooknum)))
9370761c 1367 return -EOPNOTSUPP;
fa2c1de0 1368 if (!try_module_get(type->owner))
baae3e62 1369 return -ENOENT;
fa2c1de0 1370 hookfn = type->hooks[hooknum];
9370761c 1371
2cbce139
PNA
1372 if (afi->flags & NFT_AF_NEEDS_DEV) {
1373 char ifname[IFNAMSIZ];
1374
1375 if (!ha[NFTA_HOOK_DEV]) {
1376 module_put(type->owner);
1377 return -EOPNOTSUPP;
1378 }
1379
1380 nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ);
1381 dev = dev_get_by_name(net, ifname);
1382 if (!dev) {
1383 module_put(type->owner);
1384 return -ENOENT;
1385 }
1386 } else if (ha[NFTA_HOOK_DEV]) {
1387 module_put(type->owner);
1388 return -EOPNOTSUPP;
1389 }
1390
96518518 1391 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
f5553c19
PNA
1392 if (basechain == NULL) {
1393 module_put(type->owner);
2cbce139
PNA
1394 if (dev != NULL)
1395 dev_put(dev);
96518518 1396 return -ENOMEM;
f5553c19 1397 }
9370761c 1398
2cbce139
PNA
1399 if (dev != NULL)
1400 strncpy(basechain->dev_name, dev->name, IFNAMSIZ);
1401
4401a862 1402 if (nla[NFTA_CHAIN_COUNTERS]) {
ff3cd7b3
PNA
1403 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1404 if (IS_ERR(stats)) {
fa2c1de0 1405 module_put(type->owner);
4401a862 1406 kfree(basechain);
2cbce139
PNA
1407 if (dev != NULL)
1408 dev_put(dev);
ff3cd7b3 1409 return PTR_ERR(stats);
4401a862 1410 }
ff3cd7b3 1411 basechain->stats = stats;
4401a862 1412 } else {
ce355e20 1413 stats = netdev_alloc_pcpu_stats(struct nft_stats);
c123bb71 1414 if (stats == NULL) {
fa2c1de0 1415 module_put(type->owner);
4401a862 1416 kfree(basechain);
2cbce139
PNA
1417 if (dev != NULL)
1418 dev_put(dev);
c123bb71 1419 return -ENOMEM;
4401a862 1420 }
ff3cd7b3 1421 rcu_assign_pointer(basechain->stats, stats);
4401a862
PM
1422 }
1423
5ebb335d 1424 write_pnet(&basechain->pnet, net);
9370761c 1425 basechain->type = type;
96518518
PM
1426 chain = &basechain->chain;
1427
115a60b1
PM
1428 for (i = 0; i < afi->nops; i++) {
1429 ops = &basechain->ops[i];
1430 ops->pf = family;
115a60b1
PM
1431 ops->hooknum = hooknum;
1432 ops->priority = priority;
1433 ops->priv = chain;
1434 ops->hook = afi->hooks[ops->hooknum];
2cbce139 1435 ops->dev = dev;
115a60b1
PM
1436 if (hookfn)
1437 ops->hook = hookfn;
1438 if (afi->hook_ops_init)
1439 afi->hook_ops_init(ops, i);
1440 }
96518518
PM
1441
1442 chain->flags |= NFT_BASE_CHAIN;
57de2a0c 1443 basechain->policy = policy;
96518518
PM
1444 } else {
1445 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1446 if (chain == NULL)
1447 return -ENOMEM;
1448 }
1449
1450 INIT_LIST_HEAD(&chain->rules);
1451 chain->handle = nf_tables_alloc_handle(table);
b5bc89bf 1452 chain->table = table;
96518518
PM
1453 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1454
d8ee8f7c
PNA
1455 err = nf_tables_register_hooks(table, chain, afi->nops);
1456 if (err < 0)
1457 goto err1;
96518518 1458
633c9a84 1459 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
91c7b38d
PNA
1460 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1461 if (err < 0)
1462 goto err2;
96518518 1463
4fefee57 1464 table->use++;
e688a7f8 1465 list_add_tail_rcu(&chain->list, &table->chains);
91c7b38d
PNA
1466 return 0;
1467err2:
c5598794 1468 nf_tables_unregister_hooks(table, chain, afi->nops);
91c7b38d
PNA
1469err1:
1470 nf_tables_chain_destroy(chain);
1471 return err;
96518518
PM
1472}
1473
633c9a84
PNA
1474static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1475 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
1476 const struct nlattr * const nla[])
1477{
1478 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 1479 struct nft_af_info *afi;
96518518
PM
1480 struct nft_table *table;
1481 struct nft_chain *chain;
1482 int family = nfmsg->nfgen_family;
91c7b38d 1483 struct nft_ctx ctx;
96518518 1484
99633ab2 1485 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1486 if (IS_ERR(afi))
1487 return PTR_ERR(afi);
1488
9370761c 1489 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
96518518
PM
1490 if (IS_ERR(table))
1491 return PTR_ERR(table);
1492
1493 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1494 if (IS_ERR(chain))
1495 return PTR_ERR(chain);
4fefee57 1496 if (chain->use > 0)
96518518
PM
1497 return -EBUSY;
1498
633c9a84 1499 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
0165d932 1500
ee01d542 1501 return nft_delchain(&ctx);
96518518
PM
1502}
1503
96518518
PM
1504/*
1505 * Expressions
1506 */
1507
1508/**
ef1f7df9
PM
1509 * nft_register_expr - register nf_tables expr type
1510 * @ops: expr type
96518518 1511 *
ef1f7df9 1512 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
1513 * success or a negative errno code otherwise.
1514 */
ef1f7df9 1515int nft_register_expr(struct nft_expr_type *type)
96518518
PM
1516{
1517 nfnl_lock(NFNL_SUBSYS_NFTABLES);
758dbcec 1518 if (type->family == NFPROTO_UNSPEC)
e688a7f8 1519 list_add_tail_rcu(&type->list, &nf_tables_expressions);
758dbcec 1520 else
e688a7f8 1521 list_add_rcu(&type->list, &nf_tables_expressions);
96518518
PM
1522 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1523 return 0;
1524}
1525EXPORT_SYMBOL_GPL(nft_register_expr);
1526
1527/**
ef1f7df9
PM
1528 * nft_unregister_expr - unregister nf_tables expr type
1529 * @ops: expr type
96518518 1530 *
ef1f7df9 1531 * Unregisters the expr typefor use with nf_tables.
96518518 1532 */
ef1f7df9 1533void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
1534{
1535 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 1536 list_del_rcu(&type->list);
96518518
PM
1537 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1538}
1539EXPORT_SYMBOL_GPL(nft_unregister_expr);
1540
64d46806
PM
1541static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1542 struct nlattr *nla)
96518518 1543{
ef1f7df9 1544 const struct nft_expr_type *type;
96518518 1545
ef1f7df9 1546 list_for_each_entry(type, &nf_tables_expressions, list) {
64d46806
PM
1547 if (!nla_strcmp(nla, type->name) &&
1548 (!type->family || type->family == family))
ef1f7df9 1549 return type;
96518518
PM
1550 }
1551 return NULL;
1552}
1553
64d46806
PM
1554static const struct nft_expr_type *nft_expr_type_get(u8 family,
1555 struct nlattr *nla)
96518518 1556{
ef1f7df9 1557 const struct nft_expr_type *type;
96518518
PM
1558
1559 if (nla == NULL)
1560 return ERR_PTR(-EINVAL);
1561
64d46806 1562 type = __nft_expr_type_get(family, nla);
ef1f7df9
PM
1563 if (type != NULL && try_module_get(type->owner))
1564 return type;
96518518
PM
1565
1566#ifdef CONFIG_MODULES
ef1f7df9 1567 if (type == NULL) {
64d46806
PM
1568 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1569 request_module("nft-expr-%u-%.*s", family,
1570 nla_len(nla), (char *)nla_data(nla));
1571 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1572 if (__nft_expr_type_get(family, nla))
1573 return ERR_PTR(-EAGAIN);
1574
96518518
PM
1575 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1576 request_module("nft-expr-%.*s",
1577 nla_len(nla), (char *)nla_data(nla));
1578 nfnl_lock(NFNL_SUBSYS_NFTABLES);
64d46806 1579 if (__nft_expr_type_get(family, nla))
96518518
PM
1580 return ERR_PTR(-EAGAIN);
1581 }
1582#endif
1583 return ERR_PTR(-ENOENT);
1584}
1585
1586static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1587 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1588 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1589};
1590
1591static int nf_tables_fill_expr_info(struct sk_buff *skb,
1592 const struct nft_expr *expr)
1593{
ef1f7df9 1594 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
1595 goto nla_put_failure;
1596
1597 if (expr->ops->dump) {
1598 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1599 if (data == NULL)
1600 goto nla_put_failure;
1601 if (expr->ops->dump(skb, expr) < 0)
1602 goto nla_put_failure;
1603 nla_nest_end(skb, data);
1604 }
1605
1606 return skb->len;
1607
1608nla_put_failure:
1609 return -1;
1610};
1611
0b2d8a7b
PM
1612int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1613 const struct nft_expr *expr)
1614{
1615 struct nlattr *nest;
1616
1617 nest = nla_nest_start(skb, attr);
1618 if (!nest)
1619 goto nla_put_failure;
1620 if (nf_tables_fill_expr_info(skb, expr) < 0)
1621 goto nla_put_failure;
1622 nla_nest_end(skb, nest);
1623 return 0;
1624
1625nla_put_failure:
1626 return -1;
1627}
1628
96518518
PM
1629struct nft_expr_info {
1630 const struct nft_expr_ops *ops;
ef1f7df9 1631 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
1632};
1633
0ca743a5
PNA
1634static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1635 const struct nlattr *nla,
96518518
PM
1636 struct nft_expr_info *info)
1637{
ef1f7df9 1638 const struct nft_expr_type *type;
96518518 1639 const struct nft_expr_ops *ops;
ef1f7df9 1640 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
1641 int err;
1642
ef1f7df9 1643 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
96518518
PM
1644 if (err < 0)
1645 return err;
1646
64d46806 1647 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
1648 if (IS_ERR(type))
1649 return PTR_ERR(type);
1650
1651 if (tb[NFTA_EXPR_DATA]) {
1652 err = nla_parse_nested(info->tb, type->maxattr,
1653 tb[NFTA_EXPR_DATA], type->policy);
1654 if (err < 0)
1655 goto err1;
1656 } else
1657 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1658
1659 if (type->select_ops != NULL) {
0ca743a5
PNA
1660 ops = type->select_ops(ctx,
1661 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
1662 if (IS_ERR(ops)) {
1663 err = PTR_ERR(ops);
1664 goto err1;
1665 }
1666 } else
1667 ops = type->ops;
1668
96518518
PM
1669 info->ops = ops;
1670 return 0;
ef1f7df9
PM
1671
1672err1:
1673 module_put(type->owner);
1674 return err;
96518518
PM
1675}
1676
1677static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 1678 const struct nft_expr_info *info,
96518518
PM
1679 struct nft_expr *expr)
1680{
1681 const struct nft_expr_ops *ops = info->ops;
1682 int err;
1683
1684 expr->ops = ops;
1685 if (ops->init) {
ef1f7df9 1686 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
1687 if (err < 0)
1688 goto err1;
1689 }
1690
96518518
PM
1691 return 0;
1692
1693err1:
1694 expr->ops = NULL;
1695 return err;
1696}
1697
62472bce
PM
1698static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1699 struct nft_expr *expr)
96518518
PM
1700{
1701 if (expr->ops->destroy)
62472bce 1702 expr->ops->destroy(ctx, expr);
ef1f7df9 1703 module_put(expr->ops->type->owner);
96518518
PM
1704}
1705
0b2d8a7b
PM
1706struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1707 const struct nlattr *nla)
1708{
1709 struct nft_expr_info info;
1710 struct nft_expr *expr;
1711 int err;
1712
1713 err = nf_tables_expr_parse(ctx, nla, &info);
1714 if (err < 0)
1715 goto err1;
1716
1717 err = -ENOMEM;
1718 expr = kzalloc(info.ops->size, GFP_KERNEL);
1719 if (expr == NULL)
1720 goto err2;
1721
1722 err = nf_tables_newexpr(ctx, &info, expr);
1723 if (err < 0)
1724 goto err2;
1725
1726 return expr;
1727err2:
1728 module_put(info.ops->type->owner);
1729err1:
1730 return ERR_PTR(err);
1731}
1732
1733void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1734{
1735 nf_tables_expr_destroy(ctx, expr);
1736 kfree(expr);
1737}
1738
96518518
PM
1739/*
1740 * Rules
1741 */
1742
1743static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1744 u64 handle)
1745{
1746 struct nft_rule *rule;
1747
1748 // FIXME: this sucks
1749 list_for_each_entry(rule, &chain->rules, list) {
1750 if (handle == rule->handle)
1751 return rule;
1752 }
1753
1754 return ERR_PTR(-ENOENT);
1755}
1756
1757static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1758 const struct nlattr *nla)
1759{
1760 if (nla == NULL)
1761 return ERR_PTR(-EINVAL);
1762
1763 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1764}
1765
1766static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1767 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1768 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1769 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1770 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1771 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
0ca743a5 1772 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 1773 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
1774 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1775 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1776};
1777
84d7fce6
PNA
1778static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1779 u32 portid, u32 seq, int event,
1780 u32 flags, int family,
96518518
PM
1781 const struct nft_table *table,
1782 const struct nft_chain *chain,
1783 const struct nft_rule *rule)
1784{
1785 struct nlmsghdr *nlh;
1786 struct nfgenmsg *nfmsg;
1787 const struct nft_expr *expr, *next;
1788 struct nlattr *list;
5e948466
EL
1789 const struct nft_rule *prule;
1790 int type = event | NFNL_SUBSYS_NFTABLES << 8;
96518518 1791
5e948466 1792 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
96518518
PM
1793 flags);
1794 if (nlh == NULL)
1795 goto nla_put_failure;
1796
1797 nfmsg = nlmsg_data(nlh);
1798 nfmsg->nfgen_family = family;
1799 nfmsg->version = NFNETLINK_V0;
84d7fce6 1800 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
1801
1802 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1803 goto nla_put_failure;
1804 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1805 goto nla_put_failure;
1806 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1807 goto nla_put_failure;
1808
5e948466
EL
1809 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1810 prule = list_entry(rule->list.prev, struct nft_rule, list);
1811 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1812 cpu_to_be64(prule->handle)))
1813 goto nla_put_failure;
1814 }
1815
96518518
PM
1816 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1817 if (list == NULL)
1818 goto nla_put_failure;
1819 nft_rule_for_each_expr(expr, next, rule) {
0b2d8a7b 1820 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
96518518 1821 goto nla_put_failure;
96518518
PM
1822 }
1823 nla_nest_end(skb, list);
1824
86f1ec32
PM
1825 if (rule->udata) {
1826 struct nft_userdata *udata = nft_userdata(rule);
1827 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1828 udata->data) < 0)
1829 goto nla_put_failure;
1830 }
0768b3b3 1831
053c095a
JB
1832 nlmsg_end(skb, nlh);
1833 return 0;
96518518
PM
1834
1835nla_put_failure:
1836 nlmsg_trim(skb, nlh);
1837 return -1;
1838}
1839
35151d84 1840static int nf_tables_rule_notify(const struct nft_ctx *ctx,
96518518 1841 const struct nft_rule *rule,
35151d84 1842 int event)
96518518
PM
1843{
1844 struct sk_buff *skb;
96518518
PM
1845 int err;
1846
128ad332
PNA
1847 if (!ctx->report &&
1848 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
96518518
PM
1849 return 0;
1850
1851 err = -ENOBUFS;
1852 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1853 if (skb == NULL)
1854 goto err;
1855
84d7fce6
PNA
1856 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1857 event, 0, ctx->afi->family, ctx->table,
35151d84 1858 ctx->chain, rule);
96518518
PM
1859 if (err < 0) {
1860 kfree_skb(skb);
1861 goto err;
1862 }
1863
128ad332
PNA
1864 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1865 ctx->report, GFP_KERNEL);
96518518 1866err:
128ad332
PNA
1867 if (err < 0) {
1868 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1869 err);
1870 }
96518518
PM
1871 return err;
1872}
1873
1874static int nf_tables_dump_rules(struct sk_buff *skb,
1875 struct netlink_callback *cb)
1876{
1877 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1878 const struct nft_af_info *afi;
1879 const struct nft_table *table;
1880 const struct nft_chain *chain;
1881 const struct nft_rule *rule;
1882 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1883 struct net *net = sock_net(skb->sk);
96518518
PM
1884 int family = nfmsg->nfgen_family;
1885
e688a7f8 1886 rcu_read_lock();
38e029f1
PNA
1887 cb->seq = net->nft.base_seq;
1888
e688a7f8 1889 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
96518518
PM
1890 if (family != NFPROTO_UNSPEC && family != afi->family)
1891 continue;
1892
e688a7f8
PNA
1893 list_for_each_entry_rcu(table, &afi->tables, list) {
1894 list_for_each_entry_rcu(chain, &table->chains, list) {
1895 list_for_each_entry_rcu(rule, &chain->rules, list) {
0628b123
PNA
1896 if (!nft_rule_is_active(net, rule))
1897 goto cont;
96518518
PM
1898 if (idx < s_idx)
1899 goto cont;
1900 if (idx > s_idx)
1901 memset(&cb->args[1], 0,
1902 sizeof(cb->args) - sizeof(cb->args[0]));
84d7fce6 1903 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
96518518
PM
1904 cb->nlh->nlmsg_seq,
1905 NFT_MSG_NEWRULE,
1906 NLM_F_MULTI | NLM_F_APPEND,
1907 afi->family, table, chain, rule) < 0)
1908 goto done;
38e029f1
PNA
1909
1910 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518
PM
1911cont:
1912 idx++;
1913 }
1914 }
1915 }
1916 }
1917done:
e688a7f8
PNA
1918 rcu_read_unlock();
1919
96518518
PM
1920 cb->args[0] = idx;
1921 return skb->len;
1922}
1923
7b8002a1
PNA
1924static int nf_tables_getrule(struct net *net, struct sock *nlsk,
1925 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
1926 const struct nlattr * const nla[])
1927{
1928 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1929 const struct nft_af_info *afi;
1930 const struct nft_table *table;
1931 const struct nft_chain *chain;
1932 const struct nft_rule *rule;
1933 struct sk_buff *skb2;
1934 int family = nfmsg->nfgen_family;
1935 int err;
1936
1937 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1938 struct netlink_dump_control c = {
1939 .dump = nf_tables_dump_rules,
1940 };
1941 return netlink_dump_start(nlsk, skb, nlh, &c);
1942 }
1943
99633ab2 1944 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1945 if (IS_ERR(afi))
1946 return PTR_ERR(afi);
1947
9370761c 1948 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1949 if (IS_ERR(table))
1950 return PTR_ERR(table);
55dd6f93
PNA
1951 if (table->flags & NFT_TABLE_INACTIVE)
1952 return -ENOENT;
96518518
PM
1953
1954 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1955 if (IS_ERR(chain))
1956 return PTR_ERR(chain);
91c7b38d
PNA
1957 if (chain->flags & NFT_CHAIN_INACTIVE)
1958 return -ENOENT;
96518518
PM
1959
1960 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1961 if (IS_ERR(rule))
1962 return PTR_ERR(rule);
1963
1964 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1965 if (!skb2)
1966 return -ENOMEM;
1967
84d7fce6 1968 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
1969 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1970 family, table, chain, rule);
1971 if (err < 0)
1972 goto err;
1973
1974 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1975
1976err:
1977 kfree_skb(skb2);
1978 return err;
1979}
1980
62472bce
PM
1981static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1982 struct nft_rule *rule)
96518518 1983{
96518518
PM
1984 struct nft_expr *expr;
1985
1986 /*
1987 * Careful: some expressions might not be initialized in case this
1988 * is called on error from nf_tables_newrule().
1989 */
1990 expr = nft_expr_first(rule);
1991 while (expr->ops && expr != nft_expr_last(rule)) {
62472bce 1992 nf_tables_expr_destroy(ctx, expr);
96518518
PM
1993 expr = nft_expr_next(expr);
1994 }
1995 kfree(rule);
1996}
1997
1081d11b
PNA
1998#define NFT_RULE_MAXEXPRS 128
1999
2000static struct nft_expr_info *info;
2001
633c9a84
PNA
2002static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2003 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
2004 const struct nlattr * const nla[])
2005{
2006 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 2007 struct nft_af_info *afi;
96518518
PM
2008 struct nft_table *table;
2009 struct nft_chain *chain;
2010 struct nft_rule *rule, *old_rule = NULL;
86f1ec32 2011 struct nft_userdata *udata;
1081d11b 2012 struct nft_trans *trans = NULL;
96518518
PM
2013 struct nft_expr *expr;
2014 struct nft_ctx ctx;
2015 struct nlattr *tmp;
86f1ec32 2016 unsigned int size, i, n, ulen = 0, usize = 0;
96518518
PM
2017 int err, rem;
2018 bool create;
5e948466 2019 u64 handle, pos_handle;
96518518
PM
2020
2021 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2022
99633ab2 2023 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
96518518
PM
2024 if (IS_ERR(afi))
2025 return PTR_ERR(afi);
2026
9370761c 2027 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
2028 if (IS_ERR(table))
2029 return PTR_ERR(table);
2030
2031 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2032 if (IS_ERR(chain))
2033 return PTR_ERR(chain);
2034
2035 if (nla[NFTA_RULE_HANDLE]) {
2036 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2037 rule = __nf_tables_rule_lookup(chain, handle);
2038 if (IS_ERR(rule))
2039 return PTR_ERR(rule);
2040
2041 if (nlh->nlmsg_flags & NLM_F_EXCL)
2042 return -EEXIST;
2043 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2044 old_rule = rule;
2045 else
2046 return -EOPNOTSUPP;
2047 } else {
2048 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
2049 return -EINVAL;
2050 handle = nf_tables_alloc_handle(table);
a0a7379e
PNA
2051
2052 if (chain->use == UINT_MAX)
2053 return -EOVERFLOW;
96518518
PM
2054 }
2055
5e948466
EL
2056 if (nla[NFTA_RULE_POSITION]) {
2057 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2058 return -EOPNOTSUPP;
2059
2060 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2061 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
2062 if (IS_ERR(old_rule))
2063 return PTR_ERR(old_rule);
2064 }
2065
633c9a84 2066 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
0ca743a5 2067
96518518
PM
2068 n = 0;
2069 size = 0;
2070 if (nla[NFTA_RULE_EXPRESSIONS]) {
2071 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2072 err = -EINVAL;
2073 if (nla_type(tmp) != NFTA_LIST_ELEM)
2074 goto err1;
2075 if (n == NFT_RULE_MAXEXPRS)
2076 goto err1;
0ca743a5 2077 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
96518518
PM
2078 if (err < 0)
2079 goto err1;
2080 size += info[n].ops->size;
2081 n++;
2082 }
2083 }
9889840f
PM
2084 /* Check for overflow of dlen field */
2085 err = -EFBIG;
2086 if (size >= 1 << 12)
2087 goto err1;
96518518 2088
86f1ec32 2089 if (nla[NFTA_RULE_USERDATA]) {
0768b3b3 2090 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
86f1ec32
PM
2091 if (ulen > 0)
2092 usize = sizeof(struct nft_userdata) + ulen;
2093 }
0768b3b3 2094
96518518 2095 err = -ENOMEM;
86f1ec32 2096 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
96518518
PM
2097 if (rule == NULL)
2098 goto err1;
2099
0628b123
PNA
2100 nft_rule_activate_next(net, rule);
2101
96518518
PM
2102 rule->handle = handle;
2103 rule->dlen = size;
86f1ec32 2104 rule->udata = ulen ? 1 : 0;
0768b3b3 2105
86f1ec32
PM
2106 if (ulen) {
2107 udata = nft_userdata(rule);
2108 udata->len = ulen - 1;
2109 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2110 }
96518518 2111
96518518
PM
2112 expr = nft_expr_first(rule);
2113 for (i = 0; i < n; i++) {
2114 err = nf_tables_newexpr(&ctx, &info[i], expr);
2115 if (err < 0)
2116 goto err2;
ef1f7df9 2117 info[i].ops = NULL;
96518518
PM
2118 expr = nft_expr_next(expr);
2119 }
2120
96518518 2121 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
0628b123 2122 if (nft_rule_is_active_next(net, old_rule)) {
ac904ac8 2123 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
b380e5c7 2124 old_rule);
1081d11b 2125 if (trans == NULL) {
0628b123
PNA
2126 err = -ENOMEM;
2127 goto err2;
2128 }
ee01d542 2129 nft_rule_deactivate_next(net, old_rule);
ac34b861 2130 chain->use--;
5bc5c307 2131 list_add_tail_rcu(&rule->list, &old_rule->list);
0628b123
PNA
2132 } else {
2133 err = -ENOENT;
2134 goto err2;
2135 }
96518518 2136 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
5e948466
EL
2137 if (old_rule)
2138 list_add_rcu(&rule->list, &old_rule->list);
2139 else
2140 list_add_tail_rcu(&rule->list, &chain->rules);
2141 else {
2142 if (old_rule)
2143 list_add_tail_rcu(&rule->list, &old_rule->list);
2144 else
2145 list_add_rcu(&rule->list, &chain->rules);
2146 }
96518518 2147
b380e5c7 2148 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
0628b123
PNA
2149 err = -ENOMEM;
2150 goto err3;
2151 }
4fefee57 2152 chain->use++;
96518518
PM
2153 return 0;
2154
0628b123
PNA
2155err3:
2156 list_del_rcu(&rule->list);
96518518 2157err2:
62472bce 2158 nf_tables_rule_destroy(&ctx, rule);
96518518
PM
2159err1:
2160 for (i = 0; i < n; i++) {
2161 if (info[i].ops != NULL)
ef1f7df9 2162 module_put(info[i].ops->type->owner);
96518518
PM
2163 }
2164 return err;
2165}
2166
633c9a84
PNA
2167static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2168 struct sk_buff *skb, const struct nlmsghdr *nlh,
96518518
PM
2169 const struct nlattr * const nla[])
2170{
2171 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 2172 struct nft_af_info *afi;
7c95f6d8 2173 struct nft_table *table;
cf9dc09d
PNA
2174 struct nft_chain *chain = NULL;
2175 struct nft_rule *rule;
0628b123
PNA
2176 int family = nfmsg->nfgen_family, err = 0;
2177 struct nft_ctx ctx;
96518518 2178
99633ab2 2179 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
2180 if (IS_ERR(afi))
2181 return PTR_ERR(afi);
2182
9370761c 2183 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
2184 if (IS_ERR(table))
2185 return PTR_ERR(table);
2186
cf9dc09d
PNA
2187 if (nla[NFTA_RULE_CHAIN]) {
2188 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2189 if (IS_ERR(chain))
2190 return PTR_ERR(chain);
2191 }
96518518 2192
633c9a84 2193 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
0628b123 2194
cf9dc09d
PNA
2195 if (chain) {
2196 if (nla[NFTA_RULE_HANDLE]) {
2197 rule = nf_tables_rule_lookup(chain,
2198 nla[NFTA_RULE_HANDLE]);
2199 if (IS_ERR(rule))
2200 return PTR_ERR(rule);
96518518 2201
5e266fe7 2202 err = nft_delrule(&ctx, rule);
cf9dc09d 2203 } else {
ce24b721 2204 err = nft_delrule_by_chain(&ctx);
cf9dc09d
PNA
2205 }
2206 } else {
2207 list_for_each_entry(chain, &table->chains, list) {
2208 ctx.chain = chain;
ce24b721 2209 err = nft_delrule_by_chain(&ctx);
0628b123
PNA
2210 if (err < 0)
2211 break;
2212 }
2213 }
2214
2215 return err;
2216}
2217
20a69341
PM
2218/*
2219 * Sets
2220 */
2221
2222static LIST_HEAD(nf_tables_set_ops);
2223
2224int nft_register_set(struct nft_set_ops *ops)
2225{
2226 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2227 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
20a69341
PM
2228 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2229 return 0;
2230}
2231EXPORT_SYMBOL_GPL(nft_register_set);
2232
2233void nft_unregister_set(struct nft_set_ops *ops)
2234{
2235 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2236 list_del_rcu(&ops->list);
20a69341
PM
2237 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2238}
2239EXPORT_SYMBOL_GPL(nft_unregister_set);
2240
c50b960c
PM
2241/*
2242 * Select a set implementation based on the data characteristics and the
2243 * given policy. The total memory use might not be known if no size is
2244 * given, in that case the amount of memory per element is used.
2245 */
2246static const struct nft_set_ops *
2247nft_select_set_ops(const struct nlattr * const nla[],
2248 const struct nft_set_desc *desc,
2249 enum nft_set_policies policy)
20a69341 2250{
c50b960c
PM
2251 const struct nft_set_ops *ops, *bops;
2252 struct nft_set_estimate est, best;
20a69341
PM
2253 u32 features;
2254
2255#ifdef CONFIG_MODULES
2256 if (list_empty(&nf_tables_set_ops)) {
2257 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2258 request_module("nft-set");
2259 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2260 if (!list_empty(&nf_tables_set_ops))
2261 return ERR_PTR(-EAGAIN);
2262 }
2263#endif
2264 features = 0;
2265 if (nla[NFTA_SET_FLAGS] != NULL) {
2266 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
4a8678ef 2267 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
20a69341
PM
2268 }
2269
c50b960c
PM
2270 bops = NULL;
2271 best.size = ~0;
2272 best.class = ~0;
2273
20a69341
PM
2274 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2275 if ((ops->features & features) != features)
2276 continue;
c50b960c
PM
2277 if (!ops->estimate(desc, features, &est))
2278 continue;
2279
2280 switch (policy) {
2281 case NFT_SET_POL_PERFORMANCE:
2282 if (est.class < best.class)
2283 break;
2284 if (est.class == best.class && est.size < best.size)
2285 break;
2286 continue;
2287 case NFT_SET_POL_MEMORY:
2288 if (est.size < best.size)
2289 break;
2290 if (est.size == best.size && est.class < best.class)
2291 break;
2292 continue;
2293 default:
2294 break;
2295 }
2296
20a69341
PM
2297 if (!try_module_get(ops->owner))
2298 continue;
c50b960c
PM
2299 if (bops != NULL)
2300 module_put(bops->owner);
2301
2302 bops = ops;
2303 best = est;
20a69341
PM
2304 }
2305
c50b960c
PM
2306 if (bops != NULL)
2307 return bops;
2308
20a69341
PM
2309 return ERR_PTR(-EOPNOTSUPP);
2310}
2311
2312static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2313 [NFTA_SET_TABLE] = { .type = NLA_STRING },
a9bdd836
PNA
2314 [NFTA_SET_NAME] = { .type = NLA_STRING,
2315 .len = IFNAMSIZ - 1 },
20a69341
PM
2316 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2317 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2318 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2319 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2320 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
2321 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2322 [NFTA_SET_DESC] = { .type = NLA_NESTED },
958bee14 2323 [NFTA_SET_ID] = { .type = NLA_U32 },
761da293
PM
2324 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2325 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
e6d8ecac
CFG
2326 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
2327 .len = NFT_USERDATA_MAXLEN },
c50b960c
PM
2328};
2329
2330static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2331 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
20a69341
PM
2332};
2333
633c9a84 2334static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
2335 const struct sk_buff *skb,
2336 const struct nlmsghdr *nlh,
2337 const struct nlattr * const nla[])
2338{
2339 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8
PNA
2340 struct nft_af_info *afi = NULL;
2341 struct nft_table *table = NULL;
20a69341 2342
c9c8e485
PNA
2343 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2344 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2345 if (IS_ERR(afi))
2346 return PTR_ERR(afi);
2347 }
20a69341
PM
2348
2349 if (nla[NFTA_SET_TABLE] != NULL) {
ec2c9935
PM
2350 if (afi == NULL)
2351 return -EAFNOSUPPORT;
2352
9370761c 2353 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2354 if (IS_ERR(table))
2355 return PTR_ERR(table);
2356 }
2357
633c9a84 2358 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2359 return 0;
2360}
2361
2362struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2363 const struct nlattr *nla)
2364{
2365 struct nft_set *set;
2366
2367 if (nla == NULL)
2368 return ERR_PTR(-EINVAL);
2369
2370 list_for_each_entry(set, &table->sets, list) {
2371 if (!nla_strcmp(nla, set->name))
2372 return set;
2373 }
2374 return ERR_PTR(-ENOENT);
2375}
2376
958bee14
PNA
2377struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2378 const struct nlattr *nla)
2379{
2380 struct nft_trans *trans;
2381 u32 id = ntohl(nla_get_be32(nla));
2382
2383 list_for_each_entry(trans, &net->nft.commit_list, list) {
2384 if (trans->msg_type == NFT_MSG_NEWSET &&
2385 id == nft_trans_set_id(trans))
2386 return nft_trans_set(trans);
2387 }
2388 return ERR_PTR(-ENOENT);
2389}
2390
20a69341
PM
2391static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2392 const char *name)
2393{
2394 const struct nft_set *i;
2395 const char *p;
2396 unsigned long *inuse;
60eb1894 2397 unsigned int n = 0, min = 0;
20a69341
PM
2398
2399 p = strnchr(name, IFNAMSIZ, '%');
2400 if (p != NULL) {
2401 if (p[1] != 'd' || strchr(p + 2, '%'))
2402 return -EINVAL;
2403
2404 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2405 if (inuse == NULL)
2406 return -ENOMEM;
60eb1894 2407cont:
20a69341 2408 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
2409 int tmp;
2410
2411 if (!sscanf(i->name, name, &tmp))
20a69341 2412 continue;
60eb1894 2413 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 2414 continue;
14662917 2415
60eb1894 2416 set_bit(tmp - min, inuse);
20a69341
PM
2417 }
2418
53b70287 2419 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
2420 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2421 min += BITS_PER_BYTE * PAGE_SIZE;
2422 memset(inuse, 0, PAGE_SIZE);
2423 goto cont;
2424 }
20a69341
PM
2425 free_page((unsigned long)inuse);
2426 }
2427
60eb1894 2428 snprintf(set->name, sizeof(set->name), name, min + n);
20a69341
PM
2429 list_for_each_entry(i, &ctx->table->sets, list) {
2430 if (!strcmp(set->name, i->name))
2431 return -ENFILE;
2432 }
2433 return 0;
2434}
2435
2436static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2437 const struct nft_set *set, u16 event, u16 flags)
2438{
2439 struct nfgenmsg *nfmsg;
2440 struct nlmsghdr *nlh;
c50b960c 2441 struct nlattr *desc;
128ad332
PNA
2442 u32 portid = ctx->portid;
2443 u32 seq = ctx->seq;
20a69341
PM
2444
2445 event |= NFNL_SUBSYS_NFTABLES << 8;
2446 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2447 flags);
2448 if (nlh == NULL)
2449 goto nla_put_failure;
2450
2451 nfmsg = nlmsg_data(nlh);
2452 nfmsg->nfgen_family = ctx->afi->family;
2453 nfmsg->version = NFNETLINK_V0;
84d7fce6 2454 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
20a69341
PM
2455
2456 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2457 goto nla_put_failure;
2458 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2459 goto nla_put_failure;
2460 if (set->flags != 0)
2461 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2462 goto nla_put_failure;
2463
2464 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2465 goto nla_put_failure;
2466 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2467 goto nla_put_failure;
2468 if (set->flags & NFT_SET_MAP) {
2469 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2470 goto nla_put_failure;
2471 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2472 goto nla_put_failure;
2473 }
2474
761da293
PM
2475 if (set->timeout &&
2476 nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout)))
2477 goto nla_put_failure;
2478 if (set->gc_int &&
2479 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2480 goto nla_put_failure;
2481
9363dc4b
AB
2482 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2483 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2484 goto nla_put_failure;
2485 }
2486
e6d8ecac
CFG
2487 if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
2488 goto nla_put_failure;
2489
c50b960c
PM
2490 desc = nla_nest_start(skb, NFTA_SET_DESC);
2491 if (desc == NULL)
2492 goto nla_put_failure;
2493 if (set->size &&
2494 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2495 goto nla_put_failure;
2496 nla_nest_end(skb, desc);
2497
053c095a
JB
2498 nlmsg_end(skb, nlh);
2499 return 0;
20a69341
PM
2500
2501nla_put_failure:
2502 nlmsg_trim(skb, nlh);
2503 return -1;
2504}
2505
2506static int nf_tables_set_notify(const struct nft_ctx *ctx,
2507 const struct nft_set *set,
31f8441c 2508 int event, gfp_t gfp_flags)
20a69341
PM
2509{
2510 struct sk_buff *skb;
128ad332 2511 u32 portid = ctx->portid;
20a69341
PM
2512 int err;
2513
128ad332
PNA
2514 if (!ctx->report &&
2515 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
20a69341
PM
2516 return 0;
2517
2518 err = -ENOBUFS;
31f8441c 2519 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
20a69341
PM
2520 if (skb == NULL)
2521 goto err;
2522
2523 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2524 if (err < 0) {
2525 kfree_skb(skb);
2526 goto err;
2527 }
2528
128ad332 2529 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
31f8441c 2530 ctx->report, gfp_flags);
20a69341
PM
2531err:
2532 if (err < 0)
99633ab2 2533 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
20a69341
PM
2534 return err;
2535}
2536
5b96af77 2537static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
c9c8e485
PNA
2538{
2539 const struct nft_set *set;
2540 unsigned int idx, s_idx = cb->args[0];
7c95f6d8 2541 struct nft_af_info *afi;
c9c8e485
PNA
2542 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2543 struct net *net = sock_net(skb->sk);
2544 int cur_family = cb->args[3];
5b96af77 2545 struct nft_ctx *ctx = cb->data, ctx_set;
c9c8e485
PNA
2546
2547 if (cb->args[1])
2548 return skb->len;
2549
e688a7f8 2550 rcu_read_lock();
38e029f1
PNA
2551 cb->seq = net->nft.base_seq;
2552
e688a7f8 2553 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
5b96af77
PNA
2554 if (ctx->afi && ctx->afi != afi)
2555 continue;
2556
c9c8e485
PNA
2557 if (cur_family) {
2558 if (afi->family != cur_family)
2559 continue;
2560
2561 cur_family = 0;
2562 }
e688a7f8 2563 list_for_each_entry_rcu(table, &afi->tables, list) {
5b96af77
PNA
2564 if (ctx->table && ctx->table != table)
2565 continue;
2566
c9c8e485
PNA
2567 if (cur_table) {
2568 if (cur_table != table)
2569 continue;
2570
2571 cur_table = NULL;
2572 }
c9c8e485 2573 idx = 0;
5b96af77 2574 list_for_each_entry_rcu(set, &table->sets, list) {
c9c8e485
PNA
2575 if (idx < s_idx)
2576 goto cont;
5b96af77
PNA
2577
2578 ctx_set = *ctx;
2579 ctx_set.table = table;
2580 ctx_set.afi = afi;
2581 if (nf_tables_fill_set(skb, &ctx_set, set,
c9c8e485
PNA
2582 NFT_MSG_NEWSET,
2583 NLM_F_MULTI) < 0) {
2584 cb->args[0] = idx;
2585 cb->args[2] = (unsigned long) table;
2586 cb->args[3] = afi->family;
2587 goto done;
2588 }
38e029f1 2589 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
c9c8e485
PNA
2590cont:
2591 idx++;
2592 }
2593 if (s_idx)
2594 s_idx = 0;
2595 }
2596 }
2597 cb->args[1] = 1;
2598done:
e688a7f8 2599 rcu_read_unlock();
c9c8e485
PNA
2600 return skb->len;
2601}
2602
5b96af77 2603static int nf_tables_dump_sets_done(struct netlink_callback *cb)
20a69341 2604{
5b96af77
PNA
2605 kfree(cb->data);
2606 return 0;
20a69341
PM
2607}
2608
7b8002a1
PNA
2609static int nf_tables_getset(struct net *net, struct sock *nlsk,
2610 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
2611 const struct nlattr * const nla[])
2612{
2613 const struct nft_set *set;
2614 struct nft_ctx ctx;
2615 struct sk_buff *skb2;
c9c8e485 2616 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2617 int err;
2618
01cfa0a4 2619 /* Verify existence before starting dump */
633c9a84 2620 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla);
20a69341
PM
2621 if (err < 0)
2622 return err;
2623
2624 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2625 struct netlink_dump_control c = {
2626 .dump = nf_tables_dump_sets,
5b96af77 2627 .done = nf_tables_dump_sets_done,
20a69341 2628 };
5b96af77
PNA
2629 struct nft_ctx *ctx_dump;
2630
2631 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2632 if (ctx_dump == NULL)
2633 return -ENOMEM;
2634
2635 *ctx_dump = ctx;
2636 c.data = ctx_dump;
2637
20a69341
PM
2638 return netlink_dump_start(nlsk, skb, nlh, &c);
2639 }
2640
c9c8e485
PNA
2641 /* Only accept unspec with dump */
2642 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2643 return -EAFNOSUPPORT;
2644
20a69341
PM
2645 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2646 if (IS_ERR(set))
2647 return PTR_ERR(set);
958bee14
PNA
2648 if (set->flags & NFT_SET_INACTIVE)
2649 return -ENOENT;
20a69341
PM
2650
2651 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2652 if (skb2 == NULL)
2653 return -ENOMEM;
2654
2655 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2656 if (err < 0)
2657 goto err;
2658
2659 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2660
2661err:
2662 kfree_skb(skb2);
2663 return err;
2664}
2665
c50b960c
PM
2666static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2667 struct nft_set_desc *desc,
2668 const struct nlattr *nla)
2669{
2670 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2671 int err;
2672
2673 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2674 if (err < 0)
2675 return err;
2676
2677 if (da[NFTA_SET_DESC_SIZE] != NULL)
2678 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2679
2680 return 0;
2681}
2682
633c9a84
PNA
2683static int nf_tables_newset(struct net *net, struct sock *nlsk,
2684 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
2685 const struct nlattr * const nla[])
2686{
2687 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2688 const struct nft_set_ops *ops;
7c95f6d8 2689 struct nft_af_info *afi;
20a69341
PM
2690 struct nft_table *table;
2691 struct nft_set *set;
2692 struct nft_ctx ctx;
2693 char name[IFNAMSIZ];
2694 unsigned int size;
2695 bool create;
761da293
PM
2696 u64 timeout;
2697 u32 ktype, dtype, flags, policy, gc_int;
c50b960c 2698 struct nft_set_desc desc;
e6d8ecac
CFG
2699 unsigned char *udata;
2700 u16 udlen;
20a69341
PM
2701 int err;
2702
2703 if (nla[NFTA_SET_TABLE] == NULL ||
2704 nla[NFTA_SET_NAME] == NULL ||
958bee14
PNA
2705 nla[NFTA_SET_KEY_LEN] == NULL ||
2706 nla[NFTA_SET_ID] == NULL)
20a69341
PM
2707 return -EINVAL;
2708
c50b960c
PM
2709 memset(&desc, 0, sizeof(desc));
2710
20a69341
PM
2711 ktype = NFT_DATA_VALUE;
2712 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2713 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2714 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2715 return -EINVAL;
2716 }
2717
c50b960c 2718 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
7d740264 2719 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
2720 return -EINVAL;
2721
2722 flags = 0;
2723 if (nla[NFTA_SET_FLAGS] != NULL) {
2724 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2725 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
7c6c6e95
PM
2726 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2727 NFT_SET_MAP | NFT_SET_EVAL))
20a69341 2728 return -EINVAL;
7c6c6e95
PM
2729 /* Only one of both operations is supported */
2730 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) ==
2731 (NFT_SET_MAP | NFT_SET_EVAL))
2732 return -EOPNOTSUPP;
20a69341
PM
2733 }
2734
2735 dtype = 0;
20a69341
PM
2736 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2737 if (!(flags & NFT_SET_MAP))
2738 return -EINVAL;
2739
2740 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2741 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2742 dtype != NFT_DATA_VERDICT)
2743 return -EINVAL;
2744
2745 if (dtype != NFT_DATA_VERDICT) {
2746 if (nla[NFTA_SET_DATA_LEN] == NULL)
2747 return -EINVAL;
c50b960c 2748 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
7d740264 2749 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
20a69341
PM
2750 return -EINVAL;
2751 } else
7d740264 2752 desc.dlen = sizeof(struct nft_verdict);
20a69341
PM
2753 } else if (flags & NFT_SET_MAP)
2754 return -EINVAL;
2755
761da293
PM
2756 timeout = 0;
2757 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2758 if (!(flags & NFT_SET_TIMEOUT))
2759 return -EINVAL;
2760 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2761 }
2762 gc_int = 0;
2763 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2764 if (!(flags & NFT_SET_TIMEOUT))
2765 return -EINVAL;
2766 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2767 }
2768
c50b960c
PM
2769 policy = NFT_SET_POL_PERFORMANCE;
2770 if (nla[NFTA_SET_POLICY] != NULL)
2771 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2772
2773 if (nla[NFTA_SET_DESC] != NULL) {
2774 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2775 if (err < 0)
2776 return err;
2777 }
2778
20a69341
PM
2779 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2780
99633ab2 2781 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
20a69341
PM
2782 if (IS_ERR(afi))
2783 return PTR_ERR(afi);
2784
9370761c 2785 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2786 if (IS_ERR(table))
2787 return PTR_ERR(table);
2788
633c9a84 2789 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2790
2791 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2792 if (IS_ERR(set)) {
2793 if (PTR_ERR(set) != -ENOENT)
2794 return PTR_ERR(set);
2795 set = NULL;
2796 }
2797
2798 if (set != NULL) {
2799 if (nlh->nlmsg_flags & NLM_F_EXCL)
2800 return -EEXIST;
2801 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2802 return -EOPNOTSUPP;
2803 return 0;
2804 }
2805
2806 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2807 return -ENOENT;
2808
c50b960c 2809 ops = nft_select_set_ops(nla, &desc, policy);
20a69341
PM
2810 if (IS_ERR(ops))
2811 return PTR_ERR(ops);
2812
e6d8ecac
CFG
2813 udlen = 0;
2814 if (nla[NFTA_SET_USERDATA])
2815 udlen = nla_len(nla[NFTA_SET_USERDATA]);
2816
20a69341
PM
2817 size = 0;
2818 if (ops->privsize != NULL)
2819 size = ops->privsize(nla);
2820
2821 err = -ENOMEM;
e6d8ecac 2822 set = kzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
20a69341
PM
2823 if (set == NULL)
2824 goto err1;
2825
2826 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2827 err = nf_tables_set_alloc_name(&ctx, set, name);
2828 if (err < 0)
2829 goto err2;
2830
e6d8ecac
CFG
2831 udata = NULL;
2832 if (udlen) {
2833 udata = set->data + size;
2834 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
2835 }
2836
20a69341 2837 INIT_LIST_HEAD(&set->bindings);
cc02e457 2838 write_pnet(&set->pnet, net);
20a69341
PM
2839 set->ops = ops;
2840 set->ktype = ktype;
c50b960c 2841 set->klen = desc.klen;
20a69341 2842 set->dtype = dtype;
c50b960c 2843 set->dlen = desc.dlen;
20a69341 2844 set->flags = flags;
c50b960c 2845 set->size = desc.size;
9363dc4b 2846 set->policy = policy;
e6d8ecac
CFG
2847 set->udlen = udlen;
2848 set->udata = udata;
761da293
PM
2849 set->timeout = timeout;
2850 set->gc_int = gc_int;
20a69341 2851
c50b960c 2852 err = ops->init(set, &desc, nla);
20a69341
PM
2853 if (err < 0)
2854 goto err2;
2855
958bee14 2856 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
20a69341
PM
2857 if (err < 0)
2858 goto err2;
2859
e688a7f8 2860 list_add_tail_rcu(&set->list, &table->sets);
4fefee57 2861 table->use++;
20a69341
PM
2862 return 0;
2863
2864err2:
2865 kfree(set);
2866err1:
2867 module_put(ops->owner);
2868 return err;
2869}
2870
958bee14 2871static void nft_set_destroy(struct nft_set *set)
20a69341 2872{
20a69341
PM
2873 set->ops->destroy(set);
2874 module_put(set->ops->owner);
2875 kfree(set);
2876}
2877
958bee14
PNA
2878static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2879{
e688a7f8 2880 list_del_rcu(&set->list);
31f8441c 2881 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
958bee14
PNA
2882 nft_set_destroy(set);
2883}
2884
633c9a84
PNA
2885static int nf_tables_delset(struct net *net, struct sock *nlsk,
2886 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
2887 const struct nlattr * const nla[])
2888{
c9c8e485 2889 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2890 struct nft_set *set;
2891 struct nft_ctx ctx;
2892 int err;
2893
ec2c9935
PM
2894 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2895 return -EAFNOSUPPORT;
20a69341
PM
2896 if (nla[NFTA_SET_TABLE] == NULL)
2897 return -EINVAL;
2898
633c9a84 2899 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla);
20a69341
PM
2900 if (err < 0)
2901 return err;
2902
2903 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2904 if (IS_ERR(set))
2905 return PTR_ERR(set);
2906 if (!list_empty(&set->bindings))
2907 return -EBUSY;
2908
ee01d542 2909 return nft_delset(&ctx, set);
20a69341
PM
2910}
2911
2912static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2913 const struct nft_set *set,
2914 const struct nft_set_iter *iter,
2915 const struct nft_set_elem *elem)
2916{
fe2811eb 2917 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
2918 enum nft_registers dreg;
2919
2920 dreg = nft_type_to_reg(set->dtype);
1ec10212
PM
2921 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
2922 set->dtype == NFT_DATA_VERDICT ?
2923 NFT_DATA_VERDICT : NFT_DATA_VALUE,
2924 set->dlen);
20a69341
PM
2925}
2926
2927int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2928 struct nft_set_binding *binding)
2929{
2930 struct nft_set_binding *i;
2931 struct nft_set_iter iter;
2932
2933 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2934 return -EBUSY;
2935
11113e19 2936 if (binding->flags & NFT_SET_MAP) {
20a69341
PM
2937 /* If the set is already bound to the same chain all
2938 * jumps are already validated for that chain.
2939 */
2940 list_for_each_entry(i, &set->bindings, list) {
11113e19
PM
2941 if (binding->flags & NFT_SET_MAP &&
2942 i->chain == binding->chain)
20a69341
PM
2943 goto bind;
2944 }
2945
2946 iter.skip = 0;
2947 iter.count = 0;
2948 iter.err = 0;
2949 iter.fn = nf_tables_bind_check_setelem;
2950
2951 set->ops->walk(ctx, set, &iter);
2952 if (iter.err < 0) {
2953 /* Destroy anonymous sets if binding fails */
2954 if (set->flags & NFT_SET_ANONYMOUS)
2955 nf_tables_set_destroy(ctx, set);
2956
2957 return iter.err;
2958 }
2959 }
2960bind:
2961 binding->chain = ctx->chain;
e688a7f8 2962 list_add_tail_rcu(&binding->list, &set->bindings);
20a69341
PM
2963 return 0;
2964}
2965
2966void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2967 struct nft_set_binding *binding)
2968{
e688a7f8 2969 list_del_rcu(&binding->list);
20a69341 2970
958bee14
PNA
2971 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2972 !(set->flags & NFT_SET_INACTIVE))
20a69341
PM
2973 nf_tables_set_destroy(ctx, set);
2974}
2975
3ac4c07a
PM
2976const struct nft_set_ext_type nft_set_ext_types[] = {
2977 [NFT_SET_EXT_KEY] = {
7d740264 2978 .align = __alignof__(u32),
3ac4c07a
PM
2979 },
2980 [NFT_SET_EXT_DATA] = {
7d740264 2981 .align = __alignof__(u32),
3ac4c07a 2982 },
f25ad2e9
PM
2983 [NFT_SET_EXT_EXPR] = {
2984 .align = __alignof__(struct nft_expr),
2985 },
3ac4c07a
PM
2986 [NFT_SET_EXT_FLAGS] = {
2987 .len = sizeof(u8),
2988 .align = __alignof__(u8),
2989 },
c3e1b005
PM
2990 [NFT_SET_EXT_TIMEOUT] = {
2991 .len = sizeof(u64),
2992 .align = __alignof__(u64),
2993 },
2994 [NFT_SET_EXT_EXPIRATION] = {
2995 .len = sizeof(unsigned long),
2996 .align = __alignof__(unsigned long),
2997 },
68e942e8
PM
2998 [NFT_SET_EXT_USERDATA] = {
2999 .len = sizeof(struct nft_userdata),
3000 .align = __alignof__(struct nft_userdata),
3001 },
3ac4c07a
PM
3002};
3003EXPORT_SYMBOL_GPL(nft_set_ext_types);
3004
20a69341
PM
3005/*
3006 * Set elements
3007 */
3008
3009static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3010 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
3011 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
3012 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
c3e1b005 3013 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
68e942e8
PM
3014 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
3015 .len = NFT_USERDATA_MAXLEN },
20a69341
PM
3016};
3017
3018static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3019 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
3020 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
3021 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
958bee14 3022 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
20a69341
PM
3023};
3024
633c9a84 3025static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
20a69341
PM
3026 const struct sk_buff *skb,
3027 const struct nlmsghdr *nlh,
f4c756b4 3028 const struct nlattr * const nla[])
20a69341
PM
3029{
3030 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8
PNA
3031 struct nft_af_info *afi;
3032 struct nft_table *table;
20a69341 3033
99633ab2 3034 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
20a69341
PM
3035 if (IS_ERR(afi))
3036 return PTR_ERR(afi);
3037
9370761c 3038 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
20a69341
PM
3039 if (IS_ERR(table))
3040 return PTR_ERR(table);
3041
633c9a84 3042 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
20a69341
PM
3043 return 0;
3044}
3045
3046static int nf_tables_fill_setelem(struct sk_buff *skb,
3047 const struct nft_set *set,
3048 const struct nft_set_elem *elem)
3049{
fe2811eb 3050 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
20a69341
PM
3051 unsigned char *b = skb_tail_pointer(skb);
3052 struct nlattr *nest;
3053
3054 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
3055 if (nest == NULL)
3056 goto nla_put_failure;
3057
fe2811eb
PM
3058 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3059 NFT_DATA_VALUE, set->klen) < 0)
20a69341
PM
3060 goto nla_put_failure;
3061
fe2811eb
PM
3062 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3063 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
20a69341
PM
3064 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3065 set->dlen) < 0)
3066 goto nla_put_failure;
3067
f25ad2e9
PM
3068 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3069 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3070 goto nla_put_failure;
3071
fe2811eb
PM
3072 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3073 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3074 htonl(*nft_set_ext_flags(ext))))
3075 goto nla_put_failure;
20a69341 3076
c3e1b005
PM
3077 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3078 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3079 cpu_to_be64(*nft_set_ext_timeout(ext))))
3080 goto nla_put_failure;
3081
3082 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3083 unsigned long expires, now = jiffies;
3084
3085 expires = *nft_set_ext_expiration(ext);
3086 if (time_before(now, expires))
3087 expires -= now;
3088 else
3089 expires = 0;
3090
3091 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3092 cpu_to_be64(jiffies_to_msecs(expires))))
3093 goto nla_put_failure;
3094 }
3095
68e942e8
PM
3096 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3097 struct nft_userdata *udata;
3098
3099 udata = nft_set_ext_userdata(ext);
3100 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3101 udata->len + 1, udata->data))
3102 goto nla_put_failure;
3103 }
3104
20a69341
PM
3105 nla_nest_end(skb, nest);
3106 return 0;
3107
3108nla_put_failure:
3109 nlmsg_trim(skb, b);
3110 return -EMSGSIZE;
3111}
3112
3113struct nft_set_dump_args {
3114 const struct netlink_callback *cb;
3115 struct nft_set_iter iter;
3116 struct sk_buff *skb;
3117};
3118
3119static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3120 const struct nft_set *set,
3121 const struct nft_set_iter *iter,
3122 const struct nft_set_elem *elem)
3123{
3124 struct nft_set_dump_args *args;
3125
3126 args = container_of(iter, struct nft_set_dump_args, iter);
3127 return nf_tables_fill_setelem(args->skb, set, elem);
3128}
3129
3130static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3131{
633c9a84 3132 struct net *net = sock_net(skb->sk);
20a69341
PM
3133 const struct nft_set *set;
3134 struct nft_set_dump_args args;
3135 struct nft_ctx ctx;
3136 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
3137 struct nfgenmsg *nfmsg;
3138 struct nlmsghdr *nlh;
3139 struct nlattr *nest;
3140 u32 portid, seq;
3141 int event, err;
3142
720e0dfa
MN
3143 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
3144 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
20a69341
PM
3145 if (err < 0)
3146 return err;
3147
633c9a84 3148 err = nft_ctx_init_from_elemattr(&ctx, net, cb->skb, cb->nlh,
f4c756b4 3149 (void *)nla);
20a69341
PM
3150 if (err < 0)
3151 return err;
f4c756b4
PNA
3152 if (ctx.table->flags & NFT_TABLE_INACTIVE)
3153 return -ENOENT;
20a69341
PM
3154
3155 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3156 if (IS_ERR(set))
3157 return PTR_ERR(set);
958bee14
PNA
3158 if (set->flags & NFT_SET_INACTIVE)
3159 return -ENOENT;
20a69341
PM
3160
3161 event = NFT_MSG_NEWSETELEM;
3162 event |= NFNL_SUBSYS_NFTABLES << 8;
3163 portid = NETLINK_CB(cb->skb).portid;
3164 seq = cb->nlh->nlmsg_seq;
3165
3166 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3167 NLM_F_MULTI);
3168 if (nlh == NULL)
3169 goto nla_put_failure;
3170
3171 nfmsg = nlmsg_data(nlh);
6403d962 3172 nfmsg->nfgen_family = ctx.afi->family;
20a69341 3173 nfmsg->version = NFNETLINK_V0;
84d7fce6 3174 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
20a69341
PM
3175
3176 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
3177 goto nla_put_failure;
3178 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3179 goto nla_put_failure;
3180
3181 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3182 if (nest == NULL)
3183 goto nla_put_failure;
3184
3185 args.cb = cb;
3186 args.skb = skb;
3187 args.iter.skip = cb->args[0];
3188 args.iter.count = 0;
3189 args.iter.err = 0;
3190 args.iter.fn = nf_tables_dump_setelem;
3191 set->ops->walk(&ctx, set, &args.iter);
3192
3193 nla_nest_end(skb, nest);
3194 nlmsg_end(skb, nlh);
3195
3196 if (args.iter.err && args.iter.err != -EMSGSIZE)
3197 return args.iter.err;
3198 if (args.iter.count == cb->args[0])
3199 return 0;
3200
3201 cb->args[0] = args.iter.count;
3202 return skb->len;
3203
3204nla_put_failure:
3205 return -ENOSPC;
3206}
3207
7b8002a1
PNA
3208static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
3209 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
3210 const struct nlattr * const nla[])
3211{
3212 const struct nft_set *set;
3213 struct nft_ctx ctx;
3214 int err;
3215
f4c756b4 3216 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla);
20a69341
PM
3217 if (err < 0)
3218 return err;
f4c756b4
PNA
3219 if (ctx.table->flags & NFT_TABLE_INACTIVE)
3220 return -ENOENT;
20a69341
PM
3221
3222 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3223 if (IS_ERR(set))
3224 return PTR_ERR(set);
958bee14
PNA
3225 if (set->flags & NFT_SET_INACTIVE)
3226 return -ENOENT;
20a69341
PM
3227
3228 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3229 struct netlink_dump_control c = {
3230 .dump = nf_tables_dump_set,
3231 };
3232 return netlink_dump_start(nlsk, skb, nlh, &c);
3233 }
3234 return -EOPNOTSUPP;
3235}
3236
d60ce62f
AB
3237static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3238 const struct nft_ctx *ctx, u32 seq,
3239 u32 portid, int event, u16 flags,
3240 const struct nft_set *set,
3241 const struct nft_set_elem *elem)
3242{
3243 struct nfgenmsg *nfmsg;
3244 struct nlmsghdr *nlh;
3245 struct nlattr *nest;
3246 int err;
3247
3248 event |= NFNL_SUBSYS_NFTABLES << 8;
3249 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3250 flags);
3251 if (nlh == NULL)
3252 goto nla_put_failure;
3253
3254 nfmsg = nlmsg_data(nlh);
3255 nfmsg->nfgen_family = ctx->afi->family;
3256 nfmsg->version = NFNETLINK_V0;
84d7fce6 3257 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
d60ce62f
AB
3258
3259 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3260 goto nla_put_failure;
3261 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3262 goto nla_put_failure;
3263
3264 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3265 if (nest == NULL)
3266 goto nla_put_failure;
3267
3268 err = nf_tables_fill_setelem(skb, set, elem);
3269 if (err < 0)
3270 goto nla_put_failure;
3271
3272 nla_nest_end(skb, nest);
3273
053c095a
JB
3274 nlmsg_end(skb, nlh);
3275 return 0;
d60ce62f
AB
3276
3277nla_put_failure:
3278 nlmsg_trim(skb, nlh);
3279 return -1;
3280}
3281
3282static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3283 const struct nft_set *set,
3284 const struct nft_set_elem *elem,
3285 int event, u16 flags)
3286{
128ad332
PNA
3287 struct net *net = ctx->net;
3288 u32 portid = ctx->portid;
d60ce62f
AB
3289 struct sk_buff *skb;
3290 int err;
3291
128ad332 3292 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
d60ce62f
AB
3293 return 0;
3294
3295 err = -ENOBUFS;
3296 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3297 if (skb == NULL)
3298 goto err;
3299
3300 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3301 set, elem);
3302 if (err < 0) {
3303 kfree_skb(skb);
3304 goto err;
3305 }
3306
128ad332 3307 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
d60ce62f
AB
3308 GFP_KERNEL);
3309err:
3310 if (err < 0)
3311 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3312 return err;
3313}
3314
60319eb1
PNA
3315static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3316 int msg_type,
3317 struct nft_set *set)
3318{
3319 struct nft_trans *trans;
3320
3321 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3322 if (trans == NULL)
3323 return NULL;
3324
3325 nft_trans_elem_set(trans) = set;
3326 return trans;
3327}
3328
22fe54d5
PM
3329void *nft_set_elem_init(const struct nft_set *set,
3330 const struct nft_set_ext_tmpl *tmpl,
49499c3e 3331 const u32 *key, const u32 *data,
22fe54d5 3332 u64 timeout, gfp_t gfp)
fe2811eb
PM
3333{
3334 struct nft_set_ext *ext;
3335 void *elem;
3336
3337 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3338 if (elem == NULL)
3339 return NULL;
3340
3341 ext = nft_set_elem_ext(set, elem);
3342 nft_set_ext_init(ext, tmpl);
3343
3344 memcpy(nft_set_ext_key(ext), key, set->klen);
3345 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3346 memcpy(nft_set_ext_data(ext), data, set->dlen);
c3e1b005
PM
3347 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3348 *nft_set_ext_expiration(ext) =
3349 jiffies + msecs_to_jiffies(timeout);
3350 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3351 *nft_set_ext_timeout(ext) = timeout;
fe2811eb
PM
3352
3353 return elem;
3354}
3355
61edafbb
PM
3356void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3357{
3358 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3359
3360 nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3361 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3362 nft_data_uninit(nft_set_ext_data(ext), set->dtype);
f25ad2e9
PM
3363 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3364 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
61edafbb
PM
3365
3366 kfree(elem);
3367}
3368EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3369
60319eb1 3370static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
3371 const struct nlattr *attr)
3372{
3373 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3374 struct nft_data_desc d1, d2;
fe2811eb
PM
3375 struct nft_set_ext_tmpl tmpl;
3376 struct nft_set_ext *ext;
20a69341
PM
3377 struct nft_set_elem elem;
3378 struct nft_set_binding *binding;
68e942e8 3379 struct nft_userdata *udata;
fe2811eb 3380 struct nft_data data;
20a69341 3381 enum nft_registers dreg;
60319eb1 3382 struct nft_trans *trans;
c3e1b005 3383 u64 timeout;
fe2811eb 3384 u32 flags;
68e942e8 3385 u8 ulen;
20a69341
PM
3386 int err;
3387
3388 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3389 nft_set_elem_policy);
3390 if (err < 0)
3391 return err;
3392
3393 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3394 return -EINVAL;
3395
fe2811eb
PM
3396 nft_set_ext_prepare(&tmpl);
3397
3398 flags = 0;
20a69341 3399 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
fe2811eb
PM
3400 flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3401 if (flags & ~NFT_SET_ELEM_INTERVAL_END)
20a69341 3402 return -EINVAL;
55df35d2 3403 if (!(set->flags & NFT_SET_INTERVAL) &&
fe2811eb 3404 flags & NFT_SET_ELEM_INTERVAL_END)
55df35d2 3405 return -EINVAL;
fe2811eb
PM
3406 if (flags != 0)
3407 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
20a69341
PM
3408 }
3409
3410 if (set->flags & NFT_SET_MAP) {
3411 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
fe2811eb 3412 !(flags & NFT_SET_ELEM_INTERVAL_END))
20a69341 3413 return -EINVAL;
bd7fc645 3414 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
fe2811eb 3415 flags & NFT_SET_ELEM_INTERVAL_END)
bd7fc645 3416 return -EINVAL;
20a69341
PM
3417 } else {
3418 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3419 return -EINVAL;
3420 }
3421
c3e1b005
PM
3422 timeout = 0;
3423 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3424 if (!(set->flags & NFT_SET_TIMEOUT))
3425 return -EINVAL;
3426 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3427 } else if (set->flags & NFT_SET_TIMEOUT) {
3428 timeout = set->timeout;
3429 }
3430
7d740264 3431 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
d0a11fc3 3432 nla[NFTA_SET_ELEM_KEY]);
20a69341
PM
3433 if (err < 0)
3434 goto err1;
3435 err = -EINVAL;
3436 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3437 goto err2;
3438
7d740264 3439 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
c3e1b005
PM
3440 if (timeout > 0) {
3441 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3442 if (timeout != set->timeout)
3443 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3444 }
fe2811eb 3445
20a69341 3446 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
d0a11fc3
PM
3447 err = nft_data_init(ctx, &data, sizeof(data), &d2,
3448 nla[NFTA_SET_ELEM_DATA]);
20a69341
PM
3449 if (err < 0)
3450 goto err2;
3451
3452 err = -EINVAL;
3453 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3454 goto err3;
3455
3456 dreg = nft_type_to_reg(set->dtype);
3457 list_for_each_entry(binding, &set->bindings, list) {
3458 struct nft_ctx bind_ctx = {
3459 .afi = ctx->afi,
3460 .table = ctx->table,
7c95f6d8 3461 .chain = (struct nft_chain *)binding->chain,
20a69341
PM
3462 };
3463
11113e19
PM
3464 if (!(binding->flags & NFT_SET_MAP))
3465 continue;
3466
1ec10212
PM
3467 err = nft_validate_register_store(&bind_ctx, dreg,
3468 &data,
3469 d2.type, d2.len);
20a69341
PM
3470 if (err < 0)
3471 goto err3;
3472 }
fe2811eb 3473
7d740264 3474 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
20a69341
PM
3475 }
3476
68e942e8
PM
3477 /* The full maximum length of userdata can exceed the maximum
3478 * offset value (U8_MAX) for following extensions, therefor it
3479 * must be the last extension added.
3480 */
3481 ulen = 0;
3482 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3483 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3484 if (ulen > 0)
3485 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3486 ulen);
3487 }
3488
fe2811eb 3489 err = -ENOMEM;
7d740264 3490 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
c3e1b005 3491 timeout, GFP_KERNEL);
fe2811eb
PM
3492 if (elem.priv == NULL)
3493 goto err3;
3494
3495 ext = nft_set_elem_ext(set, elem.priv);
3496 if (flags)
3497 *nft_set_ext_flags(ext) = flags;
68e942e8
PM
3498 if (ulen > 0) {
3499 udata = nft_set_ext_userdata(ext);
3500 udata->len = ulen - 1;
3501 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3502 }
fe2811eb 3503
60319eb1
PNA
3504 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3505 if (trans == NULL)
fe2811eb 3506 goto err4;
60319eb1 3507
69086658 3508 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
20a69341
PM
3509 err = set->ops->insert(set, &elem);
3510 if (err < 0)
fe2811eb 3511 goto err5;
20a69341 3512
60319eb1 3513 nft_trans_elem(trans) = elem;
46bbafce 3514 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
20a69341
PM
3515 return 0;
3516
fe2811eb 3517err5:
60319eb1 3518 kfree(trans);
fe2811eb
PM
3519err4:
3520 kfree(elem.priv);
20a69341
PM
3521err3:
3522 if (nla[NFTA_SET_ELEM_DATA] != NULL)
fe2811eb 3523 nft_data_uninit(&data, d2.type);
20a69341 3524err2:
7d740264 3525 nft_data_uninit(&elem.key.val, d1.type);
20a69341
PM
3526err1:
3527 return err;
3528}
3529
633c9a84
PNA
3530static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
3531 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
3532 const struct nlattr * const nla[])
3533{
3534 const struct nlattr *attr;
3535 struct nft_set *set;
3536 struct nft_ctx ctx;
60319eb1 3537 int rem, err = 0;
20a69341 3538
7d5570ca
PNA
3539 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3540 return -EINVAL;
3541
f4c756b4 3542 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla);
20a69341
PM
3543 if (err < 0)
3544 return err;
3545
3546 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
958bee14
PNA
3547 if (IS_ERR(set)) {
3548 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3549 set = nf_tables_set_lookup_byid(net,
3550 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3551 }
3552 if (IS_ERR(set))
3553 return PTR_ERR(set);
3554 }
3555
20a69341
PM
3556 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3557 return -EBUSY;
3558
3559 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3dd0673a
PM
3560 if (set->size &&
3561 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3562 return -ENFILE;
3563
20a69341 3564 err = nft_add_set_elem(&ctx, set, attr);
3dd0673a
PM
3565 if (err < 0) {
3566 atomic_dec(&set->nelems);
60319eb1 3567 break;
3dd0673a 3568 }
20a69341 3569 }
60319eb1 3570 return err;
20a69341
PM
3571}
3572
60319eb1 3573static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
3574 const struct nlattr *attr)
3575{
3576 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3577 struct nft_data_desc desc;
3578 struct nft_set_elem elem;
60319eb1 3579 struct nft_trans *trans;
20a69341
PM
3580 int err;
3581
3582 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3583 nft_set_elem_policy);
3584 if (err < 0)
3585 goto err1;
3586
3587 err = -EINVAL;
3588 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3589 goto err1;
3590
7d740264 3591 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
d0a11fc3 3592 nla[NFTA_SET_ELEM_KEY]);
20a69341
PM
3593 if (err < 0)
3594 goto err1;
3595
3596 err = -EINVAL;
3597 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3598 goto err2;
3599
60319eb1 3600 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
609ccf08
JL
3601 if (trans == NULL) {
3602 err = -ENOMEM;
60319eb1 3603 goto err2;
609ccf08 3604 }
20a69341 3605
cc02e457
PM
3606 elem.priv = set->ops->deactivate(set, &elem);
3607 if (elem.priv == NULL) {
3608 err = -ENOENT;
3609 goto err3;
3610 }
3611
60319eb1 3612 nft_trans_elem(trans) = elem;
46bbafce 3613 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
0dc13625 3614 return 0;
cc02e457
PM
3615
3616err3:
3617 kfree(trans);
20a69341 3618err2:
7d740264 3619 nft_data_uninit(&elem.key.val, desc.type);
20a69341
PM
3620err1:
3621 return err;
3622}
3623
633c9a84
PNA
3624static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
3625 struct sk_buff *skb, const struct nlmsghdr *nlh,
20a69341
PM
3626 const struct nlattr * const nla[])
3627{
3628 const struct nlattr *attr;
3629 struct nft_set *set;
3630 struct nft_ctx ctx;
60319eb1 3631 int rem, err = 0;
20a69341 3632
7d5570ca
PNA
3633 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3634 return -EINVAL;
3635
f4c756b4 3636 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla);
20a69341
PM
3637 if (err < 0)
3638 return err;
3639
3640 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3641 if (IS_ERR(set))
3642 return PTR_ERR(set);
3643 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3644 return -EBUSY;
3645
3646 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3647 err = nft_del_setelem(&ctx, set, attr);
3648 if (err < 0)
60319eb1 3649 break;
4fefee57 3650
3dd0673a 3651 set->ndeact++;
20a69341 3652 }
60319eb1 3653 return err;
20a69341
PM
3654}
3655
cfed7e1b
PM
3656void nft_set_gc_batch_release(struct rcu_head *rcu)
3657{
3658 struct nft_set_gc_batch *gcb;
3659 unsigned int i;
3660
3661 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3662 for (i = 0; i < gcb->head.cnt; i++)
3663 nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3664 kfree(gcb);
3665}
3666EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3667
3668struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3669 gfp_t gfp)
3670{
3671 struct nft_set_gc_batch *gcb;
3672
3673 gcb = kzalloc(sizeof(*gcb), gfp);
3674 if (gcb == NULL)
3675 return gcb;
3676 gcb->head.set = set;
3677 return gcb;
3678}
3679EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3680
84d7fce6
PNA
3681static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3682 u32 portid, u32 seq)
3683{
3684 struct nlmsghdr *nlh;
3685 struct nfgenmsg *nfmsg;
3686 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3687
3688 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3689 if (nlh == NULL)
3690 goto nla_put_failure;
3691
3692 nfmsg = nlmsg_data(nlh);
3693 nfmsg->nfgen_family = AF_UNSPEC;
3694 nfmsg->version = NFNETLINK_V0;
3695 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3696
3697 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3698 goto nla_put_failure;
3699
053c095a
JB
3700 nlmsg_end(skb, nlh);
3701 return 0;
84d7fce6
PNA
3702
3703nla_put_failure:
3704 nlmsg_trim(skb, nlh);
3705 return -EMSGSIZE;
3706}
3707
3708static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3709{
3710 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3711 struct sk_buff *skb2;
3712 int err;
3713
3714 if (nlmsg_report(nlh) &&
3715 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3716 return 0;
3717
3718 err = -ENOBUFS;
3719 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3720 if (skb2 == NULL)
3721 goto err;
3722
3723 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3724 nlh->nlmsg_seq);
3725 if (err < 0) {
3726 kfree_skb(skb2);
3727 goto err;
3728 }
3729
3730 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3731 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3732err:
3733 if (err < 0) {
3734 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3735 err);
3736 }
3737 return err;
3738}
3739
7b8002a1
PNA
3740static int nf_tables_getgen(struct net *net, struct sock *nlsk,
3741 struct sk_buff *skb, const struct nlmsghdr *nlh,
84d7fce6
PNA
3742 const struct nlattr * const nla[])
3743{
84d7fce6
PNA
3744 struct sk_buff *skb2;
3745 int err;
3746
3747 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3748 if (skb2 == NULL)
3749 return -ENOMEM;
3750
3751 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3752 nlh->nlmsg_seq);
3753 if (err < 0)
3754 goto err;
3755
3756 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3757err:
3758 kfree_skb(skb2);
3759 return err;
3760}
3761
96518518
PM
3762static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3763 [NFT_MSG_NEWTABLE] = {
55dd6f93 3764 .call_batch = nf_tables_newtable,
96518518
PM
3765 .attr_count = NFTA_TABLE_MAX,
3766 .policy = nft_table_policy,
3767 },
3768 [NFT_MSG_GETTABLE] = {
3769 .call = nf_tables_gettable,
3770 .attr_count = NFTA_TABLE_MAX,
3771 .policy = nft_table_policy,
3772 },
3773 [NFT_MSG_DELTABLE] = {
55dd6f93 3774 .call_batch = nf_tables_deltable,
96518518
PM
3775 .attr_count = NFTA_TABLE_MAX,
3776 .policy = nft_table_policy,
3777 },
3778 [NFT_MSG_NEWCHAIN] = {
91c7b38d 3779 .call_batch = nf_tables_newchain,
96518518
PM
3780 .attr_count = NFTA_CHAIN_MAX,
3781 .policy = nft_chain_policy,
3782 },
3783 [NFT_MSG_GETCHAIN] = {
3784 .call = nf_tables_getchain,
3785 .attr_count = NFTA_CHAIN_MAX,
3786 .policy = nft_chain_policy,
3787 },
3788 [NFT_MSG_DELCHAIN] = {
91c7b38d 3789 .call_batch = nf_tables_delchain,
96518518
PM
3790 .attr_count = NFTA_CHAIN_MAX,
3791 .policy = nft_chain_policy,
3792 },
3793 [NFT_MSG_NEWRULE] = {
0628b123 3794 .call_batch = nf_tables_newrule,
96518518
PM
3795 .attr_count = NFTA_RULE_MAX,
3796 .policy = nft_rule_policy,
3797 },
3798 [NFT_MSG_GETRULE] = {
3799 .call = nf_tables_getrule,
3800 .attr_count = NFTA_RULE_MAX,
3801 .policy = nft_rule_policy,
3802 },
3803 [NFT_MSG_DELRULE] = {
0628b123 3804 .call_batch = nf_tables_delrule,
96518518
PM
3805 .attr_count = NFTA_RULE_MAX,
3806 .policy = nft_rule_policy,
3807 },
20a69341 3808 [NFT_MSG_NEWSET] = {
958bee14 3809 .call_batch = nf_tables_newset,
20a69341
PM
3810 .attr_count = NFTA_SET_MAX,
3811 .policy = nft_set_policy,
3812 },
3813 [NFT_MSG_GETSET] = {
3814 .call = nf_tables_getset,
3815 .attr_count = NFTA_SET_MAX,
3816 .policy = nft_set_policy,
3817 },
3818 [NFT_MSG_DELSET] = {
958bee14 3819 .call_batch = nf_tables_delset,
20a69341
PM
3820 .attr_count = NFTA_SET_MAX,
3821 .policy = nft_set_policy,
3822 },
3823 [NFT_MSG_NEWSETELEM] = {
958bee14 3824 .call_batch = nf_tables_newsetelem,
20a69341
PM
3825 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3826 .policy = nft_set_elem_list_policy,
3827 },
3828 [NFT_MSG_GETSETELEM] = {
3829 .call = nf_tables_getsetelem,
3830 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3831 .policy = nft_set_elem_list_policy,
3832 },
3833 [NFT_MSG_DELSETELEM] = {
958bee14 3834 .call_batch = nf_tables_delsetelem,
20a69341
PM
3835 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3836 .policy = nft_set_elem_list_policy,
3837 },
84d7fce6
PNA
3838 [NFT_MSG_GETGEN] = {
3839 .call = nf_tables_getgen,
3840 },
96518518
PM
3841};
3842
91c7b38d
PNA
3843static void nft_chain_commit_update(struct nft_trans *trans)
3844{
3845 struct nft_base_chain *basechain;
3846
3847 if (nft_trans_chain_name(trans)[0])
3848 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3849
3850 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3851 return;
3852
3853 basechain = nft_base_chain(trans->ctx.chain);
3854 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3855
3856 switch (nft_trans_chain_policy(trans)) {
3857 case NF_DROP:
3858 case NF_ACCEPT:
3859 basechain->policy = nft_trans_chain_policy(trans);
3860 break;
3861 }
3862}
3863
b326dd37 3864static void nf_tables_commit_release(struct nft_trans *trans)
c7c32e72 3865{
c7c32e72
PNA
3866 switch (trans->msg_type) {
3867 case NFT_MSG_DELTABLE:
3868 nf_tables_table_destroy(&trans->ctx);
3869 break;
3870 case NFT_MSG_DELCHAIN:
3871 nf_tables_chain_destroy(trans->ctx.chain);
3872 break;
3873 case NFT_MSG_DELRULE:
3874 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3875 break;
3876 case NFT_MSG_DELSET:
3877 nft_set_destroy(nft_trans_set(trans));
3878 break;
61edafbb
PM
3879 case NFT_MSG_DELSETELEM:
3880 nft_set_elem_destroy(nft_trans_elem_set(trans),
3881 nft_trans_elem(trans).priv);
3882 break;
c7c32e72
PNA
3883 }
3884 kfree(trans);
3885}
3886
5913beaf 3887static int nf_tables_commit(struct net *net, struct sk_buff *skb)
37082f93 3888{
37082f93 3889 struct nft_trans *trans, *next;
a3716e70 3890 struct nft_trans_elem *te;
37082f93
PNA
3891
3892 /* Bump generation counter, invalidate any dump in progress */
38e029f1 3893 while (++net->nft.base_seq == 0);
37082f93
PNA
3894
3895 /* A new generation has just started */
ea4bd995 3896 net->nft.gencursor = nft_gencursor_next(net);
37082f93
PNA
3897
3898 /* Make sure all packets have left the previous generation before
3899 * purging old rules.
3900 */
3901 synchronize_rcu();
3902
3903 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
b380e5c7 3904 switch (trans->msg_type) {
55dd6f93
PNA
3905 case NFT_MSG_NEWTABLE:
3906 if (nft_trans_table_update(trans)) {
3907 if (!nft_trans_table_enable(trans)) {
3908 nf_tables_table_disable(trans->ctx.afi,
3909 trans->ctx.table);
3910 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3911 }
3912 } else {
3913 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3914 }
35151d84 3915 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
55dd6f93
PNA
3916 nft_trans_destroy(trans);
3917 break;
3918 case NFT_MSG_DELTABLE:
35151d84 3919 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
55dd6f93 3920 break;
91c7b38d
PNA
3921 case NFT_MSG_NEWCHAIN:
3922 if (nft_trans_chain_update(trans))
3923 nft_chain_commit_update(trans);
4fefee57 3924 else
91c7b38d 3925 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
4fefee57 3926
35151d84 3927 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
91c7b38d
PNA
3928 nft_trans_destroy(trans);
3929 break;
3930 case NFT_MSG_DELCHAIN:
35151d84 3931 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
c5598794
AB
3932 nf_tables_unregister_hooks(trans->ctx.table,
3933 trans->ctx.chain,
3934 trans->ctx.afi->nops);
91c7b38d 3935 break;
b380e5c7
PNA
3936 case NFT_MSG_NEWRULE:
3937 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
35151d84 3938 nf_tables_rule_notify(&trans->ctx,
37082f93 3939 nft_trans_rule(trans),
35151d84 3940 NFT_MSG_NEWRULE);
37082f93 3941 nft_trans_destroy(trans);
b380e5c7
PNA
3942 break;
3943 case NFT_MSG_DELRULE:
3944 list_del_rcu(&nft_trans_rule(trans)->list);
35151d84
PNA
3945 nf_tables_rule_notify(&trans->ctx,
3946 nft_trans_rule(trans),
3947 NFT_MSG_DELRULE);
b380e5c7 3948 break;
958bee14
PNA
3949 case NFT_MSG_NEWSET:
3950 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
4fefee57
PNA
3951 /* This avoids hitting -EBUSY when deleting the table
3952 * from the transaction.
3953 */
3954 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3955 !list_empty(&nft_trans_set(trans)->bindings))
3956 trans->ctx.table->use--;
3957
958bee14 3958 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 3959 NFT_MSG_NEWSET, GFP_KERNEL);
958bee14
PNA
3960 nft_trans_destroy(trans);
3961 break;
3962 case NFT_MSG_DELSET:
3963 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 3964 NFT_MSG_DELSET, GFP_KERNEL);
958bee14 3965 break;
60319eb1 3966 case NFT_MSG_NEWSETELEM:
cc02e457
PM
3967 te = (struct nft_trans_elem *)trans->data;
3968
3969 te->set->ops->activate(te->set, &te->elem);
3970 nf_tables_setelem_notify(&trans->ctx, te->set,
3971 &te->elem,
60319eb1
PNA
3972 NFT_MSG_NEWSETELEM, 0);
3973 nft_trans_destroy(trans);
3974 break;
3975 case NFT_MSG_DELSETELEM:
a3716e70 3976 te = (struct nft_trans_elem *)trans->data;
fe2811eb 3977
a3716e70
PNA
3978 nf_tables_setelem_notify(&trans->ctx, te->set,
3979 &te->elem,
60319eb1 3980 NFT_MSG_DELSETELEM, 0);
02263db0 3981 te->set->ops->remove(te->set, &te->elem);
3dd0673a
PM
3982 atomic_dec(&te->set->nelems);
3983 te->set->ndeact--;
60319eb1 3984 break;
37082f93 3985 }
37082f93
PNA
3986 }
3987
b326dd37
PNA
3988 synchronize_rcu();
3989
37082f93 3990 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
c7c32e72 3991 list_del(&trans->list);
b326dd37 3992 nf_tables_commit_release(trans);
37082f93 3993 }
84d7fce6
PNA
3994
3995 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
37082f93
PNA
3996
3997 return 0;
3998}
3999
b326dd37 4000static void nf_tables_abort_release(struct nft_trans *trans)
c7c32e72 4001{
c7c32e72
PNA
4002 switch (trans->msg_type) {
4003 case NFT_MSG_NEWTABLE:
4004 nf_tables_table_destroy(&trans->ctx);
4005 break;
4006 case NFT_MSG_NEWCHAIN:
4007 nf_tables_chain_destroy(trans->ctx.chain);
4008 break;
4009 case NFT_MSG_NEWRULE:
4010 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4011 break;
4012 case NFT_MSG_NEWSET:
4013 nft_set_destroy(nft_trans_set(trans));
4014 break;
61edafbb
PM
4015 case NFT_MSG_NEWSETELEM:
4016 nft_set_elem_destroy(nft_trans_elem_set(trans),
4017 nft_trans_elem(trans).priv);
4018 break;
c7c32e72
PNA
4019 }
4020 kfree(trans);
4021}
4022
5913beaf 4023static int nf_tables_abort(struct net *net, struct sk_buff *skb)
37082f93 4024{
37082f93 4025 struct nft_trans *trans, *next;
02263db0 4026 struct nft_trans_elem *te;
37082f93 4027
a907e36d
XL
4028 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
4029 list) {
b380e5c7 4030 switch (trans->msg_type) {
55dd6f93
PNA
4031 case NFT_MSG_NEWTABLE:
4032 if (nft_trans_table_update(trans)) {
4033 if (nft_trans_table_enable(trans)) {
4034 nf_tables_table_disable(trans->ctx.afi,
4035 trans->ctx.table);
4036 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
4037 }
4038 nft_trans_destroy(trans);
4039 } else {
e688a7f8 4040 list_del_rcu(&trans->ctx.table->list);
55dd6f93
PNA
4041 }
4042 break;
4043 case NFT_MSG_DELTABLE:
e688a7f8
PNA
4044 list_add_tail_rcu(&trans->ctx.table->list,
4045 &trans->ctx.afi->tables);
55dd6f93
PNA
4046 nft_trans_destroy(trans);
4047 break;
91c7b38d
PNA
4048 case NFT_MSG_NEWCHAIN:
4049 if (nft_trans_chain_update(trans)) {
982f4051 4050 free_percpu(nft_trans_chain_stats(trans));
91c7b38d
PNA
4051
4052 nft_trans_destroy(trans);
4053 } else {
4fefee57 4054 trans->ctx.table->use--;
e688a7f8 4055 list_del_rcu(&trans->ctx.chain->list);
c5598794
AB
4056 nf_tables_unregister_hooks(trans->ctx.table,
4057 trans->ctx.chain,
4058 trans->ctx.afi->nops);
91c7b38d
PNA
4059 }
4060 break;
4061 case NFT_MSG_DELCHAIN:
4fefee57 4062 trans->ctx.table->use++;
e688a7f8
PNA
4063 list_add_tail_rcu(&trans->ctx.chain->list,
4064 &trans->ctx.table->chains);
91c7b38d
PNA
4065 nft_trans_destroy(trans);
4066 break;
b380e5c7 4067 case NFT_MSG_NEWRULE:
4fefee57 4068 trans->ctx.chain->use--;
b380e5c7
PNA
4069 list_del_rcu(&nft_trans_rule(trans)->list);
4070 break;
4071 case NFT_MSG_DELRULE:
4fefee57 4072 trans->ctx.chain->use++;
b380e5c7 4073 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
37082f93 4074 nft_trans_destroy(trans);
b380e5c7 4075 break;
958bee14 4076 case NFT_MSG_NEWSET:
4fefee57 4077 trans->ctx.table->use--;
e688a7f8 4078 list_del_rcu(&nft_trans_set(trans)->list);
958bee14
PNA
4079 break;
4080 case NFT_MSG_DELSET:
4fefee57 4081 trans->ctx.table->use++;
e688a7f8
PNA
4082 list_add_tail_rcu(&nft_trans_set(trans)->list,
4083 &trans->ctx.table->sets);
958bee14
PNA
4084 nft_trans_destroy(trans);
4085 break;
60319eb1 4086 case NFT_MSG_NEWSETELEM:
02263db0 4087 te = (struct nft_trans_elem *)trans->data;
fe2811eb 4088
02263db0 4089 te->set->ops->remove(te->set, &te->elem);
3dd0673a 4090 atomic_dec(&te->set->nelems);
60319eb1
PNA
4091 break;
4092 case NFT_MSG_DELSETELEM:
cc02e457
PM
4093 te = (struct nft_trans_elem *)trans->data;
4094
cc02e457 4095 te->set->ops->activate(te->set, &te->elem);
3dd0673a 4096 te->set->ndeact--;
cc02e457 4097
60319eb1
PNA
4098 nft_trans_destroy(trans);
4099 break;
37082f93 4100 }
37082f93
PNA
4101 }
4102
b326dd37
PNA
4103 synchronize_rcu();
4104
a1cee076
PNA
4105 list_for_each_entry_safe_reverse(trans, next,
4106 &net->nft.commit_list, list) {
c7c32e72 4107 list_del(&trans->list);
b326dd37 4108 nf_tables_abort_release(trans);
37082f93
PNA
4109 }
4110
4111 return 0;
4112}
4113
96518518
PM
4114static const struct nfnetlink_subsystem nf_tables_subsys = {
4115 .name = "nf_tables",
4116 .subsys_id = NFNL_SUBSYS_NFTABLES,
4117 .cb_count = NFT_MSG_MAX,
4118 .cb = nf_tables_cb,
0628b123
PNA
4119 .commit = nf_tables_commit,
4120 .abort = nf_tables_abort,
96518518
PM
4121};
4122
7210e4e3
PNA
4123int nft_chain_validate_dependency(const struct nft_chain *chain,
4124 enum nft_chain_type type)
4125{
4126 const struct nft_base_chain *basechain;
4127
4128 if (chain->flags & NFT_BASE_CHAIN) {
4129 basechain = nft_base_chain(chain);
4130 if (basechain->type->type != type)
4131 return -EOPNOTSUPP;
4132 }
4133 return 0;
4134}
4135EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
4136
75e8d06d
PNA
4137int nft_chain_validate_hooks(const struct nft_chain *chain,
4138 unsigned int hook_flags)
4139{
4140 struct nft_base_chain *basechain;
4141
4142 if (chain->flags & NFT_BASE_CHAIN) {
4143 basechain = nft_base_chain(chain);
4144
4145 if ((1 << basechain->ops[0].hooknum) & hook_flags)
4146 return 0;
4147
4148 return -EOPNOTSUPP;
4149 }
4150
4151 return 0;
4152}
4153EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
4154
20a69341
PM
4155/*
4156 * Loop detection - walk through the ruleset beginning at the destination chain
4157 * of a new jump until either the source chain is reached (loop) or all
4158 * reachable chains have been traversed.
4159 *
4160 * The loop check is performed whenever a new jump verdict is added to an
4161 * expression or verdict map or a verdict map is bound to a new chain.
4162 */
4163
4164static int nf_tables_check_loops(const struct nft_ctx *ctx,
4165 const struct nft_chain *chain);
4166
4167static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
4168 const struct nft_set *set,
4169 const struct nft_set_iter *iter,
4170 const struct nft_set_elem *elem)
4171{
fe2811eb
PM
4172 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4173 const struct nft_data *data;
4174
4175 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4176 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
62f9c8b4
PNA
4177 return 0;
4178
fe2811eb 4179 data = nft_set_ext_data(ext);
1ca2e170 4180 switch (data->verdict.code) {
20a69341
PM
4181 case NFT_JUMP:
4182 case NFT_GOTO:
1ca2e170 4183 return nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
4184 default:
4185 return 0;
4186 }
4187}
4188
4189static int nf_tables_check_loops(const struct nft_ctx *ctx,
4190 const struct nft_chain *chain)
4191{
4192 const struct nft_rule *rule;
4193 const struct nft_expr *expr, *last;
20a69341
PM
4194 const struct nft_set *set;
4195 struct nft_set_binding *binding;
4196 struct nft_set_iter iter;
20a69341
PM
4197
4198 if (ctx->chain == chain)
4199 return -ELOOP;
4200
4201 list_for_each_entry(rule, &chain->rules, list) {
4202 nft_rule_for_each_expr(expr, last, rule) {
0ca743a5
PNA
4203 const struct nft_data *data = NULL;
4204 int err;
4205
4206 if (!expr->ops->validate)
20a69341
PM
4207 continue;
4208
0ca743a5
PNA
4209 err = expr->ops->validate(ctx, expr, &data);
4210 if (err < 0)
4211 return err;
4212
20a69341 4213 if (data == NULL)
0ca743a5 4214 continue;
20a69341 4215
1ca2e170 4216 switch (data->verdict.code) {
20a69341
PM
4217 case NFT_JUMP:
4218 case NFT_GOTO:
1ca2e170
PM
4219 err = nf_tables_check_loops(ctx,
4220 data->verdict.chain);
20a69341
PM
4221 if (err < 0)
4222 return err;
4223 default:
4224 break;
4225 }
4226 }
4227 }
4228
4229 list_for_each_entry(set, &ctx->table->sets, list) {
4230 if (!(set->flags & NFT_SET_MAP) ||
4231 set->dtype != NFT_DATA_VERDICT)
4232 continue;
4233
4234 list_for_each_entry(binding, &set->bindings, list) {
11113e19
PM
4235 if (!(binding->flags & NFT_SET_MAP) ||
4236 binding->chain != chain)
20a69341
PM
4237 continue;
4238
4239 iter.skip = 0;
4240 iter.count = 0;
4241 iter.err = 0;
4242 iter.fn = nf_tables_loop_check_setelem;
4243
4244 set->ops->walk(ctx, set, &iter);
4245 if (iter.err < 0)
4246 return iter.err;
4247 }
4248 }
4249
4250 return 0;
4251}
4252
49499c3e
PM
4253/**
4254 * nft_parse_register - parse a register value from a netlink attribute
4255 *
4256 * @attr: netlink attribute
4257 *
4258 * Parse and translate a register value from a netlink attribute.
4259 * Registers used to be 128 bit wide, these register numbers will be
4260 * mapped to the corresponding 32 bit register numbers.
4261 */
b1c96ed3
PM
4262unsigned int nft_parse_register(const struct nlattr *attr)
4263{
49499c3e
PM
4264 unsigned int reg;
4265
4266 reg = ntohl(nla_get_be32(attr));
4267 switch (reg) {
4268 case NFT_REG_VERDICT...NFT_REG_4:
4269 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
4270 default:
4271 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
4272 }
b1c96ed3
PM
4273}
4274EXPORT_SYMBOL_GPL(nft_parse_register);
4275
49499c3e
PM
4276/**
4277 * nft_dump_register - dump a register value to a netlink attribute
4278 *
4279 * @skb: socket buffer
4280 * @attr: attribute number
4281 * @reg: register number
4282 *
4283 * Construct a netlink attribute containing the register number. For
4284 * compatibility reasons, register numbers being a multiple of 4 are
4285 * translated to the corresponding 128 bit register numbers.
4286 */
b1c96ed3
PM
4287int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
4288{
49499c3e
PM
4289 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
4290 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
4291 else
4292 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
4293
b1c96ed3
PM
4294 return nla_put_be32(skb, attr, htonl(reg));
4295}
4296EXPORT_SYMBOL_GPL(nft_dump_register);
4297
96518518 4298/**
d07db988 4299 * nft_validate_register_load - validate a load from a register
96518518
PM
4300 *
4301 * @reg: the register number
d07db988 4302 * @len: the length of the data
96518518
PM
4303 *
4304 * Validate that the input register is one of the general purpose
d07db988 4305 * registers and that the length of the load is within the bounds.
96518518 4306 */
d07db988 4307int nft_validate_register_load(enum nft_registers reg, unsigned int len)
96518518 4308{
49499c3e 4309 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
96518518 4310 return -EINVAL;
d07db988
PM
4311 if (len == 0)
4312 return -EINVAL;
49499c3e 4313 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
d07db988 4314 return -ERANGE;
49499c3e 4315
96518518
PM
4316 return 0;
4317}
d07db988 4318EXPORT_SYMBOL_GPL(nft_validate_register_load);
96518518 4319
96518518 4320/**
1ec10212 4321 * nft_validate_register_store - validate an expressions' register store
96518518
PM
4322 *
4323 * @ctx: context of the expression performing the load
4324 * @reg: the destination register number
4325 * @data: the data to load
4326 * @type: the data type
45d9bcda 4327 * @len: the length of the data
96518518
PM
4328 *
4329 * Validate that a data load uses the appropriate data type for
45d9bcda
PM
4330 * the destination register and the length is within the bounds.
4331 * A value of NULL for the data means that its runtime gathered
58f40ab6 4332 * data.
96518518 4333 */
1ec10212
PM
4334int nft_validate_register_store(const struct nft_ctx *ctx,
4335 enum nft_registers reg,
4336 const struct nft_data *data,
4337 enum nft_data_types type, unsigned int len)
96518518 4338{
20a69341
PM
4339 int err;
4340
96518518
PM
4341 switch (reg) {
4342 case NFT_REG_VERDICT:
58f40ab6 4343 if (type != NFT_DATA_VERDICT)
96518518 4344 return -EINVAL;
20a69341 4345
58f40ab6 4346 if (data != NULL &&
1ca2e170
PM
4347 (data->verdict.code == NFT_GOTO ||
4348 data->verdict.code == NFT_JUMP)) {
4349 err = nf_tables_check_loops(ctx, data->verdict.chain);
20a69341
PM
4350 if (err < 0)
4351 return err;
4352
1ca2e170
PM
4353 if (ctx->chain->level + 1 >
4354 data->verdict.chain->level) {
20a69341
PM
4355 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
4356 return -EMLINK;
1ca2e170 4357 data->verdict.chain->level = ctx->chain->level + 1;
20a69341
PM
4358 }
4359 }
4360
96518518
PM
4361 return 0;
4362 default:
49499c3e 4363 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
27e6d201 4364 return -EINVAL;
45d9bcda
PM
4365 if (len == 0)
4366 return -EINVAL;
49499c3e
PM
4367 if (reg * NFT_REG32_SIZE + len >
4368 FIELD_SIZEOF(struct nft_regs, data))
45d9bcda 4369 return -ERANGE;
27e6d201 4370
96518518
PM
4371 if (data != NULL && type != NFT_DATA_VALUE)
4372 return -EINVAL;
4373 return 0;
4374 }
4375}
1ec10212 4376EXPORT_SYMBOL_GPL(nft_validate_register_store);
96518518
PM
4377
4378static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
4379 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
4380 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
4381 .len = NFT_CHAIN_MAXNAMELEN - 1 },
4382};
4383
4384static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4385 struct nft_data_desc *desc, const struct nlattr *nla)
4386{
4387 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
4388 struct nft_chain *chain;
4389 int err;
4390
4391 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
4392 if (err < 0)
4393 return err;
4394
4395 if (!tb[NFTA_VERDICT_CODE])
4396 return -EINVAL;
1ca2e170 4397 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
96518518 4398
1ca2e170 4399 switch (data->verdict.code) {
e0abdadc 4400 default:
1ca2e170 4401 switch (data->verdict.code & NF_VERDICT_MASK) {
e0abdadc
PM
4402 case NF_ACCEPT:
4403 case NF_DROP:
4404 case NF_QUEUE:
4405 break;
4406 default:
4407 return -EINVAL;
4408 }
4409 /* fall through */
96518518
PM
4410 case NFT_CONTINUE:
4411 case NFT_BREAK:
4412 case NFT_RETURN:
96518518
PM
4413 break;
4414 case NFT_JUMP:
4415 case NFT_GOTO:
4416 if (!tb[NFTA_VERDICT_CHAIN])
4417 return -EINVAL;
4418 chain = nf_tables_chain_lookup(ctx->table,
4419 tb[NFTA_VERDICT_CHAIN]);
4420 if (IS_ERR(chain))
4421 return PTR_ERR(chain);
4422 if (chain->flags & NFT_BASE_CHAIN)
4423 return -EOPNOTSUPP;
4424
96518518 4425 chain->use++;
1ca2e170 4426 data->verdict.chain = chain;
96518518 4427 break;
96518518
PM
4428 }
4429
4c4ed074 4430 desc->len = sizeof(data->verdict);
96518518
PM
4431 desc->type = NFT_DATA_VERDICT;
4432 return 0;
4433}
4434
4435static void nft_verdict_uninit(const struct nft_data *data)
4436{
1ca2e170 4437 switch (data->verdict.code) {
96518518
PM
4438 case NFT_JUMP:
4439 case NFT_GOTO:
1ca2e170 4440 data->verdict.chain->use--;
96518518
PM
4441 break;
4442 }
4443}
4444
33d5a7b1 4445int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
96518518
PM
4446{
4447 struct nlattr *nest;
4448
33d5a7b1 4449 nest = nla_nest_start(skb, type);
96518518
PM
4450 if (!nest)
4451 goto nla_put_failure;
4452
33d5a7b1 4453 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
96518518
PM
4454 goto nla_put_failure;
4455
33d5a7b1 4456 switch (v->code) {
96518518
PM
4457 case NFT_JUMP:
4458 case NFT_GOTO:
1ca2e170 4459 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
33d5a7b1 4460 v->chain->name))
96518518
PM
4461 goto nla_put_failure;
4462 }
4463 nla_nest_end(skb, nest);
4464 return 0;
4465
4466nla_put_failure:
4467 return -1;
4468}
4469
d0a11fc3
PM
4470static int nft_value_init(const struct nft_ctx *ctx,
4471 struct nft_data *data, unsigned int size,
96518518
PM
4472 struct nft_data_desc *desc, const struct nlattr *nla)
4473{
4474 unsigned int len;
4475
4476 len = nla_len(nla);
4477 if (len == 0)
4478 return -EINVAL;
d0a11fc3 4479 if (len > size)
96518518
PM
4480 return -EOVERFLOW;
4481
d0a11fc3 4482 nla_memcpy(data->data, nla, len);
96518518
PM
4483 desc->type = NFT_DATA_VALUE;
4484 desc->len = len;
4485 return 0;
4486}
4487
4488static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4489 unsigned int len)
4490{
4491 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4492}
4493
4494static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
d0a11fc3 4495 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
96518518
PM
4496 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4497};
4498
4499/**
4500 * nft_data_init - parse nf_tables data netlink attributes
4501 *
4502 * @ctx: context of the expression using the data
4503 * @data: destination struct nft_data
d0a11fc3 4504 * @size: maximum data length
96518518
PM
4505 * @desc: data description
4506 * @nla: netlink attribute containing data
4507 *
4508 * Parse the netlink data attributes and initialize a struct nft_data.
4509 * The type and length of data are returned in the data description.
4510 *
4511 * The caller can indicate that it only wants to accept data of type
4512 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4513 */
d0a11fc3
PM
4514int nft_data_init(const struct nft_ctx *ctx,
4515 struct nft_data *data, unsigned int size,
96518518
PM
4516 struct nft_data_desc *desc, const struct nlattr *nla)
4517{
4518 struct nlattr *tb[NFTA_DATA_MAX + 1];
4519 int err;
4520
4521 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4522 if (err < 0)
4523 return err;
4524
4525 if (tb[NFTA_DATA_VALUE])
d0a11fc3
PM
4526 return nft_value_init(ctx, data, size, desc,
4527 tb[NFTA_DATA_VALUE]);
96518518
PM
4528 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4529 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4530 return -EINVAL;
4531}
4532EXPORT_SYMBOL_GPL(nft_data_init);
4533
4534/**
4535 * nft_data_uninit - release a nft_data item
4536 *
4537 * @data: struct nft_data to release
4538 * @type: type of data
4539 *
4540 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4541 * all others need to be released by calling this function.
4542 */
4543void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4544{
960bd2c2 4545 if (type < NFT_DATA_VERDICT)
96518518 4546 return;
960bd2c2 4547 switch (type) {
96518518
PM
4548 case NFT_DATA_VERDICT:
4549 return nft_verdict_uninit(data);
4550 default:
4551 WARN_ON(1);
4552 }
4553}
4554EXPORT_SYMBOL_GPL(nft_data_uninit);
4555
4556int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4557 enum nft_data_types type, unsigned int len)
4558{
4559 struct nlattr *nest;
4560 int err;
4561
4562 nest = nla_nest_start(skb, attr);
4563 if (nest == NULL)
4564 return -1;
4565
4566 switch (type) {
4567 case NFT_DATA_VALUE:
4568 err = nft_value_dump(skb, data, len);
4569 break;
4570 case NFT_DATA_VERDICT:
33d5a7b1 4571 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
96518518
PM
4572 break;
4573 default:
4574 err = -EINVAL;
4575 WARN_ON(1);
4576 }
4577
4578 nla_nest_end(skb, nest);
4579 return err;
4580}
4581EXPORT_SYMBOL_GPL(nft_data_dump);
4582
df05ef87 4583static int __net_init nf_tables_init_net(struct net *net)
99633ab2
PNA
4584{
4585 INIT_LIST_HEAD(&net->nft.af_info);
0628b123 4586 INIT_LIST_HEAD(&net->nft.commit_list);
38e029f1 4587 net->nft.base_seq = 1;
99633ab2
PNA
4588 return 0;
4589}
4590
5ebe0b0e
PNA
4591int __nft_release_basechain(struct nft_ctx *ctx)
4592{
4593 struct nft_rule *rule, *nr;
4594
4595 BUG_ON(!(ctx->chain->flags & NFT_BASE_CHAIN));
4596
4597 nf_tables_unregister_hooks(ctx->chain->table, ctx->chain,
4598 ctx->afi->nops);
4599 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
4600 list_del(&rule->list);
4601 ctx->chain->use--;
4602 nf_tables_rule_destroy(ctx, rule);
4603 }
4604 list_del(&ctx->chain->list);
4605 ctx->table->use--;
4606 nf_tables_chain_destroy(ctx->chain);
4607
4608 return 0;
4609}
4610EXPORT_SYMBOL_GPL(__nft_release_basechain);
4611
df05ef87
PNA
4612/* Called by nft_unregister_afinfo() from __net_exit path, nfnl_lock is held. */
4613static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi)
4614{
4615 struct nft_table *table, *nt;
4616 struct nft_chain *chain, *nc;
4617 struct nft_rule *rule, *nr;
4618 struct nft_set *set, *ns;
4619 struct nft_ctx ctx = {
4620 .net = net,
4621 .afi = afi,
4622 };
4623
4624 list_for_each_entry_safe(table, nt, &afi->tables, list) {
4625 list_for_each_entry(chain, &table->chains, list)
4626 nf_tables_unregister_hooks(table, chain, afi->nops);
4627 /* No packets are walking on these chains anymore. */
4628 ctx.table = table;
4629 list_for_each_entry(chain, &table->chains, list) {
4630 ctx.chain = chain;
4631 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
4632 list_del(&rule->list);
4633 chain->use--;
4634 nf_tables_rule_destroy(&ctx, rule);
4635 }
4636 }
4637 list_for_each_entry_safe(set, ns, &table->sets, list) {
4638 list_del(&set->list);
4639 table->use--;
4640 nft_set_destroy(set);
4641 }
4642 list_for_each_entry_safe(chain, nc, &table->chains, list) {
4643 list_del(&chain->list);
4644 table->use--;
4645 nf_tables_chain_destroy(chain);
4646 }
4647 list_del(&table->list);
4648 nf_tables_table_destroy(&ctx);
4649 }
4650}
4651
99633ab2
PNA
4652static struct pernet_operations nf_tables_net_ops = {
4653 .init = nf_tables_init_net,
4654};
4655
96518518
PM
4656static int __init nf_tables_module_init(void)
4657{
4658 int err;
4659
4660 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4661 GFP_KERNEL);
4662 if (info == NULL) {
4663 err = -ENOMEM;
4664 goto err1;
4665 }
4666
4667 err = nf_tables_core_module_init();
4668 if (err < 0)
4669 goto err2;
4670
4671 err = nfnetlink_subsys_register(&nf_tables_subsys);
4672 if (err < 0)
4673 goto err3;
4674
4675 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
99633ab2 4676 return register_pernet_subsys(&nf_tables_net_ops);
96518518
PM
4677err3:
4678 nf_tables_core_module_exit();
4679err2:
4680 kfree(info);
4681err1:
4682 return err;
4683}
4684
4685static void __exit nf_tables_module_exit(void)
4686{
99633ab2 4687 unregister_pernet_subsys(&nf_tables_net_ops);
96518518 4688 nfnetlink_subsys_unregister(&nf_tables_subsys);
1b1bc49c 4689 rcu_barrier();
96518518
PM
4690 nf_tables_core_module_exit();
4691 kfree(info);
4692}
4693
4694module_init(nf_tables_module_init);
4695module_exit(nf_tables_module_exit);
4696
4697MODULE_LICENSE("GPL");
4698MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4699MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.386418 seconds and 5 git commands to generate.