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