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