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