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