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