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