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