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