netfilter: nf_tables: relax string validation of NFTA_CHAIN_TYPE
[deliverable/linux.git] / net / netfilter / nf_tables_api.c
CommitLineData
96518518 1/*
20a69341 2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20#include <net/netfilter/nf_tables.h>
99633ab2 21#include <net/net_namespace.h>
96518518
PM
22#include <net/sock.h>
23
96518518
PM
24static LIST_HEAD(nf_tables_expressions);
25
26/**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
99633ab2 34int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
96518518
PM
35{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
99633ab2 38 list_add_tail(&afi->list, &net->nft.af_info);
96518518
PM
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44/**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51void nft_unregister_afinfo(struct nft_af_info *afi)
52{
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
99633ab2 59static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
96518518
PM
60{
61 struct nft_af_info *afi;
62
99633ab2 63 list_for_each_entry(afi, &net->nft.af_info, list) {
96518518
PM
64 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
99633ab2
PNA
70static struct nft_af_info *
71nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
96518518
PM
72{
73 struct nft_af_info *afi;
74
99633ab2 75 afi = nft_afinfo_lookup(net, family);
96518518
PM
76 if (afi != NULL)
77 return afi;
78#ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
99633ab2 83 afi = nft_afinfo_lookup(net, family);
96518518
PM
84 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87#endif
88 return ERR_PTR(-EAFNOSUPPORT);
89}
90
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 },
4c1f7818 544 [NFTA_CHAIN_TYPE] = { .type = NLA_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);
758dbcec
TB
1096 if (type->family == NFPROTO_UNSPEC)
1097 list_add_tail(&type->list, &nf_tables_expressions);
1098 else
1099 list_add(&type->list, &nf_tables_expressions);
96518518
PM
1100 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1101 return 0;
1102}
1103EXPORT_SYMBOL_GPL(nft_register_expr);
1104
1105/**
ef1f7df9
PM
1106 * nft_unregister_expr - unregister nf_tables expr type
1107 * @ops: expr type
96518518 1108 *
ef1f7df9 1109 * Unregisters the expr typefor use with nf_tables.
96518518 1110 */
ef1f7df9 1111void nft_unregister_expr(struct nft_expr_type *type)
96518518
PM
1112{
1113 nfnl_lock(NFNL_SUBSYS_NFTABLES);
ef1f7df9 1114 list_del(&type->list);
96518518
PM
1115 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1116}
1117EXPORT_SYMBOL_GPL(nft_unregister_expr);
1118
64d46806
PM
1119static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1120 struct nlattr *nla)
96518518 1121{
ef1f7df9 1122 const struct nft_expr_type *type;
96518518 1123
ef1f7df9 1124 list_for_each_entry(type, &nf_tables_expressions, list) {
64d46806
PM
1125 if (!nla_strcmp(nla, type->name) &&
1126 (!type->family || type->family == family))
ef1f7df9 1127 return type;
96518518
PM
1128 }
1129 return NULL;
1130}
1131
64d46806
PM
1132static const struct nft_expr_type *nft_expr_type_get(u8 family,
1133 struct nlattr *nla)
96518518 1134{
ef1f7df9 1135 const struct nft_expr_type *type;
96518518
PM
1136
1137 if (nla == NULL)
1138 return ERR_PTR(-EINVAL);
1139
64d46806 1140 type = __nft_expr_type_get(family, nla);
ef1f7df9
PM
1141 if (type != NULL && try_module_get(type->owner))
1142 return type;
96518518
PM
1143
1144#ifdef CONFIG_MODULES
ef1f7df9 1145 if (type == NULL) {
64d46806
PM
1146 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1147 request_module("nft-expr-%u-%.*s", family,
1148 nla_len(nla), (char *)nla_data(nla));
1149 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1150 if (__nft_expr_type_get(family, nla))
1151 return ERR_PTR(-EAGAIN);
1152
96518518
PM
1153 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1154 request_module("nft-expr-%.*s",
1155 nla_len(nla), (char *)nla_data(nla));
1156 nfnl_lock(NFNL_SUBSYS_NFTABLES);
64d46806 1157 if (__nft_expr_type_get(family, nla))
96518518
PM
1158 return ERR_PTR(-EAGAIN);
1159 }
1160#endif
1161 return ERR_PTR(-ENOENT);
1162}
1163
1164static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1165 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1166 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1167};
1168
1169static int nf_tables_fill_expr_info(struct sk_buff *skb,
1170 const struct nft_expr *expr)
1171{
ef1f7df9 1172 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
96518518
PM
1173 goto nla_put_failure;
1174
1175 if (expr->ops->dump) {
1176 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1177 if (data == NULL)
1178 goto nla_put_failure;
1179 if (expr->ops->dump(skb, expr) < 0)
1180 goto nla_put_failure;
1181 nla_nest_end(skb, data);
1182 }
1183
1184 return skb->len;
1185
1186nla_put_failure:
1187 return -1;
1188};
1189
1190struct nft_expr_info {
1191 const struct nft_expr_ops *ops;
ef1f7df9 1192 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
96518518
PM
1193};
1194
0ca743a5
PNA
1195static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1196 const struct nlattr *nla,
96518518
PM
1197 struct nft_expr_info *info)
1198{
ef1f7df9 1199 const struct nft_expr_type *type;
96518518 1200 const struct nft_expr_ops *ops;
ef1f7df9 1201 struct nlattr *tb[NFTA_EXPR_MAX + 1];
96518518
PM
1202 int err;
1203
ef1f7df9 1204 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
96518518
PM
1205 if (err < 0)
1206 return err;
1207
64d46806 1208 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
ef1f7df9
PM
1209 if (IS_ERR(type))
1210 return PTR_ERR(type);
1211
1212 if (tb[NFTA_EXPR_DATA]) {
1213 err = nla_parse_nested(info->tb, type->maxattr,
1214 tb[NFTA_EXPR_DATA], type->policy);
1215 if (err < 0)
1216 goto err1;
1217 } else
1218 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1219
1220 if (type->select_ops != NULL) {
0ca743a5
PNA
1221 ops = type->select_ops(ctx,
1222 (const struct nlattr * const *)info->tb);
ef1f7df9
PM
1223 if (IS_ERR(ops)) {
1224 err = PTR_ERR(ops);
1225 goto err1;
1226 }
1227 } else
1228 ops = type->ops;
1229
96518518
PM
1230 info->ops = ops;
1231 return 0;
ef1f7df9
PM
1232
1233err1:
1234 module_put(type->owner);
1235 return err;
96518518
PM
1236}
1237
1238static int nf_tables_newexpr(const struct nft_ctx *ctx,
ef1f7df9 1239 const struct nft_expr_info *info,
96518518
PM
1240 struct nft_expr *expr)
1241{
1242 const struct nft_expr_ops *ops = info->ops;
1243 int err;
1244
1245 expr->ops = ops;
1246 if (ops->init) {
ef1f7df9 1247 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
96518518
PM
1248 if (err < 0)
1249 goto err1;
1250 }
1251
96518518
PM
1252 return 0;
1253
1254err1:
1255 expr->ops = NULL;
1256 return err;
1257}
1258
62472bce
PM
1259static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1260 struct nft_expr *expr)
96518518
PM
1261{
1262 if (expr->ops->destroy)
62472bce 1263 expr->ops->destroy(ctx, expr);
ef1f7df9 1264 module_put(expr->ops->type->owner);
96518518
PM
1265}
1266
1267/*
1268 * Rules
1269 */
1270
1271static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1272 u64 handle)
1273{
1274 struct nft_rule *rule;
1275
1276 // FIXME: this sucks
1277 list_for_each_entry(rule, &chain->rules, list) {
1278 if (handle == rule->handle)
1279 return rule;
1280 }
1281
1282 return ERR_PTR(-ENOENT);
1283}
1284
1285static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1286 const struct nlattr *nla)
1287{
1288 if (nla == NULL)
1289 return ERR_PTR(-EINVAL);
1290
1291 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1292}
1293
1294static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1295 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1296 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1297 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1298 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1299 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
0ca743a5 1300 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
5e948466 1301 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
0768b3b3
PNA
1302 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1303 .len = NFT_USERDATA_MAXLEN },
96518518
PM
1304};
1305
1306static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1307 int event, u32 flags, int family,
1308 const struct nft_table *table,
1309 const struct nft_chain *chain,
1310 const struct nft_rule *rule)
1311{
1312 struct nlmsghdr *nlh;
1313 struct nfgenmsg *nfmsg;
1314 const struct nft_expr *expr, *next;
1315 struct nlattr *list;
5e948466
EL
1316 const struct nft_rule *prule;
1317 int type = event | NFNL_SUBSYS_NFTABLES << 8;
96518518 1318
5e948466 1319 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
96518518
PM
1320 flags);
1321 if (nlh == NULL)
1322 goto nla_put_failure;
1323
1324 nfmsg = nlmsg_data(nlh);
1325 nfmsg->nfgen_family = family;
1326 nfmsg->version = NFNETLINK_V0;
1327 nfmsg->res_id = 0;
1328
1329 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1330 goto nla_put_failure;
1331 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1332 goto nla_put_failure;
1333 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1334 goto nla_put_failure;
1335
5e948466
EL
1336 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1337 prule = list_entry(rule->list.prev, struct nft_rule, list);
1338 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1339 cpu_to_be64(prule->handle)))
1340 goto nla_put_failure;
1341 }
1342
96518518
PM
1343 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1344 if (list == NULL)
1345 goto nla_put_failure;
1346 nft_rule_for_each_expr(expr, next, rule) {
1347 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1348 if (elem == NULL)
1349 goto nla_put_failure;
1350 if (nf_tables_fill_expr_info(skb, expr) < 0)
1351 goto nla_put_failure;
1352 nla_nest_end(skb, elem);
1353 }
1354 nla_nest_end(skb, list);
1355
0768b3b3
PNA
1356 if (rule->ulen &&
1357 nla_put(skb, NFTA_RULE_USERDATA, rule->ulen, nft_userdata(rule)))
1358 goto nla_put_failure;
1359
96518518
PM
1360 return nlmsg_end(skb, nlh);
1361
1362nla_put_failure:
1363 nlmsg_trim(skb, nlh);
1364 return -1;
1365}
1366
1367static int nf_tables_rule_notify(const struct sk_buff *oskb,
1368 const struct nlmsghdr *nlh,
1369 const struct nft_table *table,
1370 const struct nft_chain *chain,
1371 const struct nft_rule *rule,
1372 int event, u32 flags, int family)
1373{
1374 struct sk_buff *skb;
1375 u32 portid = NETLINK_CB(oskb).portid;
1376 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1377 u32 seq = nlh->nlmsg_seq;
1378 bool report;
1379 int err;
1380
1381 report = nlmsg_report(nlh);
1382 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1383 return 0;
1384
1385 err = -ENOBUFS;
1386 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1387 if (skb == NULL)
1388 goto err;
1389
1390 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1391 family, table, chain, rule);
1392 if (err < 0) {
1393 kfree_skb(skb);
1394 goto err;
1395 }
1396
1397 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1398 GFP_KERNEL);
1399err:
1400 if (err < 0)
1401 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1402 return err;
1403}
1404
0628b123
PNA
1405static inline bool
1406nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1407{
1408 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1409}
1410
1411static inline int gencursor_next(struct net *net)
1412{
1413 return net->nft.gencursor+1 == 1 ? 1 : 0;
1414}
1415
1416static inline int
1417nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1418{
1419 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1420}
1421
1422static inline void
1423nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1424{
1425 /* Now inactive, will be active in the future */
1426 rule->genmask = (1 << net->nft.gencursor);
1427}
1428
1429static inline void
1430nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1431{
1432 rule->genmask = (1 << gencursor_next(net));
1433}
1434
1435static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1436{
1437 rule->genmask = 0;
1438}
1439
96518518
PM
1440static int nf_tables_dump_rules(struct sk_buff *skb,
1441 struct netlink_callback *cb)
1442{
1443 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1444 const struct nft_af_info *afi;
1445 const struct nft_table *table;
1446 const struct nft_chain *chain;
1447 const struct nft_rule *rule;
1448 unsigned int idx = 0, s_idx = cb->args[0];
99633ab2 1449 struct net *net = sock_net(skb->sk);
96518518 1450 int family = nfmsg->nfgen_family;
0628b123
PNA
1451 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1452 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
96518518 1453
99633ab2 1454 list_for_each_entry(afi, &net->nft.af_info, list) {
96518518
PM
1455 if (family != NFPROTO_UNSPEC && family != afi->family)
1456 continue;
1457
1458 list_for_each_entry(table, &afi->tables, list) {
1459 list_for_each_entry(chain, &table->chains, list) {
1460 list_for_each_entry(rule, &chain->rules, list) {
0628b123
PNA
1461 if (!nft_rule_is_active(net, rule))
1462 goto cont;
96518518
PM
1463 if (idx < s_idx)
1464 goto cont;
1465 if (idx > s_idx)
1466 memset(&cb->args[1], 0,
1467 sizeof(cb->args) - sizeof(cb->args[0]));
1468 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1469 cb->nlh->nlmsg_seq,
1470 NFT_MSG_NEWRULE,
1471 NLM_F_MULTI | NLM_F_APPEND,
1472 afi->family, table, chain, rule) < 0)
1473 goto done;
1474cont:
1475 idx++;
1476 }
1477 }
1478 }
1479 }
1480done:
0628b123
PNA
1481 /* Invalidate this dump, a transition to the new generation happened */
1482 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1483 return -EBUSY;
1484
96518518
PM
1485 cb->args[0] = idx;
1486 return skb->len;
1487}
1488
1489static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1490 const struct nlmsghdr *nlh,
1491 const struct nlattr * const nla[])
1492{
1493 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1494 const struct nft_af_info *afi;
1495 const struct nft_table *table;
1496 const struct nft_chain *chain;
1497 const struct nft_rule *rule;
1498 struct sk_buff *skb2;
99633ab2 1499 struct net *net = sock_net(skb->sk);
96518518
PM
1500 int family = nfmsg->nfgen_family;
1501 int err;
1502
1503 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1504 struct netlink_dump_control c = {
1505 .dump = nf_tables_dump_rules,
1506 };
1507 return netlink_dump_start(nlsk, skb, nlh, &c);
1508 }
1509
99633ab2 1510 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1511 if (IS_ERR(afi))
1512 return PTR_ERR(afi);
1513
9370761c 1514 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1515 if (IS_ERR(table))
1516 return PTR_ERR(table);
1517
1518 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1519 if (IS_ERR(chain))
1520 return PTR_ERR(chain);
1521
1522 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1523 if (IS_ERR(rule))
1524 return PTR_ERR(rule);
1525
1526 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1527 if (!skb2)
1528 return -ENOMEM;
1529
1530 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1531 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1532 family, table, chain, rule);
1533 if (err < 0)
1534 goto err;
1535
1536 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1537
1538err:
1539 kfree_skb(skb2);
1540 return err;
1541}
1542
62472bce
PM
1543static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
1544 struct nft_rule *rule)
96518518 1545{
96518518
PM
1546 struct nft_expr *expr;
1547
1548 /*
1549 * Careful: some expressions might not be initialized in case this
1550 * is called on error from nf_tables_newrule().
1551 */
1552 expr = nft_expr_first(rule);
1553 while (expr->ops && expr != nft_expr_last(rule)) {
62472bce 1554 nf_tables_expr_destroy(ctx, expr);
96518518
PM
1555 expr = nft_expr_next(expr);
1556 }
1557 kfree(rule);
1558}
1559
96518518
PM
1560#define NFT_RULE_MAXEXPRS 128
1561
1562static struct nft_expr_info *info;
1563
0628b123 1564static struct nft_rule_trans *
a36e901c 1565nf_tables_trans_add(struct nft_ctx *ctx, struct nft_rule *rule)
0628b123
PNA
1566{
1567 struct nft_rule_trans *rupd;
1568
1569 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1570 if (rupd == NULL)
1571 return NULL;
1572
62472bce 1573 rupd->ctx = *ctx;
0628b123 1574 rupd->rule = rule;
0628b123
PNA
1575 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1576
1577 return rupd;
1578}
1579
96518518
PM
1580static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1581 const struct nlmsghdr *nlh,
1582 const struct nlattr * const nla[])
1583{
1584 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1585 const struct nft_af_info *afi;
99633ab2 1586 struct net *net = sock_net(skb->sk);
96518518
PM
1587 struct nft_table *table;
1588 struct nft_chain *chain;
1589 struct nft_rule *rule, *old_rule = NULL;
0628b123 1590 struct nft_rule_trans *repl = NULL;
96518518
PM
1591 struct nft_expr *expr;
1592 struct nft_ctx ctx;
1593 struct nlattr *tmp;
0768b3b3 1594 unsigned int size, i, n, ulen = 0;
96518518
PM
1595 int err, rem;
1596 bool create;
5e948466 1597 u64 handle, pos_handle;
96518518
PM
1598
1599 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1600
99633ab2 1601 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
96518518
PM
1602 if (IS_ERR(afi))
1603 return PTR_ERR(afi);
1604
9370761c 1605 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1606 if (IS_ERR(table))
1607 return PTR_ERR(table);
1608
1609 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1610 if (IS_ERR(chain))
1611 return PTR_ERR(chain);
1612
1613 if (nla[NFTA_RULE_HANDLE]) {
1614 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1615 rule = __nf_tables_rule_lookup(chain, handle);
1616 if (IS_ERR(rule))
1617 return PTR_ERR(rule);
1618
1619 if (nlh->nlmsg_flags & NLM_F_EXCL)
1620 return -EEXIST;
1621 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1622 old_rule = rule;
1623 else
1624 return -EOPNOTSUPP;
1625 } else {
1626 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1627 return -EINVAL;
1628 handle = nf_tables_alloc_handle(table);
1629 }
1630
5e948466
EL
1631 if (nla[NFTA_RULE_POSITION]) {
1632 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1633 return -EOPNOTSUPP;
1634
1635 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1636 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1637 if (IS_ERR(old_rule))
1638 return PTR_ERR(old_rule);
1639 }
1640
0ca743a5
PNA
1641 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1642
96518518
PM
1643 n = 0;
1644 size = 0;
1645 if (nla[NFTA_RULE_EXPRESSIONS]) {
1646 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1647 err = -EINVAL;
1648 if (nla_type(tmp) != NFTA_LIST_ELEM)
1649 goto err1;
1650 if (n == NFT_RULE_MAXEXPRS)
1651 goto err1;
0ca743a5 1652 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
96518518
PM
1653 if (err < 0)
1654 goto err1;
1655 size += info[n].ops->size;
1656 n++;
1657 }
1658 }
1659
0768b3b3
PNA
1660 if (nla[NFTA_RULE_USERDATA])
1661 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
1662
96518518 1663 err = -ENOMEM;
0768b3b3 1664 rule = kzalloc(sizeof(*rule) + size + ulen, GFP_KERNEL);
96518518
PM
1665 if (rule == NULL)
1666 goto err1;
1667
0628b123
PNA
1668 nft_rule_activate_next(net, rule);
1669
96518518
PM
1670 rule->handle = handle;
1671 rule->dlen = size;
0768b3b3
PNA
1672 rule->ulen = ulen;
1673
1674 if (ulen)
1675 nla_memcpy(nft_userdata(rule), nla[NFTA_RULE_USERDATA], ulen);
96518518 1676
96518518
PM
1677 expr = nft_expr_first(rule);
1678 for (i = 0; i < n; i++) {
1679 err = nf_tables_newexpr(&ctx, &info[i], expr);
1680 if (err < 0)
1681 goto err2;
ef1f7df9 1682 info[i].ops = NULL;
96518518
PM
1683 expr = nft_expr_next(expr);
1684 }
1685
96518518 1686 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
0628b123 1687 if (nft_rule_is_active_next(net, old_rule)) {
a36e901c 1688 repl = nf_tables_trans_add(&ctx, old_rule);
0628b123
PNA
1689 if (repl == NULL) {
1690 err = -ENOMEM;
1691 goto err2;
1692 }
1693 nft_rule_disactivate_next(net, old_rule);
1694 list_add_tail(&rule->list, &old_rule->list);
1695 } else {
1696 err = -ENOENT;
1697 goto err2;
1698 }
96518518 1699 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
5e948466
EL
1700 if (old_rule)
1701 list_add_rcu(&rule->list, &old_rule->list);
1702 else
1703 list_add_tail_rcu(&rule->list, &chain->rules);
1704 else {
1705 if (old_rule)
1706 list_add_tail_rcu(&rule->list, &old_rule->list);
1707 else
1708 list_add_rcu(&rule->list, &chain->rules);
1709 }
96518518 1710
a36e901c 1711 if (nf_tables_trans_add(&ctx, rule) == NULL) {
0628b123
PNA
1712 err = -ENOMEM;
1713 goto err3;
1714 }
96518518
PM
1715 return 0;
1716
0628b123
PNA
1717err3:
1718 list_del_rcu(&rule->list);
1719 if (repl) {
1720 list_del_rcu(&repl->rule->list);
1721 list_del(&repl->list);
1722 nft_rule_clear(net, repl->rule);
1723 kfree(repl);
1724 }
96518518 1725err2:
62472bce 1726 nf_tables_rule_destroy(&ctx, rule);
96518518
PM
1727err1:
1728 for (i = 0; i < n; i++) {
1729 if (info[i].ops != NULL)
ef1f7df9 1730 module_put(info[i].ops->type->owner);
96518518
PM
1731 }
1732 return err;
1733}
1734
0628b123
PNA
1735static int
1736nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1737{
1738 /* You cannot delete the same rule twice */
1739 if (nft_rule_is_active_next(ctx->net, rule)) {
a36e901c 1740 if (nf_tables_trans_add(ctx, rule) == NULL)
0628b123
PNA
1741 return -ENOMEM;
1742 nft_rule_disactivate_next(ctx->net, rule);
1743 return 0;
1744 }
1745 return -ENOENT;
1746}
1747
cf9dc09d
PNA
1748static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1749{
1750 struct nft_rule *rule;
1751 int err;
1752
1753 list_for_each_entry(rule, &ctx->chain->rules, list) {
1754 err = nf_tables_delrule_one(ctx, rule);
1755 if (err < 0)
1756 return err;
1757 }
1758 return 0;
1759}
1760
96518518
PM
1761static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1762 const struct nlmsghdr *nlh,
1763 const struct nlattr * const nla[])
1764{
1765 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1766 const struct nft_af_info *afi;
99633ab2 1767 struct net *net = sock_net(skb->sk);
96518518 1768 const struct nft_table *table;
cf9dc09d
PNA
1769 struct nft_chain *chain = NULL;
1770 struct nft_rule *rule;
0628b123
PNA
1771 int family = nfmsg->nfgen_family, err = 0;
1772 struct nft_ctx ctx;
96518518 1773
99633ab2 1774 afi = nf_tables_afinfo_lookup(net, family, false);
96518518
PM
1775 if (IS_ERR(afi))
1776 return PTR_ERR(afi);
1777
9370761c 1778 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
96518518
PM
1779 if (IS_ERR(table))
1780 return PTR_ERR(table);
1781
cf9dc09d
PNA
1782 if (nla[NFTA_RULE_CHAIN]) {
1783 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1784 if (IS_ERR(chain))
1785 return PTR_ERR(chain);
1786 }
96518518 1787
0628b123
PNA
1788 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1789
cf9dc09d
PNA
1790 if (chain) {
1791 if (nla[NFTA_RULE_HANDLE]) {
1792 rule = nf_tables_rule_lookup(chain,
1793 nla[NFTA_RULE_HANDLE]);
1794 if (IS_ERR(rule))
1795 return PTR_ERR(rule);
96518518 1796
0628b123 1797 err = nf_tables_delrule_one(&ctx, rule);
cf9dc09d
PNA
1798 } else {
1799 err = nf_table_delrule_by_chain(&ctx);
1800 }
1801 } else {
1802 list_for_each_entry(chain, &table->chains, list) {
1803 ctx.chain = chain;
1804 err = nf_table_delrule_by_chain(&ctx);
0628b123
PNA
1805 if (err < 0)
1806 break;
1807 }
1808 }
1809
1810 return err;
1811}
1812
1813static int nf_tables_commit(struct sk_buff *skb)
1814{
1815 struct net *net = sock_net(skb->sk);
1816 struct nft_rule_trans *rupd, *tmp;
96518518 1817
0628b123
PNA
1818 /* Bump generation counter, invalidate any dump in progress */
1819 net->nft.genctr++;
1820
1821 /* A new generation has just started */
1822 net->nft.gencursor = gencursor_next(net);
1823
1824 /* Make sure all packets have left the previous generation before
1825 * purging old rules.
1826 */
1827 synchronize_rcu();
1828
1829 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
0628b123
PNA
1830 /* This rule was inactive in the past and just became active.
1831 * Clear the next bit of the genmask since its meaning has
1832 * changed, now it is the future.
1833 */
1834 if (nft_rule_is_active(net, rupd->rule)) {
1835 nft_rule_clear(net, rupd->rule);
62472bce
PM
1836 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1837 rupd->ctx.table, rupd->ctx.chain,
1838 rupd->rule, NFT_MSG_NEWRULE, 0,
1839 rupd->ctx.afi->family);
0165d932 1840 list_del(&rupd->list);
0628b123
PNA
1841 kfree(rupd);
1842 continue;
96518518 1843 }
0628b123
PNA
1844
1845 /* This rule is in the past, get rid of it */
1846 list_del_rcu(&rupd->rule->list);
62472bce
PM
1847 nf_tables_rule_notify(skb, rupd->ctx.nlh,
1848 rupd->ctx.table, rupd->ctx.chain,
0628b123 1849 rupd->rule, NFT_MSG_DELRULE, 0,
62472bce 1850 rupd->ctx.afi->family);
0165d932
PNA
1851 }
1852
1853 /* Make sure we don't see any packet traversing old rules */
1854 synchronize_rcu();
1855
1856 /* Now we can safely release unused old rules */
1857 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
62472bce 1858 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
0165d932 1859 list_del(&rupd->list);
0628b123 1860 kfree(rupd);
96518518
PM
1861 }
1862
96518518
PM
1863 return 0;
1864}
1865
0628b123
PNA
1866static int nf_tables_abort(struct sk_buff *skb)
1867{
1868 struct net *net = sock_net(skb->sk);
1869 struct nft_rule_trans *rupd, *tmp;
1870
1871 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
0628b123
PNA
1872 if (!nft_rule_is_active_next(net, rupd->rule)) {
1873 nft_rule_clear(net, rupd->rule);
0165d932 1874 list_del(&rupd->list);
0628b123
PNA
1875 kfree(rupd);
1876 continue;
1877 }
1878
1879 /* This rule is inactive, get rid of it */
1880 list_del_rcu(&rupd->rule->list);
0165d932
PNA
1881 }
1882
1883 /* Make sure we don't see any packet accessing aborted rules */
1884 synchronize_rcu();
1885
1886 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
62472bce 1887 nf_tables_rule_destroy(&rupd->ctx, rupd->rule);
0165d932 1888 list_del(&rupd->list);
0628b123
PNA
1889 kfree(rupd);
1890 }
0165d932 1891
0628b123
PNA
1892 return 0;
1893}
1894
20a69341
PM
1895/*
1896 * Sets
1897 */
1898
1899static LIST_HEAD(nf_tables_set_ops);
1900
1901int nft_register_set(struct nft_set_ops *ops)
1902{
1903 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1904 list_add_tail(&ops->list, &nf_tables_set_ops);
1905 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1906 return 0;
1907}
1908EXPORT_SYMBOL_GPL(nft_register_set);
1909
1910void nft_unregister_set(struct nft_set_ops *ops)
1911{
1912 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1913 list_del(&ops->list);
1914 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1915}
1916EXPORT_SYMBOL_GPL(nft_unregister_set);
1917
c50b960c
PM
1918/*
1919 * Select a set implementation based on the data characteristics and the
1920 * given policy. The total memory use might not be known if no size is
1921 * given, in that case the amount of memory per element is used.
1922 */
1923static const struct nft_set_ops *
1924nft_select_set_ops(const struct nlattr * const nla[],
1925 const struct nft_set_desc *desc,
1926 enum nft_set_policies policy)
20a69341 1927{
c50b960c
PM
1928 const struct nft_set_ops *ops, *bops;
1929 struct nft_set_estimate est, best;
20a69341
PM
1930 u32 features;
1931
1932#ifdef CONFIG_MODULES
1933 if (list_empty(&nf_tables_set_ops)) {
1934 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1935 request_module("nft-set");
1936 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1937 if (!list_empty(&nf_tables_set_ops))
1938 return ERR_PTR(-EAGAIN);
1939 }
1940#endif
1941 features = 0;
1942 if (nla[NFTA_SET_FLAGS] != NULL) {
1943 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1944 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1945 }
1946
c50b960c
PM
1947 bops = NULL;
1948 best.size = ~0;
1949 best.class = ~0;
1950
20a69341
PM
1951 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1952 if ((ops->features & features) != features)
1953 continue;
c50b960c
PM
1954 if (!ops->estimate(desc, features, &est))
1955 continue;
1956
1957 switch (policy) {
1958 case NFT_SET_POL_PERFORMANCE:
1959 if (est.class < best.class)
1960 break;
1961 if (est.class == best.class && est.size < best.size)
1962 break;
1963 continue;
1964 case NFT_SET_POL_MEMORY:
1965 if (est.size < best.size)
1966 break;
1967 if (est.size == best.size && est.class < best.class)
1968 break;
1969 continue;
1970 default:
1971 break;
1972 }
1973
20a69341
PM
1974 if (!try_module_get(ops->owner))
1975 continue;
c50b960c
PM
1976 if (bops != NULL)
1977 module_put(bops->owner);
1978
1979 bops = ops;
1980 best = est;
20a69341
PM
1981 }
1982
c50b960c
PM
1983 if (bops != NULL)
1984 return bops;
1985
20a69341
PM
1986 return ERR_PTR(-EOPNOTSUPP);
1987}
1988
1989static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1990 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1991 [NFTA_SET_NAME] = { .type = NLA_STRING },
1992 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1993 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1994 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1995 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1996 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
c50b960c
PM
1997 [NFTA_SET_POLICY] = { .type = NLA_U32 },
1998 [NFTA_SET_DESC] = { .type = NLA_NESTED },
1999};
2000
2001static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2002 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
20a69341
PM
2003};
2004
2005static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
2006 const struct sk_buff *skb,
2007 const struct nlmsghdr *nlh,
2008 const struct nlattr * const nla[])
2009{
99633ab2 2010 struct net *net = sock_net(skb->sk);
20a69341 2011 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
c9c8e485 2012 const struct nft_af_info *afi = NULL;
20a69341
PM
2013 const struct nft_table *table = NULL;
2014
c9c8e485
PNA
2015 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2016 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2017 if (IS_ERR(afi))
2018 return PTR_ERR(afi);
2019 }
20a69341
PM
2020
2021 if (nla[NFTA_SET_TABLE] != NULL) {
ec2c9935
PM
2022 if (afi == NULL)
2023 return -EAFNOSUPPORT;
2024
9370761c 2025 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2026 if (IS_ERR(table))
2027 return PTR_ERR(table);
2028 }
2029
0ca743a5 2030 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2031 return 0;
2032}
2033
2034struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2035 const struct nlattr *nla)
2036{
2037 struct nft_set *set;
2038
2039 if (nla == NULL)
2040 return ERR_PTR(-EINVAL);
2041
2042 list_for_each_entry(set, &table->sets, list) {
2043 if (!nla_strcmp(nla, set->name))
2044 return set;
2045 }
2046 return ERR_PTR(-ENOENT);
2047}
2048
2049static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2050 const char *name)
2051{
2052 const struct nft_set *i;
2053 const char *p;
2054 unsigned long *inuse;
60eb1894 2055 unsigned int n = 0, min = 0;
20a69341
PM
2056
2057 p = strnchr(name, IFNAMSIZ, '%');
2058 if (p != NULL) {
2059 if (p[1] != 'd' || strchr(p + 2, '%'))
2060 return -EINVAL;
2061
2062 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2063 if (inuse == NULL)
2064 return -ENOMEM;
60eb1894 2065cont:
20a69341 2066 list_for_each_entry(i, &ctx->table->sets, list) {
14662917
DB
2067 int tmp;
2068
2069 if (!sscanf(i->name, name, &tmp))
20a69341 2070 continue;
60eb1894 2071 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
20a69341 2072 continue;
14662917 2073
60eb1894 2074 set_bit(tmp - min, inuse);
20a69341
PM
2075 }
2076
53b70287 2077 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
60eb1894
PM
2078 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2079 min += BITS_PER_BYTE * PAGE_SIZE;
2080 memset(inuse, 0, PAGE_SIZE);
2081 goto cont;
2082 }
20a69341
PM
2083 free_page((unsigned long)inuse);
2084 }
2085
60eb1894 2086 snprintf(set->name, sizeof(set->name), name, min + n);
20a69341
PM
2087 list_for_each_entry(i, &ctx->table->sets, list) {
2088 if (!strcmp(set->name, i->name))
2089 return -ENFILE;
2090 }
2091 return 0;
2092}
2093
2094static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2095 const struct nft_set *set, u16 event, u16 flags)
2096{
2097 struct nfgenmsg *nfmsg;
2098 struct nlmsghdr *nlh;
c50b960c 2099 struct nlattr *desc;
20a69341
PM
2100 u32 portid = NETLINK_CB(ctx->skb).portid;
2101 u32 seq = ctx->nlh->nlmsg_seq;
2102
2103 event |= NFNL_SUBSYS_NFTABLES << 8;
2104 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2105 flags);
2106 if (nlh == NULL)
2107 goto nla_put_failure;
2108
2109 nfmsg = nlmsg_data(nlh);
2110 nfmsg->nfgen_family = ctx->afi->family;
2111 nfmsg->version = NFNETLINK_V0;
2112 nfmsg->res_id = 0;
2113
2114 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2115 goto nla_put_failure;
2116 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2117 goto nla_put_failure;
2118 if (set->flags != 0)
2119 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2120 goto nla_put_failure;
2121
2122 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2123 goto nla_put_failure;
2124 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2125 goto nla_put_failure;
2126 if (set->flags & NFT_SET_MAP) {
2127 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2128 goto nla_put_failure;
2129 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2130 goto nla_put_failure;
2131 }
2132
c50b960c
PM
2133 desc = nla_nest_start(skb, NFTA_SET_DESC);
2134 if (desc == NULL)
2135 goto nla_put_failure;
2136 if (set->size &&
2137 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2138 goto nla_put_failure;
2139 nla_nest_end(skb, desc);
2140
20a69341
PM
2141 return nlmsg_end(skb, nlh);
2142
2143nla_put_failure:
2144 nlmsg_trim(skb, nlh);
2145 return -1;
2146}
2147
2148static int nf_tables_set_notify(const struct nft_ctx *ctx,
2149 const struct nft_set *set,
2150 int event)
2151{
2152 struct sk_buff *skb;
2153 u32 portid = NETLINK_CB(ctx->skb).portid;
20a69341
PM
2154 bool report;
2155 int err;
2156
2157 report = nlmsg_report(ctx->nlh);
99633ab2 2158 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
20a69341
PM
2159 return 0;
2160
2161 err = -ENOBUFS;
2162 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2163 if (skb == NULL)
2164 goto err;
2165
2166 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2167 if (err < 0) {
2168 kfree_skb(skb);
2169 goto err;
2170 }
2171
99633ab2 2172 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
20a69341
PM
2173 GFP_KERNEL);
2174err:
2175 if (err < 0)
99633ab2 2176 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
20a69341
PM
2177 return err;
2178}
2179
2180static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2181 struct netlink_callback *cb)
2182{
2183 const struct nft_set *set;
2184 unsigned int idx = 0, s_idx = cb->args[0];
2185
2186 if (cb->args[1])
2187 return skb->len;
2188
2189 list_for_each_entry(set, &ctx->table->sets, list) {
2190 if (idx < s_idx)
2191 goto cont;
2192 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2193 NLM_F_MULTI) < 0) {
2194 cb->args[0] = idx;
2195 goto done;
2196 }
2197cont:
2198 idx++;
2199 }
2200 cb->args[1] = 1;
2201done:
2202 return skb->len;
2203}
2204
c9c8e485
PNA
2205static int nf_tables_dump_sets_family(struct nft_ctx *ctx, struct sk_buff *skb,
2206 struct netlink_callback *cb)
20a69341
PM
2207{
2208 const struct nft_set *set;
e38195bf 2209 unsigned int idx, s_idx = cb->args[0];
20a69341
PM
2210 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2211
2212 if (cb->args[1])
2213 return skb->len;
2214
2215 list_for_each_entry(table, &ctx->afi->tables, list) {
e38195bf
PNA
2216 if (cur_table) {
2217 if (cur_table != table)
2218 continue;
20a69341 2219
e38195bf
PNA
2220 cur_table = NULL;
2221 }
20a69341 2222 ctx->table = table;
e38195bf 2223 idx = 0;
20a69341
PM
2224 list_for_each_entry(set, &ctx->table->sets, list) {
2225 if (idx < s_idx)
2226 goto cont;
2227 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2228 NLM_F_MULTI) < 0) {
2229 cb->args[0] = idx;
2230 cb->args[2] = (unsigned long) table;
2231 goto done;
2232 }
2233cont:
2234 idx++;
2235 }
2236 }
2237 cb->args[1] = 1;
2238done:
2239 return skb->len;
2240}
2241
c9c8e485
PNA
2242static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2243 struct netlink_callback *cb)
2244{
2245 const struct nft_set *set;
2246 unsigned int idx, s_idx = cb->args[0];
2247 const struct nft_af_info *afi;
2248 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2249 struct net *net = sock_net(skb->sk);
2250 int cur_family = cb->args[3];
2251
2252 if (cb->args[1])
2253 return skb->len;
2254
2255 list_for_each_entry(afi, &net->nft.af_info, list) {
2256 if (cur_family) {
2257 if (afi->family != cur_family)
2258 continue;
2259
2260 cur_family = 0;
2261 }
2262
2263 list_for_each_entry(table, &afi->tables, list) {
2264 if (cur_table) {
2265 if (cur_table != table)
2266 continue;
2267
2268 cur_table = NULL;
2269 }
2270
2271 ctx->table = table;
2272 ctx->afi = afi;
2273 idx = 0;
2274 list_for_each_entry(set, &ctx->table->sets, list) {
2275 if (idx < s_idx)
2276 goto cont;
2277 if (nf_tables_fill_set(skb, ctx, set,
2278 NFT_MSG_NEWSET,
2279 NLM_F_MULTI) < 0) {
2280 cb->args[0] = idx;
2281 cb->args[2] = (unsigned long) table;
2282 cb->args[3] = afi->family;
2283 goto done;
2284 }
2285cont:
2286 idx++;
2287 }
2288 if (s_idx)
2289 s_idx = 0;
2290 }
2291 }
2292 cb->args[1] = 1;
2293done:
2294 return skb->len;
2295}
2296
20a69341
PM
2297static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2298{
2299 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2300 struct nlattr *nla[NFTA_SET_MAX + 1];
2301 struct nft_ctx ctx;
2302 int err, ret;
2303
2304 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2305 nft_set_policy);
2306 if (err < 0)
2307 return err;
2308
2309 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2310 if (err < 0)
2311 return err;
2312
c9c8e485
PNA
2313 if (ctx.table == NULL) {
2314 if (ctx.afi == NULL)
2315 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2316 else
2317 ret = nf_tables_dump_sets_family(&ctx, skb, cb);
2318 } else
20a69341
PM
2319 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2320
2321 return ret;
2322}
2323
2324static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2325 const struct nlmsghdr *nlh,
2326 const struct nlattr * const nla[])
2327{
2328 const struct nft_set *set;
2329 struct nft_ctx ctx;
2330 struct sk_buff *skb2;
c9c8e485 2331 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2332 int err;
2333
2334 /* Verify existance before starting dump */
2335 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2336 if (err < 0)
2337 return err;
2338
2339 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2340 struct netlink_dump_control c = {
2341 .dump = nf_tables_dump_sets,
2342 };
2343 return netlink_dump_start(nlsk, skb, nlh, &c);
2344 }
2345
c9c8e485
PNA
2346 /* Only accept unspec with dump */
2347 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2348 return -EAFNOSUPPORT;
2349
20a69341
PM
2350 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2351 if (IS_ERR(set))
2352 return PTR_ERR(set);
2353
2354 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2355 if (skb2 == NULL)
2356 return -ENOMEM;
2357
2358 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2359 if (err < 0)
2360 goto err;
2361
2362 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2363
2364err:
2365 kfree_skb(skb2);
2366 return err;
2367}
2368
c50b960c
PM
2369static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2370 struct nft_set_desc *desc,
2371 const struct nlattr *nla)
2372{
2373 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2374 int err;
2375
2376 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla, nft_set_desc_policy);
2377 if (err < 0)
2378 return err;
2379
2380 if (da[NFTA_SET_DESC_SIZE] != NULL)
2381 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2382
2383 return 0;
2384}
2385
20a69341
PM
2386static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2387 const struct nlmsghdr *nlh,
2388 const struct nlattr * const nla[])
2389{
2390 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2391 const struct nft_set_ops *ops;
2392 const struct nft_af_info *afi;
99633ab2 2393 struct net *net = sock_net(skb->sk);
20a69341
PM
2394 struct nft_table *table;
2395 struct nft_set *set;
2396 struct nft_ctx ctx;
2397 char name[IFNAMSIZ];
2398 unsigned int size;
2399 bool create;
c50b960c
PM
2400 u32 ktype, dtype, flags, policy;
2401 struct nft_set_desc desc;
20a69341
PM
2402 int err;
2403
2404 if (nla[NFTA_SET_TABLE] == NULL ||
2405 nla[NFTA_SET_NAME] == NULL ||
2406 nla[NFTA_SET_KEY_LEN] == NULL)
2407 return -EINVAL;
2408
c50b960c
PM
2409 memset(&desc, 0, sizeof(desc));
2410
20a69341
PM
2411 ktype = NFT_DATA_VALUE;
2412 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2413 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2414 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2415 return -EINVAL;
2416 }
2417
c50b960c
PM
2418 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2419 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data))
20a69341
PM
2420 return -EINVAL;
2421
2422 flags = 0;
2423 if (nla[NFTA_SET_FLAGS] != NULL) {
2424 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2425 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2426 NFT_SET_INTERVAL | NFT_SET_MAP))
2427 return -EINVAL;
2428 }
2429
2430 dtype = 0;
20a69341
PM
2431 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2432 if (!(flags & NFT_SET_MAP))
2433 return -EINVAL;
2434
2435 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2436 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2437 dtype != NFT_DATA_VERDICT)
2438 return -EINVAL;
2439
2440 if (dtype != NFT_DATA_VERDICT) {
2441 if (nla[NFTA_SET_DATA_LEN] == NULL)
2442 return -EINVAL;
c50b960c
PM
2443 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2444 if (desc.dlen == 0 ||
2445 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
20a69341
PM
2446 return -EINVAL;
2447 } else
c50b960c 2448 desc.dlen = sizeof(struct nft_data);
20a69341
PM
2449 } else if (flags & NFT_SET_MAP)
2450 return -EINVAL;
2451
c50b960c
PM
2452 policy = NFT_SET_POL_PERFORMANCE;
2453 if (nla[NFTA_SET_POLICY] != NULL)
2454 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2455
2456 if (nla[NFTA_SET_DESC] != NULL) {
2457 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
2458 if (err < 0)
2459 return err;
2460 }
2461
20a69341
PM
2462 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2463
99633ab2 2464 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
20a69341
PM
2465 if (IS_ERR(afi))
2466 return PTR_ERR(afi);
2467
9370761c 2468 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
20a69341
PM
2469 if (IS_ERR(table))
2470 return PTR_ERR(table);
2471
0ca743a5 2472 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2473
2474 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2475 if (IS_ERR(set)) {
2476 if (PTR_ERR(set) != -ENOENT)
2477 return PTR_ERR(set);
2478 set = NULL;
2479 }
2480
2481 if (set != NULL) {
2482 if (nlh->nlmsg_flags & NLM_F_EXCL)
2483 return -EEXIST;
2484 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2485 return -EOPNOTSUPP;
2486 return 0;
2487 }
2488
2489 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2490 return -ENOENT;
2491
c50b960c 2492 ops = nft_select_set_ops(nla, &desc, policy);
20a69341
PM
2493 if (IS_ERR(ops))
2494 return PTR_ERR(ops);
2495
2496 size = 0;
2497 if (ops->privsize != NULL)
2498 size = ops->privsize(nla);
2499
2500 err = -ENOMEM;
2501 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2502 if (set == NULL)
2503 goto err1;
2504
2505 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2506 err = nf_tables_set_alloc_name(&ctx, set, name);
2507 if (err < 0)
2508 goto err2;
2509
2510 INIT_LIST_HEAD(&set->bindings);
2511 set->ops = ops;
2512 set->ktype = ktype;
c50b960c 2513 set->klen = desc.klen;
20a69341 2514 set->dtype = dtype;
c50b960c 2515 set->dlen = desc.dlen;
20a69341 2516 set->flags = flags;
c50b960c 2517 set->size = desc.size;
20a69341 2518
c50b960c 2519 err = ops->init(set, &desc, nla);
20a69341
PM
2520 if (err < 0)
2521 goto err2;
2522
2523 list_add_tail(&set->list, &table->sets);
2524 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2525 return 0;
2526
2527err2:
2528 kfree(set);
2529err1:
2530 module_put(ops->owner);
2531 return err;
2532}
2533
2534static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2535{
2536 list_del(&set->list);
ab9da5c1 2537 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
20a69341
PM
2538
2539 set->ops->destroy(set);
2540 module_put(set->ops->owner);
2541 kfree(set);
2542}
2543
2544static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2545 const struct nlmsghdr *nlh,
2546 const struct nlattr * const nla[])
2547{
c9c8e485 2548 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
20a69341
PM
2549 struct nft_set *set;
2550 struct nft_ctx ctx;
2551 int err;
2552
ec2c9935
PM
2553 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2554 return -EAFNOSUPPORT;
20a69341
PM
2555 if (nla[NFTA_SET_TABLE] == NULL)
2556 return -EINVAL;
2557
2558 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2559 if (err < 0)
2560 return err;
2561
2562 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2563 if (IS_ERR(set))
2564 return PTR_ERR(set);
2565 if (!list_empty(&set->bindings))
2566 return -EBUSY;
2567
2568 nf_tables_set_destroy(&ctx, set);
2569 return 0;
2570}
2571
2572static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2573 const struct nft_set *set,
2574 const struct nft_set_iter *iter,
2575 const struct nft_set_elem *elem)
2576{
2577 enum nft_registers dreg;
2578
2579 dreg = nft_type_to_reg(set->dtype);
2ee0d3c8
PNA
2580 return nft_validate_data_load(ctx, dreg, &elem->data,
2581 set->dtype == NFT_DATA_VERDICT ?
2582 NFT_DATA_VERDICT : NFT_DATA_VALUE);
20a69341
PM
2583}
2584
2585int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2586 struct nft_set_binding *binding)
2587{
2588 struct nft_set_binding *i;
2589 struct nft_set_iter iter;
2590
2591 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2592 return -EBUSY;
2593
2594 if (set->flags & NFT_SET_MAP) {
2595 /* If the set is already bound to the same chain all
2596 * jumps are already validated for that chain.
2597 */
2598 list_for_each_entry(i, &set->bindings, list) {
2599 if (i->chain == binding->chain)
2600 goto bind;
2601 }
2602
2603 iter.skip = 0;
2604 iter.count = 0;
2605 iter.err = 0;
2606 iter.fn = nf_tables_bind_check_setelem;
2607
2608 set->ops->walk(ctx, set, &iter);
2609 if (iter.err < 0) {
2610 /* Destroy anonymous sets if binding fails */
2611 if (set->flags & NFT_SET_ANONYMOUS)
2612 nf_tables_set_destroy(ctx, set);
2613
2614 return iter.err;
2615 }
2616 }
2617bind:
2618 binding->chain = ctx->chain;
2619 list_add_tail(&binding->list, &set->bindings);
2620 return 0;
2621}
2622
2623void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2624 struct nft_set_binding *binding)
2625{
2626 list_del(&binding->list);
2627
2628 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2629 nf_tables_set_destroy(ctx, set);
2630}
2631
2632/*
2633 * Set elements
2634 */
2635
2636static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2637 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2638 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2639 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2640};
2641
2642static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2643 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2644 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2645 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2646};
2647
2648static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2649 const struct sk_buff *skb,
2650 const struct nlmsghdr *nlh,
2651 const struct nlattr * const nla[])
2652{
2653 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2654 const struct nft_af_info *afi;
2655 const struct nft_table *table;
99633ab2 2656 struct net *net = sock_net(skb->sk);
20a69341 2657
99633ab2 2658 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
20a69341
PM
2659 if (IS_ERR(afi))
2660 return PTR_ERR(afi);
2661
9370761c 2662 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
20a69341
PM
2663 if (IS_ERR(table))
2664 return PTR_ERR(table);
2665
0ca743a5 2666 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
20a69341
PM
2667 return 0;
2668}
2669
2670static int nf_tables_fill_setelem(struct sk_buff *skb,
2671 const struct nft_set *set,
2672 const struct nft_set_elem *elem)
2673{
2674 unsigned char *b = skb_tail_pointer(skb);
2675 struct nlattr *nest;
2676
2677 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2678 if (nest == NULL)
2679 goto nla_put_failure;
2680
2681 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2682 set->klen) < 0)
2683 goto nla_put_failure;
2684
2685 if (set->flags & NFT_SET_MAP &&
2686 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2687 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2688 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2689 set->dlen) < 0)
2690 goto nla_put_failure;
2691
2692 if (elem->flags != 0)
2693 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2694 goto nla_put_failure;
2695
2696 nla_nest_end(skb, nest);
2697 return 0;
2698
2699nla_put_failure:
2700 nlmsg_trim(skb, b);
2701 return -EMSGSIZE;
2702}
2703
2704struct nft_set_dump_args {
2705 const struct netlink_callback *cb;
2706 struct nft_set_iter iter;
2707 struct sk_buff *skb;
2708};
2709
2710static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2711 const struct nft_set *set,
2712 const struct nft_set_iter *iter,
2713 const struct nft_set_elem *elem)
2714{
2715 struct nft_set_dump_args *args;
2716
2717 args = container_of(iter, struct nft_set_dump_args, iter);
2718 return nf_tables_fill_setelem(args->skb, set, elem);
2719}
2720
2721static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2722{
2723 const struct nft_set *set;
2724 struct nft_set_dump_args args;
2725 struct nft_ctx ctx;
2726 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2727 struct nfgenmsg *nfmsg;
2728 struct nlmsghdr *nlh;
2729 struct nlattr *nest;
2730 u32 portid, seq;
2731 int event, err;
2732
720e0dfa
MN
2733 err = nlmsg_parse(cb->nlh, sizeof(struct nfgenmsg), nla,
2734 NFTA_SET_ELEM_LIST_MAX, nft_set_elem_list_policy);
20a69341
PM
2735 if (err < 0)
2736 return err;
2737
2738 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2739 if (err < 0)
2740 return err;
2741
2742 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2743 if (IS_ERR(set))
2744 return PTR_ERR(set);
2745
2746 event = NFT_MSG_NEWSETELEM;
2747 event |= NFNL_SUBSYS_NFTABLES << 8;
2748 portid = NETLINK_CB(cb->skb).portid;
2749 seq = cb->nlh->nlmsg_seq;
2750
2751 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2752 NLM_F_MULTI);
2753 if (nlh == NULL)
2754 goto nla_put_failure;
2755
2756 nfmsg = nlmsg_data(nlh);
2757 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2758 nfmsg->version = NFNETLINK_V0;
2759 nfmsg->res_id = 0;
2760
2761 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2762 goto nla_put_failure;
2763 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2764 goto nla_put_failure;
2765
2766 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2767 if (nest == NULL)
2768 goto nla_put_failure;
2769
2770 args.cb = cb;
2771 args.skb = skb;
2772 args.iter.skip = cb->args[0];
2773 args.iter.count = 0;
2774 args.iter.err = 0;
2775 args.iter.fn = nf_tables_dump_setelem;
2776 set->ops->walk(&ctx, set, &args.iter);
2777
2778 nla_nest_end(skb, nest);
2779 nlmsg_end(skb, nlh);
2780
2781 if (args.iter.err && args.iter.err != -EMSGSIZE)
2782 return args.iter.err;
2783 if (args.iter.count == cb->args[0])
2784 return 0;
2785
2786 cb->args[0] = args.iter.count;
2787 return skb->len;
2788
2789nla_put_failure:
2790 return -ENOSPC;
2791}
2792
2793static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2794 const struct nlmsghdr *nlh,
2795 const struct nlattr * const nla[])
2796{
2797 const struct nft_set *set;
2798 struct nft_ctx ctx;
2799 int err;
2800
2801 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2802 if (err < 0)
2803 return err;
2804
2805 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2806 if (IS_ERR(set))
2807 return PTR_ERR(set);
2808
2809 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2810 struct netlink_dump_control c = {
2811 .dump = nf_tables_dump_set,
2812 };
2813 return netlink_dump_start(nlsk, skb, nlh, &c);
2814 }
2815 return -EOPNOTSUPP;
2816}
2817
d60ce62f
AB
2818static int nf_tables_fill_setelem_info(struct sk_buff *skb,
2819 const struct nft_ctx *ctx, u32 seq,
2820 u32 portid, int event, u16 flags,
2821 const struct nft_set *set,
2822 const struct nft_set_elem *elem)
2823{
2824 struct nfgenmsg *nfmsg;
2825 struct nlmsghdr *nlh;
2826 struct nlattr *nest;
2827 int err;
2828
2829 event |= NFNL_SUBSYS_NFTABLES << 8;
2830 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2831 flags);
2832 if (nlh == NULL)
2833 goto nla_put_failure;
2834
2835 nfmsg = nlmsg_data(nlh);
2836 nfmsg->nfgen_family = ctx->afi->family;
2837 nfmsg->version = NFNETLINK_V0;
2838 nfmsg->res_id = 0;
2839
2840 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2841 goto nla_put_failure;
2842 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2843 goto nla_put_failure;
2844
2845 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2846 if (nest == NULL)
2847 goto nla_put_failure;
2848
2849 err = nf_tables_fill_setelem(skb, set, elem);
2850 if (err < 0)
2851 goto nla_put_failure;
2852
2853 nla_nest_end(skb, nest);
2854
2855 return nlmsg_end(skb, nlh);
2856
2857nla_put_failure:
2858 nlmsg_trim(skb, nlh);
2859 return -1;
2860}
2861
2862static int nf_tables_setelem_notify(const struct nft_ctx *ctx,
2863 const struct nft_set *set,
2864 const struct nft_set_elem *elem,
2865 int event, u16 flags)
2866{
2867 const struct sk_buff *oskb = ctx->skb;
2868 struct net *net = sock_net(oskb->sk);
2869 u32 portid = NETLINK_CB(oskb).portid;
2870 bool report = nlmsg_report(ctx->nlh);
2871 struct sk_buff *skb;
2872 int err;
2873
2874 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
2875 return 0;
2876
2877 err = -ENOBUFS;
2878 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2879 if (skb == NULL)
2880 goto err;
2881
2882 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
2883 set, elem);
2884 if (err < 0) {
2885 kfree_skb(skb);
2886 goto err;
2887 }
2888
2889 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
2890 GFP_KERNEL);
2891err:
2892 if (err < 0)
2893 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
2894 return err;
2895}
2896
20a69341
PM
2897static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2898 const struct nlattr *attr)
2899{
2900 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2901 struct nft_data_desc d1, d2;
2902 struct nft_set_elem elem;
2903 struct nft_set_binding *binding;
2904 enum nft_registers dreg;
2905 int err;
2906
c50b960c
PM
2907 if (set->size && set->nelems == set->size)
2908 return -ENFILE;
2909
20a69341
PM
2910 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2911 nft_set_elem_policy);
2912 if (err < 0)
2913 return err;
2914
2915 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2916 return -EINVAL;
2917
2918 elem.flags = 0;
2919 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2920 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2921 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2922 return -EINVAL;
2923 }
2924
2925 if (set->flags & NFT_SET_MAP) {
2926 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2927 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2928 return -EINVAL;
bd7fc645
PNA
2929 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
2930 elem.flags & NFT_SET_ELEM_INTERVAL_END)
2931 return -EINVAL;
20a69341
PM
2932 } else {
2933 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2934 return -EINVAL;
2935 }
2936
2937 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2938 if (err < 0)
2939 goto err1;
2940 err = -EINVAL;
2941 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2942 goto err2;
2943
2944 err = -EEXIST;
2945 if (set->ops->get(set, &elem) == 0)
2946 goto err2;
2947
2948 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2949 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2950 if (err < 0)
2951 goto err2;
2952
2953 err = -EINVAL;
2954 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2955 goto err3;
2956
2957 dreg = nft_type_to_reg(set->dtype);
2958 list_for_each_entry(binding, &set->bindings, list) {
2959 struct nft_ctx bind_ctx = {
2960 .afi = ctx->afi,
2961 .table = ctx->table,
2962 .chain = binding->chain,
2963 };
2964
2965 err = nft_validate_data_load(&bind_ctx, dreg,
2966 &elem.data, d2.type);
2967 if (err < 0)
2968 goto err3;
2969 }
2970 }
2971
2972 err = set->ops->insert(set, &elem);
2973 if (err < 0)
2974 goto err3;
c50b960c 2975 set->nelems++;
20a69341 2976
d60ce62f 2977 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_NEWSETELEM, 0);
20a69341
PM
2978 return 0;
2979
2980err3:
2981 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2982 nft_data_uninit(&elem.data, d2.type);
2983err2:
2984 nft_data_uninit(&elem.key, d1.type);
2985err1:
2986 return err;
2987}
2988
2989static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2990 const struct nlmsghdr *nlh,
2991 const struct nlattr * const nla[])
2992{
2993 const struct nlattr *attr;
2994 struct nft_set *set;
2995 struct nft_ctx ctx;
2996 int rem, err;
2997
2998 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2999 if (err < 0)
3000 return err;
3001
3002 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3003 if (IS_ERR(set))
3004 return PTR_ERR(set);
3005 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3006 return -EBUSY;
3007
3008 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3009 err = nft_add_set_elem(&ctx, set, attr);
3010 if (err < 0)
3011 return err;
3012 }
3013 return 0;
3014}
3015
3016static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
3017 const struct nlattr *attr)
3018{
3019 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3020 struct nft_data_desc desc;
3021 struct nft_set_elem elem;
3022 int err;
3023
3024 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3025 nft_set_elem_policy);
3026 if (err < 0)
3027 goto err1;
3028
3029 err = -EINVAL;
3030 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3031 goto err1;
3032
3033 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
3034 if (err < 0)
3035 goto err1;
3036
3037 err = -EINVAL;
3038 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3039 goto err2;
3040
3041 err = set->ops->get(set, &elem);
3042 if (err < 0)
3043 goto err2;
3044
3045 set->ops->remove(set, &elem);
c50b960c 3046 set->nelems--;
20a69341 3047
d60ce62f
AB
3048 nf_tables_setelem_notify(ctx, set, &elem, NFT_MSG_DELSETELEM, 0);
3049
20a69341
PM
3050 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
3051 if (set->flags & NFT_SET_MAP)
3052 nft_data_uninit(&elem.data, set->dtype);
3053
3054err2:
3055 nft_data_uninit(&elem.key, desc.type);
3056err1:
3057 return err;
3058}
3059
3060static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3061 const struct nlmsghdr *nlh,
3062 const struct nlattr * const nla[])
3063{
3064 const struct nlattr *attr;
3065 struct nft_set *set;
3066 struct nft_ctx ctx;
3067 int rem, err;
3068
3069 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
3070 if (err < 0)
3071 return err;
3072
3073 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
3074 if (IS_ERR(set))
3075 return PTR_ERR(set);
3076 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3077 return -EBUSY;
3078
3079 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3080 err = nft_del_setelem(&ctx, set, attr);
3081 if (err < 0)
3082 return err;
3083 }
3084 return 0;
3085}
3086
96518518
PM
3087static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
3088 [NFT_MSG_NEWTABLE] = {
3089 .call = nf_tables_newtable,
3090 .attr_count = NFTA_TABLE_MAX,
3091 .policy = nft_table_policy,
3092 },
3093 [NFT_MSG_GETTABLE] = {
3094 .call = nf_tables_gettable,
3095 .attr_count = NFTA_TABLE_MAX,
3096 .policy = nft_table_policy,
3097 },
3098 [NFT_MSG_DELTABLE] = {
3099 .call = nf_tables_deltable,
3100 .attr_count = NFTA_TABLE_MAX,
3101 .policy = nft_table_policy,
3102 },
3103 [NFT_MSG_NEWCHAIN] = {
3104 .call = nf_tables_newchain,
3105 .attr_count = NFTA_CHAIN_MAX,
3106 .policy = nft_chain_policy,
3107 },
3108 [NFT_MSG_GETCHAIN] = {
3109 .call = nf_tables_getchain,
3110 .attr_count = NFTA_CHAIN_MAX,
3111 .policy = nft_chain_policy,
3112 },
3113 [NFT_MSG_DELCHAIN] = {
3114 .call = nf_tables_delchain,
3115 .attr_count = NFTA_CHAIN_MAX,
3116 .policy = nft_chain_policy,
3117 },
3118 [NFT_MSG_NEWRULE] = {
0628b123 3119 .call_batch = nf_tables_newrule,
96518518
PM
3120 .attr_count = NFTA_RULE_MAX,
3121 .policy = nft_rule_policy,
3122 },
3123 [NFT_MSG_GETRULE] = {
3124 .call = nf_tables_getrule,
3125 .attr_count = NFTA_RULE_MAX,
3126 .policy = nft_rule_policy,
3127 },
3128 [NFT_MSG_DELRULE] = {
0628b123 3129 .call_batch = nf_tables_delrule,
96518518
PM
3130 .attr_count = NFTA_RULE_MAX,
3131 .policy = nft_rule_policy,
3132 },
20a69341
PM
3133 [NFT_MSG_NEWSET] = {
3134 .call = nf_tables_newset,
3135 .attr_count = NFTA_SET_MAX,
3136 .policy = nft_set_policy,
3137 },
3138 [NFT_MSG_GETSET] = {
3139 .call = nf_tables_getset,
3140 .attr_count = NFTA_SET_MAX,
3141 .policy = nft_set_policy,
3142 },
3143 [NFT_MSG_DELSET] = {
3144 .call = nf_tables_delset,
3145 .attr_count = NFTA_SET_MAX,
3146 .policy = nft_set_policy,
3147 },
3148 [NFT_MSG_NEWSETELEM] = {
3149 .call = nf_tables_newsetelem,
3150 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3151 .policy = nft_set_elem_list_policy,
3152 },
3153 [NFT_MSG_GETSETELEM] = {
3154 .call = nf_tables_getsetelem,
3155 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3156 .policy = nft_set_elem_list_policy,
3157 },
3158 [NFT_MSG_DELSETELEM] = {
3159 .call = nf_tables_delsetelem,
3160 .attr_count = NFTA_SET_ELEM_LIST_MAX,
3161 .policy = nft_set_elem_list_policy,
3162 },
96518518
PM
3163};
3164
3165static const struct nfnetlink_subsystem nf_tables_subsys = {
3166 .name = "nf_tables",
3167 .subsys_id = NFNL_SUBSYS_NFTABLES,
3168 .cb_count = NFT_MSG_MAX,
3169 .cb = nf_tables_cb,
0628b123
PNA
3170 .commit = nf_tables_commit,
3171 .abort = nf_tables_abort,
96518518
PM
3172};
3173
20a69341
PM
3174/*
3175 * Loop detection - walk through the ruleset beginning at the destination chain
3176 * of a new jump until either the source chain is reached (loop) or all
3177 * reachable chains have been traversed.
3178 *
3179 * The loop check is performed whenever a new jump verdict is added to an
3180 * expression or verdict map or a verdict map is bound to a new chain.
3181 */
3182
3183static int nf_tables_check_loops(const struct nft_ctx *ctx,
3184 const struct nft_chain *chain);
3185
3186static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3187 const struct nft_set *set,
3188 const struct nft_set_iter *iter,
3189 const struct nft_set_elem *elem)
3190{
62f9c8b4
PNA
3191 if (elem->flags & NFT_SET_ELEM_INTERVAL_END)
3192 return 0;
3193
20a69341
PM
3194 switch (elem->data.verdict) {
3195 case NFT_JUMP:
3196 case NFT_GOTO:
3197 return nf_tables_check_loops(ctx, elem->data.chain);
3198 default:
3199 return 0;
3200 }
3201}
3202
3203static int nf_tables_check_loops(const struct nft_ctx *ctx,
3204 const struct nft_chain *chain)
3205{
3206 const struct nft_rule *rule;
3207 const struct nft_expr *expr, *last;
20a69341
PM
3208 const struct nft_set *set;
3209 struct nft_set_binding *binding;
3210 struct nft_set_iter iter;
20a69341
PM
3211
3212 if (ctx->chain == chain)
3213 return -ELOOP;
3214
3215 list_for_each_entry(rule, &chain->rules, list) {
3216 nft_rule_for_each_expr(expr, last, rule) {
0ca743a5
PNA
3217 const struct nft_data *data = NULL;
3218 int err;
3219
3220 if (!expr->ops->validate)
20a69341
PM
3221 continue;
3222
0ca743a5
PNA
3223 err = expr->ops->validate(ctx, expr, &data);
3224 if (err < 0)
3225 return err;
3226
20a69341 3227 if (data == NULL)
0ca743a5 3228 continue;
20a69341
PM
3229
3230 switch (data->verdict) {
3231 case NFT_JUMP:
3232 case NFT_GOTO:
3233 err = nf_tables_check_loops(ctx, data->chain);
3234 if (err < 0)
3235 return err;
3236 default:
3237 break;
3238 }
3239 }
3240 }
3241
3242 list_for_each_entry(set, &ctx->table->sets, list) {
3243 if (!(set->flags & NFT_SET_MAP) ||
3244 set->dtype != NFT_DATA_VERDICT)
3245 continue;
3246
3247 list_for_each_entry(binding, &set->bindings, list) {
3248 if (binding->chain != chain)
3249 continue;
3250
3251 iter.skip = 0;
3252 iter.count = 0;
3253 iter.err = 0;
3254 iter.fn = nf_tables_loop_check_setelem;
3255
3256 set->ops->walk(ctx, set, &iter);
3257 if (iter.err < 0)
3258 return iter.err;
3259 }
3260 }
3261
3262 return 0;
3263}
3264
96518518
PM
3265/**
3266 * nft_validate_input_register - validate an expressions' input register
3267 *
3268 * @reg: the register number
3269 *
3270 * Validate that the input register is one of the general purpose
3271 * registers.
3272 */
3273int nft_validate_input_register(enum nft_registers reg)
3274{
3275 if (reg <= NFT_REG_VERDICT)
3276 return -EINVAL;
3277 if (reg > NFT_REG_MAX)
3278 return -ERANGE;
3279 return 0;
3280}
3281EXPORT_SYMBOL_GPL(nft_validate_input_register);
3282
3283/**
3284 * nft_validate_output_register - validate an expressions' output register
3285 *
3286 * @reg: the register number
3287 *
3288 * Validate that the output register is one of the general purpose
3289 * registers or the verdict register.
3290 */
3291int nft_validate_output_register(enum nft_registers reg)
3292{
3293 if (reg < NFT_REG_VERDICT)
3294 return -EINVAL;
3295 if (reg > NFT_REG_MAX)
3296 return -ERANGE;
3297 return 0;
3298}
3299EXPORT_SYMBOL_GPL(nft_validate_output_register);
3300
3301/**
3302 * nft_validate_data_load - validate an expressions' data load
3303 *
3304 * @ctx: context of the expression performing the load
3305 * @reg: the destination register number
3306 * @data: the data to load
3307 * @type: the data type
3308 *
3309 * Validate that a data load uses the appropriate data type for
3310 * the destination register. A value of NULL for the data means
3311 * that its runtime gathered data, which is always of type
3312 * NFT_DATA_VALUE.
3313 */
3314int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3315 const struct nft_data *data,
3316 enum nft_data_types type)
3317{
20a69341
PM
3318 int err;
3319
96518518
PM
3320 switch (reg) {
3321 case NFT_REG_VERDICT:
3322 if (data == NULL || type != NFT_DATA_VERDICT)
3323 return -EINVAL;
20a69341
PM
3324
3325 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3326 err = nf_tables_check_loops(ctx, data->chain);
3327 if (err < 0)
3328 return err;
3329
3330 if (ctx->chain->level + 1 > data->chain->level) {
3331 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3332 return -EMLINK;
3333 data->chain->level = ctx->chain->level + 1;
3334 }
3335 }
3336
96518518
PM
3337 return 0;
3338 default:
3339 if (data != NULL && type != NFT_DATA_VALUE)
3340 return -EINVAL;
3341 return 0;
3342 }
3343}
3344EXPORT_SYMBOL_GPL(nft_validate_data_load);
3345
3346static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3347 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3348 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3349 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3350};
3351
3352static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3353 struct nft_data_desc *desc, const struct nlattr *nla)
3354{
3355 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3356 struct nft_chain *chain;
3357 int err;
3358
3359 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3360 if (err < 0)
3361 return err;
3362
3363 if (!tb[NFTA_VERDICT_CODE])
3364 return -EINVAL;
3365 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3366
3367 switch (data->verdict) {
e0abdadc
PM
3368 default:
3369 switch (data->verdict & NF_VERDICT_MASK) {
3370 case NF_ACCEPT:
3371 case NF_DROP:
3372 case NF_QUEUE:
3373 break;
3374 default:
3375 return -EINVAL;
3376 }
3377 /* fall through */
96518518
PM
3378 case NFT_CONTINUE:
3379 case NFT_BREAK:
3380 case NFT_RETURN:
3381 desc->len = sizeof(data->verdict);
3382 break;
3383 case NFT_JUMP:
3384 case NFT_GOTO:
3385 if (!tb[NFTA_VERDICT_CHAIN])
3386 return -EINVAL;
3387 chain = nf_tables_chain_lookup(ctx->table,
3388 tb[NFTA_VERDICT_CHAIN]);
3389 if (IS_ERR(chain))
3390 return PTR_ERR(chain);
3391 if (chain->flags & NFT_BASE_CHAIN)
3392 return -EOPNOTSUPP;
3393
96518518
PM
3394 chain->use++;
3395 data->chain = chain;
3396 desc->len = sizeof(data);
3397 break;
96518518
PM
3398 }
3399
3400 desc->type = NFT_DATA_VERDICT;
3401 return 0;
3402}
3403
3404static void nft_verdict_uninit(const struct nft_data *data)
3405{
3406 switch (data->verdict) {
3407 case NFT_JUMP:
3408 case NFT_GOTO:
3409 data->chain->use--;
3410 break;
3411 }
3412}
3413
3414static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3415{
3416 struct nlattr *nest;
3417
3418 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3419 if (!nest)
3420 goto nla_put_failure;
3421
3422 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3423 goto nla_put_failure;
3424
3425 switch (data->verdict) {
3426 case NFT_JUMP:
3427 case NFT_GOTO:
3428 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3429 goto nla_put_failure;
3430 }
3431 nla_nest_end(skb, nest);
3432 return 0;
3433
3434nla_put_failure:
3435 return -1;
3436}
3437
3438static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3439 struct nft_data_desc *desc, const struct nlattr *nla)
3440{
3441 unsigned int len;
3442
3443 len = nla_len(nla);
3444 if (len == 0)
3445 return -EINVAL;
3446 if (len > sizeof(data->data))
3447 return -EOVERFLOW;
3448
3449 nla_memcpy(data->data, nla, sizeof(data->data));
3450 desc->type = NFT_DATA_VALUE;
3451 desc->len = len;
3452 return 0;
3453}
3454
3455static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3456 unsigned int len)
3457{
3458 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3459}
3460
3461static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3462 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3463 .len = FIELD_SIZEOF(struct nft_data, data) },
3464 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3465};
3466
3467/**
3468 * nft_data_init - parse nf_tables data netlink attributes
3469 *
3470 * @ctx: context of the expression using the data
3471 * @data: destination struct nft_data
3472 * @desc: data description
3473 * @nla: netlink attribute containing data
3474 *
3475 * Parse the netlink data attributes and initialize a struct nft_data.
3476 * The type and length of data are returned in the data description.
3477 *
3478 * The caller can indicate that it only wants to accept data of type
3479 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3480 */
3481int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3482 struct nft_data_desc *desc, const struct nlattr *nla)
3483{
3484 struct nlattr *tb[NFTA_DATA_MAX + 1];
3485 int err;
3486
3487 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3488 if (err < 0)
3489 return err;
3490
3491 if (tb[NFTA_DATA_VALUE])
3492 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3493 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3494 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3495 return -EINVAL;
3496}
3497EXPORT_SYMBOL_GPL(nft_data_init);
3498
3499/**
3500 * nft_data_uninit - release a nft_data item
3501 *
3502 * @data: struct nft_data to release
3503 * @type: type of data
3504 *
3505 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3506 * all others need to be released by calling this function.
3507 */
3508void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3509{
3510 switch (type) {
3511 case NFT_DATA_VALUE:
3512 return;
3513 case NFT_DATA_VERDICT:
3514 return nft_verdict_uninit(data);
3515 default:
3516 WARN_ON(1);
3517 }
3518}
3519EXPORT_SYMBOL_GPL(nft_data_uninit);
3520
3521int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3522 enum nft_data_types type, unsigned int len)
3523{
3524 struct nlattr *nest;
3525 int err;
3526
3527 nest = nla_nest_start(skb, attr);
3528 if (nest == NULL)
3529 return -1;
3530
3531 switch (type) {
3532 case NFT_DATA_VALUE:
3533 err = nft_value_dump(skb, data, len);
3534 break;
3535 case NFT_DATA_VERDICT:
3536 err = nft_verdict_dump(skb, data);
3537 break;
3538 default:
3539 err = -EINVAL;
3540 WARN_ON(1);
3541 }
3542
3543 nla_nest_end(skb, nest);
3544 return err;
3545}
3546EXPORT_SYMBOL_GPL(nft_data_dump);
3547
99633ab2
PNA
3548static int nf_tables_init_net(struct net *net)
3549{
3550 INIT_LIST_HEAD(&net->nft.af_info);
0628b123 3551 INIT_LIST_HEAD(&net->nft.commit_list);
99633ab2
PNA
3552 return 0;
3553}
3554
3555static struct pernet_operations nf_tables_net_ops = {
3556 .init = nf_tables_init_net,
3557};
3558
96518518
PM
3559static int __init nf_tables_module_init(void)
3560{
3561 int err;
3562
3563 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3564 GFP_KERNEL);
3565 if (info == NULL) {
3566 err = -ENOMEM;
3567 goto err1;
3568 }
3569
3570 err = nf_tables_core_module_init();
3571 if (err < 0)
3572 goto err2;
3573
3574 err = nfnetlink_subsys_register(&nf_tables_subsys);
3575 if (err < 0)
3576 goto err3;
3577
3578 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
99633ab2 3579 return register_pernet_subsys(&nf_tables_net_ops);
96518518
PM
3580err3:
3581 nf_tables_core_module_exit();
3582err2:
3583 kfree(info);
3584err1:
3585 return err;
3586}
3587
3588static void __exit nf_tables_module_exit(void)
3589{
99633ab2 3590 unregister_pernet_subsys(&nf_tables_net_ops);
96518518
PM
3591 nfnetlink_subsys_unregister(&nf_tables_subsys);
3592 nf_tables_core_module_exit();
3593 kfree(info);
3594}
3595
3596module_init(nf_tables_module_init);
3597module_exit(nf_tables_module_exit);
3598
3599MODULE_LICENSE("GPL");
3600MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3601MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
This page took 0.232684 seconds and 5 git commands to generate.