1
2
3
4
5
6
7
8
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
24static LIST_HEAD(nf_tables_expressions);
25static LIST_HEAD(nf_tables_objects);
26
27
28
29
30
31
32
33
34
35int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
36{
37 INIT_LIST_HEAD(&afi->tables);
38 nfnl_lock(NFNL_SUBSYS_NFTABLES);
39 list_add_tail_rcu(&afi->list, &net->nft.af_info);
40 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
41 return 0;
42}
43EXPORT_SYMBOL_GPL(nft_register_afinfo);
44
45static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi);
46
47
48
49
50
51
52
53
54void nft_unregister_afinfo(struct net *net, struct nft_af_info *afi)
55{
56 nfnl_lock(NFNL_SUBSYS_NFTABLES);
57 __nft_release_afinfo(net, afi);
58 list_del_rcu(&afi->list);
59 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
60}
61EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
62
63static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
64{
65 struct nft_af_info *afi;
66
67 list_for_each_entry(afi, &net->nft.af_info, list) {
68 if (afi->family == family)
69 return afi;
70 }
71 return NULL;
72}
73
74static struct nft_af_info *
75nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
76{
77 struct nft_af_info *afi;
78
79 afi = nft_afinfo_lookup(net, family);
80 if (afi != NULL)
81 return afi;
82#ifdef CONFIG_MODULES
83 if (autoload) {
84 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
85 request_module("nft-afinfo-%u", family);
86 nfnl_lock(NFNL_SUBSYS_NFTABLES);
87 afi = nft_afinfo_lookup(net, family);
88 if (afi != NULL)
89 return ERR_PTR(-EAGAIN);
90 }
91#endif
92 return ERR_PTR(-EAFNOSUPPORT);
93}
94
95static void nft_ctx_init(struct nft_ctx *ctx,
96 struct net *net,
97 const struct sk_buff *skb,
98 const struct nlmsghdr *nlh,
99 struct nft_af_info *afi,
100 struct nft_table *table,
101 struct nft_chain *chain,
102 const struct nlattr * const *nla)
103{
104 ctx->net = net;
105 ctx->afi = afi;
106 ctx->table = table;
107 ctx->chain = chain;
108 ctx->nla = nla;
109 ctx->portid = NETLINK_CB(skb).portid;
110 ctx->report = nlmsg_report(nlh);
111 ctx->seq = nlh->nlmsg_seq;
112}
113
114static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,
115 int msg_type, u32 size, gfp_t gfp)
116{
117 struct nft_trans *trans;
118
119 trans = kzalloc(sizeof(struct nft_trans) + size, gfp);
120 if (trans == NULL)
121 return NULL;
122
123 trans->msg_type = msg_type;
124 trans->ctx = *ctx;
125
126 return trans;
127}
128
129static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,
130 int msg_type, u32 size)
131{
132 return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL);
133}
134
135static void nft_trans_destroy(struct nft_trans *trans)
136{
137 list_del(&trans->list);
138 kfree(trans);
139}
140
141static int nf_tables_register_hooks(struct net *net,
142 const struct nft_table *table,
143 struct nft_chain *chain,
144 unsigned int hook_nops)
145{
146 if (table->flags & NFT_TABLE_F_DORMANT ||
147 !nft_is_base_chain(chain))
148 return 0;
149
150 return nf_register_net_hooks(net, nft_base_chain(chain)->ops,
151 hook_nops);
152}
153
154static void nf_tables_unregister_hooks(struct net *net,
155 const struct nft_table *table,
156 struct nft_chain *chain,
157 unsigned int hook_nops)
158{
159 if (table->flags & NFT_TABLE_F_DORMANT ||
160 !nft_is_base_chain(chain))
161 return;
162
163 nf_unregister_net_hooks(net, nft_base_chain(chain)->ops, hook_nops);
164}
165
166static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type)
167{
168 struct nft_trans *trans;
169
170 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table));
171 if (trans == NULL)
172 return -ENOMEM;
173
174 if (msg_type == NFT_MSG_NEWTABLE)
175 nft_activate_next(ctx->net, ctx->table);
176
177 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
178 return 0;
179}
180
181static int nft_deltable(struct nft_ctx *ctx)
182{
183 int err;
184
185 err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE);
186 if (err < 0)
187 return err;
188
189 nft_deactivate_next(ctx->net, ctx->table);
190 return err;
191}
192
193static int nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)
194{
195 struct nft_trans *trans;
196
197 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain));
198 if (trans == NULL)
199 return -ENOMEM;
200
201 if (msg_type == NFT_MSG_NEWCHAIN)
202 nft_activate_next(ctx->net, ctx->chain);
203
204 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
205 return 0;
206}
207
208static int nft_delchain(struct nft_ctx *ctx)
209{
210 int err;
211
212 err = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN);
213 if (err < 0)
214 return err;
215
216 ctx->table->use--;
217 nft_deactivate_next(ctx->net, ctx->chain);
218
219 return err;
220}
221
222static int
223nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule)
224{
225
226 if (nft_is_active_next(ctx->net, rule)) {
227 nft_deactivate_next(ctx->net, rule);
228 ctx->chain->use--;
229 return 0;
230 }
231 return -ENOENT;
232}
233
234static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,
235 struct nft_rule *rule)
236{
237 struct nft_trans *trans;
238
239 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule));
240 if (trans == NULL)
241 return NULL;
242
243 if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) {
244 nft_trans_rule_id(trans) =
245 ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID]));
246 }
247 nft_trans_rule(trans) = rule;
248 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
249
250 return trans;
251}
252
253static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule)
254{
255 struct nft_trans *trans;
256 int err;
257
258 trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule);
259 if (trans == NULL)
260 return -ENOMEM;
261
262 err = nf_tables_delrule_deactivate(ctx, rule);
263 if (err < 0) {
264 nft_trans_destroy(trans);
265 return err;
266 }
267
268 return 0;
269}
270
271static int nft_delrule_by_chain(struct nft_ctx *ctx)
272{
273 struct nft_rule *rule;
274 int err;
275
276 list_for_each_entry(rule, &ctx->chain->rules, list) {
277 err = nft_delrule(ctx, rule);
278 if (err < 0)
279 return err;
280 }
281 return 0;
282}
283
284static int nft_trans_set_add(struct nft_ctx *ctx, int msg_type,
285 struct nft_set *set)
286{
287 struct nft_trans *trans;
288
289 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set));
290 if (trans == NULL)
291 return -ENOMEM;
292
293 if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) {
294 nft_trans_set_id(trans) =
295 ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID]));
296 nft_activate_next(ctx->net, set);
297 }
298 nft_trans_set(trans) = set;
299 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
300
301 return 0;
302}
303
304static int nft_delset(struct nft_ctx *ctx, struct nft_set *set)
305{
306 int err;
307
308 err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set);
309 if (err < 0)
310 return err;
311
312 nft_deactivate_next(ctx->net, set);
313 ctx->table->use--;
314
315 return err;
316}
317
318static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type,
319 struct nft_object *obj)
320{
321 struct nft_trans *trans;
322
323 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj));
324 if (trans == NULL)
325 return -ENOMEM;
326
327 if (msg_type == NFT_MSG_NEWOBJ)
328 nft_activate_next(ctx->net, obj);
329
330 nft_trans_obj(trans) = obj;
331 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
332
333 return 0;
334}
335
336static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj)
337{
338 int err;
339
340 err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj);
341 if (err < 0)
342 return err;
343
344 nft_deactivate_next(ctx->net, obj);
345 ctx->table->use--;
346
347 return err;
348}
349
350
351
352
353
354static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
355 const struct nlattr *nla,
356 u8 genmask)
357{
358 struct nft_table *table;
359
360 list_for_each_entry(table, &afi->tables, list) {
361 if (!nla_strcmp(nla, table->name) &&
362 nft_active_genmask(table, genmask))
363 return table;
364 }
365 return NULL;
366}
367
368static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
369 const struct nlattr *nla,
370 u8 genmask)
371{
372 struct nft_table *table;
373
374 if (nla == NULL)
375 return ERR_PTR(-EINVAL);
376
377 table = nft_table_lookup(afi, nla, genmask);
378 if (table != NULL)
379 return table;
380
381 return ERR_PTR(-ENOENT);
382}
383
384static inline u64 nf_tables_alloc_handle(struct nft_table *table)
385{
386 return ++table->hgenerator;
387}
388
389static const struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
390
391static const struct nf_chain_type *
392__nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
393{
394 int i;
395
396 for (i = 0; i < NFT_CHAIN_T_MAX; i++) {
397 if (chain_type[family][i] != NULL &&
398 !nla_strcmp(nla, chain_type[family][i]->name))
399 return chain_type[family][i];
400 }
401 return NULL;
402}
403
404static const struct nf_chain_type *
405nf_tables_chain_type_lookup(const struct nft_af_info *afi,
406 const struct nlattr *nla,
407 bool autoload)
408{
409 const struct nf_chain_type *type;
410
411 type = __nf_tables_chain_type_lookup(afi->family, nla);
412 if (type != NULL)
413 return type;
414#ifdef CONFIG_MODULES
415 if (autoload) {
416 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
417 request_module("nft-chain-%u-%.*s", afi->family,
418 nla_len(nla), (const char *)nla_data(nla));
419 nfnl_lock(NFNL_SUBSYS_NFTABLES);
420 type = __nf_tables_chain_type_lookup(afi->family, nla);
421 if (type != NULL)
422 return ERR_PTR(-EAGAIN);
423 }
424#endif
425 return ERR_PTR(-ENOENT);
426}
427
428static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
429 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
430 .len = NFT_TABLE_MAXNAMELEN - 1 },
431 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
432};
433
434static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net,
435 u32 portid, u32 seq, int event, u32 flags,
436 int family, const struct nft_table *table)
437{
438 struct nlmsghdr *nlh;
439 struct nfgenmsg *nfmsg;
440
441 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
442 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
443 if (nlh == NULL)
444 goto nla_put_failure;
445
446 nfmsg = nlmsg_data(nlh);
447 nfmsg->nfgen_family = family;
448 nfmsg->version = NFNETLINK_V0;
449 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
450
451 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
452 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) ||
453 nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)))
454 goto nla_put_failure;
455
456 nlmsg_end(skb, nlh);
457 return 0;
458
459nla_put_failure:
460 nlmsg_trim(skb, nlh);
461 return -1;
462}
463
464static void nf_tables_table_notify(const struct nft_ctx *ctx, int event)
465{
466 struct sk_buff *skb;
467 int err;
468
469 if (!ctx->report &&
470 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
471 return;
472
473 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
474 if (skb == NULL)
475 goto err;
476
477 err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq,
478 event, 0, ctx->afi->family, ctx->table);
479 if (err < 0) {
480 kfree_skb(skb);
481 goto err;
482 }
483
484 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
485 ctx->report, GFP_KERNEL);
486 return;
487err:
488 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
489}
490
491static int nf_tables_dump_tables(struct sk_buff *skb,
492 struct netlink_callback *cb)
493{
494 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
495 const struct nft_af_info *afi;
496 const struct nft_table *table;
497 unsigned int idx = 0, s_idx = cb->args[0];
498 struct net *net = sock_net(skb->sk);
499 int family = nfmsg->nfgen_family;
500
501 rcu_read_lock();
502 cb->seq = net->nft.base_seq;
503
504 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
505 if (family != NFPROTO_UNSPEC && family != afi->family)
506 continue;
507
508 list_for_each_entry_rcu(table, &afi->tables, list) {
509 if (idx < s_idx)
510 goto cont;
511 if (idx > s_idx)
512 memset(&cb->args[1], 0,
513 sizeof(cb->args) - sizeof(cb->args[0]));
514 if (!nft_is_active(net, table))
515 continue;
516 if (nf_tables_fill_table_info(skb, net,
517 NETLINK_CB(cb->skb).portid,
518 cb->nlh->nlmsg_seq,
519 NFT_MSG_NEWTABLE,
520 NLM_F_MULTI,
521 afi->family, table) < 0)
522 goto done;
523
524 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
525cont:
526 idx++;
527 }
528 }
529done:
530 rcu_read_unlock();
531 cb->args[0] = idx;
532 return skb->len;
533}
534
535static int nf_tables_gettable(struct net *net, struct sock *nlsk,
536 struct sk_buff *skb, const struct nlmsghdr *nlh,
537 const struct nlattr * const nla[])
538{
539 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
540 u8 genmask = nft_genmask_cur(net);
541 const struct nft_af_info *afi;
542 const struct nft_table *table;
543 struct sk_buff *skb2;
544 int family = nfmsg->nfgen_family;
545 int err;
546
547 if (nlh->nlmsg_flags & NLM_F_DUMP) {
548 struct netlink_dump_control c = {
549 .dump = nf_tables_dump_tables,
550 };
551 return netlink_dump_start(nlsk, skb, nlh, &c);
552 }
553
554 afi = nf_tables_afinfo_lookup(net, family, false);
555 if (IS_ERR(afi))
556 return PTR_ERR(afi);
557
558 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask);
559 if (IS_ERR(table))
560 return PTR_ERR(table);
561
562 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
563 if (!skb2)
564 return -ENOMEM;
565
566 err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid,
567 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
568 family, table);
569 if (err < 0)
570 goto err;
571
572 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
573
574err:
575 kfree_skb(skb2);
576 return err;
577}
578
579static void _nf_tables_table_disable(struct net *net,
580 const struct nft_af_info *afi,
581 struct nft_table *table,
582 u32 cnt)
583{
584 struct nft_chain *chain;
585 u32 i = 0;
586
587 list_for_each_entry(chain, &table->chains, list) {
588 if (!nft_is_active_next(net, chain))
589 continue;
590 if (!nft_is_base_chain(chain))
591 continue;
592
593 if (cnt && i++ == cnt)
594 break;
595
596 nf_unregister_net_hooks(net, nft_base_chain(chain)->ops,
597 afi->nops);
598 }
599}
600
601static int nf_tables_table_enable(struct net *net,
602 const struct nft_af_info *afi,
603 struct nft_table *table)
604{
605 struct nft_chain *chain;
606 int err, i = 0;
607
608 list_for_each_entry(chain, &table->chains, list) {
609 if (!nft_is_active_next(net, chain))
610 continue;
611 if (!nft_is_base_chain(chain))
612 continue;
613
614 err = nf_register_net_hooks(net, nft_base_chain(chain)->ops,
615 afi->nops);
616 if (err < 0)
617 goto err;
618
619 i++;
620 }
621 return 0;
622err:
623 if (i)
624 _nf_tables_table_disable(net, afi, table, i);
625 return err;
626}
627
628static void nf_tables_table_disable(struct net *net,
629 const struct nft_af_info *afi,
630 struct nft_table *table)
631{
632 _nf_tables_table_disable(net, afi, table, 0);
633}
634
635static int nf_tables_updtable(struct nft_ctx *ctx)
636{
637 struct nft_trans *trans;
638 u32 flags;
639 int ret = 0;
640
641 if (!ctx->nla[NFTA_TABLE_FLAGS])
642 return 0;
643
644 flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS]));
645 if (flags & ~NFT_TABLE_F_DORMANT)
646 return -EINVAL;
647
648 if (flags == ctx->table->flags)
649 return 0;
650
651 trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE,
652 sizeof(struct nft_trans_table));
653 if (trans == NULL)
654 return -ENOMEM;
655
656 if ((flags & NFT_TABLE_F_DORMANT) &&
657 !(ctx->table->flags & NFT_TABLE_F_DORMANT)) {
658 nft_trans_table_enable(trans) = false;
659 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
660 ctx->table->flags & NFT_TABLE_F_DORMANT) {
661 ret = nf_tables_table_enable(ctx->net, ctx->afi, ctx->table);
662 if (ret >= 0) {
663 ctx->table->flags &= ~NFT_TABLE_F_DORMANT;
664 nft_trans_table_enable(trans) = true;
665 }
666 }
667 if (ret < 0)
668 goto err;
669
670 nft_trans_table_update(trans) = true;
671 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
672 return 0;
673err:
674 nft_trans_destroy(trans);
675 return ret;
676}
677
678static int nf_tables_newtable(struct net *net, struct sock *nlsk,
679 struct sk_buff *skb, const struct nlmsghdr *nlh,
680 const struct nlattr * const nla[])
681{
682 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
683 u8 genmask = nft_genmask_next(net);
684 const struct nlattr *name;
685 struct nft_af_info *afi;
686 struct nft_table *table;
687 int family = nfmsg->nfgen_family;
688 u32 flags = 0;
689 struct nft_ctx ctx;
690 int err;
691
692 afi = nf_tables_afinfo_lookup(net, family, true);
693 if (IS_ERR(afi))
694 return PTR_ERR(afi);
695
696 name = nla[NFTA_TABLE_NAME];
697 table = nf_tables_table_lookup(afi, name, genmask);
698 if (IS_ERR(table)) {
699 if (PTR_ERR(table) != -ENOENT)
700 return PTR_ERR(table);
701 } else {
702 if (nlh->nlmsg_flags & NLM_F_EXCL)
703 return -EEXIST;
704 if (nlh->nlmsg_flags & NLM_F_REPLACE)
705 return -EOPNOTSUPP;
706
707 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
708 return nf_tables_updtable(&ctx);
709 }
710
711 if (nla[NFTA_TABLE_FLAGS]) {
712 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
713 if (flags & ~NFT_TABLE_F_DORMANT)
714 return -EINVAL;
715 }
716
717 err = -EAFNOSUPPORT;
718 if (!try_module_get(afi->owner))
719 goto err1;
720
721 err = -ENOMEM;
722 table = kzalloc(sizeof(*table), GFP_KERNEL);
723 if (table == NULL)
724 goto err2;
725
726 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
727 INIT_LIST_HEAD(&table->chains);
728 INIT_LIST_HEAD(&table->sets);
729 INIT_LIST_HEAD(&table->objects);
730 table->flags = flags;
731
732 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
733 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
734 if (err < 0)
735 goto err3;
736
737 list_add_tail_rcu(&table->list, &afi->tables);
738 return 0;
739err3:
740 kfree(table);
741err2:
742 module_put(afi->owner);
743err1:
744 return err;
745}
746
747static int nft_flush_table(struct nft_ctx *ctx)
748{
749 int err;
750 struct nft_chain *chain, *nc;
751 struct nft_object *obj, *ne;
752 struct nft_set *set, *ns;
753
754 list_for_each_entry(chain, &ctx->table->chains, list) {
755 if (!nft_is_active_next(ctx->net, chain))
756 continue;
757
758 ctx->chain = chain;
759
760 err = nft_delrule_by_chain(ctx);
761 if (err < 0)
762 goto out;
763 }
764
765 list_for_each_entry_safe(set, ns, &ctx->table->sets, list) {
766 if (!nft_is_active_next(ctx->net, set))
767 continue;
768
769 if (set->flags & NFT_SET_ANONYMOUS &&
770 !list_empty(&set->bindings))
771 continue;
772
773 err = nft_delset(ctx, set);
774 if (err < 0)
775 goto out;
776 }
777
778 list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) {
779 err = nft_delobj(ctx, obj);
780 if (err < 0)
781 goto out;
782 }
783
784 list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) {
785 if (!nft_is_active_next(ctx->net, chain))
786 continue;
787
788 ctx->chain = chain;
789
790 err = nft_delchain(ctx);
791 if (err < 0)
792 goto out;
793 }
794
795 err = nft_deltable(ctx);
796out:
797 return err;
798}
799
800static int nft_flush(struct nft_ctx *ctx, int family)
801{
802 struct nft_af_info *afi;
803 struct nft_table *table, *nt;
804 const struct nlattr * const *nla = ctx->nla;
805 int err = 0;
806
807 list_for_each_entry(afi, &ctx->net->nft.af_info, list) {
808 if (family != AF_UNSPEC && afi->family != family)
809 continue;
810
811 ctx->afi = afi;
812 list_for_each_entry_safe(table, nt, &afi->tables, list) {
813 if (!nft_is_active_next(ctx->net, table))
814 continue;
815
816 if (nla[NFTA_TABLE_NAME] &&
817 nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0)
818 continue;
819
820 ctx->table = table;
821
822 err = nft_flush_table(ctx);
823 if (err < 0)
824 goto out;
825 }
826 }
827out:
828 return err;
829}
830
831static int nf_tables_deltable(struct net *net, struct sock *nlsk,
832 struct sk_buff *skb, const struct nlmsghdr *nlh,
833 const struct nlattr * const nla[])
834{
835 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
836 u8 genmask = nft_genmask_next(net);
837 struct nft_af_info *afi;
838 struct nft_table *table;
839 int family = nfmsg->nfgen_family;
840 struct nft_ctx ctx;
841
842 nft_ctx_init(&ctx, net, skb, nlh, NULL, NULL, NULL, nla);
843 if (family == AF_UNSPEC || nla[NFTA_TABLE_NAME] == NULL)
844 return nft_flush(&ctx, family);
845
846 afi = nf_tables_afinfo_lookup(net, family, false);
847 if (IS_ERR(afi))
848 return PTR_ERR(afi);
849
850 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME], genmask);
851 if (IS_ERR(table))
852 return PTR_ERR(table);
853
854 ctx.afi = afi;
855 ctx.table = table;
856
857 return nft_flush_table(&ctx);
858}
859
860static void nf_tables_table_destroy(struct nft_ctx *ctx)
861{
862 BUG_ON(ctx->table->use > 0);
863
864 kfree(ctx->table);
865 module_put(ctx->afi->owner);
866}
867
868int nft_register_chain_type(const struct nf_chain_type *ctype)
869{
870 int err = 0;
871
872 nfnl_lock(NFNL_SUBSYS_NFTABLES);
873 if (chain_type[ctype->family][ctype->type] != NULL) {
874 err = -EBUSY;
875 goto out;
876 }
877 chain_type[ctype->family][ctype->type] = ctype;
878out:
879 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
880 return err;
881}
882EXPORT_SYMBOL_GPL(nft_register_chain_type);
883
884void nft_unregister_chain_type(const struct nf_chain_type *ctype)
885{
886 nfnl_lock(NFNL_SUBSYS_NFTABLES);
887 chain_type[ctype->family][ctype->type] = NULL;
888 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
889}
890EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
891
892
893
894
895
896static struct nft_chain *
897nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle,
898 u8 genmask)
899{
900 struct nft_chain *chain;
901
902 list_for_each_entry(chain, &table->chains, list) {
903 if (chain->handle == handle &&
904 nft_active_genmask(chain, genmask))
905 return chain;
906 }
907
908 return ERR_PTR(-ENOENT);
909}
910
911static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
912 const struct nlattr *nla,
913 u8 genmask)
914{
915 struct nft_chain *chain;
916
917 if (nla == NULL)
918 return ERR_PTR(-EINVAL);
919
920 list_for_each_entry(chain, &table->chains, list) {
921 if (!nla_strcmp(nla, chain->name) &&
922 nft_active_genmask(chain, genmask))
923 return chain;
924 }
925
926 return ERR_PTR(-ENOENT);
927}
928
929static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
930 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING,
931 .len = NFT_TABLE_MAXNAMELEN - 1 },
932 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
933 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
934 .len = NFT_CHAIN_MAXNAMELEN - 1 },
935 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
936 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
937 [NFTA_CHAIN_TYPE] = { .type = NLA_STRING },
938 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
939};
940
941static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
942 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
943 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
944 [NFTA_HOOK_DEV] = { .type = NLA_STRING,
945 .len = IFNAMSIZ - 1 },
946};
947
948static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
949{
950 struct nft_stats *cpu_stats, total;
951 struct nlattr *nest;
952 unsigned int seq;
953 u64 pkts, bytes;
954 int cpu;
955
956 memset(&total, 0, sizeof(total));
957 for_each_possible_cpu(cpu) {
958 cpu_stats = per_cpu_ptr(stats, cpu);
959 do {
960 seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
961 pkts = cpu_stats->pkts;
962 bytes = cpu_stats->bytes;
963 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq));
964 total.pkts += pkts;
965 total.bytes += bytes;
966 }
967 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
968 if (nest == NULL)
969 goto nla_put_failure;
970
971 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts),
972 NFTA_COUNTER_PAD) ||
973 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
974 NFTA_COUNTER_PAD))
975 goto nla_put_failure;
976
977 nla_nest_end(skb, nest);
978 return 0;
979
980nla_put_failure:
981 return -ENOSPC;
982}
983
984static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
985 u32 portid, u32 seq, int event, u32 flags,
986 int family, const struct nft_table *table,
987 const struct nft_chain *chain)
988{
989 struct nlmsghdr *nlh;
990 struct nfgenmsg *nfmsg;
991
992 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
993 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
994 if (nlh == NULL)
995 goto nla_put_failure;
996
997 nfmsg = nlmsg_data(nlh);
998 nfmsg->nfgen_family = family;
999 nfmsg->version = NFNETLINK_V0;
1000 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1001
1002 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
1003 goto nla_put_failure;
1004 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle),
1005 NFTA_CHAIN_PAD))
1006 goto nla_put_failure;
1007 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
1008 goto nla_put_failure;
1009
1010 if (nft_is_base_chain(chain)) {
1011 const struct nft_base_chain *basechain = nft_base_chain(chain);
1012 const struct nf_hook_ops *ops = &basechain->ops[0];
1013 struct nlattr *nest;
1014
1015 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
1016 if (nest == NULL)
1017 goto nla_put_failure;
1018 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
1019 goto nla_put_failure;
1020 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
1021 goto nla_put_failure;
1022 if (basechain->dev_name[0] &&
1023 nla_put_string(skb, NFTA_HOOK_DEV, basechain->dev_name))
1024 goto nla_put_failure;
1025 nla_nest_end(skb, nest);
1026
1027 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
1028 htonl(basechain->policy)))
1029 goto nla_put_failure;
1030
1031 if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
1032 goto nla_put_failure;
1033
1034 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
1035 goto nla_put_failure;
1036 }
1037
1038 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
1039 goto nla_put_failure;
1040
1041 nlmsg_end(skb, nlh);
1042 return 0;
1043
1044nla_put_failure:
1045 nlmsg_trim(skb, nlh);
1046 return -1;
1047}
1048
1049static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event)
1050{
1051 struct sk_buff *skb;
1052 int err;
1053
1054 if (!ctx->report &&
1055 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1056 return;
1057
1058 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1059 if (skb == NULL)
1060 goto err;
1061
1062 err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq,
1063 event, 0, ctx->afi->family, ctx->table,
1064 ctx->chain);
1065 if (err < 0) {
1066 kfree_skb(skb);
1067 goto err;
1068 }
1069
1070 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1071 ctx->report, GFP_KERNEL);
1072 return;
1073err:
1074 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1075}
1076
1077static int nf_tables_dump_chains(struct sk_buff *skb,
1078 struct netlink_callback *cb)
1079{
1080 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1081 const struct nft_af_info *afi;
1082 const struct nft_table *table;
1083 const struct nft_chain *chain;
1084 unsigned int idx = 0, s_idx = cb->args[0];
1085 struct net *net = sock_net(skb->sk);
1086 int family = nfmsg->nfgen_family;
1087
1088 rcu_read_lock();
1089 cb->seq = net->nft.base_seq;
1090
1091 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1092 if (family != NFPROTO_UNSPEC && family != afi->family)
1093 continue;
1094
1095 list_for_each_entry_rcu(table, &afi->tables, list) {
1096 list_for_each_entry_rcu(chain, &table->chains, list) {
1097 if (idx < s_idx)
1098 goto cont;
1099 if (idx > s_idx)
1100 memset(&cb->args[1], 0,
1101 sizeof(cb->args) - sizeof(cb->args[0]));
1102 if (!nft_is_active(net, chain))
1103 continue;
1104 if (nf_tables_fill_chain_info(skb, net,
1105 NETLINK_CB(cb->skb).portid,
1106 cb->nlh->nlmsg_seq,
1107 NFT_MSG_NEWCHAIN,
1108 NLM_F_MULTI,
1109 afi->family, table, chain) < 0)
1110 goto done;
1111
1112 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1113cont:
1114 idx++;
1115 }
1116 }
1117 }
1118done:
1119 rcu_read_unlock();
1120 cb->args[0] = idx;
1121 return skb->len;
1122}
1123
1124static int nf_tables_getchain(struct net *net, struct sock *nlsk,
1125 struct sk_buff *skb, const struct nlmsghdr *nlh,
1126 const struct nlattr * const nla[])
1127{
1128 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1129 u8 genmask = nft_genmask_cur(net);
1130 const struct nft_af_info *afi;
1131 const struct nft_table *table;
1132 const struct nft_chain *chain;
1133 struct sk_buff *skb2;
1134 int family = nfmsg->nfgen_family;
1135 int err;
1136
1137 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1138 struct netlink_dump_control c = {
1139 .dump = nf_tables_dump_chains,
1140 };
1141 return netlink_dump_start(nlsk, skb, nlh, &c);
1142 }
1143
1144 afi = nf_tables_afinfo_lookup(net, family, false);
1145 if (IS_ERR(afi))
1146 return PTR_ERR(afi);
1147
1148 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
1149 if (IS_ERR(table))
1150 return PTR_ERR(table);
1151
1152 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
1153 if (IS_ERR(chain))
1154 return PTR_ERR(chain);
1155
1156 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1157 if (!skb2)
1158 return -ENOMEM;
1159
1160 err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid,
1161 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
1162 family, table, chain);
1163 if (err < 0)
1164 goto err;
1165
1166 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1167
1168err:
1169 kfree_skb(skb2);
1170 return err;
1171}
1172
1173static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
1174 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
1175 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
1176};
1177
1178static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
1179{
1180 struct nlattr *tb[NFTA_COUNTER_MAX+1];
1181 struct nft_stats __percpu *newstats;
1182 struct nft_stats *stats;
1183 int err;
1184
1185 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy,
1186 NULL);
1187 if (err < 0)
1188 return ERR_PTR(err);
1189
1190 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
1191 return ERR_PTR(-EINVAL);
1192
1193 newstats = netdev_alloc_pcpu_stats(struct nft_stats);
1194 if (newstats == NULL)
1195 return ERR_PTR(-ENOMEM);
1196
1197
1198
1199
1200 preempt_disable();
1201 stats = this_cpu_ptr(newstats);
1202 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
1203 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
1204 preempt_enable();
1205
1206 return newstats;
1207}
1208
1209static void nft_chain_stats_replace(struct nft_base_chain *chain,
1210 struct nft_stats __percpu *newstats)
1211{
1212 if (newstats == NULL)
1213 return;
1214
1215 if (chain->stats) {
1216 struct nft_stats __percpu *oldstats =
1217 nft_dereference(chain->stats);
1218
1219 rcu_assign_pointer(chain->stats, newstats);
1220 synchronize_rcu();
1221 free_percpu(oldstats);
1222 } else
1223 rcu_assign_pointer(chain->stats, newstats);
1224}
1225
1226static void nf_tables_chain_destroy(struct nft_chain *chain)
1227{
1228 BUG_ON(chain->use > 0);
1229
1230 if (nft_is_base_chain(chain)) {
1231 struct nft_base_chain *basechain = nft_base_chain(chain);
1232
1233 module_put(basechain->type->owner);
1234 free_percpu(basechain->stats);
1235 if (basechain->ops[0].dev != NULL)
1236 dev_put(basechain->ops[0].dev);
1237 kfree(basechain);
1238 } else {
1239 kfree(chain);
1240 }
1241}
1242
1243struct nft_chain_hook {
1244 u32 num;
1245 u32 priority;
1246 const struct nf_chain_type *type;
1247 struct net_device *dev;
1248};
1249
1250static int nft_chain_parse_hook(struct net *net,
1251 const struct nlattr * const nla[],
1252 struct nft_af_info *afi,
1253 struct nft_chain_hook *hook, bool create)
1254{
1255 struct nlattr *ha[NFTA_HOOK_MAX + 1];
1256 const struct nf_chain_type *type;
1257 struct net_device *dev;
1258 int err;
1259
1260 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
1261 nft_hook_policy, NULL);
1262 if (err < 0)
1263 return err;
1264
1265 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
1266 ha[NFTA_HOOK_PRIORITY] == NULL)
1267 return -EINVAL;
1268
1269 hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
1270 if (hook->num >= afi->nhooks)
1271 return -EINVAL;
1272
1273 hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
1274
1275 type = chain_type[afi->family][NFT_CHAIN_T_DEFAULT];
1276 if (nla[NFTA_CHAIN_TYPE]) {
1277 type = nf_tables_chain_type_lookup(afi, nla[NFTA_CHAIN_TYPE],
1278 create);
1279 if (IS_ERR(type))
1280 return PTR_ERR(type);
1281 }
1282 if (!(type->hook_mask & (1 << hook->num)))
1283 return -EOPNOTSUPP;
1284 if (!try_module_get(type->owner))
1285 return -ENOENT;
1286
1287 hook->type = type;
1288
1289 hook->dev = NULL;
1290 if (afi->flags & NFT_AF_NEEDS_DEV) {
1291 char ifname[IFNAMSIZ];
1292
1293 if (!ha[NFTA_HOOK_DEV]) {
1294 module_put(type->owner);
1295 return -EOPNOTSUPP;
1296 }
1297
1298 nla_strlcpy(ifname, ha[NFTA_HOOK_DEV], IFNAMSIZ);
1299 dev = dev_get_by_name(net, ifname);
1300 if (!dev) {
1301 module_put(type->owner);
1302 return -ENOENT;
1303 }
1304 hook->dev = dev;
1305 } else if (ha[NFTA_HOOK_DEV]) {
1306 module_put(type->owner);
1307 return -EOPNOTSUPP;
1308 }
1309
1310 return 0;
1311}
1312
1313static void nft_chain_release_hook(struct nft_chain_hook *hook)
1314{
1315 module_put(hook->type->owner);
1316 if (hook->dev != NULL)
1317 dev_put(hook->dev);
1318}
1319
1320static int nf_tables_newchain(struct net *net, struct sock *nlsk,
1321 struct sk_buff *skb, const struct nlmsghdr *nlh,
1322 const struct nlattr * const nla[])
1323{
1324 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1325 const struct nlattr * uninitialized_var(name);
1326 struct nft_af_info *afi;
1327 struct nft_table *table;
1328 struct nft_chain *chain;
1329 struct nft_base_chain *basechain = NULL;
1330 u8 genmask = nft_genmask_next(net);
1331 int family = nfmsg->nfgen_family;
1332 u8 policy = NF_ACCEPT;
1333 u64 handle = 0;
1334 unsigned int i;
1335 struct nft_stats __percpu *stats;
1336 int err;
1337 bool create;
1338 struct nft_ctx ctx;
1339
1340 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1341
1342 afi = nf_tables_afinfo_lookup(net, family, true);
1343 if (IS_ERR(afi))
1344 return PTR_ERR(afi);
1345
1346 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
1347 if (IS_ERR(table))
1348 return PTR_ERR(table);
1349
1350 chain = NULL;
1351 name = nla[NFTA_CHAIN_NAME];
1352
1353 if (nla[NFTA_CHAIN_HANDLE]) {
1354 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
1355 chain = nf_tables_chain_lookup_byhandle(table, handle, genmask);
1356 if (IS_ERR(chain))
1357 return PTR_ERR(chain);
1358 } else {
1359 chain = nf_tables_chain_lookup(table, name, genmask);
1360 if (IS_ERR(chain)) {
1361 if (PTR_ERR(chain) != -ENOENT)
1362 return PTR_ERR(chain);
1363 chain = NULL;
1364 }
1365 }
1366
1367 if (nla[NFTA_CHAIN_POLICY]) {
1368 if (chain != NULL &&
1369 !nft_is_base_chain(chain))
1370 return -EOPNOTSUPP;
1371
1372 if (chain == NULL &&
1373 nla[NFTA_CHAIN_HOOK] == NULL)
1374 return -EOPNOTSUPP;
1375
1376 policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY]));
1377 switch (policy) {
1378 case NF_DROP:
1379 case NF_ACCEPT:
1380 break;
1381 default:
1382 return -EINVAL;
1383 }
1384 }
1385
1386 if (chain != NULL) {
1387 struct nft_stats *stats = NULL;
1388 struct nft_trans *trans;
1389
1390 if (nlh->nlmsg_flags & NLM_F_EXCL)
1391 return -EEXIST;
1392 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1393 return -EOPNOTSUPP;
1394
1395 if (nla[NFTA_CHAIN_HOOK]) {
1396 struct nft_base_chain *basechain;
1397 struct nft_chain_hook hook;
1398 struct nf_hook_ops *ops;
1399
1400 if (!nft_is_base_chain(chain))
1401 return -EBUSY;
1402
1403 err = nft_chain_parse_hook(net, nla, afi, &hook,
1404 create);
1405 if (err < 0)
1406 return err;
1407
1408 basechain = nft_base_chain(chain);
1409 if (basechain->type != hook.type) {
1410 nft_chain_release_hook(&hook);
1411 return -EBUSY;
1412 }
1413
1414 for (i = 0; i < afi->nops; i++) {
1415 ops = &basechain->ops[i];
1416 if (ops->hooknum != hook.num ||
1417 ops->priority != hook.priority ||
1418 ops->dev != hook.dev) {
1419 nft_chain_release_hook(&hook);
1420 return -EBUSY;
1421 }
1422 }
1423 nft_chain_release_hook(&hook);
1424 }
1425
1426 if (nla[NFTA_CHAIN_HANDLE] && name) {
1427 struct nft_chain *chain2;
1428
1429 chain2 = nf_tables_chain_lookup(table,
1430 nla[NFTA_CHAIN_NAME],
1431 genmask);
1432 if (IS_ERR(chain2))
1433 return PTR_ERR(chain2);
1434 }
1435
1436 if (nla[NFTA_CHAIN_COUNTERS]) {
1437 if (!nft_is_base_chain(chain))
1438 return -EOPNOTSUPP;
1439
1440 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1441 if (IS_ERR(stats))
1442 return PTR_ERR(stats);
1443 }
1444
1445 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1446 trans = nft_trans_alloc(&ctx, NFT_MSG_NEWCHAIN,
1447 sizeof(struct nft_trans_chain));
1448 if (trans == NULL) {
1449 free_percpu(stats);
1450 return -ENOMEM;
1451 }
1452
1453 nft_trans_chain_stats(trans) = stats;
1454 nft_trans_chain_update(trans) = true;
1455
1456 if (nla[NFTA_CHAIN_POLICY])
1457 nft_trans_chain_policy(trans) = policy;
1458 else
1459 nft_trans_chain_policy(trans) = -1;
1460
1461 if (nla[NFTA_CHAIN_HANDLE] && name) {
1462 nla_strlcpy(nft_trans_chain_name(trans), name,
1463 NFT_CHAIN_MAXNAMELEN);
1464 }
1465 list_add_tail(&trans->list, &net->nft.commit_list);
1466 return 0;
1467 }
1468
1469 if (table->use == UINT_MAX)
1470 return -EOVERFLOW;
1471
1472 if (nla[NFTA_CHAIN_HOOK]) {
1473 struct nft_chain_hook hook;
1474 struct nf_hook_ops *ops;
1475 nf_hookfn *hookfn;
1476
1477 err = nft_chain_parse_hook(net, nla, afi, &hook, create);
1478 if (err < 0)
1479 return err;
1480
1481 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
1482 if (basechain == NULL) {
1483 nft_chain_release_hook(&hook);
1484 return -ENOMEM;
1485 }
1486
1487 if (hook.dev != NULL)
1488 strncpy(basechain->dev_name, hook.dev->name, IFNAMSIZ);
1489
1490 if (nla[NFTA_CHAIN_COUNTERS]) {
1491 stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
1492 if (IS_ERR(stats)) {
1493 nft_chain_release_hook(&hook);
1494 kfree(basechain);
1495 return PTR_ERR(stats);
1496 }
1497 basechain->stats = stats;
1498 } else {
1499 stats = netdev_alloc_pcpu_stats(struct nft_stats);
1500 if (stats == NULL) {
1501 nft_chain_release_hook(&hook);
1502 kfree(basechain);
1503 return -ENOMEM;
1504 }
1505 rcu_assign_pointer(basechain->stats, stats);
1506 }
1507
1508 hookfn = hook.type->hooks[hook.num];
1509 basechain->type = hook.type;
1510 chain = &basechain->chain;
1511
1512 for (i = 0; i < afi->nops; i++) {
1513 ops = &basechain->ops[i];
1514 ops->pf = family;
1515 ops->hooknum = hook.num;
1516 ops->priority = hook.priority;
1517 ops->priv = chain;
1518 ops->hook = afi->hooks[ops->hooknum];
1519 ops->dev = hook.dev;
1520 if (hookfn)
1521 ops->hook = hookfn;
1522 if (afi->hook_ops_init)
1523 afi->hook_ops_init(ops, i);
1524 }
1525
1526 chain->flags |= NFT_BASE_CHAIN;
1527 basechain->policy = policy;
1528 } else {
1529 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1530 if (chain == NULL)
1531 return -ENOMEM;
1532 }
1533
1534 INIT_LIST_HEAD(&chain->rules);
1535 chain->handle = nf_tables_alloc_handle(table);
1536 chain->table = table;
1537 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1538
1539 err = nf_tables_register_hooks(net, table, chain, afi->nops);
1540 if (err < 0)
1541 goto err1;
1542
1543 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1544 err = nft_trans_chain_add(&ctx, NFT_MSG_NEWCHAIN);
1545 if (err < 0)
1546 goto err2;
1547
1548 table->use++;
1549 list_add_tail_rcu(&chain->list, &table->chains);
1550 return 0;
1551err2:
1552 nf_tables_unregister_hooks(net, table, chain, afi->nops);
1553err1:
1554 nf_tables_chain_destroy(chain);
1555 return err;
1556}
1557
1558static int nf_tables_delchain(struct net *net, struct sock *nlsk,
1559 struct sk_buff *skb, const struct nlmsghdr *nlh,
1560 const struct nlattr * const nla[])
1561{
1562 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1563 u8 genmask = nft_genmask_next(net);
1564 struct nft_af_info *afi;
1565 struct nft_table *table;
1566 struct nft_chain *chain;
1567 int family = nfmsg->nfgen_family;
1568 struct nft_ctx ctx;
1569
1570 afi = nf_tables_afinfo_lookup(net, family, false);
1571 if (IS_ERR(afi))
1572 return PTR_ERR(afi);
1573
1574 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE], genmask);
1575 if (IS_ERR(table))
1576 return PTR_ERR(table);
1577
1578 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME], genmask);
1579 if (IS_ERR(chain))
1580 return PTR_ERR(chain);
1581 if (chain->use > 0)
1582 return -EBUSY;
1583
1584 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
1585
1586 return nft_delchain(&ctx);
1587}
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600int nft_register_expr(struct nft_expr_type *type)
1601{
1602 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1603 if (type->family == NFPROTO_UNSPEC)
1604 list_add_tail_rcu(&type->list, &nf_tables_expressions);
1605 else
1606 list_add_rcu(&type->list, &nf_tables_expressions);
1607 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(nft_register_expr);
1611
1612
1613
1614
1615
1616
1617
1618void nft_unregister_expr(struct nft_expr_type *type)
1619{
1620 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1621 list_del_rcu(&type->list);
1622 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1623}
1624EXPORT_SYMBOL_GPL(nft_unregister_expr);
1625
1626static const struct nft_expr_type *__nft_expr_type_get(u8 family,
1627 struct nlattr *nla)
1628{
1629 const struct nft_expr_type *type;
1630
1631 list_for_each_entry(type, &nf_tables_expressions, list) {
1632 if (!nla_strcmp(nla, type->name) &&
1633 (!type->family || type->family == family))
1634 return type;
1635 }
1636 return NULL;
1637}
1638
1639static const struct nft_expr_type *nft_expr_type_get(u8 family,
1640 struct nlattr *nla)
1641{
1642 const struct nft_expr_type *type;
1643
1644 if (nla == NULL)
1645 return ERR_PTR(-EINVAL);
1646
1647 type = __nft_expr_type_get(family, nla);
1648 if (type != NULL && try_module_get(type->owner))
1649 return type;
1650
1651#ifdef CONFIG_MODULES
1652 if (type == NULL) {
1653 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1654 request_module("nft-expr-%u-%.*s", family,
1655 nla_len(nla), (char *)nla_data(nla));
1656 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1657 if (__nft_expr_type_get(family, nla))
1658 return ERR_PTR(-EAGAIN);
1659
1660 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1661 request_module("nft-expr-%.*s",
1662 nla_len(nla), (char *)nla_data(nla));
1663 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1664 if (__nft_expr_type_get(family, nla))
1665 return ERR_PTR(-EAGAIN);
1666 }
1667#endif
1668 return ERR_PTR(-ENOENT);
1669}
1670
1671static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1672 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1673 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1674};
1675
1676static int nf_tables_fill_expr_info(struct sk_buff *skb,
1677 const struct nft_expr *expr)
1678{
1679 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1680 goto nla_put_failure;
1681
1682 if (expr->ops->dump) {
1683 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1684 if (data == NULL)
1685 goto nla_put_failure;
1686 if (expr->ops->dump(skb, expr) < 0)
1687 goto nla_put_failure;
1688 nla_nest_end(skb, data);
1689 }
1690
1691 return skb->len;
1692
1693nla_put_failure:
1694 return -1;
1695};
1696
1697int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1698 const struct nft_expr *expr)
1699{
1700 struct nlattr *nest;
1701
1702 nest = nla_nest_start(skb, attr);
1703 if (!nest)
1704 goto nla_put_failure;
1705 if (nf_tables_fill_expr_info(skb, expr) < 0)
1706 goto nla_put_failure;
1707 nla_nest_end(skb, nest);
1708 return 0;
1709
1710nla_put_failure:
1711 return -1;
1712}
1713
1714struct nft_expr_info {
1715 const struct nft_expr_ops *ops;
1716 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1717};
1718
1719static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1720 const struct nlattr *nla,
1721 struct nft_expr_info *info)
1722{
1723 const struct nft_expr_type *type;
1724 const struct nft_expr_ops *ops;
1725 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1726 int err;
1727
1728 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy, NULL);
1729 if (err < 0)
1730 return err;
1731
1732 type = nft_expr_type_get(ctx->afi->family, tb[NFTA_EXPR_NAME]);
1733 if (IS_ERR(type))
1734 return PTR_ERR(type);
1735
1736 if (tb[NFTA_EXPR_DATA]) {
1737 err = nla_parse_nested(info->tb, type->maxattr,
1738 tb[NFTA_EXPR_DATA], type->policy, NULL);
1739 if (err < 0)
1740 goto err1;
1741 } else
1742 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1743
1744 if (type->select_ops != NULL) {
1745 ops = type->select_ops(ctx,
1746 (const struct nlattr * const *)info->tb);
1747 if (IS_ERR(ops)) {
1748 err = PTR_ERR(ops);
1749 goto err1;
1750 }
1751 } else
1752 ops = type->ops;
1753
1754 info->ops = ops;
1755 return 0;
1756
1757err1:
1758 module_put(type->owner);
1759 return err;
1760}
1761
1762static int nf_tables_newexpr(const struct nft_ctx *ctx,
1763 const struct nft_expr_info *info,
1764 struct nft_expr *expr)
1765{
1766 const struct nft_expr_ops *ops = info->ops;
1767 int err;
1768
1769 expr->ops = ops;
1770 if (ops->init) {
1771 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1772 if (err < 0)
1773 goto err1;
1774 }
1775
1776 if (ops->validate) {
1777 const struct nft_data *data = NULL;
1778
1779 err = ops->validate(ctx, expr, &data);
1780 if (err < 0)
1781 goto err2;
1782 }
1783
1784 return 0;
1785
1786err2:
1787 if (ops->destroy)
1788 ops->destroy(ctx, expr);
1789err1:
1790 expr->ops = NULL;
1791 return err;
1792}
1793
1794static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1795 struct nft_expr *expr)
1796{
1797 if (expr->ops->destroy)
1798 expr->ops->destroy(ctx, expr);
1799 module_put(expr->ops->type->owner);
1800}
1801
1802struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1803 const struct nlattr *nla)
1804{
1805 struct nft_expr_info info;
1806 struct nft_expr *expr;
1807 int err;
1808
1809 err = nf_tables_expr_parse(ctx, nla, &info);
1810 if (err < 0)
1811 goto err1;
1812
1813 err = -ENOMEM;
1814 expr = kzalloc(info.ops->size, GFP_KERNEL);
1815 if (expr == NULL)
1816 goto err2;
1817
1818 err = nf_tables_newexpr(ctx, &info, expr);
1819 if (err < 0)
1820 goto err3;
1821
1822 return expr;
1823err3:
1824 kfree(expr);
1825err2:
1826 module_put(info.ops->type->owner);
1827err1:
1828 return ERR_PTR(err);
1829}
1830
1831void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1832{
1833 nf_tables_expr_destroy(ctx, expr);
1834 kfree(expr);
1835}
1836
1837
1838
1839
1840
1841static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1842 u64 handle)
1843{
1844 struct nft_rule *rule;
1845
1846
1847 list_for_each_entry(rule, &chain->rules, list) {
1848 if (handle == rule->handle)
1849 return rule;
1850 }
1851
1852 return ERR_PTR(-ENOENT);
1853}
1854
1855static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1856 const struct nlattr *nla)
1857{
1858 if (nla == NULL)
1859 return ERR_PTR(-EINVAL);
1860
1861 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1862}
1863
1864static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1865 [NFTA_RULE_TABLE] = { .type = NLA_STRING,
1866 .len = NFT_TABLE_MAXNAMELEN - 1 },
1867 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1868 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1869 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1870 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1871 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1872 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1873 [NFTA_RULE_USERDATA] = { .type = NLA_BINARY,
1874 .len = NFT_USERDATA_MAXLEN },
1875};
1876
1877static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1878 u32 portid, u32 seq, int event,
1879 u32 flags, int family,
1880 const struct nft_table *table,
1881 const struct nft_chain *chain,
1882 const struct nft_rule *rule)
1883{
1884 struct nlmsghdr *nlh;
1885 struct nfgenmsg *nfmsg;
1886 const struct nft_expr *expr, *next;
1887 struct nlattr *list;
1888 const struct nft_rule *prule;
1889 u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
1890
1891 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags);
1892 if (nlh == NULL)
1893 goto nla_put_failure;
1894
1895 nfmsg = nlmsg_data(nlh);
1896 nfmsg->nfgen_family = family;
1897 nfmsg->version = NFNETLINK_V0;
1898 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
1899
1900 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1901 goto nla_put_failure;
1902 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1903 goto nla_put_failure;
1904 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle),
1905 NFTA_RULE_PAD))
1906 goto nla_put_failure;
1907
1908 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1909 prule = list_prev_entry(rule, list);
1910 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1911 cpu_to_be64(prule->handle),
1912 NFTA_RULE_PAD))
1913 goto nla_put_failure;
1914 }
1915
1916 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1917 if (list == NULL)
1918 goto nla_put_failure;
1919 nft_rule_for_each_expr(expr, next, rule) {
1920 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
1921 goto nla_put_failure;
1922 }
1923 nla_nest_end(skb, list);
1924
1925 if (rule->udata) {
1926 struct nft_userdata *udata = nft_userdata(rule);
1927 if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1,
1928 udata->data) < 0)
1929 goto nla_put_failure;
1930 }
1931
1932 nlmsg_end(skb, nlh);
1933 return 0;
1934
1935nla_put_failure:
1936 nlmsg_trim(skb, nlh);
1937 return -1;
1938}
1939
1940static void nf_tables_rule_notify(const struct nft_ctx *ctx,
1941 const struct nft_rule *rule, int event)
1942{
1943 struct sk_buff *skb;
1944 int err;
1945
1946 if (!ctx->report &&
1947 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
1948 return;
1949
1950 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1951 if (skb == NULL)
1952 goto err;
1953
1954 err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq,
1955 event, 0, ctx->afi->family, ctx->table,
1956 ctx->chain, rule);
1957 if (err < 0) {
1958 kfree_skb(skb);
1959 goto err;
1960 }
1961
1962 nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES,
1963 ctx->report, GFP_KERNEL);
1964 return;
1965err:
1966 nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS);
1967}
1968
1969struct nft_rule_dump_ctx {
1970 char table[NFT_TABLE_MAXNAMELEN];
1971 char chain[NFT_CHAIN_MAXNAMELEN];
1972};
1973
1974static int nf_tables_dump_rules(struct sk_buff *skb,
1975 struct netlink_callback *cb)
1976{
1977 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1978 const struct nft_rule_dump_ctx *ctx = cb->data;
1979 const struct nft_af_info *afi;
1980 const struct nft_table *table;
1981 const struct nft_chain *chain;
1982 const struct nft_rule *rule;
1983 unsigned int idx = 0, s_idx = cb->args[0];
1984 struct net *net = sock_net(skb->sk);
1985 int family = nfmsg->nfgen_family;
1986
1987 rcu_read_lock();
1988 cb->seq = net->nft.base_seq;
1989
1990 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
1991 if (family != NFPROTO_UNSPEC && family != afi->family)
1992 continue;
1993
1994 list_for_each_entry_rcu(table, &afi->tables, list) {
1995 if (ctx && ctx->table[0] &&
1996 strcmp(ctx->table, table->name) != 0)
1997 continue;
1998
1999 list_for_each_entry_rcu(chain, &table->chains, list) {
2000 if (ctx && ctx->chain[0] &&
2001 strcmp(ctx->chain, chain->name) != 0)
2002 continue;
2003
2004 list_for_each_entry_rcu(rule, &chain->rules, list) {
2005 if (!nft_is_active(net, rule))
2006 goto cont;
2007 if (idx < s_idx)
2008 goto cont;
2009 if (idx > s_idx)
2010 memset(&cb->args[1], 0,
2011 sizeof(cb->args) - sizeof(cb->args[0]));
2012 if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid,
2013 cb->nlh->nlmsg_seq,
2014 NFT_MSG_NEWRULE,
2015 NLM_F_MULTI | NLM_F_APPEND,
2016 afi->family, table, chain, rule) < 0)
2017 goto done;
2018
2019 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2020cont:
2021 idx++;
2022 }
2023 }
2024 }
2025 }
2026done:
2027 rcu_read_unlock();
2028
2029 cb->args[0] = idx;
2030 return skb->len;
2031}
2032
2033static int nf_tables_dump_rules_done(struct netlink_callback *cb)
2034{
2035 kfree(cb->data);
2036 return 0;
2037}
2038
2039static int nf_tables_getrule(struct net *net, struct sock *nlsk,
2040 struct sk_buff *skb, const struct nlmsghdr *nlh,
2041 const struct nlattr * const nla[])
2042{
2043 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2044 u8 genmask = nft_genmask_cur(net);
2045 const struct nft_af_info *afi;
2046 const struct nft_table *table;
2047 const struct nft_chain *chain;
2048 const struct nft_rule *rule;
2049 struct sk_buff *skb2;
2050 int family = nfmsg->nfgen_family;
2051 int err;
2052
2053 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2054 struct netlink_dump_control c = {
2055 .dump = nf_tables_dump_rules,
2056 .done = nf_tables_dump_rules_done,
2057 };
2058
2059 if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) {
2060 struct nft_rule_dump_ctx *ctx;
2061
2062 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2063 if (!ctx)
2064 return -ENOMEM;
2065
2066 if (nla[NFTA_RULE_TABLE])
2067 nla_strlcpy(ctx->table, nla[NFTA_RULE_TABLE],
2068 sizeof(ctx->table));
2069 if (nla[NFTA_RULE_CHAIN])
2070 nla_strlcpy(ctx->chain, nla[NFTA_RULE_CHAIN],
2071 sizeof(ctx->chain));
2072 c.data = ctx;
2073 }
2074
2075 return netlink_dump_start(nlsk, skb, nlh, &c);
2076 }
2077
2078 afi = nf_tables_afinfo_lookup(net, family, false);
2079 if (IS_ERR(afi))
2080 return PTR_ERR(afi);
2081
2082 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
2083 if (IS_ERR(table))
2084 return PTR_ERR(table);
2085
2086 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
2087 if (IS_ERR(chain))
2088 return PTR_ERR(chain);
2089
2090 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
2091 if (IS_ERR(rule))
2092 return PTR_ERR(rule);
2093
2094 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2095 if (!skb2)
2096 return -ENOMEM;
2097
2098 err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid,
2099 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
2100 family, table, chain, rule);
2101 if (err < 0)
2102 goto err;
2103
2104 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2105
2106err:
2107 kfree_skb(skb2);
2108 return err;
2109}
2110
2111static void nf_tables_rule_destroy(const struct nft_ctx *ctx,
2112 struct nft_rule *rule)
2113{
2114 struct nft_expr *expr;
2115
2116
2117
2118
2119
2120 expr = nft_expr_first(rule);
2121 while (expr != nft_expr_last(rule) && expr->ops) {
2122 nf_tables_expr_destroy(ctx, expr);
2123 expr = nft_expr_next(expr);
2124 }
2125 kfree(rule);
2126}
2127
2128#define NFT_RULE_MAXEXPRS 128
2129
2130static struct nft_expr_info *info;
2131
2132static int nf_tables_newrule(struct net *net, struct sock *nlsk,
2133 struct sk_buff *skb, const struct nlmsghdr *nlh,
2134 const struct nlattr * const nla[])
2135{
2136 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2137 u8 genmask = nft_genmask_next(net);
2138 struct nft_af_info *afi;
2139 struct nft_table *table;
2140 struct nft_chain *chain;
2141 struct nft_rule *rule, *old_rule = NULL;
2142 struct nft_userdata *udata;
2143 struct nft_trans *trans = NULL;
2144 struct nft_expr *expr;
2145 struct nft_ctx ctx;
2146 struct nlattr *tmp;
2147 unsigned int size, i, n, ulen = 0, usize = 0;
2148 int err, rem;
2149 bool create;
2150 u64 handle, pos_handle;
2151
2152 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2153
2154 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2155 if (IS_ERR(afi))
2156 return PTR_ERR(afi);
2157
2158 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
2159 if (IS_ERR(table))
2160 return PTR_ERR(table);
2161
2162 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN], genmask);
2163 if (IS_ERR(chain))
2164 return PTR_ERR(chain);
2165
2166 if (nla[NFTA_RULE_HANDLE]) {
2167 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
2168 rule = __nf_tables_rule_lookup(chain, handle);
2169 if (IS_ERR(rule))
2170 return PTR_ERR(rule);
2171
2172 if (nlh->nlmsg_flags & NLM_F_EXCL)
2173 return -EEXIST;
2174 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2175 old_rule = rule;
2176 else
2177 return -EOPNOTSUPP;
2178 } else {
2179 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
2180 return -EINVAL;
2181 handle = nf_tables_alloc_handle(table);
2182
2183 if (chain->use == UINT_MAX)
2184 return -EOVERFLOW;
2185 }
2186
2187 if (nla[NFTA_RULE_POSITION]) {
2188 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2189 return -EOPNOTSUPP;
2190
2191 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
2192 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
2193 if (IS_ERR(old_rule))
2194 return PTR_ERR(old_rule);
2195 }
2196
2197 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
2198
2199 n = 0;
2200 size = 0;
2201 if (nla[NFTA_RULE_EXPRESSIONS]) {
2202 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
2203 err = -EINVAL;
2204 if (nla_type(tmp) != NFTA_LIST_ELEM)
2205 goto err1;
2206 if (n == NFT_RULE_MAXEXPRS)
2207 goto err1;
2208 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
2209 if (err < 0)
2210 goto err1;
2211 size += info[n].ops->size;
2212 n++;
2213 }
2214 }
2215
2216 err = -EFBIG;
2217 if (size >= 1 << 12)
2218 goto err1;
2219
2220 if (nla[NFTA_RULE_USERDATA]) {
2221 ulen = nla_len(nla[NFTA_RULE_USERDATA]);
2222 if (ulen > 0)
2223 usize = sizeof(struct nft_userdata) + ulen;
2224 }
2225
2226 err = -ENOMEM;
2227 rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL);
2228 if (rule == NULL)
2229 goto err1;
2230
2231 nft_activate_next(net, rule);
2232
2233 rule->handle = handle;
2234 rule->dlen = size;
2235 rule->udata = ulen ? 1 : 0;
2236
2237 if (ulen) {
2238 udata = nft_userdata(rule);
2239 udata->len = ulen - 1;
2240 nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen);
2241 }
2242
2243 expr = nft_expr_first(rule);
2244 for (i = 0; i < n; i++) {
2245 err = nf_tables_newexpr(&ctx, &info[i], expr);
2246 if (err < 0)
2247 goto err2;
2248 info[i].ops = NULL;
2249 expr = nft_expr_next(expr);
2250 }
2251
2252 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
2253 if (nft_is_active_next(net, old_rule)) {
2254 trans = nft_trans_rule_add(&ctx, NFT_MSG_DELRULE,
2255 old_rule);
2256 if (trans == NULL) {
2257 err = -ENOMEM;
2258 goto err2;
2259 }
2260 nft_deactivate_next(net, old_rule);
2261 chain->use--;
2262 list_add_tail_rcu(&rule->list, &old_rule->list);
2263 } else {
2264 err = -ENOENT;
2265 goto err2;
2266 }
2267 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
2268 if (old_rule)
2269 list_add_rcu(&rule->list, &old_rule->list);
2270 else
2271 list_add_tail_rcu(&rule->list, &chain->rules);
2272 else {
2273 if (old_rule)
2274 list_add_tail_rcu(&rule->list, &old_rule->list);
2275 else
2276 list_add_rcu(&rule->list, &chain->rules);
2277 }
2278
2279 if (nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule) == NULL) {
2280 err = -ENOMEM;
2281 goto err3;
2282 }
2283 chain->use++;
2284 return 0;
2285
2286err3:
2287 list_del_rcu(&rule->list);
2288err2:
2289 nf_tables_rule_destroy(&ctx, rule);
2290err1:
2291 for (i = 0; i < n; i++) {
2292 if (info[i].ops != NULL)
2293 module_put(info[i].ops->type->owner);
2294 }
2295 return err;
2296}
2297
2298static struct nft_rule *nft_rule_lookup_byid(const struct net *net,
2299 const struct nlattr *nla)
2300{
2301 u32 id = ntohl(nla_get_be32(nla));
2302 struct nft_trans *trans;
2303
2304 list_for_each_entry(trans, &net->nft.commit_list, list) {
2305 struct nft_rule *rule = nft_trans_rule(trans);
2306
2307 if (trans->msg_type == NFT_MSG_NEWRULE &&
2308 id == nft_trans_rule_id(trans))
2309 return rule;
2310 }
2311 return ERR_PTR(-ENOENT);
2312}
2313
2314static int nf_tables_delrule(struct net *net, struct sock *nlsk,
2315 struct sk_buff *skb, const struct nlmsghdr *nlh,
2316 const struct nlattr * const nla[])
2317{
2318 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2319 u8 genmask = nft_genmask_next(net);
2320 struct nft_af_info *afi;
2321 struct nft_table *table;
2322 struct nft_chain *chain = NULL;
2323 struct nft_rule *rule;
2324 int family = nfmsg->nfgen_family, err = 0;
2325 struct nft_ctx ctx;
2326
2327 afi = nf_tables_afinfo_lookup(net, family, false);
2328 if (IS_ERR(afi))
2329 return PTR_ERR(afi);
2330
2331 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE], genmask);
2332 if (IS_ERR(table))
2333 return PTR_ERR(table);
2334
2335 if (nla[NFTA_RULE_CHAIN]) {
2336 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN],
2337 genmask);
2338 if (IS_ERR(chain))
2339 return PTR_ERR(chain);
2340 }
2341
2342 nft_ctx_init(&ctx, net, skb, nlh, afi, table, chain, nla);
2343
2344 if (chain) {
2345 if (nla[NFTA_RULE_HANDLE]) {
2346 rule = nf_tables_rule_lookup(chain,
2347 nla[NFTA_RULE_HANDLE]);
2348 if (IS_ERR(rule))
2349 return PTR_ERR(rule);
2350
2351 err = nft_delrule(&ctx, rule);
2352 } else if (nla[NFTA_RULE_ID]) {
2353 rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]);
2354 if (IS_ERR(rule))
2355 return PTR_ERR(rule);
2356
2357 err = nft_delrule(&ctx, rule);
2358 } else {
2359 err = nft_delrule_by_chain(&ctx);
2360 }
2361 } else {
2362 list_for_each_entry(chain, &table->chains, list) {
2363 if (!nft_is_active_next(net, chain))
2364 continue;
2365
2366 ctx.chain = chain;
2367 err = nft_delrule_by_chain(&ctx);
2368 if (err < 0)
2369 break;
2370 }
2371 }
2372
2373 return err;
2374}
2375
2376
2377
2378
2379
2380static LIST_HEAD(nf_tables_set_ops);
2381
2382int nft_register_set(struct nft_set_ops *ops)
2383{
2384 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2385 list_add_tail_rcu(&ops->list, &nf_tables_set_ops);
2386 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2387 return 0;
2388}
2389EXPORT_SYMBOL_GPL(nft_register_set);
2390
2391void nft_unregister_set(struct nft_set_ops *ops)
2392{
2393 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2394 list_del_rcu(&ops->list);
2395 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2396}
2397EXPORT_SYMBOL_GPL(nft_unregister_set);
2398
2399
2400
2401
2402
2403
2404static const struct nft_set_ops *
2405nft_select_set_ops(const struct nlattr * const nla[],
2406 const struct nft_set_desc *desc,
2407 enum nft_set_policies policy)
2408{
2409 const struct nft_set_ops *ops, *bops;
2410 struct nft_set_estimate est, best;
2411 u32 features;
2412
2413#ifdef CONFIG_MODULES
2414 if (list_empty(&nf_tables_set_ops)) {
2415 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
2416 request_module("nft-set");
2417 nfnl_lock(NFNL_SUBSYS_NFTABLES);
2418 if (!list_empty(&nf_tables_set_ops))
2419 return ERR_PTR(-EAGAIN);
2420 }
2421#endif
2422 features = 0;
2423 if (nla[NFTA_SET_FLAGS] != NULL) {
2424 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2425 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT |
2426 NFT_SET_OBJECT;
2427 }
2428
2429 bops = NULL;
2430 best.size = ~0;
2431 best.lookup = ~0;
2432 best.space = ~0;
2433
2434 list_for_each_entry(ops, &nf_tables_set_ops, list) {
2435 if ((ops->features & features) != features)
2436 continue;
2437 if (!ops->estimate(desc, features, &est))
2438 continue;
2439
2440 switch (policy) {
2441 case NFT_SET_POL_PERFORMANCE:
2442 if (est.lookup < best.lookup)
2443 break;
2444 if (est.lookup == best.lookup) {
2445 if (!desc->size) {
2446 if (est.space < best.space)
2447 break;
2448 } else if (est.size < best.size) {
2449 break;
2450 }
2451 }
2452 continue;
2453 case NFT_SET_POL_MEMORY:
2454 if (!desc->size) {
2455 if (est.space < best.space)
2456 break;
2457 if (est.space == best.space &&
2458 est.lookup < best.lookup)
2459 break;
2460 } else if (est.size < best.size) {
2461 break;
2462 }
2463 continue;
2464 default:
2465 break;
2466 }
2467
2468 if (!try_module_get(ops->owner))
2469 continue;
2470 if (bops != NULL)
2471 module_put(bops->owner);
2472
2473 bops = ops;
2474 best = est;
2475 }
2476
2477 if (bops != NULL)
2478 return bops;
2479
2480 return ERR_PTR(-EOPNOTSUPP);
2481}
2482
2483static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2484 [NFTA_SET_TABLE] = { .type = NLA_STRING,
2485 .len = NFT_TABLE_MAXNAMELEN - 1 },
2486 [NFTA_SET_NAME] = { .type = NLA_STRING,
2487 .len = NFT_SET_MAXNAMELEN - 1 },
2488 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
2489 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
2490 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
2491 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
2492 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
2493 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2494 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2495 [NFTA_SET_ID] = { .type = NLA_U32 },
2496 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2497 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
2498 [NFTA_SET_USERDATA] = { .type = NLA_BINARY,
2499 .len = NFT_USERDATA_MAXLEN },
2500 [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 },
2501};
2502
2503static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
2504 [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 },
2505};
2506
2507static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net,
2508 const struct sk_buff *skb,
2509 const struct nlmsghdr *nlh,
2510 const struct nlattr * const nla[],
2511 u8 genmask)
2512{
2513 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2514 struct nft_af_info *afi = NULL;
2515 struct nft_table *table = NULL;
2516
2517 if (nfmsg->nfgen_family != NFPROTO_UNSPEC) {
2518 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2519 if (IS_ERR(afi))
2520 return PTR_ERR(afi);
2521 }
2522
2523 if (nla[NFTA_SET_TABLE] != NULL) {
2524 if (afi == NULL)
2525 return -EAFNOSUPPORT;
2526
2527 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE],
2528 genmask);
2529 if (IS_ERR(table))
2530 return PTR_ERR(table);
2531 }
2532
2533 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
2534 return 0;
2535}
2536
2537static struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
2538 const struct nlattr *nla, u8 genmask)
2539{
2540 struct nft_set *set;
2541
2542 if (nla == NULL)
2543 return ERR_PTR(-EINVAL);
2544
2545 list_for_each_entry(set, &table->sets, list) {
2546 if (!nla_strcmp(nla, set->name) &&
2547 nft_active_genmask(set, genmask))
2548 return set;
2549 }
2550 return ERR_PTR(-ENOENT);
2551}
2552
2553static struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
2554 const struct nlattr *nla,
2555 u8 genmask)
2556{
2557 struct nft_trans *trans;
2558 u32 id = ntohl(nla_get_be32(nla));
2559
2560 list_for_each_entry(trans, &net->nft.commit_list, list) {
2561 struct nft_set *set = nft_trans_set(trans);
2562
2563 if (trans->msg_type == NFT_MSG_NEWSET &&
2564 id == nft_trans_set_id(trans) &&
2565 nft_active_genmask(set, genmask))
2566 return set;
2567 }
2568 return ERR_PTR(-ENOENT);
2569}
2570
2571struct nft_set *nft_set_lookup(const struct net *net,
2572 const struct nft_table *table,
2573 const struct nlattr *nla_set_name,
2574 const struct nlattr *nla_set_id,
2575 u8 genmask)
2576{
2577 struct nft_set *set;
2578
2579 set = nf_tables_set_lookup(table, nla_set_name, genmask);
2580 if (IS_ERR(set)) {
2581 if (!nla_set_id)
2582 return set;
2583
2584 set = nf_tables_set_lookup_byid(net, nla_set_id, genmask);
2585 }
2586 return set;
2587}
2588EXPORT_SYMBOL_GPL(nft_set_lookup);
2589
2590static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
2591 const char *name)
2592{
2593 const struct nft_set *i;
2594 const char *p;
2595 unsigned long *inuse;
2596 unsigned int n = 0, min = 0;
2597
2598 p = strnchr(name, NFT_SET_MAXNAMELEN, '%');
2599 if (p != NULL) {
2600 if (p[1] != 'd' || strchr(p + 2, '%'))
2601 return -EINVAL;
2602
2603 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
2604 if (inuse == NULL)
2605 return -ENOMEM;
2606cont:
2607 list_for_each_entry(i, &ctx->table->sets, list) {
2608 int tmp;
2609
2610 if (!nft_is_active_next(ctx->net, set))
2611 continue;
2612 if (!sscanf(i->name, name, &tmp))
2613 continue;
2614 if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE)
2615 continue;
2616
2617 set_bit(tmp - min, inuse);
2618 }
2619
2620 n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE);
2621 if (n >= BITS_PER_BYTE * PAGE_SIZE) {
2622 min += BITS_PER_BYTE * PAGE_SIZE;
2623 memset(inuse, 0, PAGE_SIZE);
2624 goto cont;
2625 }
2626 free_page((unsigned long)inuse);
2627 }
2628
2629 snprintf(set->name, sizeof(set->name), name, min + n);
2630 list_for_each_entry(i, &ctx->table->sets, list) {
2631 if (!nft_is_active_next(ctx->net, i))
2632 continue;
2633 if (!strcmp(set->name, i->name))
2634 return -ENFILE;
2635 }
2636 return 0;
2637}
2638
2639static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2640 const struct nft_set *set, u16 event, u16 flags)
2641{
2642 struct nfgenmsg *nfmsg;
2643 struct nlmsghdr *nlh;
2644 struct nlattr *desc;
2645 u32 portid = ctx->portid;
2646 u32 seq = ctx->seq;
2647
2648 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
2649 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2650 flags);
2651 if (nlh == NULL)
2652 goto nla_put_failure;
2653
2654 nfmsg = nlmsg_data(nlh);
2655 nfmsg->nfgen_family = ctx->afi->family;
2656 nfmsg->version = NFNETLINK_V0;
2657 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
2658
2659 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2660 goto nla_put_failure;
2661 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2662 goto nla_put_failure;
2663 if (set->flags != 0)
2664 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2665 goto nla_put_failure;
2666
2667 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2668 goto nla_put_failure;
2669 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2670 goto nla_put_failure;
2671 if (set->flags & NFT_SET_MAP) {
2672 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2673 goto nla_put_failure;
2674 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2675 goto nla_put_failure;
2676 }
2677 if (set->flags & NFT_SET_OBJECT &&
2678 nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype)))
2679 goto nla_put_failure;
2680
2681 if (set->timeout &&
2682 nla_put_be64(skb, NFTA_SET_TIMEOUT,
2683 cpu_to_be64(jiffies_to_msecs(set->timeout)),
2684 NFTA_SET_PAD))
2685 goto nla_put_failure;
2686 if (set->gc_int &&
2687 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2688 goto nla_put_failure;
2689
2690 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2691 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2692 goto nla_put_failure;
2693 }
2694
2695 if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata))
2696 goto nla_put_failure;
2697
2698 desc = nla_nest_start(skb, NFTA_SET_DESC);
2699 if (desc == NULL)
2700 goto nla_put_failure;
2701 if (set->size &&
2702 nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size)))
2703 goto nla_put_failure;
2704 nla_nest_end(skb, desc);
2705
2706 nlmsg_end(skb, nlh);
2707 return 0;
2708
2709nla_put_failure:
2710 nlmsg_trim(skb, nlh);
2711 return -1;
2712}
2713
2714static void nf_tables_set_notify(const struct nft_ctx *ctx,
2715 const struct nft_set *set, int event,
2716 gfp_t gfp_flags)
2717{
2718 struct sk_buff *skb;
2719 u32 portid = ctx->portid;
2720 int err;
2721
2722 if (!ctx->report &&
2723 !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2724 return;
2725
2726 skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags);
2727 if (skb == NULL)
2728 goto err;
2729
2730 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2731 if (err < 0) {
2732 kfree_skb(skb);
2733 goto err;
2734 }
2735
2736 nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report,
2737 gfp_flags);
2738 return;
2739err:
2740 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
2741}
2742
2743static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2744{
2745 const struct nft_set *set;
2746 unsigned int idx, s_idx = cb->args[0];
2747 struct nft_af_info *afi;
2748 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2749 struct net *net = sock_net(skb->sk);
2750 int cur_family = cb->args[3];
2751 struct nft_ctx *ctx = cb->data, ctx_set;
2752
2753 if (cb->args[1])
2754 return skb->len;
2755
2756 rcu_read_lock();
2757 cb->seq = net->nft.base_seq;
2758
2759 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
2760 if (ctx->afi && ctx->afi != afi)
2761 continue;
2762
2763 if (cur_family) {
2764 if (afi->family != cur_family)
2765 continue;
2766
2767 cur_family = 0;
2768 }
2769 list_for_each_entry_rcu(table, &afi->tables, list) {
2770 if (ctx->table && ctx->table != table)
2771 continue;
2772
2773 if (cur_table) {
2774 if (cur_table != table)
2775 continue;
2776
2777 cur_table = NULL;
2778 }
2779 idx = 0;
2780 list_for_each_entry_rcu(set, &table->sets, list) {
2781 if (idx < s_idx)
2782 goto cont;
2783 if (!nft_is_active(net, set))
2784 goto cont;
2785
2786 ctx_set = *ctx;
2787 ctx_set.table = table;
2788 ctx_set.afi = afi;
2789 if (nf_tables_fill_set(skb, &ctx_set, set,
2790 NFT_MSG_NEWSET,
2791 NLM_F_MULTI) < 0) {
2792 cb->args[0] = idx;
2793 cb->args[2] = (unsigned long) table;
2794 cb->args[3] = afi->family;
2795 goto done;
2796 }
2797 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2798cont:
2799 idx++;
2800 }
2801 if (s_idx)
2802 s_idx = 0;
2803 }
2804 }
2805 cb->args[1] = 1;
2806done:
2807 rcu_read_unlock();
2808 return skb->len;
2809}
2810
2811static int nf_tables_dump_sets_done(struct netlink_callback *cb)
2812{
2813 kfree(cb->data);
2814 return 0;
2815}
2816
2817static int nf_tables_getset(struct net *net, struct sock *nlsk,
2818 struct sk_buff *skb, const struct nlmsghdr *nlh,
2819 const struct nlattr * const nla[])
2820{
2821 u8 genmask = nft_genmask_cur(net);
2822 const struct nft_set *set;
2823 struct nft_ctx ctx;
2824 struct sk_buff *skb2;
2825 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2826 int err;
2827
2828
2829 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
2830 if (err < 0)
2831 return err;
2832
2833 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2834 struct netlink_dump_control c = {
2835 .dump = nf_tables_dump_sets,
2836 .done = nf_tables_dump_sets_done,
2837 };
2838 struct nft_ctx *ctx_dump;
2839
2840 ctx_dump = kmalloc(sizeof(*ctx_dump), GFP_KERNEL);
2841 if (ctx_dump == NULL)
2842 return -ENOMEM;
2843
2844 *ctx_dump = ctx;
2845 c.data = ctx_dump;
2846
2847 return netlink_dump_start(nlsk, skb, nlh, &c);
2848 }
2849
2850
2851 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
2852 return -EAFNOSUPPORT;
2853 if (!nla[NFTA_SET_TABLE])
2854 return -EINVAL;
2855
2856 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
2857 if (IS_ERR(set))
2858 return PTR_ERR(set);
2859
2860 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2861 if (skb2 == NULL)
2862 return -ENOMEM;
2863
2864 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2865 if (err < 0)
2866 goto err;
2867
2868 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2869
2870err:
2871 kfree_skb(skb2);
2872 return err;
2873}
2874
2875static int nf_tables_set_desc_parse(const struct nft_ctx *ctx,
2876 struct nft_set_desc *desc,
2877 const struct nlattr *nla)
2878{
2879 struct nlattr *da[NFTA_SET_DESC_MAX + 1];
2880 int err;
2881
2882 err = nla_parse_nested(da, NFTA_SET_DESC_MAX, nla,
2883 nft_set_desc_policy, NULL);
2884 if (err < 0)
2885 return err;
2886
2887 if (da[NFTA_SET_DESC_SIZE] != NULL)
2888 desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE]));
2889
2890 return 0;
2891}
2892
2893static int nf_tables_newset(struct net *net, struct sock *nlsk,
2894 struct sk_buff *skb, const struct nlmsghdr *nlh,
2895 const struct nlattr * const nla[])
2896{
2897 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2898 u8 genmask = nft_genmask_next(net);
2899 const struct nft_set_ops *ops;
2900 struct nft_af_info *afi;
2901 struct nft_table *table;
2902 struct nft_set *set;
2903 struct nft_ctx ctx;
2904 char name[NFT_SET_MAXNAMELEN];
2905 unsigned int size;
2906 bool create;
2907 u64 timeout;
2908 u32 ktype, dtype, flags, policy, gc_int, objtype;
2909 struct nft_set_desc desc;
2910 unsigned char *udata;
2911 u16 udlen;
2912 int err;
2913
2914 if (nla[NFTA_SET_TABLE] == NULL ||
2915 nla[NFTA_SET_NAME] == NULL ||
2916 nla[NFTA_SET_KEY_LEN] == NULL ||
2917 nla[NFTA_SET_ID] == NULL)
2918 return -EINVAL;
2919
2920 memset(&desc, 0, sizeof(desc));
2921
2922 ktype = NFT_DATA_VALUE;
2923 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2924 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2925 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2926 return -EINVAL;
2927 }
2928
2929 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2930 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
2931 return -EINVAL;
2932
2933 flags = 0;
2934 if (nla[NFTA_SET_FLAGS] != NULL) {
2935 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2936 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2937 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2938 NFT_SET_MAP | NFT_SET_EVAL |
2939 NFT_SET_OBJECT))
2940 return -EINVAL;
2941
2942 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT)) ==
2943 (NFT_SET_MAP | NFT_SET_EVAL | NFT_SET_OBJECT))
2944 return -EOPNOTSUPP;
2945 }
2946
2947 dtype = 0;
2948 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2949 if (!(flags & NFT_SET_MAP))
2950 return -EINVAL;
2951
2952 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2953 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2954 dtype != NFT_DATA_VERDICT)
2955 return -EINVAL;
2956
2957 if (dtype != NFT_DATA_VERDICT) {
2958 if (nla[NFTA_SET_DATA_LEN] == NULL)
2959 return -EINVAL;
2960 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2961 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
2962 return -EINVAL;
2963 } else
2964 desc.dlen = sizeof(struct nft_verdict);
2965 } else if (flags & NFT_SET_MAP)
2966 return -EINVAL;
2967
2968 if (nla[NFTA_SET_OBJ_TYPE] != NULL) {
2969 if (!(flags & NFT_SET_OBJECT))
2970 return -EINVAL;
2971
2972 objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE]));
2973 if (objtype == NFT_OBJECT_UNSPEC ||
2974 objtype > NFT_OBJECT_MAX)
2975 return -EINVAL;
2976 } else if (flags & NFT_SET_OBJECT)
2977 return -EINVAL;
2978 else
2979 objtype = NFT_OBJECT_UNSPEC;
2980
2981 timeout = 0;
2982 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2983 if (!(flags & NFT_SET_TIMEOUT))
2984 return -EINVAL;
2985 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
2986 nla[NFTA_SET_TIMEOUT])));
2987 }
2988 gc_int = 0;
2989 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2990 if (!(flags & NFT_SET_TIMEOUT))
2991 return -EINVAL;
2992 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2993 }
2994
2995 policy = NFT_SET_POL_PERFORMANCE;
2996 if (nla[NFTA_SET_POLICY] != NULL)
2997 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
2998
2999 if (nla[NFTA_SET_DESC] != NULL) {
3000 err = nf_tables_set_desc_parse(&ctx, &desc, nla[NFTA_SET_DESC]);
3001 if (err < 0)
3002 return err;
3003 }
3004
3005 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
3006
3007 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
3008 if (IS_ERR(afi))
3009 return PTR_ERR(afi);
3010
3011 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE], genmask);
3012 if (IS_ERR(table))
3013 return PTR_ERR(table);
3014
3015 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
3016
3017 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME], genmask);
3018 if (IS_ERR(set)) {
3019 if (PTR_ERR(set) != -ENOENT)
3020 return PTR_ERR(set);
3021 } else {
3022 if (nlh->nlmsg_flags & NLM_F_EXCL)
3023 return -EEXIST;
3024 if (nlh->nlmsg_flags & NLM_F_REPLACE)
3025 return -EOPNOTSUPP;
3026 return 0;
3027 }
3028
3029 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
3030 return -ENOENT;
3031
3032 ops = nft_select_set_ops(nla, &desc, policy);
3033 if (IS_ERR(ops))
3034 return PTR_ERR(ops);
3035
3036 udlen = 0;
3037 if (nla[NFTA_SET_USERDATA])
3038 udlen = nla_len(nla[NFTA_SET_USERDATA]);
3039
3040 size = 0;
3041 if (ops->privsize != NULL)
3042 size = ops->privsize(nla);
3043
3044 err = -ENOMEM;
3045 set = kzalloc(sizeof(*set) + size + udlen, GFP_KERNEL);
3046 if (set == NULL)
3047 goto err1;
3048
3049 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
3050 err = nf_tables_set_alloc_name(&ctx, set, name);
3051 if (err < 0)
3052 goto err2;
3053
3054 udata = NULL;
3055 if (udlen) {
3056 udata = set->data + size;
3057 nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen);
3058 }
3059
3060 INIT_LIST_HEAD(&set->bindings);
3061 set->ops = ops;
3062 set->ktype = ktype;
3063 set->klen = desc.klen;
3064 set->dtype = dtype;
3065 set->objtype = objtype;
3066 set->dlen = desc.dlen;
3067 set->flags = flags;
3068 set->size = desc.size;
3069 set->policy = policy;
3070 set->udlen = udlen;
3071 set->udata = udata;
3072 set->timeout = timeout;
3073 set->gc_int = gc_int;
3074
3075 err = ops->init(set, &desc, nla);
3076 if (err < 0)
3077 goto err2;
3078
3079 err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set);
3080 if (err < 0)
3081 goto err3;
3082
3083 list_add_tail_rcu(&set->list, &table->sets);
3084 table->use++;
3085 return 0;
3086
3087err3:
3088 ops->destroy(set);
3089err2:
3090 kfree(set);
3091err1:
3092 module_put(ops->owner);
3093 return err;
3094}
3095
3096static void nft_set_destroy(struct nft_set *set)
3097{
3098 set->ops->destroy(set);
3099 module_put(set->ops->owner);
3100 kfree(set);
3101}
3102
3103static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
3104{
3105 list_del_rcu(&set->list);
3106 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, GFP_ATOMIC);
3107 nft_set_destroy(set);
3108}
3109
3110static int nf_tables_delset(struct net *net, struct sock *nlsk,
3111 struct sk_buff *skb, const struct nlmsghdr *nlh,
3112 const struct nlattr * const nla[])
3113{
3114 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3115 u8 genmask = nft_genmask_next(net);
3116 struct nft_set *set;
3117 struct nft_ctx ctx;
3118 int err;
3119
3120 if (nfmsg->nfgen_family == NFPROTO_UNSPEC)
3121 return -EAFNOSUPPORT;
3122 if (nla[NFTA_SET_TABLE] == NULL)
3123 return -EINVAL;
3124
3125 err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, genmask);
3126 if (err < 0)
3127 return err;
3128
3129 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask);
3130 if (IS_ERR(set))
3131 return PTR_ERR(set);
3132 if (!list_empty(&set->bindings))
3133 return -EBUSY;
3134
3135 return nft_delset(&ctx, set);
3136}
3137
3138static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
3139 struct nft_set *set,
3140 const struct nft_set_iter *iter,
3141 struct nft_set_elem *elem)
3142{
3143 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3144 enum nft_registers dreg;
3145
3146 dreg = nft_type_to_reg(set->dtype);
3147 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
3148 set->dtype == NFT_DATA_VERDICT ?
3149 NFT_DATA_VERDICT : NFT_DATA_VALUE,
3150 set->dlen);
3151}
3152
3153int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
3154 struct nft_set_binding *binding)
3155{
3156 struct nft_set_binding *i;
3157 struct nft_set_iter iter;
3158
3159 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
3160 return -EBUSY;
3161
3162 if (binding->flags & NFT_SET_MAP) {
3163
3164
3165
3166 list_for_each_entry(i, &set->bindings, list) {
3167 if (i->flags & NFT_SET_MAP &&
3168 i->chain == binding->chain)
3169 goto bind;
3170 }
3171
3172 iter.genmask = nft_genmask_next(ctx->net);
3173 iter.skip = 0;
3174 iter.count = 0;
3175 iter.err = 0;
3176 iter.fn = nf_tables_bind_check_setelem;
3177
3178 set->ops->walk(ctx, set, &iter);
3179 if (iter.err < 0)
3180 return iter.err;
3181 }
3182bind:
3183 binding->chain = ctx->chain;
3184 list_add_tail_rcu(&binding->list, &set->bindings);
3185 return 0;
3186}
3187EXPORT_SYMBOL_GPL(nf_tables_bind_set);
3188
3189void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
3190 struct nft_set_binding *binding)
3191{
3192 list_del_rcu(&binding->list);
3193
3194 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS &&
3195 nft_is_active(ctx->net, set))
3196 nf_tables_set_destroy(ctx, set);
3197}
3198EXPORT_SYMBOL_GPL(nf_tables_unbind_set);
3199
3200const struct nft_set_ext_type nft_set_ext_types[] = {
3201 [NFT_SET_EXT_KEY] = {
3202 .align = __alignof__(u32),
3203 },
3204 [NFT_SET_EXT_DATA] = {
3205 .align = __alignof__(u32),
3206 },
3207 [NFT_SET_EXT_EXPR] = {
3208 .align = __alignof__(struct nft_expr),
3209 },
3210 [NFT_SET_EXT_OBJREF] = {
3211 .len = sizeof(struct nft_object *),
3212 .align = __alignof__(struct nft_object *),
3213 },
3214 [NFT_SET_EXT_FLAGS] = {
3215 .len = sizeof(u8),
3216 .align = __alignof__(u8),
3217 },
3218 [NFT_SET_EXT_TIMEOUT] = {
3219 .len = sizeof(u64),
3220 .align = __alignof__(u64),
3221 },
3222 [NFT_SET_EXT_EXPIRATION] = {
3223 .len = sizeof(unsigned long),
3224 .align = __alignof__(unsigned long),
3225 },
3226 [NFT_SET_EXT_USERDATA] = {
3227 .len = sizeof(struct nft_userdata),
3228 .align = __alignof__(struct nft_userdata),
3229 },
3230};
3231EXPORT_SYMBOL_GPL(nft_set_ext_types);
3232
3233
3234
3235
3236
3237static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
3238 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
3239 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
3240 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
3241 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
3242 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
3243 .len = NFT_USERDATA_MAXLEN },
3244};
3245
3246static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
3247 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING,
3248 .len = NFT_TABLE_MAXNAMELEN - 1 },
3249 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING,
3250 .len = NFT_SET_MAXNAMELEN - 1 },
3251 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
3252 [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 },
3253};
3254
3255static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net,
3256 const struct sk_buff *skb,
3257 const struct nlmsghdr *nlh,
3258 const struct nlattr * const nla[],
3259 u8 genmask)
3260{
3261 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3262 struct nft_af_info *afi;
3263 struct nft_table *table;
3264
3265 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
3266 if (IS_ERR(afi))
3267 return PTR_ERR(afi);
3268
3269 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE],
3270 genmask);
3271 if (IS_ERR(table))
3272 return PTR_ERR(table);
3273
3274 nft_ctx_init(ctx, net, skb, nlh, afi, table, NULL, nla);
3275 return 0;
3276}
3277
3278static int nf_tables_fill_setelem(struct sk_buff *skb,
3279 const struct nft_set *set,
3280 const struct nft_set_elem *elem)
3281{
3282 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3283 unsigned char *b = skb_tail_pointer(skb);
3284 struct nlattr *nest;
3285
3286 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
3287 if (nest == NULL)
3288 goto nla_put_failure;
3289
3290 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
3291 NFT_DATA_VALUE, set->klen) < 0)
3292 goto nla_put_failure;
3293
3294 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3295 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
3296 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
3297 set->dlen) < 0)
3298 goto nla_put_failure;
3299
3300 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
3301 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3302 goto nla_put_failure;
3303
3304 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
3305 nla_put_string(skb, NFTA_SET_ELEM_OBJREF,
3306 (*nft_set_ext_obj(ext))->name) < 0)
3307 goto nla_put_failure;
3308
3309 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3310 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3311 htonl(*nft_set_ext_flags(ext))))
3312 goto nla_put_failure;
3313
3314 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3315 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3316 cpu_to_be64(jiffies_to_msecs(
3317 *nft_set_ext_timeout(ext))),
3318 NFTA_SET_ELEM_PAD))
3319 goto nla_put_failure;
3320
3321 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3322 unsigned long expires, now = jiffies;
3323
3324 expires = *nft_set_ext_expiration(ext);
3325 if (time_before(now, expires))
3326 expires -= now;
3327 else
3328 expires = 0;
3329
3330 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3331 cpu_to_be64(jiffies_to_msecs(expires)),
3332 NFTA_SET_ELEM_PAD))
3333 goto nla_put_failure;
3334 }
3335
3336 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3337 struct nft_userdata *udata;
3338
3339 udata = nft_set_ext_userdata(ext);
3340 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3341 udata->len + 1, udata->data))
3342 goto nla_put_failure;
3343 }
3344
3345 nla_nest_end(skb, nest);
3346 return 0;
3347
3348nla_put_failure:
3349 nlmsg_trim(skb, b);
3350 return -EMSGSIZE;
3351}
3352
3353struct nft_set_dump_args {
3354 const struct netlink_callback *cb;
3355 struct nft_set_iter iter;
3356 struct sk_buff *skb;
3357};
3358
3359static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
3360 struct nft_set *set,
3361 const struct nft_set_iter *iter,
3362 struct nft_set_elem *elem)
3363{
3364 struct nft_set_dump_args *args;
3365
3366 args = container_of(iter, struct nft_set_dump_args, iter);
3367 return nf_tables_fill_setelem(args->skb, set, elem);
3368}
3369
3370struct nft_set_dump_ctx {
3371 const struct nft_set *set;
3372 struct nft_ctx ctx;
3373};
3374
3375static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
3376{
3377 struct nft_set_dump_ctx *dump_ctx = cb->data;
3378 struct net *net = sock_net(skb->sk);
3379 struct nft_af_info *afi;
3380 struct nft_table *table;
3381 struct nft_set *set;
3382 struct nft_set_dump_args args;
3383 bool set_found = false;
3384 struct nfgenmsg *nfmsg;
3385 struct nlmsghdr *nlh;
3386 struct nlattr *nest;
3387 u32 portid, seq;
3388 int event;
3389
3390 rcu_read_lock();
3391 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
3392 if (afi != dump_ctx->ctx.afi)
3393 continue;
3394
3395 list_for_each_entry_rcu(table, &afi->tables, list) {
3396 if (table != dump_ctx->ctx.table)
3397 continue;
3398
3399 list_for_each_entry_rcu(set, &table->sets, list) {
3400 if (set == dump_ctx->set) {
3401 set_found = true;
3402 break;
3403 }
3404 }
3405 break;
3406 }
3407 break;
3408 }
3409
3410 if (!set_found) {
3411 rcu_read_unlock();
3412 return -ENOENT;
3413 }
3414
3415 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM);
3416 portid = NETLINK_CB(cb->skb).portid;
3417 seq = cb->nlh->nlmsg_seq;
3418
3419 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3420 NLM_F_MULTI);
3421 if (nlh == NULL)
3422 goto nla_put_failure;
3423
3424 nfmsg = nlmsg_data(nlh);
3425 nfmsg->nfgen_family = afi->family;
3426 nfmsg->version = NFNETLINK_V0;
3427 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
3428
3429 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name))
3430 goto nla_put_failure;
3431 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
3432 goto nla_put_failure;
3433
3434 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3435 if (nest == NULL)
3436 goto nla_put_failure;
3437
3438 args.cb = cb;
3439 args.skb = skb;
3440 args.iter.genmask = nft_genmask_cur(net);
3441 args.iter.skip = cb->args[0];
3442 args.iter.count = 0;
3443 args.iter.err = 0;
3444 args.iter.fn = nf_tables_dump_setelem;
3445 set->ops->walk(&dump_ctx->ctx, set, &args.iter);
3446 rcu_read_unlock();
3447
3448 nla_nest_end(skb, nest);
3449 nlmsg_end(skb, nlh);
3450
3451 if (args.iter.err && args.iter.err != -EMSGSIZE)
3452 return args.iter.err;
3453 if (args.iter.count == cb->args[0])
3454 return 0;
3455
3456 cb->args[0] = args.iter.count;
3457 return skb->len;
3458
3459nla_put_failure:
3460 rcu_read_unlock();
3461 return -ENOSPC;
3462}
3463
3464static int nf_tables_dump_set_done(struct netlink_callback *cb)
3465{
3466 kfree(cb->data);
3467 return 0;
3468}
3469
3470static int nf_tables_getsetelem(struct net *net, struct sock *nlsk,
3471 struct sk_buff *skb, const struct nlmsghdr *nlh,
3472 const struct nlattr * const nla[])
3473{
3474 u8 genmask = nft_genmask_cur(net);
3475 const struct nft_set *set;
3476 struct nft_ctx ctx;
3477 int err;
3478
3479 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
3480 if (err < 0)
3481 return err;
3482
3483 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3484 genmask);
3485 if (IS_ERR(set))
3486 return PTR_ERR(set);
3487
3488 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3489 struct netlink_dump_control c = {
3490 .dump = nf_tables_dump_set,
3491 .done = nf_tables_dump_set_done,
3492 };
3493 struct nft_set_dump_ctx *dump_ctx;
3494
3495 dump_ctx = kmalloc(sizeof(*dump_ctx), GFP_KERNEL);
3496 if (!dump_ctx)
3497 return -ENOMEM;
3498
3499 dump_ctx->set = set;
3500 dump_ctx->ctx = ctx;
3501
3502 c.data = dump_ctx;
3503 return netlink_dump_start(nlsk, skb, nlh, &c);
3504 }
3505 return -EOPNOTSUPP;
3506}
3507
3508static int nf_tables_fill_setelem_info(struct sk_buff *skb,
3509 const struct nft_ctx *ctx, u32 seq,
3510 u32 portid, int event, u16 flags,
3511 const struct nft_set *set,
3512 const struct nft_set_elem *elem)
3513{
3514 struct nfgenmsg *nfmsg;
3515 struct nlmsghdr *nlh;
3516 struct nlattr *nest;
3517 int err;
3518
3519 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
3520 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
3521 flags);
3522 if (nlh == NULL)
3523 goto nla_put_failure;
3524
3525 nfmsg = nlmsg_data(nlh);
3526 nfmsg->nfgen_family = ctx->afi->family;
3527 nfmsg->version = NFNETLINK_V0;
3528 nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff);
3529
3530 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
3531 goto nla_put_failure;
3532 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
3533 goto nla_put_failure;
3534
3535 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
3536 if (nest == NULL)
3537 goto nla_put_failure;
3538
3539 err = nf_tables_fill_setelem(skb, set, elem);
3540 if (err < 0)
3541 goto nla_put_failure;
3542
3543 nla_nest_end(skb, nest);
3544
3545 nlmsg_end(skb, nlh);
3546 return 0;
3547
3548nla_put_failure:
3549 nlmsg_trim(skb, nlh);
3550 return -1;
3551}
3552
3553static void nf_tables_setelem_notify(const struct nft_ctx *ctx,
3554 const struct nft_set *set,
3555 const struct nft_set_elem *elem,
3556 int event, u16 flags)
3557{
3558 struct net *net = ctx->net;
3559 u32 portid = ctx->portid;
3560 struct sk_buff *skb;
3561 int err;
3562
3563 if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
3564 return;
3565
3566 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
3567 if (skb == NULL)
3568 goto err;
3569
3570 err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags,
3571 set, elem);
3572 if (err < 0) {
3573 kfree_skb(skb);
3574 goto err;
3575 }
3576
3577 nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report,
3578 GFP_KERNEL);
3579 return;
3580err:
3581 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
3582}
3583
3584static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3585 int msg_type,
3586 struct nft_set *set)
3587{
3588 struct nft_trans *trans;
3589
3590 trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem));
3591 if (trans == NULL)
3592 return NULL;
3593
3594 nft_trans_elem_set(trans) = set;
3595 return trans;
3596}
3597
3598void *nft_set_elem_init(const struct nft_set *set,
3599 const struct nft_set_ext_tmpl *tmpl,
3600 const u32 *key, const u32 *data,
3601 u64 timeout, gfp_t gfp)
3602{
3603 struct nft_set_ext *ext;
3604 void *elem;
3605
3606 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3607 if (elem == NULL)
3608 return NULL;
3609
3610 ext = nft_set_elem_ext(set, elem);
3611 nft_set_ext_init(ext, tmpl);
3612
3613 memcpy(nft_set_ext_key(ext), key, set->klen);
3614 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3615 memcpy(nft_set_ext_data(ext), data, set->dlen);
3616 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3617 *nft_set_ext_expiration(ext) =
3618 jiffies + timeout;
3619 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3620 *nft_set_ext_timeout(ext) = timeout;
3621
3622 return elem;
3623}
3624
3625void nft_set_elem_destroy(const struct nft_set *set, void *elem,
3626 bool destroy_expr)
3627{
3628 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3629
3630 nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE);
3631 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3632 nft_data_release(nft_set_ext_data(ext), set->dtype);
3633 if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3634 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
3635 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
3636 (*nft_set_ext_obj(ext))->use--;
3637 kfree(elem);
3638}
3639EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3640
3641
3642
3643
3644static void nf_tables_set_elem_destroy(const struct nft_set *set, void *elem)
3645{
3646 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3647
3648 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3649 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
3650 kfree(elem);
3651}
3652
3653static int nft_setelem_parse_flags(const struct nft_set *set,
3654 const struct nlattr *attr, u32 *flags)
3655{
3656 if (attr == NULL)
3657 return 0;
3658
3659 *flags = ntohl(nla_get_be32(attr));
3660 if (*flags & ~NFT_SET_ELEM_INTERVAL_END)
3661 return -EINVAL;
3662 if (!(set->flags & NFT_SET_INTERVAL) &&
3663 *flags & NFT_SET_ELEM_INTERVAL_END)
3664 return -EINVAL;
3665
3666 return 0;
3667}
3668
3669static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3670 const struct nlattr *attr, u32 nlmsg_flags)
3671{
3672 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3673 u8 genmask = nft_genmask_next(ctx->net);
3674 struct nft_data_desc d1, d2;
3675 struct nft_set_ext_tmpl tmpl;
3676 struct nft_set_ext *ext, *ext2;
3677 struct nft_set_elem elem;
3678 struct nft_set_binding *binding;
3679 struct nft_object *obj = NULL;
3680 struct nft_userdata *udata;
3681 struct nft_data data;
3682 enum nft_registers dreg;
3683 struct nft_trans *trans;
3684 u32 flags = 0;
3685 u64 timeout;
3686 u8 ulen;
3687 int err;
3688
3689 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3690 nft_set_elem_policy, NULL);
3691 if (err < 0)
3692 return err;
3693
3694 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3695 return -EINVAL;
3696
3697 nft_set_ext_prepare(&tmpl);
3698
3699 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3700 if (err < 0)
3701 return err;
3702 if (flags != 0)
3703 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3704
3705 if (set->flags & NFT_SET_MAP) {
3706 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3707 !(flags & NFT_SET_ELEM_INTERVAL_END))
3708 return -EINVAL;
3709 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3710 flags & NFT_SET_ELEM_INTERVAL_END)
3711 return -EINVAL;
3712 } else {
3713 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3714 return -EINVAL;
3715 }
3716
3717 timeout = 0;
3718 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3719 if (!(set->flags & NFT_SET_TIMEOUT))
3720 return -EINVAL;
3721 timeout = msecs_to_jiffies(be64_to_cpu(nla_get_be64(
3722 nla[NFTA_SET_ELEM_TIMEOUT])));
3723 } else if (set->flags & NFT_SET_TIMEOUT) {
3724 timeout = set->timeout;
3725 }
3726
3727 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
3728 nla[NFTA_SET_ELEM_KEY]);
3729 if (err < 0)
3730 goto err1;
3731 err = -EINVAL;
3732 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3733 goto err2;
3734
3735 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
3736 if (timeout > 0) {
3737 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3738 if (timeout != set->timeout)
3739 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3740 }
3741
3742 if (nla[NFTA_SET_ELEM_OBJREF] != NULL) {
3743 if (!(set->flags & NFT_SET_OBJECT)) {
3744 err = -EINVAL;
3745 goto err2;
3746 }
3747 obj = nf_tables_obj_lookup(ctx->table, nla[NFTA_SET_ELEM_OBJREF],
3748 set->objtype, genmask);
3749 if (IS_ERR(obj)) {
3750 err = PTR_ERR(obj);
3751 goto err2;
3752 }
3753 nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF);
3754 }
3755
3756 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3757 err = nft_data_init(ctx, &data, sizeof(data), &d2,
3758 nla[NFTA_SET_ELEM_DATA]);
3759 if (err < 0)
3760 goto err2;
3761
3762 err = -EINVAL;
3763 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
3764 goto err3;
3765
3766 dreg = nft_type_to_reg(set->dtype);
3767 list_for_each_entry(binding, &set->bindings, list) {
3768 struct nft_ctx bind_ctx = {
3769 .net = ctx->net,
3770 .afi = ctx->afi,
3771 .table = ctx->table,
3772 .chain = (struct nft_chain *)binding->chain,
3773 };
3774
3775 if (!(binding->flags & NFT_SET_MAP))
3776 continue;
3777
3778 err = nft_validate_register_store(&bind_ctx, dreg,
3779 &data,
3780 d2.type, d2.len);
3781 if (err < 0)
3782 goto err3;
3783 }
3784
3785 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
3786 }
3787
3788
3789
3790
3791
3792 ulen = 0;
3793 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3794 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3795 if (ulen > 0)
3796 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3797 ulen);
3798 }
3799
3800 err = -ENOMEM;
3801 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
3802 timeout, GFP_KERNEL);
3803 if (elem.priv == NULL)
3804 goto err3;
3805
3806 ext = nft_set_elem_ext(set, elem.priv);
3807 if (flags)
3808 *nft_set_ext_flags(ext) = flags;
3809 if (ulen > 0) {
3810 udata = nft_set_ext_userdata(ext);
3811 udata->len = ulen - 1;
3812 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3813 }
3814 if (obj) {
3815 *nft_set_ext_obj(ext) = obj;
3816 obj->use++;
3817 }
3818
3819 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3820 if (trans == NULL)
3821 goto err4;
3822
3823 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
3824 err = set->ops->insert(ctx->net, set, &elem, &ext2);
3825 if (err) {
3826 if (err == -EEXIST) {
3827 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^
3828 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) ||
3829 nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^
3830 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF))
3831 return -EBUSY;
3832 if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
3833 nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) &&
3834 memcmp(nft_set_ext_data(ext),
3835 nft_set_ext_data(ext2), set->dlen) != 0) ||
3836 (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) &&
3837 nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) &&
3838 *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2)))
3839 err = -EBUSY;
3840 else if (!(nlmsg_flags & NLM_F_EXCL))
3841 err = 0;
3842 }
3843 goto err5;
3844 }
3845
3846 if (set->size &&
3847 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
3848 err = -ENFILE;
3849 goto err6;
3850 }
3851
3852 nft_trans_elem(trans) = elem;
3853 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3854 return 0;
3855
3856err6:
3857 set->ops->remove(ctx->net, set, &elem);
3858err5:
3859 kfree(trans);
3860err4:
3861 kfree(elem.priv);
3862err3:
3863 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3864 nft_data_release(&data, d2.type);
3865err2:
3866 nft_data_release(&elem.key.val, d1.type);
3867err1:
3868 return err;
3869}
3870
3871static int nf_tables_newsetelem(struct net *net, struct sock *nlsk,
3872 struct sk_buff *skb, const struct nlmsghdr *nlh,
3873 const struct nlattr * const nla[])
3874{
3875 u8 genmask = nft_genmask_next(net);
3876 const struct nlattr *attr;
3877 struct nft_set *set;
3878 struct nft_ctx ctx;
3879 int rem, err = 0;
3880
3881 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL)
3882 return -EINVAL;
3883
3884 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
3885 if (err < 0)
3886 return err;
3887
3888 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
3889 genmask);
3890 if (IS_ERR(set)) {
3891 if (nla[NFTA_SET_ELEM_LIST_SET_ID]) {
3892 set = nf_tables_set_lookup_byid(net,
3893 nla[NFTA_SET_ELEM_LIST_SET_ID],
3894 genmask);
3895 }
3896 if (IS_ERR(set))
3897 return PTR_ERR(set);
3898 }
3899
3900 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
3901 return -EBUSY;
3902
3903 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3904 err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags);
3905 if (err < 0)
3906 break;
3907 }
3908 return err;
3909}
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922static void nft_data_hold(const struct nft_data *data, enum nft_data_types type)
3923{
3924 if (type == NFT_DATA_VERDICT) {
3925 switch (data->verdict.code) {
3926 case NFT_JUMP:
3927 case NFT_GOTO:
3928 data->verdict.chain->use++;
3929 break;
3930 }
3931 }
3932}
3933
3934static void nft_set_elem_activate(const struct net *net,
3935 const struct nft_set *set,
3936 struct nft_set_elem *elem)
3937{
3938 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3939
3940 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3941 nft_data_hold(nft_set_ext_data(ext), set->dtype);
3942 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
3943 (*nft_set_ext_obj(ext))->use++;
3944}
3945
3946static void nft_set_elem_deactivate(const struct net *net,
3947 const struct nft_set *set,
3948 struct nft_set_elem *elem)
3949{
3950 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
3951
3952 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3953 nft_data_release(nft_set_ext_data(ext), set->dtype);
3954 if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF))
3955 (*nft_set_ext_obj(ext))->use--;
3956}
3957
3958static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3959 const struct nlattr *attr)
3960{
3961 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3962 struct nft_set_ext_tmpl tmpl;
3963 struct nft_data_desc desc;
3964 struct nft_set_elem elem;
3965 struct nft_set_ext *ext;
3966 struct nft_trans *trans;
3967 u32 flags = 0;
3968 void *priv;
3969 int err;
3970
3971 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3972 nft_set_elem_policy, NULL);
3973 if (err < 0)
3974 goto err1;
3975
3976 err = -EINVAL;
3977 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3978 goto err1;
3979
3980 nft_set_ext_prepare(&tmpl);
3981
3982 err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags);
3983 if (err < 0)
3984 return err;
3985 if (flags != 0)
3986 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3987
3988 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
3989 nla[NFTA_SET_ELEM_KEY]);
3990 if (err < 0)
3991 goto err1;
3992
3993 err = -EINVAL;
3994 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3995 goto err2;
3996
3997 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, desc.len);
3998
3999 err = -ENOMEM;
4000 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
4001 GFP_KERNEL);
4002 if (elem.priv == NULL)
4003 goto err2;
4004
4005 ext = nft_set_elem_ext(set, elem.priv);
4006 if (flags)
4007 *nft_set_ext_flags(ext) = flags;
4008
4009 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
4010 if (trans == NULL) {
4011 err = -ENOMEM;
4012 goto err3;
4013 }
4014
4015 priv = set->ops->deactivate(ctx->net, set, &elem);
4016 if (priv == NULL) {
4017 err = -ENOENT;
4018 goto err4;
4019 }
4020 kfree(elem.priv);
4021 elem.priv = priv;
4022
4023 nft_set_elem_deactivate(ctx->net, set, &elem);
4024
4025 nft_trans_elem(trans) = elem;
4026 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4027 return 0;
4028
4029err4:
4030 kfree(trans);
4031err3:
4032 kfree(elem.priv);
4033err2:
4034 nft_data_release(&elem.key.val, desc.type);
4035err1:
4036 return err;
4037}
4038
4039static int nft_flush_set(const struct nft_ctx *ctx,
4040 struct nft_set *set,
4041 const struct nft_set_iter *iter,
4042 struct nft_set_elem *elem)
4043{
4044 struct nft_trans *trans;
4045 int err;
4046
4047 trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM,
4048 sizeof(struct nft_trans_elem), GFP_ATOMIC);
4049 if (!trans)
4050 return -ENOMEM;
4051
4052 if (!set->ops->flush(ctx->net, set, elem->priv)) {
4053 err = -ENOENT;
4054 goto err1;
4055 }
4056 set->ndeact++;
4057
4058 nft_trans_elem_set(trans) = set;
4059 nft_trans_elem(trans) = *elem;
4060 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
4061
4062 return 0;
4063err1:
4064 kfree(trans);
4065 return err;
4066}
4067
4068static int nf_tables_delsetelem(struct net *net, struct sock *nlsk,
4069 struct sk_buff *skb, const struct nlmsghdr *nlh,
4070 const struct nlattr * const nla[])
4071{
4072 u8 genmask = nft_genmask_next(net);
4073 const struct nlattr *attr;
4074 struct nft_set *set;
4075 struct nft_ctx ctx;
4076 int rem, err = 0;
4077
4078 err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, genmask);
4079 if (err < 0)
4080 return err;
4081
4082 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET],
4083 genmask);
4084 if (IS_ERR(set))
4085 return PTR_ERR(set);
4086 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
4087 return -EBUSY;
4088
4089 if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) {
4090 struct nft_set_iter iter = {
4091 .genmask = genmask,
4092 .fn = nft_flush_set,
4093 };
4094 set->ops->walk(&ctx, set, &iter);
4095
4096 return iter.err;
4097 }
4098
4099 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
4100 err = nft_del_setelem(&ctx, set, attr);
4101 if (err < 0)
4102 break;
4103
4104 set->ndeact++;
4105 }
4106 return err;
4107}
4108
4109void nft_set_gc_batch_release(struct rcu_head *rcu)
4110{
4111 struct nft_set_gc_batch *gcb;
4112 unsigned int i;
4113
4114 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
4115 for (i = 0; i < gcb->head.cnt; i++)
4116 nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true);
4117 kfree(gcb);
4118}
4119EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
4120
4121struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
4122 gfp_t gfp)
4123{
4124 struct nft_set_gc_batch *gcb;
4125
4126 gcb = kzalloc(sizeof(*gcb), gfp);
4127 if (gcb == NULL)
4128 return gcb;
4129 gcb->head.set = set;
4130 return gcb;
4131}
4132EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145int nft_register_obj(struct nft_object_type *obj_type)
4146{
4147 if (obj_type->type == NFT_OBJECT_UNSPEC)
4148 return -EINVAL;
4149
4150 nfnl_lock(NFNL_SUBSYS_NFTABLES);
4151 list_add_rcu(&obj_type->list, &nf_tables_objects);
4152 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4153 return 0;
4154}
4155EXPORT_SYMBOL_GPL(nft_register_obj);
4156
4157
4158
4159
4160
4161
4162
4163void nft_unregister_obj(struct nft_object_type *obj_type)
4164{
4165 nfnl_lock(NFNL_SUBSYS_NFTABLES);
4166 list_del_rcu(&obj_type->list);
4167 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4168}
4169EXPORT_SYMBOL_GPL(nft_unregister_obj);
4170
4171struct nft_object *nf_tables_obj_lookup(const struct nft_table *table,
4172 const struct nlattr *nla,
4173 u32 objtype, u8 genmask)
4174{
4175 struct nft_object *obj;
4176
4177 list_for_each_entry(obj, &table->objects, list) {
4178 if (!nla_strcmp(nla, obj->name) &&
4179 objtype == obj->type->type &&
4180 nft_active_genmask(obj, genmask))
4181 return obj;
4182 }
4183 return ERR_PTR(-ENOENT);
4184}
4185EXPORT_SYMBOL_GPL(nf_tables_obj_lookup);
4186
4187static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = {
4188 [NFTA_OBJ_TABLE] = { .type = NLA_STRING,
4189 .len = NFT_TABLE_MAXNAMELEN - 1 },
4190 [NFTA_OBJ_NAME] = { .type = NLA_STRING,
4191 .len = NFT_OBJ_MAXNAMELEN - 1 },
4192 [NFTA_OBJ_TYPE] = { .type = NLA_U32 },
4193 [NFTA_OBJ_DATA] = { .type = NLA_NESTED },
4194};
4195
4196static struct nft_object *nft_obj_init(const struct nft_ctx *ctx,
4197 const struct nft_object_type *type,
4198 const struct nlattr *attr)
4199{
4200 struct nlattr *tb[type->maxattr + 1];
4201 struct nft_object *obj;
4202 int err;
4203
4204 if (attr) {
4205 err = nla_parse_nested(tb, type->maxattr, attr, type->policy,
4206 NULL);
4207 if (err < 0)
4208 goto err1;
4209 } else {
4210 memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1));
4211 }
4212
4213 err = -ENOMEM;
4214 obj = kzalloc(sizeof(struct nft_object) + type->size, GFP_KERNEL);
4215 if (obj == NULL)
4216 goto err1;
4217
4218 err = type->init(ctx, (const struct nlattr * const *)tb, obj);
4219 if (err < 0)
4220 goto err2;
4221
4222 obj->type = type;
4223 return obj;
4224err2:
4225 kfree(obj);
4226err1:
4227 return ERR_PTR(err);
4228}
4229
4230static int nft_object_dump(struct sk_buff *skb, unsigned int attr,
4231 struct nft_object *obj, bool reset)
4232{
4233 struct nlattr *nest;
4234
4235 nest = nla_nest_start(skb, attr);
4236 if (!nest)
4237 goto nla_put_failure;
4238 if (obj->type->dump(skb, obj, reset) < 0)
4239 goto nla_put_failure;
4240 nla_nest_end(skb, nest);
4241 return 0;
4242
4243nla_put_failure:
4244 return -1;
4245}
4246
4247static const struct nft_object_type *__nft_obj_type_get(u32 objtype)
4248{
4249 const struct nft_object_type *type;
4250
4251 list_for_each_entry(type, &nf_tables_objects, list) {
4252 if (objtype == type->type)
4253 return type;
4254 }
4255 return NULL;
4256}
4257
4258static const struct nft_object_type *nft_obj_type_get(u32 objtype)
4259{
4260 const struct nft_object_type *type;
4261
4262 type = __nft_obj_type_get(objtype);
4263 if (type != NULL && try_module_get(type->owner))
4264 return type;
4265
4266#ifdef CONFIG_MODULES
4267 if (type == NULL) {
4268 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
4269 request_module("nft-obj-%u", objtype);
4270 nfnl_lock(NFNL_SUBSYS_NFTABLES);
4271 if (__nft_obj_type_get(objtype))
4272 return ERR_PTR(-EAGAIN);
4273 }
4274#endif
4275 return ERR_PTR(-ENOENT);
4276}
4277
4278static int nf_tables_newobj(struct net *net, struct sock *nlsk,
4279 struct sk_buff *skb, const struct nlmsghdr *nlh,
4280 const struct nlattr * const nla[])
4281{
4282 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
4283 const struct nft_object_type *type;
4284 u8 genmask = nft_genmask_next(net);
4285 int family = nfmsg->nfgen_family;
4286 struct nft_af_info *afi;
4287 struct nft_table *table;
4288 struct nft_object *obj;
4289 struct nft_ctx ctx;
4290 u32 objtype;
4291 int err;
4292
4293 if (!nla[NFTA_OBJ_TYPE] ||
4294 !nla[NFTA_OBJ_NAME] ||
4295 !nla[NFTA_OBJ_DATA])
4296 return -EINVAL;
4297
4298 afi = nf_tables_afinfo_lookup(net, family, true);
4299 if (IS_ERR(afi))
4300 return PTR_ERR(afi);
4301
4302 table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask);
4303 if (IS_ERR(table))
4304 return PTR_ERR(table);
4305
4306 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4307 obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask);
4308 if (IS_ERR(obj)) {
4309 err = PTR_ERR(obj);
4310 if (err != -ENOENT)
4311 return err;
4312
4313 } else {
4314 if (nlh->nlmsg_flags & NLM_F_EXCL)
4315 return -EEXIST;
4316
4317 return 0;
4318 }
4319
4320 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
4321
4322 type = nft_obj_type_get(objtype);
4323 if (IS_ERR(type))
4324 return PTR_ERR(type);
4325
4326 obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]);
4327 if (IS_ERR(obj)) {
4328 err = PTR_ERR(obj);
4329 goto err1;
4330 }
4331 obj->table = table;
4332 nla_strlcpy(obj->name, nla[NFTA_OBJ_NAME], NFT_OBJ_MAXNAMELEN);
4333
4334 err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj);
4335 if (err < 0)
4336 goto err2;
4337
4338 list_add_tail_rcu(&obj->list, &table->objects);
4339 table->use++;
4340 return 0;
4341err2:
4342 if (obj->type->destroy)
4343 obj->type->destroy(obj);
4344 kfree(obj);
4345err1:
4346 module_put(type->owner);
4347 return err;
4348}
4349
4350static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,
4351 u32 portid, u32 seq, int event, u32 flags,
4352 int family, const struct nft_table *table,
4353 struct nft_object *obj, bool reset)
4354{
4355 struct nfgenmsg *nfmsg;
4356 struct nlmsghdr *nlh;
4357
4358 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event);
4359 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
4360 if (nlh == NULL)
4361 goto nla_put_failure;
4362
4363 nfmsg = nlmsg_data(nlh);
4364 nfmsg->nfgen_family = family;
4365 nfmsg->version = NFNETLINK_V0;
4366 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
4367
4368 if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) ||
4369 nla_put_string(skb, NFTA_OBJ_NAME, obj->name) ||
4370 nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->type->type)) ||
4371 nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) ||
4372 nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset))
4373 goto nla_put_failure;
4374
4375 nlmsg_end(skb, nlh);
4376 return 0;
4377
4378nla_put_failure:
4379 nlmsg_trim(skb, nlh);
4380 return -1;
4381}
4382
4383struct nft_obj_filter {
4384 char table[NFT_OBJ_MAXNAMELEN];
4385 u32 type;
4386};
4387
4388static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb)
4389{
4390 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
4391 const struct nft_af_info *afi;
4392 const struct nft_table *table;
4393 unsigned int idx = 0, s_idx = cb->args[0];
4394 struct nft_obj_filter *filter = cb->data;
4395 struct net *net = sock_net(skb->sk);
4396 int family = nfmsg->nfgen_family;
4397 struct nft_object *obj;
4398 bool reset = false;
4399
4400 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
4401 reset = true;
4402
4403 rcu_read_lock();
4404 cb->seq = net->nft.base_seq;
4405
4406 list_for_each_entry_rcu(afi, &net->nft.af_info, list) {
4407 if (family != NFPROTO_UNSPEC && family != afi->family)
4408 continue;
4409
4410 list_for_each_entry_rcu(table, &afi->tables, list) {
4411 list_for_each_entry_rcu(obj, &table->objects, list) {
4412 if (!nft_is_active(net, obj))
4413 goto cont;
4414 if (idx < s_idx)
4415 goto cont;
4416 if (idx > s_idx)
4417 memset(&cb->args[1], 0,
4418 sizeof(cb->args) - sizeof(cb->args[0]));
4419 if (filter && filter->table[0] &&
4420 strcmp(filter->table, table->name))
4421 goto cont;
4422 if (filter &&
4423 filter->type != NFT_OBJECT_UNSPEC &&
4424 obj->type->type != filter->type)
4425 goto cont;
4426
4427 if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid,
4428 cb->nlh->nlmsg_seq,
4429 NFT_MSG_NEWOBJ,
4430 NLM_F_MULTI | NLM_F_APPEND,
4431 afi->family, table, obj, reset) < 0)
4432 goto done;
4433
4434 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
4435cont:
4436 idx++;
4437 }
4438 }
4439 }
4440done:
4441 rcu_read_unlock();
4442
4443 cb->args[0] = idx;
4444 return skb->len;
4445}
4446
4447static int nf_tables_dump_obj_done(struct netlink_callback *cb)
4448{
4449 kfree(cb->data);
4450
4451 return 0;
4452}
4453
4454static struct nft_obj_filter *
4455nft_obj_filter_alloc(const struct nlattr * const nla[])
4456{
4457 struct nft_obj_filter *filter;
4458
4459 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
4460 if (!filter)
4461 return ERR_PTR(-ENOMEM);
4462
4463 if (nla[NFTA_OBJ_TABLE])
4464 nla_strlcpy(filter->table, nla[NFTA_OBJ_TABLE],
4465 NFT_TABLE_MAXNAMELEN);
4466 if (nla[NFTA_OBJ_TYPE])
4467 filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4468
4469 return filter;
4470}
4471
4472static int nf_tables_getobj(struct net *net, struct sock *nlsk,
4473 struct sk_buff *skb, const struct nlmsghdr *nlh,
4474 const struct nlattr * const nla[])
4475{
4476 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
4477 u8 genmask = nft_genmask_cur(net);
4478 int family = nfmsg->nfgen_family;
4479 const struct nft_af_info *afi;
4480 const struct nft_table *table;
4481 struct nft_object *obj;
4482 struct sk_buff *skb2;
4483 bool reset = false;
4484 u32 objtype;
4485 int err;
4486
4487 if (nlh->nlmsg_flags & NLM_F_DUMP) {
4488 struct netlink_dump_control c = {
4489 .dump = nf_tables_dump_obj,
4490 .done = nf_tables_dump_obj_done,
4491 };
4492
4493 if (nla[NFTA_OBJ_TABLE] ||
4494 nla[NFTA_OBJ_TYPE]) {
4495 struct nft_obj_filter *filter;
4496
4497 filter = nft_obj_filter_alloc(nla);
4498 if (IS_ERR(filter))
4499 return -ENOMEM;
4500
4501 c.data = filter;
4502 }
4503 return netlink_dump_start(nlsk, skb, nlh, &c);
4504 }
4505
4506 if (!nla[NFTA_OBJ_NAME] ||
4507 !nla[NFTA_OBJ_TYPE])
4508 return -EINVAL;
4509
4510 afi = nf_tables_afinfo_lookup(net, family, false);
4511 if (IS_ERR(afi))
4512 return PTR_ERR(afi);
4513
4514 table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask);
4515 if (IS_ERR(table))
4516 return PTR_ERR(table);
4517
4518 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4519 obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask);
4520 if (IS_ERR(obj))
4521 return PTR_ERR(obj);
4522
4523 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
4524 if (!skb2)
4525 return -ENOMEM;
4526
4527 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET)
4528 reset = true;
4529
4530 err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid,
4531 nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0,
4532 family, table, obj, reset);
4533 if (err < 0)
4534 goto err;
4535
4536 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
4537err:
4538 kfree_skb(skb2);
4539 return err;
4540}
4541
4542static void nft_obj_destroy(struct nft_object *obj)
4543{
4544 if (obj->type->destroy)
4545 obj->type->destroy(obj);
4546
4547 module_put(obj->type->owner);
4548 kfree(obj);
4549}
4550
4551static int nf_tables_delobj(struct net *net, struct sock *nlsk,
4552 struct sk_buff *skb, const struct nlmsghdr *nlh,
4553 const struct nlattr * const nla[])
4554{
4555 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
4556 u8 genmask = nft_genmask_next(net);
4557 int family = nfmsg->nfgen_family;
4558 struct nft_af_info *afi;
4559 struct nft_table *table;
4560 struct nft_object *obj;
4561 struct nft_ctx ctx;
4562 u32 objtype;
4563
4564 if (!nla[NFTA_OBJ_TYPE] ||
4565 !nla[NFTA_OBJ_NAME])
4566 return -EINVAL;
4567
4568 afi = nf_tables_afinfo_lookup(net, family, true);
4569 if (IS_ERR(afi))
4570 return PTR_ERR(afi);
4571
4572 table = nf_tables_table_lookup(afi, nla[NFTA_OBJ_TABLE], genmask);
4573 if (IS_ERR(table))
4574 return PTR_ERR(table);
4575
4576 objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE]));
4577 obj = nf_tables_obj_lookup(table, nla[NFTA_OBJ_NAME], objtype, genmask);
4578 if (IS_ERR(obj))
4579 return PTR_ERR(obj);
4580 if (obj->use > 0)
4581 return -EBUSY;
4582
4583 nft_ctx_init(&ctx, net, skb, nlh, afi, table, NULL, nla);
4584
4585 return nft_delobj(&ctx, obj);
4586}
4587
4588void nft_obj_notify(struct net *net, struct nft_table *table,
4589 struct nft_object *obj, u32 portid, u32 seq, int event,
4590 int family, int report, gfp_t gfp)
4591{
4592 struct sk_buff *skb;
4593 int err;
4594
4595 if (!report &&
4596 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
4597 return;
4598
4599 skb = nlmsg_new(NLMSG_GOODSIZE, gfp);
4600 if (skb == NULL)
4601 goto err;
4602
4603 err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family,
4604 table, obj, false);
4605 if (err < 0) {
4606 kfree_skb(skb);
4607 goto err;
4608 }
4609
4610 nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp);
4611 return;
4612err:
4613 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS);
4614}
4615EXPORT_SYMBOL_GPL(nft_obj_notify);
4616
4617static void nf_tables_obj_notify(const struct nft_ctx *ctx,
4618 struct nft_object *obj, int event)
4619{
4620 nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event,
4621 ctx->afi->family, ctx->report, GFP_KERNEL);
4622}
4623
4624static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
4625 u32 portid, u32 seq)
4626{
4627 struct nlmsghdr *nlh;
4628 struct nfgenmsg *nfmsg;
4629 int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN);
4630
4631 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0);
4632 if (nlh == NULL)
4633 goto nla_put_failure;
4634
4635 nfmsg = nlmsg_data(nlh);
4636 nfmsg->nfgen_family = AF_UNSPEC;
4637 nfmsg->version = NFNETLINK_V0;
4638 nfmsg->res_id = htons(net->nft.base_seq & 0xffff);
4639
4640 if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)))
4641 goto nla_put_failure;
4642
4643 nlmsg_end(skb, nlh);
4644 return 0;
4645
4646nla_put_failure:
4647 nlmsg_trim(skb, nlh);
4648 return -EMSGSIZE;
4649}
4650
4651static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb,
4652 int event)
4653{
4654 struct nlmsghdr *nlh = nlmsg_hdr(skb);
4655 struct sk_buff *skb2;
4656 int err;
4657
4658 if (nlmsg_report(nlh) &&
4659 !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
4660 return;
4661
4662 skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4663 if (skb2 == NULL)
4664 goto err;
4665
4666 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
4667 nlh->nlmsg_seq);
4668 if (err < 0) {
4669 kfree_skb(skb2);
4670 goto err;
4671 }
4672
4673 nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
4674 nlmsg_report(nlh), GFP_KERNEL);
4675 return;
4676err:
4677 nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES,
4678 -ENOBUFS);
4679}
4680
4681static int nf_tables_getgen(struct net *net, struct sock *nlsk,
4682 struct sk_buff *skb, const struct nlmsghdr *nlh,
4683 const struct nlattr * const nla[])
4684{
4685 struct sk_buff *skb2;
4686 int err;
4687
4688 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
4689 if (skb2 == NULL)
4690 return -ENOMEM;
4691
4692 err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid,
4693 nlh->nlmsg_seq);
4694 if (err < 0)
4695 goto err;
4696
4697 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
4698err:
4699 kfree_skb(skb2);
4700 return err;
4701}
4702
4703static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
4704 [NFT_MSG_NEWTABLE] = {
4705 .call_batch = nf_tables_newtable,
4706 .attr_count = NFTA_TABLE_MAX,
4707 .policy = nft_table_policy,
4708 },
4709 [NFT_MSG_GETTABLE] = {
4710 .call = nf_tables_gettable,
4711 .attr_count = NFTA_TABLE_MAX,
4712 .policy = nft_table_policy,
4713 },
4714 [NFT_MSG_DELTABLE] = {
4715 .call_batch = nf_tables_deltable,
4716 .attr_count = NFTA_TABLE_MAX,
4717 .policy = nft_table_policy,
4718 },
4719 [NFT_MSG_NEWCHAIN] = {
4720 .call_batch = nf_tables_newchain,
4721 .attr_count = NFTA_CHAIN_MAX,
4722 .policy = nft_chain_policy,
4723 },
4724 [NFT_MSG_GETCHAIN] = {
4725 .call = nf_tables_getchain,
4726 .attr_count = NFTA_CHAIN_MAX,
4727 .policy = nft_chain_policy,
4728 },
4729 [NFT_MSG_DELCHAIN] = {
4730 .call_batch = nf_tables_delchain,
4731 .attr_count = NFTA_CHAIN_MAX,
4732 .policy = nft_chain_policy,
4733 },
4734 [NFT_MSG_NEWRULE] = {
4735 .call_batch = nf_tables_newrule,
4736 .attr_count = NFTA_RULE_MAX,
4737 .policy = nft_rule_policy,
4738 },
4739 [NFT_MSG_GETRULE] = {
4740 .call = nf_tables_getrule,
4741 .attr_count = NFTA_RULE_MAX,
4742 .policy = nft_rule_policy,
4743 },
4744 [NFT_MSG_DELRULE] = {
4745 .call_batch = nf_tables_delrule,
4746 .attr_count = NFTA_RULE_MAX,
4747 .policy = nft_rule_policy,
4748 },
4749 [NFT_MSG_NEWSET] = {
4750 .call_batch = nf_tables_newset,
4751 .attr_count = NFTA_SET_MAX,
4752 .policy = nft_set_policy,
4753 },
4754 [NFT_MSG_GETSET] = {
4755 .call = nf_tables_getset,
4756 .attr_count = NFTA_SET_MAX,
4757 .policy = nft_set_policy,
4758 },
4759 [NFT_MSG_DELSET] = {
4760 .call_batch = nf_tables_delset,
4761 .attr_count = NFTA_SET_MAX,
4762 .policy = nft_set_policy,
4763 },
4764 [NFT_MSG_NEWSETELEM] = {
4765 .call_batch = nf_tables_newsetelem,
4766 .attr_count = NFTA_SET_ELEM_LIST_MAX,
4767 .policy = nft_set_elem_list_policy,
4768 },
4769 [NFT_MSG_GETSETELEM] = {
4770 .call = nf_tables_getsetelem,
4771 .attr_count = NFTA_SET_ELEM_LIST_MAX,
4772 .policy = nft_set_elem_list_policy,
4773 },
4774 [NFT_MSG_DELSETELEM] = {
4775 .call_batch = nf_tables_delsetelem,
4776 .attr_count = NFTA_SET_ELEM_LIST_MAX,
4777 .policy = nft_set_elem_list_policy,
4778 },
4779 [NFT_MSG_GETGEN] = {
4780 .call = nf_tables_getgen,
4781 },
4782 [NFT_MSG_NEWOBJ] = {
4783 .call_batch = nf_tables_newobj,
4784 .attr_count = NFTA_OBJ_MAX,
4785 .policy = nft_obj_policy,
4786 },
4787 [NFT_MSG_GETOBJ] = {
4788 .call = nf_tables_getobj,
4789 .attr_count = NFTA_OBJ_MAX,
4790 .policy = nft_obj_policy,
4791 },
4792 [NFT_MSG_DELOBJ] = {
4793 .call_batch = nf_tables_delobj,
4794 .attr_count = NFTA_OBJ_MAX,
4795 .policy = nft_obj_policy,
4796 },
4797 [NFT_MSG_GETOBJ_RESET] = {
4798 .call = nf_tables_getobj,
4799 .attr_count = NFTA_OBJ_MAX,
4800 .policy = nft_obj_policy,
4801 },
4802};
4803
4804static void nft_chain_commit_update(struct nft_trans *trans)
4805{
4806 struct nft_base_chain *basechain;
4807
4808 if (nft_trans_chain_name(trans)[0])
4809 strcpy(trans->ctx.chain->name, nft_trans_chain_name(trans));
4810
4811 if (!nft_is_base_chain(trans->ctx.chain))
4812 return;
4813
4814 basechain = nft_base_chain(trans->ctx.chain);
4815 nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
4816
4817 switch (nft_trans_chain_policy(trans)) {
4818 case NF_DROP:
4819 case NF_ACCEPT:
4820 basechain->policy = nft_trans_chain_policy(trans);
4821 break;
4822 }
4823}
4824
4825static void nf_tables_commit_release(struct nft_trans *trans)
4826{
4827 switch (trans->msg_type) {
4828 case NFT_MSG_DELTABLE:
4829 nf_tables_table_destroy(&trans->ctx);
4830 break;
4831 case NFT_MSG_DELCHAIN:
4832 nf_tables_chain_destroy(trans->ctx.chain);
4833 break;
4834 case NFT_MSG_DELRULE:
4835 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4836 break;
4837 case NFT_MSG_DELSET:
4838 nft_set_destroy(nft_trans_set(trans));
4839 break;
4840 case NFT_MSG_DELSETELEM:
4841 nf_tables_set_elem_destroy(nft_trans_elem_set(trans),
4842 nft_trans_elem(trans).priv);
4843 break;
4844 case NFT_MSG_DELOBJ:
4845 nft_obj_destroy(nft_trans_obj(trans));
4846 break;
4847 }
4848 kfree(trans);
4849}
4850
4851static int nf_tables_commit(struct net *net, struct sk_buff *skb)
4852{
4853 struct nft_trans *trans, *next;
4854 struct nft_trans_elem *te;
4855
4856
4857 while (++net->nft.base_seq == 0);
4858
4859
4860 net->nft.gencursor = nft_gencursor_next(net);
4861
4862
4863
4864
4865 synchronize_rcu();
4866
4867 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
4868 switch (trans->msg_type) {
4869 case NFT_MSG_NEWTABLE:
4870 if (nft_trans_table_update(trans)) {
4871 if (!nft_trans_table_enable(trans)) {
4872 nf_tables_table_disable(net,
4873 trans->ctx.afi,
4874 trans->ctx.table);
4875 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
4876 }
4877 } else {
4878 nft_clear(net, trans->ctx.table);
4879 }
4880 nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE);
4881 nft_trans_destroy(trans);
4882 break;
4883 case NFT_MSG_DELTABLE:
4884 list_del_rcu(&trans->ctx.table->list);
4885 nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE);
4886 break;
4887 case NFT_MSG_NEWCHAIN:
4888 if (nft_trans_chain_update(trans))
4889 nft_chain_commit_update(trans);
4890 else
4891 nft_clear(net, trans->ctx.chain);
4892
4893 nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN);
4894 nft_trans_destroy(trans);
4895 break;
4896 case NFT_MSG_DELCHAIN:
4897 list_del_rcu(&trans->ctx.chain->list);
4898 nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN);
4899 nf_tables_unregister_hooks(trans->ctx.net,
4900 trans->ctx.table,
4901 trans->ctx.chain,
4902 trans->ctx.afi->nops);
4903 break;
4904 case NFT_MSG_NEWRULE:
4905 nft_clear(trans->ctx.net, nft_trans_rule(trans));
4906 nf_tables_rule_notify(&trans->ctx,
4907 nft_trans_rule(trans),
4908 NFT_MSG_NEWRULE);
4909 nft_trans_destroy(trans);
4910 break;
4911 case NFT_MSG_DELRULE:
4912 list_del_rcu(&nft_trans_rule(trans)->list);
4913 nf_tables_rule_notify(&trans->ctx,
4914 nft_trans_rule(trans),
4915 NFT_MSG_DELRULE);
4916 break;
4917 case NFT_MSG_NEWSET:
4918 nft_clear(net, nft_trans_set(trans));
4919
4920
4921
4922 if (nft_trans_set(trans)->flags & NFT_SET_ANONYMOUS &&
4923 !list_empty(&nft_trans_set(trans)->bindings))
4924 trans->ctx.table->use--;
4925
4926 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
4927 NFT_MSG_NEWSET, GFP_KERNEL);
4928 nft_trans_destroy(trans);
4929 break;
4930 case NFT_MSG_DELSET:
4931 list_del_rcu(&nft_trans_set(trans)->list);
4932 nf_tables_set_notify(&trans->ctx, nft_trans_set(trans),
4933 NFT_MSG_DELSET, GFP_KERNEL);
4934 break;
4935 case NFT_MSG_NEWSETELEM:
4936 te = (struct nft_trans_elem *)trans->data;
4937
4938 te->set->ops->activate(net, te->set, &te->elem);
4939 nf_tables_setelem_notify(&trans->ctx, te->set,
4940 &te->elem,
4941 NFT_MSG_NEWSETELEM, 0);
4942 nft_trans_destroy(trans);
4943 break;
4944 case NFT_MSG_DELSETELEM:
4945 te = (struct nft_trans_elem *)trans->data;
4946
4947 nf_tables_setelem_notify(&trans->ctx, te->set,
4948 &te->elem,
4949 NFT_MSG_DELSETELEM, 0);
4950 te->set->ops->remove(net, te->set, &te->elem);
4951 atomic_dec(&te->set->nelems);
4952 te->set->ndeact--;
4953 break;
4954 case NFT_MSG_NEWOBJ:
4955 nft_clear(net, nft_trans_obj(trans));
4956 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
4957 NFT_MSG_NEWOBJ);
4958 nft_trans_destroy(trans);
4959 break;
4960 case NFT_MSG_DELOBJ:
4961 list_del_rcu(&nft_trans_obj(trans)->list);
4962 nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans),
4963 NFT_MSG_DELOBJ);
4964 break;
4965 }
4966 }
4967
4968 synchronize_rcu();
4969
4970 list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) {
4971 list_del(&trans->list);
4972 nf_tables_commit_release(trans);
4973 }
4974
4975 nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN);
4976
4977 return 0;
4978}
4979
4980static void nf_tables_abort_release(struct nft_trans *trans)
4981{
4982 switch (trans->msg_type) {
4983 case NFT_MSG_NEWTABLE:
4984 nf_tables_table_destroy(&trans->ctx);
4985 break;
4986 case NFT_MSG_NEWCHAIN:
4987 nf_tables_chain_destroy(trans->ctx.chain);
4988 break;
4989 case NFT_MSG_NEWRULE:
4990 nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans));
4991 break;
4992 case NFT_MSG_NEWSET:
4993 nft_set_destroy(nft_trans_set(trans));
4994 break;
4995 case NFT_MSG_NEWSETELEM:
4996 nft_set_elem_destroy(nft_trans_elem_set(trans),
4997 nft_trans_elem(trans).priv, true);
4998 break;
4999 case NFT_MSG_NEWOBJ:
5000 nft_obj_destroy(nft_trans_obj(trans));
5001 break;
5002 }
5003 kfree(trans);
5004}
5005
5006static int nf_tables_abort(struct net *net, struct sk_buff *skb)
5007{
5008 struct nft_trans *trans, *next;
5009 struct nft_trans_elem *te;
5010
5011 list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list,
5012 list) {
5013 switch (trans->msg_type) {
5014 case NFT_MSG_NEWTABLE:
5015 if (nft_trans_table_update(trans)) {
5016 if (nft_trans_table_enable(trans)) {
5017 nf_tables_table_disable(net,
5018 trans->ctx.afi,
5019 trans->ctx.table);
5020 trans->ctx.table->flags |= NFT_TABLE_F_DORMANT;
5021 }
5022 nft_trans_destroy(trans);
5023 } else {
5024 list_del_rcu(&trans->ctx.table->list);
5025 }
5026 break;
5027 case NFT_MSG_DELTABLE:
5028 nft_clear(trans->ctx.net, trans->ctx.table);
5029 nft_trans_destroy(trans);
5030 break;
5031 case NFT_MSG_NEWCHAIN:
5032 if (nft_trans_chain_update(trans)) {
5033 free_percpu(nft_trans_chain_stats(trans));
5034
5035 nft_trans_destroy(trans);
5036 } else {
5037 trans->ctx.table->use--;
5038 list_del_rcu(&trans->ctx.chain->list);
5039 nf_tables_unregister_hooks(trans->ctx.net,
5040 trans->ctx.table,
5041 trans->ctx.chain,
5042 trans->ctx.afi->nops);
5043 }
5044 break;
5045 case NFT_MSG_DELCHAIN:
5046 trans->ctx.table->use++;
5047 nft_clear(trans->ctx.net, trans->ctx.chain);
5048 nft_trans_destroy(trans);
5049 break;
5050 case NFT_MSG_NEWRULE:
5051 trans->ctx.chain->use--;
5052 list_del_rcu(&nft_trans_rule(trans)->list);
5053 break;
5054 case NFT_MSG_DELRULE:
5055 trans->ctx.chain->use++;
5056 nft_clear(trans->ctx.net, nft_trans_rule(trans));
5057 nft_trans_destroy(trans);
5058 break;
5059 case NFT_MSG_NEWSET:
5060 trans->ctx.table->use--;
5061 list_del_rcu(&nft_trans_set(trans)->list);
5062 break;
5063 case NFT_MSG_DELSET:
5064 trans->ctx.table->use++;
5065 nft_clear(trans->ctx.net, nft_trans_set(trans));
5066 nft_trans_destroy(trans);
5067 break;
5068 case NFT_MSG_NEWSETELEM:
5069 te = (struct nft_trans_elem *)trans->data;
5070
5071 te->set->ops->remove(net, te->set, &te->elem);
5072 atomic_dec(&te->set->nelems);
5073 break;
5074 case NFT_MSG_DELSETELEM:
5075 te = (struct nft_trans_elem *)trans->data;
5076
5077 nft_set_elem_activate(net, te->set, &te->elem);
5078 te->set->ops->activate(net, te->set, &te->elem);
5079 te->set->ndeact--;
5080
5081 nft_trans_destroy(trans);
5082 break;
5083 case NFT_MSG_NEWOBJ:
5084 trans->ctx.table->use--;
5085 list_del_rcu(&nft_trans_obj(trans)->list);
5086 break;
5087 case NFT_MSG_DELOBJ:
5088 trans->ctx.table->use++;
5089 nft_clear(trans->ctx.net, nft_trans_obj(trans));
5090 nft_trans_destroy(trans);
5091 break;
5092 }
5093 }
5094
5095 synchronize_rcu();
5096
5097 list_for_each_entry_safe_reverse(trans, next,
5098 &net->nft.commit_list, list) {
5099 list_del(&trans->list);
5100 nf_tables_abort_release(trans);
5101 }
5102
5103 return 0;
5104}
5105
5106static bool nf_tables_valid_genid(struct net *net, u32 genid)
5107{
5108 return net->nft.base_seq == genid;
5109}
5110
5111static const struct nfnetlink_subsystem nf_tables_subsys = {
5112 .name = "nf_tables",
5113 .subsys_id = NFNL_SUBSYS_NFTABLES,
5114 .cb_count = NFT_MSG_MAX,
5115 .cb = nf_tables_cb,
5116 .commit = nf_tables_commit,
5117 .abort = nf_tables_abort,
5118 .valid_genid = nf_tables_valid_genid,
5119};
5120
5121int nft_chain_validate_dependency(const struct nft_chain *chain,
5122 enum nft_chain_type type)
5123{
5124 const struct nft_base_chain *basechain;
5125
5126 if (nft_is_base_chain(chain)) {
5127 basechain = nft_base_chain(chain);
5128 if (basechain->type->type != type)
5129 return -EOPNOTSUPP;
5130 }
5131 return 0;
5132}
5133EXPORT_SYMBOL_GPL(nft_chain_validate_dependency);
5134
5135int nft_chain_validate_hooks(const struct nft_chain *chain,
5136 unsigned int hook_flags)
5137{
5138 struct nft_base_chain *basechain;
5139
5140 if (nft_is_base_chain(chain)) {
5141 basechain = nft_base_chain(chain);
5142
5143 if ((1 << basechain->ops[0].hooknum) & hook_flags)
5144 return 0;
5145
5146 return -EOPNOTSUPP;
5147 }
5148
5149 return 0;
5150}
5151EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162static int nf_tables_check_loops(const struct nft_ctx *ctx,
5163 const struct nft_chain *chain);
5164
5165static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
5166 struct nft_set *set,
5167 const struct nft_set_iter *iter,
5168 struct nft_set_elem *elem)
5169{
5170 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
5171 const struct nft_data *data;
5172
5173 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
5174 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
5175 return 0;
5176
5177 data = nft_set_ext_data(ext);
5178 switch (data->verdict.code) {
5179 case NFT_JUMP:
5180 case NFT_GOTO:
5181 return nf_tables_check_loops(ctx, data->verdict.chain);
5182 default:
5183 return 0;
5184 }
5185}
5186
5187static int nf_tables_check_loops(const struct nft_ctx *ctx,
5188 const struct nft_chain *chain)
5189{
5190 const struct nft_rule *rule;
5191 const struct nft_expr *expr, *last;
5192 struct nft_set *set;
5193 struct nft_set_binding *binding;
5194 struct nft_set_iter iter;
5195
5196 if (ctx->chain == chain)
5197 return -ELOOP;
5198
5199 list_for_each_entry(rule, &chain->rules, list) {
5200 nft_rule_for_each_expr(expr, last, rule) {
5201 const struct nft_data *data = NULL;
5202 int err;
5203
5204 if (!expr->ops->validate)
5205 continue;
5206
5207 err = expr->ops->validate(ctx, expr, &data);
5208 if (err < 0)
5209 return err;
5210
5211 if (data == NULL)
5212 continue;
5213
5214 switch (data->verdict.code) {
5215 case NFT_JUMP:
5216 case NFT_GOTO:
5217 err = nf_tables_check_loops(ctx,
5218 data->verdict.chain);
5219 if (err < 0)
5220 return err;
5221 default:
5222 break;
5223 }
5224 }
5225 }
5226
5227 list_for_each_entry(set, &ctx->table->sets, list) {
5228 if (!nft_is_active_next(ctx->net, set))
5229 continue;
5230 if (!(set->flags & NFT_SET_MAP) ||
5231 set->dtype != NFT_DATA_VERDICT)
5232 continue;
5233
5234 list_for_each_entry(binding, &set->bindings, list) {
5235 if (!(binding->flags & NFT_SET_MAP) ||
5236 binding->chain != chain)
5237 continue;
5238
5239 iter.genmask = nft_genmask_next(ctx->net);
5240 iter.skip = 0;
5241 iter.count = 0;
5242 iter.err = 0;
5243 iter.fn = nf_tables_loop_check_setelem;
5244
5245 set->ops->walk(ctx, set, &iter);
5246 if (iter.err < 0)
5247 return iter.err;
5248 }
5249 }
5250
5251 return 0;
5252}
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest)
5267{
5268 u32 val;
5269
5270 val = ntohl(nla_get_be32(attr));
5271 if (val > max)
5272 return -ERANGE;
5273
5274 *dest = val;
5275 return 0;
5276}
5277EXPORT_SYMBOL_GPL(nft_parse_u32_check);
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288unsigned int nft_parse_register(const struct nlattr *attr)
5289{
5290 unsigned int reg;
5291
5292 reg = ntohl(nla_get_be32(attr));
5293 switch (reg) {
5294 case NFT_REG_VERDICT...NFT_REG_4:
5295 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
5296 default:
5297 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
5298 }
5299}
5300EXPORT_SYMBOL_GPL(nft_parse_register);
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
5314{
5315 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
5316 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
5317 else
5318 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
5319
5320 return nla_put_be32(skb, attr, htonl(reg));
5321}
5322EXPORT_SYMBOL_GPL(nft_dump_register);
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333int nft_validate_register_load(enum nft_registers reg, unsigned int len)
5334{
5335 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
5336 return -EINVAL;
5337 if (len == 0)
5338 return -EINVAL;
5339 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
5340 return -ERANGE;
5341
5342 return 0;
5343}
5344EXPORT_SYMBOL_GPL(nft_validate_register_load);
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360int nft_validate_register_store(const struct nft_ctx *ctx,
5361 enum nft_registers reg,
5362 const struct nft_data *data,
5363 enum nft_data_types type, unsigned int len)
5364{
5365 int err;
5366
5367 switch (reg) {
5368 case NFT_REG_VERDICT:
5369 if (type != NFT_DATA_VERDICT)
5370 return -EINVAL;
5371
5372 if (data != NULL &&
5373 (data->verdict.code == NFT_GOTO ||
5374 data->verdict.code == NFT_JUMP)) {
5375 err = nf_tables_check_loops(ctx, data->verdict.chain);
5376 if (err < 0)
5377 return err;
5378
5379 if (ctx->chain->level + 1 >
5380 data->verdict.chain->level) {
5381 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
5382 return -EMLINK;
5383 data->verdict.chain->level = ctx->chain->level + 1;
5384 }
5385 }
5386
5387 return 0;
5388 default:
5389 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
5390 return -EINVAL;
5391 if (len == 0)
5392 return -EINVAL;
5393 if (reg * NFT_REG32_SIZE + len >
5394 FIELD_SIZEOF(struct nft_regs, data))
5395 return -ERANGE;
5396
5397 if (data != NULL && type != NFT_DATA_VALUE)
5398 return -EINVAL;
5399 return 0;
5400 }
5401}
5402EXPORT_SYMBOL_GPL(nft_validate_register_store);
5403
5404static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
5405 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
5406 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
5407 .len = NFT_CHAIN_MAXNAMELEN - 1 },
5408};
5409
5410static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
5411 struct nft_data_desc *desc, const struct nlattr *nla)
5412{
5413 u8 genmask = nft_genmask_next(ctx->net);
5414 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
5415 struct nft_chain *chain;
5416 int err;
5417
5418 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy,
5419 NULL);
5420 if (err < 0)
5421 return err;
5422
5423 if (!tb[NFTA_VERDICT_CODE])
5424 return -EINVAL;
5425 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
5426
5427 switch (data->verdict.code) {
5428 default:
5429 switch (data->verdict.code & NF_VERDICT_MASK) {
5430 case NF_ACCEPT:
5431 case NF_DROP:
5432 case NF_QUEUE:
5433 break;
5434 default:
5435 return -EINVAL;
5436 }
5437
5438 case NFT_CONTINUE:
5439 case NFT_BREAK:
5440 case NFT_RETURN:
5441 break;
5442 case NFT_JUMP:
5443 case NFT_GOTO:
5444 if (!tb[NFTA_VERDICT_CHAIN])
5445 return -EINVAL;
5446 chain = nf_tables_chain_lookup(ctx->table,
5447 tb[NFTA_VERDICT_CHAIN], genmask);
5448 if (IS_ERR(chain))
5449 return PTR_ERR(chain);
5450 if (nft_is_base_chain(chain))
5451 return -EOPNOTSUPP;
5452
5453 chain->use++;
5454 data->verdict.chain = chain;
5455 break;
5456 }
5457
5458 desc->len = sizeof(data->verdict);
5459 desc->type = NFT_DATA_VERDICT;
5460 return 0;
5461}
5462
5463static void nft_verdict_uninit(const struct nft_data *data)
5464{
5465 switch (data->verdict.code) {
5466 case NFT_JUMP:
5467 case NFT_GOTO:
5468 data->verdict.chain->use--;
5469 break;
5470 }
5471}
5472
5473int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v)
5474{
5475 struct nlattr *nest;
5476
5477 nest = nla_nest_start(skb, type);
5478 if (!nest)
5479 goto nla_put_failure;
5480
5481 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code)))
5482 goto nla_put_failure;
5483
5484 switch (v->code) {
5485 case NFT_JUMP:
5486 case NFT_GOTO:
5487 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
5488 v->chain->name))
5489 goto nla_put_failure;
5490 }
5491 nla_nest_end(skb, nest);
5492 return 0;
5493
5494nla_put_failure:
5495 return -1;
5496}
5497
5498static int nft_value_init(const struct nft_ctx *ctx,
5499 struct nft_data *data, unsigned int size,
5500 struct nft_data_desc *desc, const struct nlattr *nla)
5501{
5502 unsigned int len;
5503
5504 len = nla_len(nla);
5505 if (len == 0)
5506 return -EINVAL;
5507 if (len > size)
5508 return -EOVERFLOW;
5509
5510 nla_memcpy(data->data, nla, len);
5511 desc->type = NFT_DATA_VALUE;
5512 desc->len = len;
5513 return 0;
5514}
5515
5516static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
5517 unsigned int len)
5518{
5519 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
5520}
5521
5522static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
5523 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
5524 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
5525};
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542int nft_data_init(const struct nft_ctx *ctx,
5543 struct nft_data *data, unsigned int size,
5544 struct nft_data_desc *desc, const struct nlattr *nla)
5545{
5546 struct nlattr *tb[NFTA_DATA_MAX + 1];
5547 int err;
5548
5549 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy, NULL);
5550 if (err < 0)
5551 return err;
5552
5553 if (tb[NFTA_DATA_VALUE])
5554 return nft_value_init(ctx, data, size, desc,
5555 tb[NFTA_DATA_VALUE]);
5556 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
5557 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
5558 return -EINVAL;
5559}
5560EXPORT_SYMBOL_GPL(nft_data_init);
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571void nft_data_release(const struct nft_data *data, enum nft_data_types type)
5572{
5573 if (type < NFT_DATA_VERDICT)
5574 return;
5575 switch (type) {
5576 case NFT_DATA_VERDICT:
5577 return nft_verdict_uninit(data);
5578 default:
5579 WARN_ON(1);
5580 }
5581}
5582EXPORT_SYMBOL_GPL(nft_data_release);
5583
5584int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
5585 enum nft_data_types type, unsigned int len)
5586{
5587 struct nlattr *nest;
5588 int err;
5589
5590 nest = nla_nest_start(skb, attr);
5591 if (nest == NULL)
5592 return -1;
5593
5594 switch (type) {
5595 case NFT_DATA_VALUE:
5596 err = nft_value_dump(skb, data, len);
5597 break;
5598 case NFT_DATA_VERDICT:
5599 err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict);
5600 break;
5601 default:
5602 err = -EINVAL;
5603 WARN_ON(1);
5604 }
5605
5606 nla_nest_end(skb, nest);
5607 return err;
5608}
5609EXPORT_SYMBOL_GPL(nft_data_dump);
5610
5611static int __net_init nf_tables_init_net(struct net *net)
5612{
5613 INIT_LIST_HEAD(&net->nft.af_info);
5614 INIT_LIST_HEAD(&net->nft.commit_list);
5615 net->nft.base_seq = 1;
5616 return 0;
5617}
5618
5619int __nft_release_basechain(struct nft_ctx *ctx)
5620{
5621 struct nft_rule *rule, *nr;
5622
5623 BUG_ON(!nft_is_base_chain(ctx->chain));
5624
5625 nf_tables_unregister_hooks(ctx->net, ctx->chain->table, ctx->chain,
5626 ctx->afi->nops);
5627 list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) {
5628 list_del(&rule->list);
5629 ctx->chain->use--;
5630 nf_tables_rule_destroy(ctx, rule);
5631 }
5632 list_del(&ctx->chain->list);
5633 ctx->table->use--;
5634 nf_tables_chain_destroy(ctx->chain);
5635
5636 return 0;
5637}
5638EXPORT_SYMBOL_GPL(__nft_release_basechain);
5639
5640
5641static void __nft_release_afinfo(struct net *net, struct nft_af_info *afi)
5642{
5643 struct nft_table *table, *nt;
5644 struct nft_chain *chain, *nc;
5645 struct nft_object *obj, *ne;
5646 struct nft_rule *rule, *nr;
5647 struct nft_set *set, *ns;
5648 struct nft_ctx ctx = {
5649 .net = net,
5650 .afi = afi,
5651 };
5652
5653 list_for_each_entry_safe(table, nt, &afi->tables, list) {
5654 list_for_each_entry(chain, &table->chains, list)
5655 nf_tables_unregister_hooks(net, table, chain,
5656 afi->nops);
5657
5658 ctx.table = table;
5659 list_for_each_entry(chain, &table->chains, list) {
5660 ctx.chain = chain;
5661 list_for_each_entry_safe(rule, nr, &chain->rules, list) {
5662 list_del(&rule->list);
5663 chain->use--;
5664 nf_tables_rule_destroy(&ctx, rule);
5665 }
5666 }
5667 list_for_each_entry_safe(set, ns, &table->sets, list) {
5668 list_del(&set->list);
5669 table->use--;
5670 nft_set_destroy(set);
5671 }
5672 list_for_each_entry_safe(obj, ne, &table->objects, list) {
5673 list_del(&obj->list);
5674 table->use--;
5675 nft_obj_destroy(obj);
5676 }
5677 list_for_each_entry_safe(chain, nc, &table->chains, list) {
5678 list_del(&chain->list);
5679 table->use--;
5680 nf_tables_chain_destroy(chain);
5681 }
5682 list_del(&table->list);
5683 nf_tables_table_destroy(&ctx);
5684 }
5685}
5686
5687static struct pernet_operations nf_tables_net_ops = {
5688 .init = nf_tables_init_net,
5689};
5690
5691static int __init nf_tables_module_init(void)
5692{
5693 int err;
5694
5695 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
5696 GFP_KERNEL);
5697 if (info == NULL) {
5698 err = -ENOMEM;
5699 goto err1;
5700 }
5701
5702 err = nf_tables_core_module_init();
5703 if (err < 0)
5704 goto err2;
5705
5706 err = nfnetlink_subsys_register(&nf_tables_subsys);
5707 if (err < 0)
5708 goto err3;
5709
5710 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
5711 return register_pernet_subsys(&nf_tables_net_ops);
5712err3:
5713 nf_tables_core_module_exit();
5714err2:
5715 kfree(info);
5716err1:
5717 return err;
5718}
5719
5720static void __exit nf_tables_module_exit(void)
5721{
5722 unregister_pernet_subsys(&nf_tables_net_ops);
5723 nfnetlink_subsys_unregister(&nf_tables_subsys);
5724 rcu_barrier();
5725 nf_tables_core_module_exit();
5726 kfree(info);
5727}
5728
5729module_init(nf_tables_module_init);
5730module_exit(nf_tables_module_exit);
5731
5732MODULE_LICENSE("GPL");
5733MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
5734MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
5735