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