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