tcp: fix ipv4 mapped request socks
[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 &&
d6b6cb1d
PNA
1231 !(chain->flags & NFT_BASE_CHAIN)))
1232 return -EOPNOTSUPP;
1233
1234 if (chain == NULL &&
57de2a0c
PM
1235 nla[NFTA_CHAIN_HOOK] == NULL)
1236 return -EOPNOTSUPP;
1237
8f46df18 1238 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
57de2a0c
PM
1239 switch (policy) {
1240 case NF_DROP:
1241 case NF_ACCEPT:
1242 break;
1243 default:
1244 return -EINVAL;
1245 }
1246 }
1247
96518518 1248 if (chain != NULL) {
91c7b38d
PNA
1249 struct nft_stats *stats = NULL;
1250 struct nft_trans *trans;
1251
1252 if (chain->flags & NFT_CHAIN_INACTIVE)
1253 return -ENOENT;
96518518
PM
1254 if (nlh->nlmsg_flags & NLM_F_EXCL)
1255 return -EEXIST;
1256 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1257 return -EOPNOTSUPP;
1258
1259 if (nla[NFTA_CHAIN_HANDLE] && name &&
1260 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
1261 return -EEXIST;
1262
0ca743a5
PNA
1263 if (nla[NFTA_CHAIN_COUNTERS]) {
1264 if (!(chain->flags & NFT_BASE_CHAIN))
1265 return -EOPNOTSUPP;
1266
ff3cd7b3
PNA
1267 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1268 if (IS_ERR(stats))
1269 return PTR_ERR(stats);
0ca743a5
PNA
1270 }
1271
91c7b38d
PNA
1272 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1273 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1274 sizeof(struct nft_trans_chain));
f5553c19
PNA
1275 if (trans == NULL) {
1276 free_percpu(stats);
91c7b38d 1277 return -ENOMEM;
f5553c19 1278 }
4401a862 1279
91c7b38d
PNA
1280 nft_trans_chain_stats(trans) = stats;
1281 nft_trans_chain_update(trans) = true;
4401a862 1282
91c7b38d
PNA
1283 if (nla[NFTA_CHAIN_POLICY])
1284 nft_trans_chain_policy(trans) = policy;
1285 else
1286 nft_trans_chain_policy(trans) = -1;
96518518 1287
91c7b38d
PNA
1288 if (nla[NFTA_CHAIN_HANDLE] && name) {
1289 nla_strlcpy(nft_trans_chain_name(trans), name,
1290 NFT_CHAIN_MAXNAMELEN);
1291 }
1292 list_add_tail(&trans->list, &net->nft.commit_list);
1293 return 0;
96518518
PM
1294 }
1295
75820676
PM
1296 if (table->use == UINT_MAX)
1297 return -EOVERFLOW;
1298
96518518 1299 if (nla[NFTA_CHAIN_HOOK]) {
2a37d755 1300 const struct nf_chain_type *type;
96518518 1301 struct nf_hook_ops *ops;
9370761c 1302 nf_hookfn *hookfn;
115a60b1 1303 u32 hooknum, priority;
9370761c 1304
baae3e62 1305 type = chain_type[family][NFT_CHAIN_T_DEFAULT];
9370761c
PNA
1306 if (nla[NFTA_CHAIN_TYPE]) {
1307 type = nf_tables_chain_type_lookup(afi,
1308 nla[NFTA_CHAIN_TYPE],
1309 create);
93b0806f
PM
1310 if (IS_ERR(type))
1311 return PTR_ERR(type);
9370761c 1312 }
96518518
PM
1313
1314 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1315 nft_hook_policy);
1316 if (err < 0)
1317 return err;
1318 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1319 ha[NFTA_HOOK_PRIORITY] == NULL)
1320 return -EINVAL;
9370761c
PNA
1321
1322 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1323 if (hooknum >= afi->nhooks)
96518518 1324 return -EINVAL;
115a60b1 1325 priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
96518518 1326
baae3e62 1327 if (!(type->hook_mask & (1 << hooknum)))
9370761c 1328 return -EOPNOTSUPP;
fa2c1de0 1329 if (!try_module_get(type->owner))
baae3e62 1330 return -ENOENT;
fa2c1de0 1331 hookfn = type->hooks[hooknum];
9370761c 1332
96518518 1333 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
f5553c19
PNA
1334 if (basechain == NULL) {
1335 module_put(type->owner);
96518518 1336 return -ENOMEM;
f5553c19 1337 }
9370761c 1338
4401a862 1339 if (nla[NFTA_CHAIN_COUNTERS]) {
ff3cd7b3
PNA
1340 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1341 if (IS_ERR(stats)) {
fa2c1de0 1342 module_put(type->owner);
4401a862 1343 kfree(basechain);
ff3cd7b3 1344 return PTR_ERR(stats);
4401a862 1345 }
ff3cd7b3 1346 basechain->stats = stats;
4401a862 1347 } else {
ce355e20 1348 stats = netdev_alloc_pcpu_stats(struct nft_stats);
c123bb71 1349 if (stats == NULL) {
fa2c1de0 1350 module_put(type->owner);
4401a862 1351 kfree(basechain);
c123bb71 1352 return -ENOMEM;
4401a862 1353 }
ff3cd7b3 1354 rcu_assign_pointer(basechain->stats, stats);
4401a862
PM
1355 }
1356
9370761c 1357 basechain->type = type;
96518518
PM
1358 chain = &basechain->chain;
1359
115a60b1
PM
1360 for (i = 0; i < afi->nops; i++) {
1361 ops = &basechain->ops[i];
1362 ops->pf = family;
1363 ops->owner = afi->owner;
1364 ops->hooknum = hooknum;
1365 ops->priority = priority;
1366 ops->priv = chain;
1367 ops->hook = afi->hooks[ops->hooknum];
1368 if (hookfn)
1369 ops->hook = hookfn;
1370 if (afi->hook_ops_init)
1371 afi->hook_ops_init(ops, i);
1372 }
96518518
PM
1373
1374 chain->flags |= NFT_BASE_CHAIN;
57de2a0c 1375 basechain->policy = policy;
96518518
PM
1376 } else {
1377 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1378 if (chain == NULL)
1379 return -ENOMEM;
1380 }
1381
1382 INIT_LIST_HEAD(&chain->rules);
1383 chain->handle = nf_tables_alloc_handle(table);
0628b123 1384 chain->net = net;
b5bc89bf 1385 chain->table = table;
96518518
PM
1386 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1387
9ddf6323
PNA
1388 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1389 chain->flags & NFT_BASE_CHAIN) {
115a60b1 1390 err = nf_register_hooks(nft_base_chain(chain)->ops, afi->nops);
91c7b38d
PNA
1391 if (err < 0)
1392 goto err1;
0ca743a5 1393 }
96518518 1394
91c7b38d
PNA
1395 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1396 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1397 if (err < 0)
1398 goto err2;
96518518 1399
4fefee57 1400 table->use++;
e688a7f8 1401 list_add_tail_rcu(&chain->list, &table->chains);
91c7b38d
PNA
1402 return 0;
1403err2:
c5598794 1404 nf_tables_unregister_hooks(table, chain, afi->nops);
91c7b38d
PNA
1405err1:
1406 nf_tables_chain_destroy(chain);
1407 return err;
96518518
PM
1408}
1409
1410static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1411 const struct nlmsghdr *nlh,
1412 const struct nlattr * const nla[])
1413{
1414 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 1415 struct nft_af_info *afi;
96518518
PM
1416 struct nft_table *table;
1417 struct nft_chain *chain;
99633ab2 1418 struct net *net = sock_net(skb->sk);
96518518 1419 int family = nfmsg->nfgen_family;
91c7b38d 1420 struct nft_ctx ctx;
96518518 1421
99633ab2 1422 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1423 if (IS_ERR(afi))
1424 return PTR_ERR(afi);
1425
9370761c 1426 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
96518518
PM
1427 if (IS_ERR(table))
1428 return PTR_ERR(table);
55dd6f93
PNA
1429 if (table->flags & NFT_TABLE_INACTIVE)
1430 return -ENOENT;
96518518
PM
1431
1432 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1433 if (IS_ERR(chain))
1434 return PTR_ERR(chain);
91c7b38d
PNA
1435 if (chain->flags & NFT_CHAIN_INACTIVE)
1436 return -ENOENT;
4fefee57 1437 if (chain->use > 0)
96518518
PM
1438 return -EBUSY;
1439
91c7b38d 1440 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
0165d932 1441
ee01d542 1442 return nft_delchain(&ctx);
96518518
PM
1443}
1444
96518518
PM
1445/*
1446 * Expressions
1447 */
1448
1449/**
ef1f7df9
PM
1450 * nft_register_expr - register nf_tables expr type
1451 * @ops: expr type
96518518 1452 *
ef1f7df9 1453 * Registers the expr type for use with nf_tables. Returns zero on
96518518
PM
1454 * success or a negative errno code otherwise.
1455 */
ef1f7df9 1456int nft_register_expr(struct nft_expr_type *type)
96518518
PM
1457{
1458 nfnl_lock(NFNL_SUBSYS_NFTABLES);
758dbcec 1459 if (type->family == NFPROTO_UNSPEC)
e688a7f8 1460 list_add_tail_rcu(&type->list, &nf_tables_expressions);
758dbcec 1461 else
e688a7f8 1462 list_add_rcu(&type->list, &nf_tables_expressions);
96518518
PM
1463 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(nft_register_expr);
1467
1468/**
ef1f7df9
PM
1469 * nft_unregister_expr - unregister nf_tables expr type
1470 * @ops: expr type
96518518 1471 *
ef1f7df9 1472 * Unregisters the expr typefor use with nf_tables.
96518518 1473 */
ef1f7df9 1474void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
1475{
1476 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 1477 list_del_rcu(&type->list);
96518518
PM
1478 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1479}
1480EXPORT_SYMBOL_GPL(nft_unregister_expr);
1481
64d46806
PM
1482static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1483 struct nlattr *nla)
96518518 1484{
ef1f7df9 1485 const struct nft_expr_type *type;
96518518 1486
ef1f7df9 1487 list_for_each_entry(type, &nf_tables_expressions, list) {
64d46806
PM
1488 if (!nla_strcmp(nla, type->name) &&
1489 (!type->family || type->family == family))
ef1f7df9 1490 return type;
96518518
PM
1491 }
1492 return NULL;
1493}
1494
64d46806
PM
1495static const struct nft_expr_type *nft_expr_type_get(u8 family,
1496 struct nlattr *nla)
96518518 1497{
ef1f7df9 1498 const struct nft_expr_type *type;
96518518
PM
1499
1500 if (nla == NULL)
1501 return ERR_PTR(-EINVAL);
1502
64d46806 1503 type = __nft_expr_type_get(family, nla);
ef1f7df9
PM
1504 if (type != NULL && try_module_get(type->owner))
1505 return type;
96518518
PM
1506
1507#ifdef CONFIG_MODULES
ef1f7df9 1508 if (type == NULL) {
64d46806
PM
1509 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1510 request_module("nft-expr-%u-%.*s", family,
1511 nla_len(nla), (char *)nla_data(nla));
1512 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1513 if (__nft_expr_type_get(family, nla))
1514 return ERR_PTR(-EAGAIN);
1515
96518518
PM
1516 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1517 request_module("nft-expr-%.*s",
1518 nla_len(nla), (char *)nla_data(nla));
1519 nfnl_lock(NFNL_SUBSYS_NFTABLES);
64d46806 1520 if (__nft_expr_type_get(family, nla))
96518518
PM
1521 return ERR_PTR(-EAGAIN);
1522 }
1523#endif
1524 return ERR_PTR(-ENOENT);
1525}
1526
1527static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1528 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1529 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1530};
1531
1532static int nf_tables_fill_expr_info(struct sk_buff *skb,
1533 const struct nft_expr *expr)
1534{
ef1f7df9 1535 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
1536 goto nla_put_failure;
1537
1538 if (expr->ops->dump) {
1539 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1540 if (data == NULL)
1541 goto nla_put_failure;
1542 if (expr->ops->dump(skb, expr) < 0)
1543 goto nla_put_failure;
1544 nla_nest_end(skb, data);
1545 }
1546
1547 return skb->len;
1548
1549nla_put_failure:
1550 return -1;
1551};
1552
1553struct nft_expr_info {
1554 const struct nft_expr_ops *ops;
ef1f7df9 1555 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
1556};
1557
0ca743a5
PNA
1558static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1559 const struct nlattr *nla,
96518518
PM
1560 struct nft_expr_info *info)
1561{
ef1f7df9 1562 const struct nft_expr_type *type;
96518518 1563 const struct nft_expr_ops *ops;
ef1f7df9 1564 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
1565 int err;
1566
ef1f7df9 1567 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
96518518
PM
1568 if (err < 0)
1569 return err;
1570
64d46806 1571 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
1572 if (IS_ERR(type))
1573 return PTR_ERR(type);
1574
1575 if (tb[NFTA_EXPR_DATA]) {
1576 err = nla_parse_nested(info->tb, type->maxattr,
1577 tb[NFTA_EXPR_DATA], type->policy);
1578 if (err < 0)
1579 goto err1;
1580 } else
1581 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1582
1583 if (type->select_ops != NULL) {
0ca743a5
PNA
1584 ops = type->select_ops(ctx,
1585 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
1586 if (IS_ERR(ops)) {
1587 err = PTR_ERR(ops);
1588 goto err1;
1589 }
1590 } else
1591 ops = type->ops;
1592
96518518
PM
1593 info->ops = ops;
1594 return 0;
ef1f7df9
PM
1595
1596err1:
1597 module_put(type->owner);
1598 return err;
96518518
PM
1599}
1600
1601static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 1602 const struct nft_expr_info *info,
96518518
PM
1603 struct nft_expr *expr)
1604{
1605 const struct nft_expr_ops *ops = info->ops;
1606 int err;
1607
1608 expr->ops = ops;
1609 if (ops->init) {
ef1f7df9 1610 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
1611 if (err < 0)
1612 goto err1;
1613 }
1614
96518518
PM
1615 return 0;
1616
1617err1:
1618 expr->ops = NULL;
1619 return err;
1620}
1621
62472bce
PM
1622static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1623 struct nft_expr *expr)
96518518
PM
1624{
1625 if (expr->ops->destroy)
62472bce 1626 expr->ops->destroy(ctx, expr);
ef1f7df9 1627 module_put(expr->ops->type->owner);
96518518
PM
1628}
1629
1630/*
1631 * Rules
1632 */
1633
1634static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1635 u64 handle)
1636{
1637 struct nft_rule *rule;
1638
1639 // FIXME: this sucks
1640 list_for_each_entry(rule, &chain->rules, list) {
1641 if (handle == rule->handle)
1642 return rule;
1643 }
1644
1645 return ERR_PTR(-ENOENT);
1646}
1647
1648static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1649 const struct nlattr *nla)
1650{
1651 if (nla == NULL)
1652 return ERR_PTR(-EINVAL);
1653
1654 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1655}
1656
1657static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1658 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1659 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1660 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1661 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1662 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
0ca743a5 1663 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 1664 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
1665 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1666 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1667};
1668
84d7fce6
PNA
1669static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1670 u32 portid, u32 seq, int event,
1671 u32 flags, int family,
96518518
PM
1672 const struct nft_table *table,
1673 const struct nft_chain *chain,
1674 const struct nft_rule *rule)
1675{
1676 struct nlmsghdr *nlh;
1677 struct nfgenmsg *nfmsg;
1678 const struct nft_expr *expr, *next;
1679 struct nlattr *list;
5e948466
EL
1680 const struct nft_rule *prule;
1681 int type = event | NFNL_SUBSYS_NFTABLES << 8;
96518518 1682
5e948466 1683 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
96518518
PM
1684 flags);
1685 if (nlh == NULL)
1686 goto nla_put_failure;
1687
1688 nfmsg = nlmsg_data(nlh);
1689 nfmsg->nfgen_family = family;
1690 nfmsg->version = NFNETLINK_V0;
84d7fce6 1691 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
96518518
PM
1692
1693 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1694 goto nla_put_failure;
1695 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1696 goto nla_put_failure;
1697 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1698 goto nla_put_failure;
1699
5e948466
EL
1700 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1701 prule = list_entry(rule->list.prev, struct nft_rule, list);
1702 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1703 cpu_to_be64(prule->handle)))
1704 goto nla_put_failure;
1705 }
1706
96518518
PM
1707 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1708 if (list == NULL)
1709 goto nla_put_failure;
1710 nft_rule_for_each_expr(expr, next, rule) {
1711 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1712 if (elem == NULL)
1713 goto nla_put_failure;
1714 if (nf_tables_fill_expr_info(skb, expr) < 0)
1715 goto nla_put_failure;
1716 nla_nest_end(skb, elem);
1717 }
1718 nla_nest_end(skb, list);
1719
86f1ec32
PM
1720 if (rule->udata) {
1721 struct nft_userdata *udata = nft_userdata(rule);
1722 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1723 udata->data) < 0)
1724 goto nla_put_failure;
1725 }
0768b3b3 1726
053c095a
JB
1727 nlmsg_end(skb, nlh);
1728 return 0;
96518518
PM
1729
1730nla_put_failure:
1731 nlmsg_trim(skb, nlh);
1732 return -1;
1733}
1734
35151d84 1735static int nf_tables_rule_notify(const struct nft_ctx *ctx,
96518518 1736 const struct nft_rule *rule,
35151d84 1737 int event)
96518518
PM
1738{
1739 struct sk_buff *skb;
96518518
PM
1740 int err;
1741
128ad332
PNA
1742 if (!ctx->report &&
1743 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
96518518
PM
1744 return 0;
1745
1746 err = -ENOBUFS;
1747 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1748 if (skb == NULL)
1749 goto err;
1750
84d7fce6
PNA
1751 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1752 event, 0, ctx->afi->family, ctx->table,
35151d84 1753 ctx->chain, rule);
96518518
PM
1754 if (err < 0) {
1755 kfree_skb(skb);
1756 goto err;
1757 }
1758
128ad332
PNA
1759 err = nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1760 ctx->report, GFP_KERNEL);
96518518 1761err:
128ad332
PNA
1762 if (err < 0) {
1763 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1764 err);
1765 }
96518518
PM
1766 return err;
1767}
1768
1769static int nf_tables_dump_rules(struct sk_buff *skb,
1770 struct netlink_callback *cb)
1771{
1772 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1773 const struct nft_af_info *afi;
1774 const struct nft_table *table;
1775 const struct nft_chain *chain;
1776 const struct nft_rule *rule;
1777 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1778 struct net *net = sock_net(skb->sk);
96518518
PM
1779 int family = nfmsg->nfgen_family;
1780
e688a7f8 1781 rcu_read_lock();
38e029f1
PNA
1782 cb->seq = net->nft.base_seq;
1783
e688a7f8 1784 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
96518518
PM
1785 if (family != NFPROTO_UNSPEC && family != afi->family)
1786 continue;
1787
e688a7f8
PNA
1788 list_for_each_entry_rcu(table, &afi->tables, list) {
1789 list_for_each_entry_rcu(chain, &table->chains, list) {
1790 list_for_each_entry_rcu(rule, &chain->rules, list) {
0628b123
PNA
1791 if (!nft_rule_is_active(net, rule))
1792 goto cont;
96518518
PM
1793 if (idx < s_idx)
1794 goto cont;
1795 if (idx > s_idx)
1796 memset(&cb->args[1], 0,
1797 sizeof(cb->args) - sizeof(cb->args[0]));
84d7fce6 1798 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
96518518
PM
1799 cb->nlh->nlmsg_seq,
1800 NFT_MSG_NEWRULE,
1801 NLM_F_MULTI | NLM_F_APPEND,
1802 afi->family, table, chain, rule) < 0)
1803 goto done;
38e029f1
PNA
1804
1805 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
96518518
PM
1806cont:
1807 idx++;
1808 }
1809 }
1810 }
1811 }
1812done:
e688a7f8
PNA
1813 rcu_read_unlock();
1814
96518518
PM
1815 cb->args[0] = idx;
1816 return skb->len;
1817}
1818
1819static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1820 const struct nlmsghdr *nlh,
1821 const struct nlattr * const nla[])
1822{
1823 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1824 const struct nft_af_info *afi;
1825 const struct nft_table *table;
1826 const struct nft_chain *chain;
1827 const struct nft_rule *rule;
1828 struct sk_buff *skb2;
99633ab2 1829 struct net *net = sock_net(skb->sk);
96518518
PM
1830 int family = nfmsg->nfgen_family;
1831 int err;
1832
1833 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1834 struct netlink_dump_control c = {
1835 .dump = nf_tables_dump_rules,
1836 };
1837 return netlink_dump_start(nlsk, skb, nlh, &c);
1838 }
1839
99633ab2 1840 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1841 if (IS_ERR(afi))
1842 return PTR_ERR(afi);
1843
9370761c 1844 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1845 if (IS_ERR(table))
1846 return PTR_ERR(table);
55dd6f93
PNA
1847 if (table->flags & NFT_TABLE_INACTIVE)
1848 return -ENOENT;
96518518
PM
1849
1850 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1851 if (IS_ERR(chain))
1852 return PTR_ERR(chain);
91c7b38d
PNA
1853 if (chain->flags & NFT_CHAIN_INACTIVE)
1854 return -ENOENT;
96518518
PM
1855
1856 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1857 if (IS_ERR(rule))
1858 return PTR_ERR(rule);
1859
1860 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1861 if (!skb2)
1862 return -ENOMEM;
1863
84d7fce6 1864 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
96518518
PM
1865 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1866 family, table, chain, rule);
1867 if (err < 0)
1868 goto err;
1869
1870 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1871
1872err:
1873 kfree_skb(skb2);
1874 return err;
1875}
1876
62472bce
PM
1877static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1878 struct nft_rule *rule)
96518518 1879{
96518518
PM
1880 struct nft_expr *expr;
1881
1882 /*
1883 * Careful: some expressions might not be initialized in case this
1884 * is called on error from nf_tables_newrule().
1885 */
1886 expr = nft_expr_first(rule);
1887 while (expr->ops && expr != nft_expr_last(rule)) {
62472bce 1888 nf_tables_expr_destroy(ctx, expr);
96518518
PM
1889 expr = nft_expr_next(expr);
1890 }
1891 kfree(rule);
1892}
1893
1081d11b
PNA
1894#define NFT_RULE_MAXEXPRS 128
1895
1896static struct nft_expr_info *info;
1897
96518518
PM
1898static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1899 const struct nlmsghdr *nlh,
1900 const struct nlattr * const nla[])
1901{
1902 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 1903 struct nft_af_info *afi;
99633ab2 1904 struct net *net = sock_net(skb->sk);
96518518
PM
1905 struct nft_table *table;
1906 struct nft_chain *chain;
1907 struct nft_rule *rule, *old_rule = NULL;
86f1ec32 1908 struct nft_userdata *udata;
1081d11b 1909 struct nft_trans *trans = NULL;
96518518
PM
1910 struct nft_expr *expr;
1911 struct nft_ctx ctx;
1912 struct nlattr *tmp;
86f1ec32 1913 unsigned int size, i, n, ulen = 0, usize = 0;
96518518
PM
1914 int err, rem;
1915 bool create;
5e948466 1916 u64 handle, pos_handle;
96518518
PM
1917
1918 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1919
99633ab2 1920 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
96518518
PM
1921 if (IS_ERR(afi))
1922 return PTR_ERR(afi);
1923
9370761c 1924 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1925 if (IS_ERR(table))
1926 return PTR_ERR(table);
1927
1928 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1929 if (IS_ERR(chain))
1930 return PTR_ERR(chain);
1931
1932 if (nla[NFTA_RULE_HANDLE]) {
1933 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1934 rule = __nf_tables_rule_lookup(chain, handle);
1935 if (IS_ERR(rule))
1936 return PTR_ERR(rule);
1937
1938 if (nlh->nlmsg_flags & NLM_F_EXCL)
1939 return -EEXIST;
1940 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1941 old_rule = rule;
1942 else
1943 return -EOPNOTSUPP;
1944 } else {
1945 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1946 return -EINVAL;
1947 handle = nf_tables_alloc_handle(table);
a0a7379e
PNA
1948
1949 if (chain->use == UINT_MAX)
1950 return -EOVERFLOW;
96518518
PM
1951 }
1952
5e948466
EL
1953 if (nla[NFTA_RULE_POSITION]) {
1954 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1955 return -EOPNOTSUPP;
1956
1957 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1958 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1959 if (IS_ERR(old_rule))
1960 return PTR_ERR(old_rule);
1961 }
1962
0ca743a5
PNA
1963 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1964
96518518
PM
1965 n = 0;
1966 size = 0;
1967 if (nla[NFTA_RULE_EXPRESSIONS]) {
1968 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1969 err = -EINVAL;
1970 if (nla_type(tmp) != NFTA_LIST_ELEM)
1971 goto err1;
1972 if (n == NFT_RULE_MAXEXPRS)
1973 goto err1;
0ca743a5 1974 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
96518518
PM
1975 if (err < 0)
1976 goto err1;
1977 size += info[n].ops->size;
1978 n++;
1979 }
1980 }
9889840f
PM
1981 /* Check for overflow of dlen field */
1982 err = -EFBIG;
1983 if (size >= 1 << 12)
1984 goto err1;
96518518 1985
86f1ec32 1986 if (nla[NFTA_RULE_USERDATA]) {
0768b3b3 1987 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
86f1ec32
PM
1988 if (ulen > 0)
1989 usize = sizeof(struct nft_userdata) + ulen;
1990 }
0768b3b3 1991
96518518 1992 err = -ENOMEM;
86f1ec32 1993 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
96518518
PM
1994 if (rule == NULL)
1995 goto err1;
1996
0628b123
PNA
1997 nft_rule_activate_next(net, rule);
1998
96518518
PM
1999 rule->handle = handle;
2000 rule->dlen = size;
86f1ec32 2001 rule->udata = ulen ? 1 : 0;
0768b3b3 2002
86f1ec32
PM
2003 if (ulen) {
2004 udata = nft_userdata(rule);
2005 udata->len = ulen - 1;
2006 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2007 }
96518518 2008
96518518
PM
2009 expr = nft_expr_first(rule);
2010 for (i = 0; i < n; i++) {
2011 err = nf_tables_newexpr(&ctx, &info[i], expr);
2012 if (err < 0)
2013 goto err2;
ef1f7df9 2014 info[i].ops = NULL;
96518518
PM
2015 expr = nft_expr_next(expr);
2016 }
2017
96518518 2018 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
0628b123 2019 if (nft_rule_is_active_next(net, old_rule)) {
ac904ac8 2020 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
b380e5c7 2021 old_rule);
1081d11b 2022 if (trans == NULL) {
0628b123
PNA
2023 err = -ENOMEM;
2024 goto err2;
2025 }
ee01d542 2026 nft_rule_deactivate_next(net, old_rule);
ac34b861 2027 chain->use--;
5bc5c307 2028 list_add_tail_rcu(&rule->list, &old_rule->list);
0628b123
PNA
2029 } else {
2030 err = -ENOENT;
2031 goto err2;
2032 }
96518518 2033 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
5e948466
EL
2034 if (old_rule)
2035 list_add_rcu(&rule->list, &old_rule->list);
2036 else
2037 list_add_tail_rcu(&rule->list, &chain->rules);
2038 else {
2039 if (old_rule)
2040 list_add_tail_rcu(&rule->list, &old_rule->list);
2041 else
2042 list_add_rcu(&rule->list, &chain->rules);
2043 }
96518518 2044
b380e5c7 2045 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
0628b123
PNA
2046 err = -ENOMEM;
2047 goto err3;
2048 }
4fefee57 2049 chain->use++;
96518518
PM
2050 return 0;
2051
0628b123
PNA
2052err3:
2053 list_del_rcu(&rule->list);
96518518 2054err2:
62472bce 2055 nf_tables_rule_destroy(&ctx, rule);
96518518
PM
2056err1:
2057 for (i = 0; i < n; i++) {
2058 if (info[i].ops != NULL)
ef1f7df9 2059 module_put(info[i].ops->type->owner);
96518518
PM
2060 }
2061 return err;
2062}
2063
2064static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
2065 const struct nlmsghdr *nlh,
2066 const struct nlattr * const nla[])
2067{
2068 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8 2069 struct nft_af_info *afi;
99633ab2 2070 struct net *net = sock_net(skb->sk);
7c95f6d8 2071 struct nft_table *table;
cf9dc09d
PNA
2072 struct nft_chain *chain = NULL;
2073 struct nft_rule *rule;
0628b123
PNA
2074 int family = nfmsg->nfgen_family, err = 0;
2075 struct nft_ctx ctx;
96518518 2076
99633ab2 2077 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
2078 if (IS_ERR(afi))
2079 return PTR_ERR(afi);
2080
9370761c 2081 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
2082 if (IS_ERR(table))
2083 return PTR_ERR(table);
55dd6f93
PNA
2084 if (table->flags & NFT_TABLE_INACTIVE)
2085 return -ENOENT;
96518518 2086
cf9dc09d
PNA
2087 if (nla[NFTA_RULE_CHAIN]) {
2088 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
2089 if (IS_ERR(chain))
2090 return PTR_ERR(chain);
2091 }
96518518 2092
0628b123
PNA
2093 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
2094
cf9dc09d
PNA
2095 if (chain) {
2096 if (nla[NFTA_RULE_HANDLE]) {
2097 rule = nf_tables_rule_lookup(chain,
2098 nla[NFTA_RULE_HANDLE]);
2099 if (IS_ERR(rule))
2100 return PTR_ERR(rule);
96518518 2101
5e266fe7 2102 err = nft_delrule(&ctx, rule);
cf9dc09d 2103 } else {
ce24b721 2104 err = nft_delrule_by_chain(&ctx);
cf9dc09d
PNA
2105 }
2106 } else {
2107 list_for_each_entry(chain, &table->chains, list) {
2108 ctx.chain = chain;
ce24b721 2109 err = nft_delrule_by_chain(&ctx);
0628b123
PNA
2110 if (err < 0)
2111 break;
2112 }
2113 }
2114
2115 return err;
2116}
2117
20a69341
PM
2118/*
2119 * Sets
2120 */
2121
2122static LIST_HEAD(nf_tables_set_ops);
2123
2124int nft_register_set(struct nft_set_ops *ops)
2125{
2126 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2127 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
20a69341
PM
2128 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2129 return 0;
2130}
2131EXPORT_SYMBOL_GPL(nft_register_set);
2132
2133void nft_unregister_set(struct nft_set_ops *ops)
2134{
2135 nfnl_lock(NFNL_SUBSYS_NFTABLES);
e688a7f8 2136 list_del_rcu(&ops->list);
20a69341
PM
2137 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2138}
2139EXPORT_SYMBOL_GPL(nft_unregister_set);
2140
c50b960c
PM
2141/*
2142 * Select a set implementation based on the data characteristics and the
2143 * given policy. The total memory use might not be known if no size is
2144 * given, in that case the amount of memory per element is used.
2145 */
2146static const struct nft_set_ops *
2147nft_select_set_ops(const struct nlattr * const nla[],
2148 const struct nft_set_desc *desc,
2149 enum nft_set_policies policy)
20a69341 2150{
c50b960c
PM
2151 const struct nft_set_ops *ops, *bops;
2152 struct nft_set_estimate est, best;
20a69341
PM
2153 u32 features;
2154
2155#ifdef CONFIG_MODULES
2156 if (list_empty(&nf_tables_set_ops)) {
2157 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2158 request_module("nft-set");
2159 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2160 if (!list_empty(&nf_tables_set_ops))
2161 return ERR_PTR(-EAGAIN);
2162 }
2163#endif
2164 features = 0;
2165 if (nla[NFTA_SET_FLAGS] != NULL) {
2166 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2167 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
2168 }
2169
c50b960c
PM
2170 bops = NULL;
2171 best.size = ~0;
2172 best.class = ~0;
2173
20a69341
PM
2174 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2175 if ((ops->features & features) != features)
2176 continue;
c50b960c
PM
2177 if (!ops->estimate(desc, features, &est))
2178 continue;
2179
2180 switch (policy) {
2181 case NFT_SET_POL_PERFORMANCE:
2182 if (est.class < best.class)
2183 break;
2184 if (est.class == best.class && est.size < best.size)
2185 break;
2186 continue;
2187 case NFT_SET_POL_MEMORY:
2188 if (est.size < best.size)
2189 break;
2190 if (est.size == best.size && est.class < best.class)
2191 break;
2192 continue;
2193 default:
2194 break;
2195 }
2196
20a69341
PM
2197 if (!try_module_get(ops->owner))
2198 continue;
c50b960c
PM
2199 if (bops != NULL)
2200 module_put(bops->owner);
2201
2202 bops = ops;
2203 best = est;
20a69341
PM
2204 }
2205
c50b960c
PM
2206 if (bops != NULL)
2207 return bops;
2208
20a69341
PM
2209 return ERR_PTR(-EOPNOTSUPP);
2210}
2211
2212static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2213 [NFTA_SET_TABLE] = { .type = NLA_STRING },
a9bdd836
PNA
2214 [NFTA_SET_NAME] = { .type = NLA_STRING,
2215 .len = IFNAMSIZ - 1 },
20a69341
PM
2216 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2217 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2218 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2219 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2220 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
2221 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2222 [NFTA_SET_DESC] = { .type = NLA_NESTED },
958bee14 2223 [NFTA_SET_ID] = { .type = NLA_U32 },
c50b960c
PM
2224};
2225
2226static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2227 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
20a69341
PM
2228};
2229
2230static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2231 const struct sk_buff *skb,
2232 const struct nlmsghdr *nlh,
2233 const struct nlattr * const nla[])
2234{
99633ab2 2235 struct net *net = sock_net(skb->sk);
20a69341 2236 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8
PNA
2237 struct nft_af_info *afi = NULL;
2238 struct nft_table *table = NULL;
20a69341 2239
c9c8e485
PNA
2240 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2241 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2242 if (IS_ERR(afi))
2243 return PTR_ERR(afi);
2244 }
20a69341
PM
2245
2246 if (nla[NFTA_SET_TABLE] != NULL) {
ec2c9935
PM
2247 if (afi == NULL)
2248 return -EAFNOSUPPORT;
2249
9370761c 2250 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2251 if (IS_ERR(table))
2252 return PTR_ERR(table);
55dd6f93
PNA
2253 if (table->flags & NFT_TABLE_INACTIVE)
2254 return -ENOENT;
20a69341
PM
2255 }
2256
0ca743a5 2257 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2258 return 0;
2259}
2260
2261struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2262 const struct nlattr *nla)
2263{
2264 struct nft_set *set;
2265
2266 if (nla == NULL)
2267 return ERR_PTR(-EINVAL);
2268
2269 list_for_each_entry(set, &table->sets, list) {
2270 if (!nla_strcmp(nla, set->name))
2271 return set;
2272 }
2273 return ERR_PTR(-ENOENT);
2274}
2275
958bee14
PNA
2276struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2277 const struct nlattr *nla)
2278{
2279 struct nft_trans *trans;
2280 u32 id = ntohl(nla_get_be32(nla));
2281
2282 list_for_each_entry(trans, &net->nft.commit_list, list) {
2283 if (trans->msg_type == NFT_MSG_NEWSET &&
2284 id == nft_trans_set_id(trans))
2285 return nft_trans_set(trans);
2286 }
2287 return ERR_PTR(-ENOENT);
2288}
2289
20a69341
PM
2290static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2291 const char *name)
2292{
2293 const struct nft_set *i;
2294 const char *p;
2295 unsigned long *inuse;
60eb1894 2296 unsigned int n = 0, min = 0;
20a69341
PM
2297
2298 p = strnchr(name, IFNAMSIZ, '%');
2299 if (p != NULL) {
2300 if (p[1] != 'd' || strchr(p + 2, '%'))
2301 return -EINVAL;
2302
2303 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2304 if (inuse == NULL)
2305 return -ENOMEM;
60eb1894 2306cont:
20a69341 2307 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
2308 int tmp;
2309
2310 if (!sscanf(i->name, name, &tmp))
20a69341 2311 continue;
60eb1894 2312 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 2313 continue;
14662917 2314
60eb1894 2315 set_bit(tmp - min, inuse);
20a69341
PM
2316 }
2317
53b70287 2318 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
2319 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2320 min += BITS_PER_BYTE * PAGE_SIZE;
2321 memset(inuse, 0, PAGE_SIZE);
2322 goto cont;
2323 }
20a69341
PM
2324 free_page((unsigned long)inuse);
2325 }
2326
60eb1894 2327 snprintf(set->name, sizeof(set->name), name, min + n);
20a69341
PM
2328 list_for_each_entry(i, &ctx->table->sets, list) {
2329 if (!strcmp(set->name, i->name))
2330 return -ENFILE;
2331 }
2332 return 0;
2333}
2334
2335static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2336 const struct nft_set *set, u16 event, u16 flags)
2337{
2338 struct nfgenmsg *nfmsg;
2339 struct nlmsghdr *nlh;
c50b960c 2340 struct nlattr *desc;
128ad332
PNA
2341 u32 portid = ctx->portid;
2342 u32 seq = ctx->seq;
20a69341
PM
2343
2344 event |= NFNL_SUBSYS_NFTABLES << 8;
2345 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2346 flags);
2347 if (nlh == NULL)
2348 goto nla_put_failure;
2349
2350 nfmsg = nlmsg_data(nlh);
2351 nfmsg->nfgen_family = ctx->afi->family;
2352 nfmsg->version = NFNETLINK_V0;
84d7fce6 2353 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
20a69341
PM
2354
2355 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2356 goto nla_put_failure;
2357 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2358 goto nla_put_failure;
2359 if (set->flags != 0)
2360 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2361 goto nla_put_failure;
2362
2363 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2364 goto nla_put_failure;
2365 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2366 goto nla_put_failure;
2367 if (set->flags & NFT_SET_MAP) {
2368 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2369 goto nla_put_failure;
2370 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2371 goto nla_put_failure;
2372 }
2373
9363dc4b
AB
2374 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2375 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2376 goto nla_put_failure;
2377 }
2378
c50b960c
PM
2379 desc = nla_nest_start(skb, NFTA_SET_DESC);
2380 if (desc == NULL)
2381 goto nla_put_failure;
2382 if (set->size &&
2383 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2384 goto nla_put_failure;
2385 nla_nest_end(skb, desc);
2386
053c095a
JB
2387 nlmsg_end(skb, nlh);
2388 return 0;
20a69341
PM
2389
2390nla_put_failure:
2391 nlmsg_trim(skb, nlh);
2392 return -1;
2393}
2394
2395static int nf_tables_set_notify(const struct nft_ctx *ctx,
2396 const struct nft_set *set,
31f8441c 2397 int event, gfp_t gfp_flags)
20a69341
PM
2398{
2399 struct sk_buff *skb;
128ad332 2400 u32 portid = ctx->portid;
20a69341
PM
2401 int err;
2402
128ad332
PNA
2403 if (!ctx->report &&
2404 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
20a69341
PM
2405 return 0;
2406
2407 err = -ENOBUFS;
31f8441c 2408 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
20a69341
PM
2409 if (skb == NULL)
2410 goto err;
2411
2412 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2413 if (err < 0) {
2414 kfree_skb(skb);
2415 goto err;
2416 }
2417
128ad332 2418 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES,
31f8441c 2419 ctx->report, gfp_flags);
20a69341
PM
2420err:
2421 if (err < 0)
99633ab2 2422 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
20a69341
PM
2423 return err;
2424}
2425
5b96af77 2426static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
c9c8e485
PNA
2427{
2428 const struct nft_set *set;
2429 unsigned int idx, s_idx = cb->args[0];
7c95f6d8 2430 struct nft_af_info *afi;
c9c8e485
PNA
2431 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2432 struct net *net = sock_net(skb->sk);
2433 int cur_family = cb->args[3];
5b96af77 2434 struct nft_ctx *ctx = cb->data, ctx_set;
c9c8e485
PNA
2435
2436 if (cb->args[1])
2437 return skb->len;
2438
e688a7f8 2439 rcu_read_lock();
38e029f1
PNA
2440 cb->seq = net->nft.base_seq;
2441
e688a7f8 2442 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
5b96af77
PNA
2443 if (ctx->afi && ctx->afi != afi)
2444 continue;
2445
c9c8e485
PNA
2446 if (cur_family) {
2447 if (afi->family != cur_family)
2448 continue;
2449
2450 cur_family = 0;
2451 }
e688a7f8 2452 list_for_each_entry_rcu(table, &afi->tables, list) {
5b96af77
PNA
2453 if (ctx->table && ctx->table != table)
2454 continue;
2455
c9c8e485
PNA
2456 if (cur_table) {
2457 if (cur_table != table)
2458 continue;
2459
2460 cur_table = NULL;
2461 }
c9c8e485 2462 idx = 0;
5b96af77 2463 list_for_each_entry_rcu(set, &table->sets, list) {
c9c8e485
PNA
2464 if (idx < s_idx)
2465 goto cont;
5b96af77
PNA
2466
2467 ctx_set = *ctx;
2468 ctx_set.table = table;
2469 ctx_set.afi = afi;
2470 if (nf_tables_fill_set(skb, &ctx_set, set,
c9c8e485
PNA
2471 NFT_MSG_NEWSET,
2472 NLM_F_MULTI) < 0) {
2473 cb->args[0] = idx;
2474 cb->args[2] = (unsigned long) table;
2475 cb->args[3] = afi->family;
2476 goto done;
2477 }
38e029f1 2478 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
c9c8e485
PNA
2479cont:
2480 idx++;
2481 }
2482 if (s_idx)
2483 s_idx = 0;
2484 }
2485 }
2486 cb->args[1] = 1;
2487done:
e688a7f8 2488 rcu_read_unlock();
c9c8e485
PNA
2489 return skb->len;
2490}
2491
5b96af77 2492static int nf_tables_dump_sets_done(struct netlink_callback *cb)
20a69341 2493{
5b96af77
PNA
2494 kfree(cb->data);
2495 return 0;
20a69341
PM
2496}
2497
2498static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2499 const struct nlmsghdr *nlh,
2500 const struct nlattr * const nla[])
2501{
2502 const struct nft_set *set;
2503 struct nft_ctx ctx;
2504 struct sk_buff *skb2;
c9c8e485 2505 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2506 int err;
2507
01cfa0a4 2508 /* Verify existence before starting dump */
20a69341
PM
2509 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2510 if (err < 0)
2511 return err;
2512
2513 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2514 struct netlink_dump_control c = {
2515 .dump = nf_tables_dump_sets,
5b96af77 2516 .done = nf_tables_dump_sets_done,
20a69341 2517 };
5b96af77
PNA
2518 struct nft_ctx *ctx_dump;
2519
2520 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2521 if (ctx_dump == NULL)
2522 return -ENOMEM;
2523
2524 *ctx_dump = ctx;
2525 c.data = ctx_dump;
2526
20a69341
PM
2527 return netlink_dump_start(nlsk, skb, nlh, &c);
2528 }
2529
c9c8e485
PNA
2530 /* Only accept unspec with dump */
2531 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2532 return -EAFNOSUPPORT;
2533
20a69341
PM
2534 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2535 if (IS_ERR(set))
2536 return PTR_ERR(set);
958bee14
PNA
2537 if (set->flags & NFT_SET_INACTIVE)
2538 return -ENOENT;
20a69341
PM
2539
2540 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2541 if (skb2 == NULL)
2542 return -ENOMEM;
2543
2544 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2545 if (err < 0)
2546 goto err;
2547
2548 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2549
2550err:
2551 kfree_skb(skb2);
2552 return err;
2553}
2554
c50b960c
PM
2555static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2556 struct nft_set_desc *desc,
2557 const struct nlattr *nla)
2558{
2559 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2560 int err;
2561
2562 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2563 if (err < 0)
2564 return err;
2565
2566 if (da[NFTA_SET_DESC_SIZE] != NULL)
2567 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2568
2569 return 0;
2570}
2571
20a69341
PM
2572static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2573 const struct nlmsghdr *nlh,
2574 const struct nlattr * const nla[])
2575{
2576 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2577 const struct nft_set_ops *ops;
7c95f6d8 2578 struct nft_af_info *afi;
99633ab2 2579 struct net *net = sock_net(skb->sk);
20a69341
PM
2580 struct nft_table *table;
2581 struct nft_set *set;
2582 struct nft_ctx ctx;
2583 char name[IFNAMSIZ];
2584 unsigned int size;
2585 bool create;
c50b960c
PM
2586 u32 ktype, dtype, flags, policy;
2587 struct nft_set_desc desc;
20a69341
PM
2588 int err;
2589
2590 if (nla[NFTA_SET_TABLE] == NULL ||
2591 nla[NFTA_SET_NAME] == NULL ||
958bee14
PNA
2592 nla[NFTA_SET_KEY_LEN] == NULL ||
2593 nla[NFTA_SET_ID] == NULL)
20a69341
PM
2594 return -EINVAL;
2595
c50b960c
PM
2596 memset(&desc, 0, sizeof(desc));
2597
20a69341
PM
2598 ktype = NFT_DATA_VALUE;
2599 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2600 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2601 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2602 return -EINVAL;
2603 }
2604
c50b960c
PM
2605 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2606 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
20a69341
PM
2607 return -EINVAL;
2608
2609 flags = 0;
2610 if (nla[NFTA_SET_FLAGS] != NULL) {
2611 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2612 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2613 NFT_SET_INTERVAL | NFT_SET_MAP))
2614 return -EINVAL;
2615 }
2616
2617 dtype = 0;
20a69341
PM
2618 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2619 if (!(flags & NFT_SET_MAP))
2620 return -EINVAL;
2621
2622 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2623 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2624 dtype != NFT_DATA_VERDICT)
2625 return -EINVAL;
2626
2627 if (dtype != NFT_DATA_VERDICT) {
2628 if (nla[NFTA_SET_DATA_LEN] == NULL)
2629 return -EINVAL;
c50b960c
PM
2630 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2631 if (desc.dlen == 0 ||
2632 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
20a69341
PM
2633 return -EINVAL;
2634 } else
c50b960c 2635 desc.dlen = sizeof(struct nft_data);
20a69341
PM
2636 } else if (flags & NFT_SET_MAP)
2637 return -EINVAL;
2638
c50b960c
PM
2639 policy = NFT_SET_POL_PERFORMANCE;
2640 if (nla[NFTA_SET_POLICY] != NULL)
2641 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2642
2643 if (nla[NFTA_SET_DESC] != NULL) {
2644 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2645 if (err < 0)
2646 return err;
2647 }
2648
20a69341
PM
2649 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2650
99633ab2 2651 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
20a69341
PM
2652 if (IS_ERR(afi))
2653 return PTR_ERR(afi);
2654
9370761c 2655 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2656 if (IS_ERR(table))
2657 return PTR_ERR(table);
2658
0ca743a5 2659 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2660
2661 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2662 if (IS_ERR(set)) {
2663 if (PTR_ERR(set) != -ENOENT)
2664 return PTR_ERR(set);
2665 set = NULL;
2666 }
2667
2668 if (set != NULL) {
2669 if (nlh->nlmsg_flags & NLM_F_EXCL)
2670 return -EEXIST;
2671 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2672 return -EOPNOTSUPP;
2673 return 0;
2674 }
2675
2676 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2677 return -ENOENT;
2678
c50b960c 2679 ops = nft_select_set_ops(nla, &desc, policy);
20a69341
PM
2680 if (IS_ERR(ops))
2681 return PTR_ERR(ops);
2682
2683 size = 0;
2684 if (ops->privsize != NULL)
2685 size = ops->privsize(nla);
2686
2687 err = -ENOMEM;
2688 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2689 if (set == NULL)
2690 goto err1;
2691
2692 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2693 err = nf_tables_set_alloc_name(&ctx, set, name);
2694 if (err < 0)
2695 goto err2;
2696
2697 INIT_LIST_HEAD(&set->bindings);
2698 set->ops = ops;
2699 set->ktype = ktype;
c50b960c 2700 set->klen = desc.klen;
20a69341 2701 set->dtype = dtype;
c50b960c 2702 set->dlen = desc.dlen;
20a69341 2703 set->flags = flags;
c50b960c 2704 set->size = desc.size;
9363dc4b 2705 set->policy = policy;
20a69341 2706
c50b960c 2707 err = ops->init(set, &desc, nla);
20a69341
PM
2708 if (err < 0)
2709 goto err2;
2710
958bee14 2711 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
20a69341
PM
2712 if (err < 0)
2713 goto err2;
2714
e688a7f8 2715 list_add_tail_rcu(&set->list, &table->sets);
4fefee57 2716 table->use++;
20a69341
PM
2717 return 0;
2718
2719err2:
2720 kfree(set);
2721err1:
2722 module_put(ops->owner);
2723 return err;
2724}
2725
958bee14 2726static void nft_set_destroy(struct nft_set *set)
20a69341 2727{
20a69341
PM
2728 set->ops->destroy(set);
2729 module_put(set->ops->owner);
2730 kfree(set);
2731}
2732
958bee14
PNA
2733static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2734{
e688a7f8 2735 list_del_rcu(&set->list);
31f8441c 2736 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
958bee14
PNA
2737 nft_set_destroy(set);
2738}
2739
20a69341
PM
2740static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2741 const struct nlmsghdr *nlh,
2742 const struct nlattr * const nla[])
2743{
c9c8e485 2744 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2745 struct nft_set *set;
2746 struct nft_ctx ctx;
2747 int err;
2748
ec2c9935
PM
2749 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2750 return -EAFNOSUPPORT;
20a69341
PM
2751 if (nla[NFTA_SET_TABLE] == NULL)
2752 return -EINVAL;
2753
2754 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2755 if (err < 0)
2756 return err;
2757
2758 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2759 if (IS_ERR(set))
2760 return PTR_ERR(set);
958bee14
PNA
2761 if (set->flags & NFT_SET_INACTIVE)
2762 return -ENOENT;
20a69341
PM
2763 if (!list_empty(&set->bindings))
2764 return -EBUSY;
2765
ee01d542 2766 return nft_delset(&ctx, set);
20a69341
PM
2767}
2768
2769static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2770 const struct nft_set *set,
2771 const struct nft_set_iter *iter,
2772 const struct nft_set_elem *elem)
2773{
2774 enum nft_registers dreg;
2775
2776 dreg = nft_type_to_reg(set->dtype);
2ee0d3c8
PNA
2777 return nft_validate_data_load(ctx, dreg, &elem->data,
2778 set->dtype == NFT_DATA_VERDICT ?
2779 NFT_DATA_VERDICT : NFT_DATA_VALUE);
20a69341
PM
2780}
2781
2782int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2783 struct nft_set_binding *binding)
2784{
2785 struct nft_set_binding *i;
2786 struct nft_set_iter iter;
2787
2788 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2789 return -EBUSY;
2790
2791 if (set->flags & NFT_SET_MAP) {
2792 /* If the set is already bound to the same chain all
2793 * jumps are already validated for that chain.
2794 */
2795 list_for_each_entry(i, &set->bindings, list) {
2796 if (i->chain == binding->chain)
2797 goto bind;
2798 }
2799
2800 iter.skip = 0;
2801 iter.count = 0;
2802 iter.err = 0;
2803 iter.fn = nf_tables_bind_check_setelem;
2804
2805 set->ops->walk(ctx, set, &iter);
2806 if (iter.err < 0) {
2807 /* Destroy anonymous sets if binding fails */
2808 if (set->flags & NFT_SET_ANONYMOUS)
2809 nf_tables_set_destroy(ctx, set);
2810
2811 return iter.err;
2812 }
2813 }
2814bind:
2815 binding->chain = ctx->chain;
e688a7f8 2816 list_add_tail_rcu(&binding->list, &set->bindings);
20a69341
PM
2817 return 0;
2818}
2819
2820void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2821 struct nft_set_binding *binding)
2822{
e688a7f8 2823 list_del_rcu(&binding->list);
20a69341 2824
958bee14
PNA
2825 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
2826 !(set->flags & NFT_SET_INACTIVE))
20a69341
PM
2827 nf_tables_set_destroy(ctx, set);
2828}
2829
2830/*
2831 * Set elements
2832 */
2833
2834static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2835 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2836 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2837 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2838};
2839
2840static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2841 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2842 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2843 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
958bee14 2844 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
20a69341
PM
2845};
2846
2847static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2848 const struct sk_buff *skb,
2849 const struct nlmsghdr *nlh,
55dd6f93
PNA
2850 const struct nlattr * const nla[],
2851 bool trans)
20a69341
PM
2852{
2853 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7c95f6d8
PNA
2854 struct nft_af_info *afi;
2855 struct nft_table *table;
99633ab2 2856 struct net *net = sock_net(skb->sk);
20a69341 2857
99633ab2 2858 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
20a69341
PM
2859 if (IS_ERR(afi))
2860 return PTR_ERR(afi);
2861
9370761c 2862 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
20a69341
PM
2863 if (IS_ERR(table))
2864 return PTR_ERR(table);
55dd6f93
PNA
2865 if (!trans && (table->flags & NFT_TABLE_INACTIVE))
2866 return -ENOENT;
20a69341 2867
0ca743a5 2868 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2869 return 0;
2870}
2871
2872static int nf_tables_fill_setelem(struct sk_buff *skb,
2873 const struct nft_set *set,
2874 const struct nft_set_elem *elem)
2875{
2876 unsigned char *b = skb_tail_pointer(skb);
2877 struct nlattr *nest;
2878
2879 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2880 if (nest == NULL)
2881 goto nla_put_failure;
2882
2883 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2884 set->klen) < 0)
2885 goto nla_put_failure;
2886
2887 if (set->flags & NFT_SET_MAP &&
2888 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2889 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2890 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2891 set->dlen) < 0)
2892 goto nla_put_failure;
2893
2894 if (elem->flags != 0)
2895 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2896 goto nla_put_failure;
2897
2898 nla_nest_end(skb, nest);
2899 return 0;
2900
2901nla_put_failure:
2902 nlmsg_trim(skb, b);
2903 return -EMSGSIZE;
2904}
2905
2906struct nft_set_dump_args {
2907 const struct netlink_callback *cb;
2908 struct nft_set_iter iter;
2909 struct sk_buff *skb;
2910};
2911
2912static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2913 const struct nft_set *set,
2914 const struct nft_set_iter *iter,
2915 const struct nft_set_elem *elem)
2916{
2917 struct nft_set_dump_args *args;
2918
2919 args = container_of(iter, struct nft_set_dump_args, iter);
2920 return nf_tables_fill_setelem(args->skb, set, elem);
2921}
2922
2923static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2924{
2925 const struct nft_set *set;
2926 struct nft_set_dump_args args;
2927 struct nft_ctx ctx;
2928 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2929 struct nfgenmsg *nfmsg;
2930 struct nlmsghdr *nlh;
2931 struct nlattr *nest;
2932 u32 portid, seq;
2933 int event, err;
2934
720e0dfa
MN
2935 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2936 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
20a69341
PM
2937 if (err < 0)
2938 return err;
2939
55dd6f93
PNA
2940 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla,
2941 false);
20a69341
PM
2942 if (err < 0)
2943 return err;
2944
2945 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2946 if (IS_ERR(set))
2947 return PTR_ERR(set);
958bee14
PNA
2948 if (set->flags & NFT_SET_INACTIVE)
2949 return -ENOENT;
20a69341
PM
2950
2951 event = NFT_MSG_NEWSETELEM;
2952 event |= NFNL_SUBSYS_NFTABLES << 8;
2953 portid = NETLINK_CB(cb->skb).portid;
2954 seq = cb->nlh->nlmsg_seq;
2955
2956 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2957 NLM_F_MULTI);
2958 if (nlh == NULL)
2959 goto nla_put_failure;
2960
2961 nfmsg = nlmsg_data(nlh);
6403d962 2962 nfmsg->nfgen_family = ctx.afi->family;
20a69341 2963 nfmsg->version = NFNETLINK_V0;
84d7fce6 2964 nfmsg->res_id = htons(ctx.net->nft.base_seq & 0xffff);
20a69341
PM
2965
2966 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2967 goto nla_put_failure;
2968 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2969 goto nla_put_failure;
2970
2971 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2972 if (nest == NULL)
2973 goto nla_put_failure;
2974
2975 args.cb = cb;
2976 args.skb = skb;
2977 args.iter.skip = cb->args[0];
2978 args.iter.count = 0;
2979 args.iter.err = 0;
2980 args.iter.fn = nf_tables_dump_setelem;
2981 set->ops->walk(&ctx, set, &args.iter);
2982
2983 nla_nest_end(skb, nest);
2984 nlmsg_end(skb, nlh);
2985
2986 if (args.iter.err && args.iter.err != -EMSGSIZE)
2987 return args.iter.err;
2988 if (args.iter.count == cb->args[0])
2989 return 0;
2990
2991 cb->args[0] = args.iter.count;
2992 return skb->len;
2993
2994nla_put_failure:
2995 return -ENOSPC;
2996}
2997
2998static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2999 const struct nlmsghdr *nlh,
3000 const struct nlattr * const nla[])
3001{
3002 const struct nft_set *set;
3003 struct nft_ctx ctx;
3004 int err;
3005
55dd6f93 3006 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
20a69341
PM
3007 if (err < 0)
3008 return err;
3009
3010 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3011 if (IS_ERR(set))
3012 return PTR_ERR(set);
958bee14
PNA
3013 if (set->flags & NFT_SET_INACTIVE)
3014 return -ENOENT;
20a69341
PM
3015
3016 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3017 struct netlink_dump_control c = {
3018 .dump = nf_tables_dump_set,
3019 };
3020 return netlink_dump_start(nlsk, skb, nlh, &c);
3021 }
3022 return -EOPNOTSUPP;
3023}
3024
d60ce62f
AB
3025static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3026 const struct nft_ctx *ctx, u32 seq,
3027 u32 portid, int event, u16 flags,
3028 const struct nft_set *set,
3029 const struct nft_set_elem *elem)
3030{
3031 struct nfgenmsg *nfmsg;
3032 struct nlmsghdr *nlh;
3033 struct nlattr *nest;
3034 int err;
3035
3036 event |= NFNL_SUBSYS_NFTABLES << 8;
3037 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3038 flags);
3039 if (nlh == NULL)
3040 goto nla_put_failure;
3041
3042 nfmsg = nlmsg_data(nlh);
3043 nfmsg->nfgen_family = ctx->afi->family;
3044 nfmsg->version = NFNETLINK_V0;
84d7fce6 3045 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
d60ce62f
AB
3046
3047 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3048 goto nla_put_failure;
3049 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3050 goto nla_put_failure;
3051
3052 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3053 if (nest == NULL)
3054 goto nla_put_failure;
3055
3056 err = nf_tables_fill_setelem(skb, set, elem);
3057 if (err < 0)
3058 goto nla_put_failure;
3059
3060 nla_nest_end(skb, nest);
3061
053c095a
JB
3062 nlmsg_end(skb, nlh);
3063 return 0;
d60ce62f
AB
3064
3065nla_put_failure:
3066 nlmsg_trim(skb, nlh);
3067 return -1;
3068}
3069
3070static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
3071 const struct nft_set *set,
3072 const struct nft_set_elem *elem,
3073 int event, u16 flags)
3074{
128ad332
PNA
3075 struct net *net = ctx->net;
3076 u32 portid = ctx->portid;
d60ce62f
AB
3077 struct sk_buff *skb;
3078 int err;
3079
128ad332 3080 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
d60ce62f
AB
3081 return 0;
3082
3083 err = -ENOBUFS;
3084 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3085 if (skb == NULL)
3086 goto err;
3087
3088 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3089 set, elem);
3090 if (err < 0) {
3091 kfree_skb(skb);
3092 goto err;
3093 }
3094
128ad332 3095 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
d60ce62f
AB
3096 GFP_KERNEL);
3097err:
3098 if (err < 0)
3099 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
3100 return err;
3101}
3102
60319eb1
PNA
3103static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3104 int msg_type,
3105 struct nft_set *set)
3106{
3107 struct nft_trans *trans;
3108
3109 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3110 if (trans == NULL)
3111 return NULL;
3112
3113 nft_trans_elem_set(trans) = set;
3114 return trans;
3115}
3116
3117static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
3118 const struct nlattr *attr)
3119{
3120 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3121 struct nft_data_desc d1, d2;
3122 struct nft_set_elem elem;
3123 struct nft_set_binding *binding;
3124 enum nft_registers dreg;
60319eb1 3125 struct nft_trans *trans;
20a69341
PM
3126 int err;
3127
c50b960c
PM
3128 if (set->size && set->nelems == set->size)
3129 return -ENFILE;
3130
20a69341
PM
3131 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3132 nft_set_elem_policy);
3133 if (err < 0)
3134 return err;
3135
3136 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3137 return -EINVAL;
3138
3139 elem.flags = 0;
3140 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3141 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3142 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
3143 return -EINVAL;
55df35d2
PM
3144 if (!(set->flags & NFT_SET_INTERVAL) &&
3145 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3146 return -EINVAL;
20a69341
PM
3147 }
3148
3149 if (set->flags & NFT_SET_MAP) {
3150 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3151 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
3152 return -EINVAL;
bd7fc645
PNA
3153 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3154 elem.flags & NFT_SET_ELEM_INTERVAL_END)
3155 return -EINVAL;
20a69341
PM
3156 } else {
3157 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3158 return -EINVAL;
3159 }
3160
3161 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
3162 if (err < 0)
3163 goto err1;
3164 err = -EINVAL;
3165 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3166 goto err2;
3167
3168 err = -EEXIST;
3169 if (set->ops->get(set, &elem) == 0)
3170 goto err2;
3171
3172 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3173 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
3174 if (err < 0)
3175 goto err2;
3176
3177 err = -EINVAL;
3178 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3179 goto err3;
3180
3181 dreg = nft_type_to_reg(set->dtype);
3182 list_for_each_entry(binding, &set->bindings, list) {
3183 struct nft_ctx bind_ctx = {
3184 .afi = ctx->afi,
3185 .table = ctx->table,
7c95f6d8 3186 .chain = (struct nft_chain *)binding->chain,
20a69341
PM
3187 };
3188
3189 err = nft_validate_data_load(&bind_ctx, dreg,
3190 &elem.data, d2.type);
3191 if (err < 0)
3192 goto err3;
3193 }
3194 }
3195
60319eb1
PNA
3196 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3197 if (trans == NULL)
3198 goto err3;
3199
20a69341
PM
3200 err = set->ops->insert(set, &elem);
3201 if (err < 0)
60319eb1 3202 goto err4;
20a69341 3203
60319eb1 3204 nft_trans_elem(trans) = elem;
46bbafce 3205 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
20a69341
PM
3206 return 0;
3207
60319eb1
PNA
3208err4:
3209 kfree(trans);
20a69341
PM
3210err3:
3211 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3212 nft_data_uninit(&elem.data, d2.type);
3213err2:
3214 nft_data_uninit(&elem.key, d1.type);
3215err1:
3216 return err;
3217}
3218
3219static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3220 const struct nlmsghdr *nlh,
3221 const struct nlattr * const nla[])
3222{
958bee14 3223 struct net *net = sock_net(skb->sk);
20a69341
PM
3224 const struct nlattr *attr;
3225 struct nft_set *set;
3226 struct nft_ctx ctx;
60319eb1 3227 int rem, err = 0;
20a69341 3228
7d5570ca
PNA
3229 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3230 return -EINVAL;
3231
55dd6f93 3232 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, true);
20a69341
PM
3233 if (err < 0)
3234 return err;
3235
3236 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
958bee14
PNA
3237 if (IS_ERR(set)) {
3238 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3239 set = nf_tables_set_lookup_byid(net,
3240 nla[NFTA_SET_ELEM_LIST_SET_ID]);
3241 }
3242 if (IS_ERR(set))
3243 return PTR_ERR(set);
3244 }
3245
20a69341
PM
3246 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3247 return -EBUSY;
3248
3249 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3250 err = nft_add_set_elem(&ctx, set, attr);
3251 if (err < 0)
60319eb1 3252 break;
4fefee57
PNA
3253
3254 set->nelems++;
20a69341 3255 }
60319eb1 3256 return err;
20a69341
PM
3257}
3258
60319eb1 3259static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
20a69341
PM
3260 const struct nlattr *attr)
3261{
3262 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3263 struct nft_data_desc desc;
3264 struct nft_set_elem elem;
60319eb1 3265 struct nft_trans *trans;
20a69341
PM
3266 int err;
3267
3268 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3269 nft_set_elem_policy);
3270 if (err < 0)
3271 goto err1;
3272
3273 err = -EINVAL;
3274 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3275 goto err1;
3276
3277 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3278 if (err < 0)
3279 goto err1;
3280
3281 err = -EINVAL;
3282 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3283 goto err2;
3284
3285 err = set->ops->get(set, &elem);
3286 if (err < 0)
3287 goto err2;
3288
60319eb1 3289 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
609ccf08
JL
3290 if (trans == NULL) {
3291 err = -ENOMEM;
60319eb1 3292 goto err2;
609ccf08 3293 }
20a69341 3294
60319eb1 3295 nft_trans_elem(trans) = elem;
46bbafce 3296 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
0dc13625 3297 return 0;
20a69341
PM
3298err2:
3299 nft_data_uninit(&elem.key, desc.type);
3300err1:
3301 return err;
3302}
3303
3304static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3305 const struct nlmsghdr *nlh,
3306 const struct nlattr * const nla[])
3307{
3308 const struct nlattr *attr;
3309 struct nft_set *set;
3310 struct nft_ctx ctx;
60319eb1 3311 int rem, err = 0;
20a69341 3312
7d5570ca
PNA
3313 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3314 return -EINVAL;
3315
55dd6f93 3316 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla, false);
20a69341
PM
3317 if (err < 0)
3318 return err;
3319
3320 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3321 if (IS_ERR(set))
3322 return PTR_ERR(set);
3323 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3324 return -EBUSY;
3325
3326 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3327 err = nft_del_setelem(&ctx, set, attr);
3328 if (err < 0)
60319eb1 3329 break;
4fefee57
PNA
3330
3331 set->nelems--;
20a69341 3332 }
60319eb1 3333 return err;
20a69341
PM
3334}
3335
84d7fce6
PNA
3336static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3337 u32 portid, u32 seq)
3338{
3339 struct nlmsghdr *nlh;
3340 struct nfgenmsg *nfmsg;
3341 int event = (NFNL_SUBSYS_NFTABLES << 8) | NFT_MSG_NEWGEN;
3342
3343 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
3344 if (nlh == NULL)
3345 goto nla_put_failure;
3346
3347 nfmsg = nlmsg_data(nlh);
3348 nfmsg->nfgen_family = AF_UNSPEC;
3349 nfmsg->version = NFNETLINK_V0;
3350 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3351
3352 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
3353 goto nla_put_failure;
3354
053c095a
JB
3355 nlmsg_end(skb, nlh);
3356 return 0;
84d7fce6
PNA
3357
3358nla_put_failure:
3359 nlmsg_trim(skb, nlh);
3360 return -EMSGSIZE;
3361}
3362
3363static int nf_tables_gen_notify(struct net *net, struct sk_buff *skb, int event)
3364{
3365 struct nlmsghdr *nlh = nlmsg_hdr(skb);
3366 struct sk_buff *skb2;
3367 int err;
3368
3369 if (nlmsg_report(nlh) &&
3370 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3371 return 0;
3372
3373 err = -ENOBUFS;
3374 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3375 if (skb2 == NULL)
3376 goto err;
3377
3378 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3379 nlh->nlmsg_seq);
3380 if (err < 0) {
3381 kfree_skb(skb2);
3382 goto err;
3383 }
3384
3385 err = nfnetlink_send(skb2, net, NETLINK_CB(skb).portid,
3386 NFNLGRP_NFTABLES, nlmsg_report(nlh), GFP_KERNEL);
3387err:
3388 if (err < 0) {
3389 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
3390 err);
3391 }
3392 return err;
3393}
3394
3395static int nf_tables_getgen(struct sock *nlsk, struct sk_buff *skb,
3396 const struct nlmsghdr *nlh,
3397 const struct nlattr * const nla[])
3398{
3399 struct net *net = sock_net(skb->sk);
3400 struct sk_buff *skb2;
3401 int err;
3402
3403 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
3404 if (skb2 == NULL)
3405 return -ENOMEM;
3406
3407 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
3408 nlh->nlmsg_seq);
3409 if (err < 0)
3410 goto err;
3411
3412 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
3413err:
3414 kfree_skb(skb2);
3415 return err;
3416}
3417
96518518
PM
3418static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3419 [NFT_MSG_NEWTABLE] = {
55dd6f93 3420 .call_batch = nf_tables_newtable,
96518518
PM
3421 .attr_count = NFTA_TABLE_MAX,
3422 .policy = nft_table_policy,
3423 },
3424 [NFT_MSG_GETTABLE] = {
3425 .call = nf_tables_gettable,
3426 .attr_count = NFTA_TABLE_MAX,
3427 .policy = nft_table_policy,
3428 },
3429 [NFT_MSG_DELTABLE] = {
55dd6f93 3430 .call_batch = nf_tables_deltable,
96518518
PM
3431 .attr_count = NFTA_TABLE_MAX,
3432 .policy = nft_table_policy,
3433 },
3434 [NFT_MSG_NEWCHAIN] = {
91c7b38d 3435 .call_batch = nf_tables_newchain,
96518518
PM
3436 .attr_count = NFTA_CHAIN_MAX,
3437 .policy = nft_chain_policy,
3438 },
3439 [NFT_MSG_GETCHAIN] = {
3440 .call = nf_tables_getchain,
3441 .attr_count = NFTA_CHAIN_MAX,
3442 .policy = nft_chain_policy,
3443 },
3444 [NFT_MSG_DELCHAIN] = {
91c7b38d 3445 .call_batch = nf_tables_delchain,
96518518
PM
3446 .attr_count = NFTA_CHAIN_MAX,
3447 .policy = nft_chain_policy,
3448 },
3449 [NFT_MSG_NEWRULE] = {
0628b123 3450 .call_batch = nf_tables_newrule,
96518518
PM
3451 .attr_count = NFTA_RULE_MAX,
3452 .policy = nft_rule_policy,
3453 },
3454 [NFT_MSG_GETRULE] = {
3455 .call = nf_tables_getrule,
3456 .attr_count = NFTA_RULE_MAX,
3457 .policy = nft_rule_policy,
3458 },
3459 [NFT_MSG_DELRULE] = {
0628b123 3460 .call_batch = nf_tables_delrule,
96518518
PM
3461 .attr_count = NFTA_RULE_MAX,
3462 .policy = nft_rule_policy,
3463 },
20a69341 3464 [NFT_MSG_NEWSET] = {
958bee14 3465 .call_batch = nf_tables_newset,
20a69341
PM
3466 .attr_count = NFTA_SET_MAX,
3467 .policy = nft_set_policy,
3468 },
3469 [NFT_MSG_GETSET] = {
3470 .call = nf_tables_getset,
3471 .attr_count = NFTA_SET_MAX,
3472 .policy = nft_set_policy,
3473 },
3474 [NFT_MSG_DELSET] = {
958bee14 3475 .call_batch = nf_tables_delset,
20a69341
PM
3476 .attr_count = NFTA_SET_MAX,
3477 .policy = nft_set_policy,
3478 },
3479 [NFT_MSG_NEWSETELEM] = {
958bee14 3480 .call_batch = nf_tables_newsetelem,
20a69341
PM
3481 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3482 .policy = nft_set_elem_list_policy,
3483 },
3484 [NFT_MSG_GETSETELEM] = {
3485 .call = nf_tables_getsetelem,
3486 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3487 .policy = nft_set_elem_list_policy,
3488 },
3489 [NFT_MSG_DELSETELEM] = {
958bee14 3490 .call_batch = nf_tables_delsetelem,
20a69341
PM
3491 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3492 .policy = nft_set_elem_list_policy,
3493 },
84d7fce6
PNA
3494 [NFT_MSG_GETGEN] = {
3495 .call = nf_tables_getgen,
3496 },
96518518
PM
3497};
3498
91c7b38d
PNA
3499static void nft_chain_commit_update(struct nft_trans *trans)
3500{
3501 struct nft_base_chain *basechain;
3502
3503 if (nft_trans_chain_name(trans)[0])
3504 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
3505
3506 if (!(trans->ctx.chain->flags & NFT_BASE_CHAIN))
3507 return;
3508
3509 basechain = nft_base_chain(trans->ctx.chain);
3510 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
3511
3512 switch (nft_trans_chain_policy(trans)) {
3513 case NF_DROP:
3514 case NF_ACCEPT:
3515 basechain->policy = nft_trans_chain_policy(trans);
3516 break;
3517 }
3518}
3519
b326dd37 3520static void nf_tables_commit_release(struct nft_trans *trans)
c7c32e72 3521{
c7c32e72
PNA
3522 switch (trans->msg_type) {
3523 case NFT_MSG_DELTABLE:
3524 nf_tables_table_destroy(&trans->ctx);
3525 break;
3526 case NFT_MSG_DELCHAIN:
3527 nf_tables_chain_destroy(trans->ctx.chain);
3528 break;
3529 case NFT_MSG_DELRULE:
3530 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3531 break;
3532 case NFT_MSG_DELSET:
3533 nft_set_destroy(nft_trans_set(trans));
3534 break;
3535 }
3536 kfree(trans);
3537}
3538
37082f93
PNA
3539static int nf_tables_commit(struct sk_buff *skb)
3540{
3541 struct net *net = sock_net(skb->sk);
3542 struct nft_trans *trans, *next;
a3716e70 3543 struct nft_trans_elem *te;
37082f93
PNA
3544
3545 /* Bump generation counter, invalidate any dump in progress */
38e029f1 3546 while (++net->nft.base_seq == 0);
37082f93
PNA
3547
3548 /* A new generation has just started */
3549 net->nft.gencursor = gencursor_next(net);
3550
3551 /* Make sure all packets have left the previous generation before
3552 * purging old rules.
3553 */
3554 synchronize_rcu();
3555
3556 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
b380e5c7 3557 switch (trans->msg_type) {
55dd6f93
PNA
3558 case NFT_MSG_NEWTABLE:
3559 if (nft_trans_table_update(trans)) {
3560 if (!nft_trans_table_enable(trans)) {
3561 nf_tables_table_disable(trans->ctx.afi,
3562 trans->ctx.table);
3563 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3564 }
3565 } else {
3566 trans->ctx.table->flags &= ~NFT_TABLE_INACTIVE;
3567 }
35151d84 3568 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
55dd6f93
PNA
3569 nft_trans_destroy(trans);
3570 break;
3571 case NFT_MSG_DELTABLE:
35151d84 3572 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
55dd6f93 3573 break;
91c7b38d
PNA
3574 case NFT_MSG_NEWCHAIN:
3575 if (nft_trans_chain_update(trans))
3576 nft_chain_commit_update(trans);
4fefee57 3577 else
91c7b38d 3578 trans->ctx.chain->flags &= ~NFT_CHAIN_INACTIVE;
4fefee57 3579
35151d84 3580 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
91c7b38d
PNA
3581 nft_trans_destroy(trans);
3582 break;
3583 case NFT_MSG_DELCHAIN:
35151d84 3584 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
c5598794
AB
3585 nf_tables_unregister_hooks(trans->ctx.table,
3586 trans->ctx.chain,
3587 trans->ctx.afi->nops);
91c7b38d 3588 break;
b380e5c7
PNA
3589 case NFT_MSG_NEWRULE:
3590 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
35151d84 3591 nf_tables_rule_notify(&trans->ctx,
37082f93 3592 nft_trans_rule(trans),
35151d84 3593 NFT_MSG_NEWRULE);
37082f93 3594 nft_trans_destroy(trans);
b380e5c7
PNA
3595 break;
3596 case NFT_MSG_DELRULE:
3597 list_del_rcu(&nft_trans_rule(trans)->list);
35151d84
PNA
3598 nf_tables_rule_notify(&trans->ctx,
3599 nft_trans_rule(trans),
3600 NFT_MSG_DELRULE);
b380e5c7 3601 break;
958bee14
PNA
3602 case NFT_MSG_NEWSET:
3603 nft_trans_set(trans)->flags &= ~NFT_SET_INACTIVE;
4fefee57
PNA
3604 /* This avoids hitting -EBUSY when deleting the table
3605 * from the transaction.
3606 */
3607 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
3608 !list_empty(&nft_trans_set(trans)->bindings))
3609 trans->ctx.table->use--;
3610
958bee14 3611 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 3612 NFT_MSG_NEWSET, GFP_KERNEL);
958bee14
PNA
3613 nft_trans_destroy(trans);
3614 break;
3615 case NFT_MSG_DELSET:
3616 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
31f8441c 3617 NFT_MSG_DELSET, GFP_KERNEL);
958bee14 3618 break;
60319eb1 3619 case NFT_MSG_NEWSETELEM:
60319eb1
PNA
3620 nf_tables_setelem_notify(&trans->ctx,
3621 nft_trans_elem_set(trans),
3622 &nft_trans_elem(trans),
3623 NFT_MSG_NEWSETELEM, 0);
3624 nft_trans_destroy(trans);
3625 break;
3626 case NFT_MSG_DELSETELEM:
a3716e70
PNA
3627 te = (struct nft_trans_elem *)trans->data;
3628 nf_tables_setelem_notify(&trans->ctx, te->set,
3629 &te->elem,
60319eb1 3630 NFT_MSG_DELSETELEM, 0);
a3716e70 3631 te->set->ops->get(te->set, &te->elem);
a3716e70 3632 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
02263db0
PNA
3633 if (te->set->flags & NFT_SET_MAP &&
3634 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3635 nft_data_uninit(&te->elem.data, te->set->dtype);
3636 te->set->ops->remove(te->set, &te->elem);
60319eb1
PNA
3637 nft_trans_destroy(trans);
3638 break;
37082f93 3639 }
37082f93
PNA
3640 }
3641
b326dd37
PNA
3642 synchronize_rcu();
3643
37082f93 3644 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
c7c32e72 3645 list_del(&trans->list);
b326dd37 3646 nf_tables_commit_release(trans);
37082f93 3647 }
84d7fce6
PNA
3648
3649 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
37082f93
PNA
3650
3651 return 0;
3652}
3653
b326dd37 3654static void nf_tables_abort_release(struct nft_trans *trans)
c7c32e72 3655{
c7c32e72
PNA
3656 switch (trans->msg_type) {
3657 case NFT_MSG_NEWTABLE:
3658 nf_tables_table_destroy(&trans->ctx);
3659 break;
3660 case NFT_MSG_NEWCHAIN:
3661 nf_tables_chain_destroy(trans->ctx.chain);
3662 break;
3663 case NFT_MSG_NEWRULE:
3664 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
3665 break;
3666 case NFT_MSG_NEWSET:
3667 nft_set_destroy(nft_trans_set(trans));
3668 break;
3669 }
3670 kfree(trans);
3671}
3672
37082f93
PNA
3673static int nf_tables_abort(struct sk_buff *skb)
3674{
3675 struct net *net = sock_net(skb->sk);
3676 struct nft_trans *trans, *next;
02263db0 3677 struct nft_trans_elem *te;
37082f93
PNA
3678
3679 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
b380e5c7 3680 switch (trans->msg_type) {
55dd6f93
PNA
3681 case NFT_MSG_NEWTABLE:
3682 if (nft_trans_table_update(trans)) {
3683 if (nft_trans_table_enable(trans)) {
3684 nf_tables_table_disable(trans->ctx.afi,
3685 trans->ctx.table);
3686 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
3687 }
3688 nft_trans_destroy(trans);
3689 } else {
e688a7f8 3690 list_del_rcu(&trans->ctx.table->list);
55dd6f93
PNA
3691 }
3692 break;
3693 case NFT_MSG_DELTABLE:
e688a7f8
PNA
3694 list_add_tail_rcu(&trans->ctx.table->list,
3695 &trans->ctx.afi->tables);
55dd6f93
PNA
3696 nft_trans_destroy(trans);
3697 break;
91c7b38d
PNA
3698 case NFT_MSG_NEWCHAIN:
3699 if (nft_trans_chain_update(trans)) {
982f4051 3700 free_percpu(nft_trans_chain_stats(trans));
91c7b38d
PNA
3701
3702 nft_trans_destroy(trans);
3703 } else {
4fefee57 3704 trans->ctx.table->use--;
e688a7f8 3705 list_del_rcu(&trans->ctx.chain->list);
c5598794
AB
3706 nf_tables_unregister_hooks(trans->ctx.table,
3707 trans->ctx.chain,
3708 trans->ctx.afi->nops);
91c7b38d
PNA
3709 }
3710 break;
3711 case NFT_MSG_DELCHAIN:
4fefee57 3712 trans->ctx.table->use++;
e688a7f8
PNA
3713 list_add_tail_rcu(&trans->ctx.chain->list,
3714 &trans->ctx.table->chains);
91c7b38d
PNA
3715 nft_trans_destroy(trans);
3716 break;
b380e5c7 3717 case NFT_MSG_NEWRULE:
4fefee57 3718 trans->ctx.chain->use--;
b380e5c7
PNA
3719 list_del_rcu(&nft_trans_rule(trans)->list);
3720 break;
3721 case NFT_MSG_DELRULE:
4fefee57 3722 trans->ctx.chain->use++;
b380e5c7 3723 nft_rule_clear(trans->ctx.net, nft_trans_rule(trans));
37082f93 3724 nft_trans_destroy(trans);
b380e5c7 3725 break;
958bee14 3726 case NFT_MSG_NEWSET:
4fefee57 3727 trans->ctx.table->use--;
e688a7f8 3728 list_del_rcu(&nft_trans_set(trans)->list);
958bee14
PNA
3729 break;
3730 case NFT_MSG_DELSET:
4fefee57 3731 trans->ctx.table->use++;
e688a7f8
PNA
3732 list_add_tail_rcu(&nft_trans_set(trans)->list,
3733 &trans->ctx.table->sets);
958bee14
PNA
3734 nft_trans_destroy(trans);
3735 break;
60319eb1 3736 case NFT_MSG_NEWSETELEM:
4fefee57 3737 nft_trans_elem_set(trans)->nelems--;
02263db0
PNA
3738 te = (struct nft_trans_elem *)trans->data;
3739 te->set->ops->get(te->set, &te->elem);
3740 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3741 if (te->set->flags & NFT_SET_MAP &&
3742 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3743 nft_data_uninit(&te->elem.data, te->set->dtype);
3744 te->set->ops->remove(te->set, &te->elem);
60319eb1
PNA
3745 nft_trans_destroy(trans);
3746 break;
3747 case NFT_MSG_DELSETELEM:
4fefee57 3748 nft_trans_elem_set(trans)->nelems++;
60319eb1
PNA
3749 nft_trans_destroy(trans);
3750 break;
37082f93 3751 }
37082f93
PNA
3752 }
3753
b326dd37
PNA
3754 synchronize_rcu();
3755
a1cee076
PNA
3756 list_for_each_entry_safe_reverse(trans, next,
3757 &net->nft.commit_list, list) {
c7c32e72 3758 list_del(&trans->list);
b326dd37 3759 nf_tables_abort_release(trans);
37082f93
PNA
3760 }
3761
3762 return 0;
3763}
3764
96518518
PM
3765static const struct nfnetlink_subsystem nf_tables_subsys = {
3766 .name = "nf_tables",
3767 .subsys_id = NFNL_SUBSYS_NFTABLES,
3768 .cb_count = NFT_MSG_MAX,
3769 .cb = nf_tables_cb,
0628b123
PNA
3770 .commit = nf_tables_commit,
3771 .abort = nf_tables_abort,
96518518
PM
3772};
3773
7210e4e3
PNA
3774int nft_chain_validate_dependency(const struct nft_chain *chain,
3775 enum nft_chain_type type)
3776{
3777 const struct nft_base_chain *basechain;
3778
3779 if (chain->flags & NFT_BASE_CHAIN) {
3780 basechain = nft_base_chain(chain);
3781 if (basechain->type->type != type)
3782 return -EOPNOTSUPP;
3783 }
3784 return 0;
3785}
3786EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
3787
75e8d06d
PNA
3788int nft_chain_validate_hooks(const struct nft_chain *chain,
3789 unsigned int hook_flags)
3790{
3791 struct nft_base_chain *basechain;
3792
3793 if (chain->flags & NFT_BASE_CHAIN) {
3794 basechain = nft_base_chain(chain);
3795
3796 if ((1 << basechain->ops[0].hooknum) & hook_flags)
3797 return 0;
3798
3799 return -EOPNOTSUPP;
3800 }
3801
3802 return 0;
3803}
3804EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
3805
20a69341
PM
3806/*
3807 * Loop detection - walk through the ruleset beginning at the destination chain
3808 * of a new jump until either the source chain is reached (loop) or all
3809 * reachable chains have been traversed.
3810 *
3811 * The loop check is performed whenever a new jump verdict is added to an
3812 * expression or verdict map or a verdict map is bound to a new chain.
3813 */
3814
3815static int nf_tables_check_loops(const struct nft_ctx *ctx,
3816 const struct nft_chain *chain);
3817
3818static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3819 const struct nft_set *set,
3820 const struct nft_set_iter *iter,
3821 const struct nft_set_elem *elem)
3822{
62f9c8b4
PNA
3823 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3824 return 0;
3825
20a69341
PM
3826 switch (elem->data.verdict) {
3827 case NFT_JUMP:
3828 case NFT_GOTO:
3829 return nf_tables_check_loops(ctx, elem->data.chain);
3830 default:
3831 return 0;
3832 }
3833}
3834
3835static int nf_tables_check_loops(const struct nft_ctx *ctx,
3836 const struct nft_chain *chain)
3837{
3838 const struct nft_rule *rule;
3839 const struct nft_expr *expr, *last;
20a69341
PM
3840 const struct nft_set *set;
3841 struct nft_set_binding *binding;
3842 struct nft_set_iter iter;
20a69341
PM
3843
3844 if (ctx->chain == chain)
3845 return -ELOOP;
3846
3847 list_for_each_entry(rule, &chain->rules, list) {
3848 nft_rule_for_each_expr(expr, last, rule) {
0ca743a5
PNA
3849 const struct nft_data *data = NULL;
3850 int err;
3851
3852 if (!expr->ops->validate)
20a69341
PM
3853 continue;
3854
0ca743a5
PNA
3855 err = expr->ops->validate(ctx, expr, &data);
3856 if (err < 0)
3857 return err;
3858
20a69341 3859 if (data == NULL)
0ca743a5 3860 continue;
20a69341
PM
3861
3862 switch (data->verdict) {
3863 case NFT_JUMP:
3864 case NFT_GOTO:
3865 err = nf_tables_check_loops(ctx, data->chain);
3866 if (err < 0)
3867 return err;
3868 default:
3869 break;
3870 }
3871 }
3872 }
3873
3874 list_for_each_entry(set, &ctx->table->sets, list) {
3875 if (!(set->flags & NFT_SET_MAP) ||
3876 set->dtype != NFT_DATA_VERDICT)
3877 continue;
3878
3879 list_for_each_entry(binding, &set->bindings, list) {
3880 if (binding->chain != chain)
3881 continue;
3882
3883 iter.skip = 0;
3884 iter.count = 0;
3885 iter.err = 0;
3886 iter.fn = nf_tables_loop_check_setelem;
3887
3888 set->ops->walk(ctx, set, &iter);
3889 if (iter.err < 0)
3890 return iter.err;
3891 }
3892 }
3893
3894 return 0;
3895}
3896
96518518
PM
3897/**
3898 * nft_validate_input_register - validate an expressions' input register
3899 *
3900 * @reg: the register number
3901 *
3902 * Validate that the input register is one of the general purpose
3903 * registers.
3904 */
3905int nft_validate_input_register(enum nft_registers reg)
3906{
3907 if (reg <= NFT_REG_VERDICT)
3908 return -EINVAL;
3909 if (reg > NFT_REG_MAX)
3910 return -ERANGE;
3911 return 0;
3912}
3913EXPORT_SYMBOL_GPL(nft_validate_input_register);
3914
3915/**
3916 * nft_validate_output_register - validate an expressions' output register
3917 *
3918 * @reg: the register number
3919 *
3920 * Validate that the output register is one of the general purpose
3921 * registers or the verdict register.
3922 */
3923int nft_validate_output_register(enum nft_registers reg)
3924{
3925 if (reg < NFT_REG_VERDICT)
3926 return -EINVAL;
3927 if (reg > NFT_REG_MAX)
3928 return -ERANGE;
3929 return 0;
3930}
3931EXPORT_SYMBOL_GPL(nft_validate_output_register);
3932
3933/**
3934 * nft_validate_data_load - validate an expressions' data load
3935 *
3936 * @ctx: context of the expression performing the load
3937 * @reg: the destination register number
3938 * @data: the data to load
3939 * @type: the data type
3940 *
3941 * Validate that a data load uses the appropriate data type for
3942 * the destination register. A value of NULL for the data means
3943 * that its runtime gathered data, which is always of type
3944 * NFT_DATA_VALUE.
3945 */
3946int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3947 const struct nft_data *data,
3948 enum nft_data_types type)
3949{
20a69341
PM
3950 int err;
3951
96518518
PM
3952 switch (reg) {
3953 case NFT_REG_VERDICT:
3954 if (data == NULL || type != NFT_DATA_VERDICT)
3955 return -EINVAL;
20a69341
PM
3956
3957 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3958 err = nf_tables_check_loops(ctx, data->chain);
3959 if (err < 0)
3960 return err;
3961
3962 if (ctx->chain->level + 1 > data->chain->level) {
3963 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3964 return -EMLINK;
3965 data->chain->level = ctx->chain->level + 1;
3966 }
3967 }
3968
96518518
PM
3969 return 0;
3970 default:
3971 if (data != NULL && type != NFT_DATA_VALUE)
3972 return -EINVAL;
3973 return 0;
3974 }
3975}
3976EXPORT_SYMBOL_GPL(nft_validate_data_load);
3977
3978static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3979 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3980 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3981 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3982};
3983
3984static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3985 struct nft_data_desc *desc, const struct nlattr *nla)
3986{
3987 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3988 struct nft_chain *chain;
3989 int err;
3990
3991 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3992 if (err < 0)
3993 return err;
3994
3995 if (!tb[NFTA_VERDICT_CODE])
3996 return -EINVAL;
3997 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3998
3999 switch (data->verdict) {
e0abdadc
PM
4000 default:
4001 switch (data->verdict & NF_VERDICT_MASK) {
4002 case NF_ACCEPT:
4003 case NF_DROP:
4004 case NF_QUEUE:
4005 break;
4006 default:
4007 return -EINVAL;
4008 }
4009 /* fall through */
96518518
PM
4010 case NFT_CONTINUE:
4011 case NFT_BREAK:
4012 case NFT_RETURN:
4013 desc->len = sizeof(data->verdict);
4014 break;
4015 case NFT_JUMP:
4016 case NFT_GOTO:
4017 if (!tb[NFTA_VERDICT_CHAIN])
4018 return -EINVAL;
4019 chain = nf_tables_chain_lookup(ctx->table,
4020 tb[NFTA_VERDICT_CHAIN]);
4021 if (IS_ERR(chain))
4022 return PTR_ERR(chain);
4023 if (chain->flags & NFT_BASE_CHAIN)
4024 return -EOPNOTSUPP;
4025
96518518
PM
4026 chain->use++;
4027 data->chain = chain;
4028 desc->len = sizeof(data);
4029 break;
96518518
PM
4030 }
4031
4032 desc->type = NFT_DATA_VERDICT;
4033 return 0;
4034}
4035
4036static void nft_verdict_uninit(const struct nft_data *data)
4037{
4038 switch (data->verdict) {
4039 case NFT_JUMP:
4040 case NFT_GOTO:
4041 data->chain->use--;
4042 break;
4043 }
4044}
4045
4046static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4047{
4048 struct nlattr *nest;
4049
4050 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
4051 if (!nest)
4052 goto nla_put_failure;
4053
4054 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
4055 goto nla_put_failure;
4056
4057 switch (data->verdict) {
4058 case NFT_JUMP:
4059 case NFT_GOTO:
4060 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
4061 goto nla_put_failure;
4062 }
4063 nla_nest_end(skb, nest);
4064 return 0;
4065
4066nla_put_failure:
4067 return -1;
4068}
4069
4070static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4071 struct nft_data_desc *desc, const struct nlattr *nla)
4072{
4073 unsigned int len;
4074
4075 len = nla_len(nla);
4076 if (len == 0)
4077 return -EINVAL;
4078 if (len > sizeof(data->data))
4079 return -EOVERFLOW;
4080
4081 nla_memcpy(data->data, nla, sizeof(data->data));
4082 desc->type = NFT_DATA_VALUE;
4083 desc->len = len;
4084 return 0;
4085}
4086
4087static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4088 unsigned int len)
4089{
4090 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
4091}
4092
4093static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4094 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
4095 .len = FIELD_SIZEOF(struct nft_data, data) },
4096 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4097};
4098
4099/**
4100 * nft_data_init - parse nf_tables data netlink attributes
4101 *
4102 * @ctx: context of the expression using the data
4103 * @data: destination struct nft_data
4104 * @desc: data description
4105 * @nla: netlink attribute containing data
4106 *
4107 * Parse the netlink data attributes and initialize a struct nft_data.
4108 * The type and length of data are returned in the data description.
4109 *
4110 * The caller can indicate that it only wants to accept data of type
4111 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4112 */
4113int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4114 struct nft_data_desc *desc, const struct nlattr *nla)
4115{
4116 struct nlattr *tb[NFTA_DATA_MAX + 1];
4117 int err;
4118
4119 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
4120 if (err < 0)
4121 return err;
4122
4123 if (tb[NFTA_DATA_VALUE])
4124 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
4125 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4126 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4127 return -EINVAL;
4128}
4129EXPORT_SYMBOL_GPL(nft_data_init);
4130
4131/**
4132 * nft_data_uninit - release a nft_data item
4133 *
4134 * @data: struct nft_data to release
4135 * @type: type of data
4136 *
4137 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
4138 * all others need to be released by calling this function.
4139 */
4140void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
4141{
4142 switch (type) {
4143 case NFT_DATA_VALUE:
4144 return;
4145 case NFT_DATA_VERDICT:
4146 return nft_verdict_uninit(data);
4147 default:
4148 WARN_ON(1);
4149 }
4150}
4151EXPORT_SYMBOL_GPL(nft_data_uninit);
4152
4153int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
4154 enum nft_data_types type, unsigned int len)
4155{
4156 struct nlattr *nest;
4157 int err;
4158
4159 nest = nla_nest_start(skb, attr);
4160 if (nest == NULL)
4161 return -1;
4162
4163 switch (type) {
4164 case NFT_DATA_VALUE:
4165 err = nft_value_dump(skb, data, len);
4166 break;
4167 case NFT_DATA_VERDICT:
4168 err = nft_verdict_dump(skb, data);
4169 break;
4170 default:
4171 err = -EINVAL;
4172 WARN_ON(1);
4173 }
4174
4175 nla_nest_end(skb, nest);
4176 return err;
4177}
4178EXPORT_SYMBOL_GPL(nft_data_dump);
4179
99633ab2
PNA
4180static int nf_tables_init_net(struct net *net)
4181{
4182 INIT_LIST_HEAD(&net->nft.af_info);
0628b123 4183 INIT_LIST_HEAD(&net->nft.commit_list);
38e029f1 4184 net->nft.base_seq = 1;
99633ab2
PNA
4185 return 0;
4186}
4187
4188static struct pernet_operations nf_tables_net_ops = {
4189 .init = nf_tables_init_net,
4190};
4191
96518518
PM
4192static int __init nf_tables_module_init(void)
4193{
4194 int err;
4195
4196 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
4197 GFP_KERNEL);
4198 if (info == NULL) {
4199 err = -ENOMEM;
4200 goto err1;
4201 }
4202
4203 err = nf_tables_core_module_init();
4204 if (err < 0)
4205 goto err2;
4206
4207 err = nfnetlink_subsys_register(&nf_tables_subsys);
4208 if (err < 0)
4209 goto err3;
4210
4211 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
99633ab2 4212 return register_pernet_subsys(&nf_tables_net_ops);
96518518
PM
4213err3:
4214 nf_tables_core_module_exit();
4215err2:
4216 kfree(info);
4217err1:
4218 return err;
4219}
4220
4221static void __exit nf_tables_module_exit(void)
4222{
99633ab2 4223 unregister_pernet_subsys(&nf_tables_net_ops);
96518518 4224 nfnetlink_subsys_unregister(&nf_tables_subsys);
1b1bc49c 4225 rcu_barrier();
96518518
PM
4226 nf_tables_core_module_exit();
4227 kfree(info);
4228}
4229
4230module_init(nf_tables_module_init);
4231module_exit(nf_tables_module_exit);
4232
4233MODULE_LICENSE("GPL");
4234MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4235MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.305861 seconds and 5 git commands to generate.