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