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