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