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