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